-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
120 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
export type ModalDto<T = void> = { | ||
export type ModalDto = { | ||
header: JSX.Element; | ||
body: JSX.Element; | ||
footer?: JSX.Element; | ||
state?: T; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 32 additions & 69 deletions
101
...nner.App/ClientApp/src/components/modals/LocationDetailsForm/LocationDetailsFormState.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,46 @@ | ||
import React, { | ||
createContext, useContext, useMemo, useReducer, | ||
} from 'react'; | ||
import { atom, selectorFamily } from 'recoil'; | ||
import LocationDto, { LocationEmpty } from '../../../Common/Dto/LocationDto'; | ||
|
||
const enum LocationFormStateActions { | ||
setStep, | ||
updateLocation, | ||
setError, | ||
clearError | ||
} | ||
import { Nullable } from '../../../Common/Dto/Nullable'; | ||
|
||
type Error = { | ||
[name: string]: string; | ||
} | ||
|
||
type State = { | ||
step: number; | ||
location: LocationDto; | ||
errors?: Error; | ||
const enum LocationFormStateActions { | ||
setError, | ||
clearError | ||
} | ||
|
||
type Action = | ||
| { type: LocationFormStateActions.setStep, data: number } | ||
| { type: LocationFormStateActions.updateLocation, data: LocationDto } | ||
| { type: LocationFormStateActions.clearError, data: Error } | ||
| { type: LocationFormStateActions.setError, data: Error } | ||
|
||
type Dispatch = (action: Action) => void; | ||
|
||
const initialState: State = { | ||
step: 0, | ||
location: LocationEmpty, | ||
errors: undefined, | ||
}; | ||
|
||
const reducer: React.Reducer<State, Action> = (state: State, action: Action) => { | ||
let newState: State = state; | ||
|
||
switch (action.type) { | ||
case LocationFormStateActions.setError: | ||
newState = { ...state, errors: { ...state.errors, [action.data.name]: action.data.value } }; | ||
break; | ||
case LocationFormStateActions.clearError: | ||
newState = { ...state, errors: { ...state.errors, [action.data.name]: '' } }; | ||
break; | ||
case LocationFormStateActions.updateLocation: | ||
newState = { ...state, location: action.data }; | ||
break; | ||
case LocationFormStateActions.setStep: | ||
newState = { ...state, step: action.data }; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return newState; | ||
}; | ||
|
||
const LocationFormContext = createContext<{ state: State, dispatch: Dispatch}>({ | ||
state: initialState, | ||
dispatch: () => undefined, | ||
const locationFormStepState = atom<number>({ | ||
key: 'locationForm.step', | ||
default: 0, | ||
}); | ||
|
||
const useLocationFormState = () => useContext(LocationFormContext); | ||
|
||
type Props = { | ||
children: JSX.Element | ||
} | ||
const locationFormErrorState = atom<Nullable<Error>>({ | ||
key: 'locationForm.error', | ||
default: undefined, | ||
}); | ||
|
||
function LocationFormStateProvider({ children }: Props) { | ||
const [state, dispatch] = useReducer<React.Reducer<State, Action>>(reducer, initialState); | ||
const locationFormDataState = atom<LocationDto>({ | ||
key: 'locationForm.data', | ||
default: LocationEmpty, | ||
}); | ||
|
||
const value = useMemo<{state: State, dispatch: Dispatch}>(() => ({ state, dispatch }), [state]); | ||
const updateError = selectorFamily<Nullable<Error>, LocationFormStateActions>({ | ||
key: 'locationsForm.updateError', | ||
get: () => ({ get }) => get(locationFormErrorState), | ||
set: (action) => ({ set }, value) => { | ||
if (value instanceof Error) { | ||
if (action === LocationFormStateActions.setError) { | ||
set(locationFormErrorState, value); | ||
} else if (action === LocationFormStateActions.clearError) { | ||
set(locationFormErrorState, undefined); | ||
} | ||
} | ||
}, | ||
|
||
return ( | ||
<LocationFormContext.Provider value={value}> | ||
{children} | ||
</LocationFormContext.Provider> | ||
); | ||
} | ||
}); | ||
|
||
export { LocationFormStateActions, useLocationFormState, LocationFormStateProvider }; | ||
export { | ||
locationFormDataState, locationFormErrorState, locationFormStepState, updateError, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.