Skip to content

Commit

Permalink
Updated form store
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurczewski committed Oct 14, 2024
1 parent 020d81e commit 6fd8e0b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
8 changes: 4 additions & 4 deletions libs/forms/feature/src/use-form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface UseFormField {
appForm?: boolean
}

export const useFormField = ({ formName, appForm }: UseFormField) => {
export const useFormField = ({ formName, appForm }: UseFormField = {}) => {
const dispatch = useDispatch<Dispatch>()
const activeDeviceId = useSelector(selectActiveApiDeviceId)
const store = useStore<ReduxRootState>()
Expand All @@ -50,8 +50,9 @@ export const useFormField = ({ formName, appForm }: UseFormField) => {
if (!targetFormName || !field) return undefined

const source = form || forms?.[targetFormName]
const fields = fromDefault ? source?.defaultFields : source?.fields
return get(fields, field) as T
if (!source) return
const fields = fromDefault ? source.defaultFields : source.fields
return get(fields, field) as T | undefined
},
[form, formName, forms]
)
Expand All @@ -60,7 +61,6 @@ export const useFormField = ({ formName, appForm }: UseFormField) => {
(field: string, value: unknown, { customFormName }: SetOptions = {}) => {
const targetFormName = customFormName || formName
if (!targetFormName || !field) return

if (getField(field, { customFormName }) === value) return
dispatch(
setFormField({
Expand Down
1 change: 1 addition & 0 deletions libs/forms/feature/src/use-form-register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const useFormRegister = ({ formName, appForm, options }: Params) => {
const dispatch = useDispatch<Dispatch>()
const activeDeviceId = useSelector(selectActiveApiDeviceId)
const form = useSelector((state: ReduxRootState) => {
if(!appForm && !activeDeviceId) return undefined
return selectForm(state, {
formName,
deviceId: appForm ? undefined : activeDeviceId,
Expand Down
35 changes: 19 additions & 16 deletions libs/forms/store/src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,32 @@ const initialState: FormState = {
export const formsReducer = createReducer(initialState, (builder) => {
builder.addCase(registerForm, (state, action) => {
const { formName, form, deviceId } = action.payload
if (deviceId && deviceId !== appForms) {
state[deviceId] = {
...state[deviceId],
[formName]: form,
}
} else {
state.app = {
...state.app,
[formName]: form,
}
const source = deviceId
? deviceId === appForms
? undefined
: deviceId
: appForms

if (!source || !formName) return
state[source] = {
...state[source],
[formName]: form,
}
})
builder.addCase(setFormField, (state, action) => {
const { formName, field, value, deviceId } = action.payload
if (deviceId && deviceId !== appForms) {
state[deviceId][formName].fields[field] = value
} else {
state.app[formName].fields[field] = value
}
const source = deviceId
? deviceId === appForms
? undefined
: deviceId
: appForms

if (!source || !state[source]?.[formName]) return
state[source][formName].fields[field] = value
})
builder.addCase(resetForm, (state, action) => {
const { formName, deviceId } = action.payload
const defaultValues = state[deviceId ?? "app"][formName].defaultFields
const defaultValues = state[deviceId || "app"]?.[formName]?.defaultFields

if (!defaultValues) return
if (deviceId && deviceId !== appForms) {
Expand Down

0 comments on commit 6fd8e0b

Please sign in to comment.