Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Sep 26, 2023
1 parent fa16918 commit 7fd6414
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/shared/stores/entry-form-store.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { setIn, getIn } from 'final-form'
import create from 'zustand'
import { devtools } from 'zustand/middleware'

const inititalState = {
errors: {},
warnings: {},
}

export const useEntryFormStore = create(
devtools((set, get) => ({
...inititalState,
setErrors: (errors) => set({ errors: errors ?? {} }),
getErrors: () => get().errors,
getNumberOfErrors: () => countLeaves(get().getErrors()),
getWarnings: () => get().warnings,
getWarning: (fieldname) => getIn(get().getWarnings(), fieldname),
setWarning: (fieldname, warning) => {
const warnings = get().getWarnings() || {}
const newWarnings = setIn(warnings, fieldname, warning)
set({ warnings: newWarnings })
},
}))
)
export const useEntryFormStore = create((set, get) => ({
...inititalState,
setErrors: (errors) => set({ errors: errors ?? {} }),
getErrors: () => get().errors,
getNumberOfErrors: () => countLeaves(get().getErrors()),
getWarnings: () => get().warnings,
getWarning: (fieldname) => getIn(get().getWarnings(), fieldname),
setWarning: (fieldname, warning) => {
const warnings = get().getWarnings()
// setIn from final-form is used to create the same structure as errors
const newWarnings = setIn(warnings, fieldname, warning)
set({ warnings: newWarnings })
},
// could add getNumberOfWarnings if needed
}))

// errors object is the same shape as form-Values
// eg. { [dataElementId] : {
Expand Down

0 comments on commit 7fd6414

Please sign in to comment.