Skip to content

Commit

Permalink
fix: prevent rendering before initial values store populated
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp committed Dec 6, 2024
1 parent 6a98cff commit ef9051b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/data-workspace/data-workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ export const DataWorkspace = ({ selectionHasNoFormMessage }) => {
const dataValueSet = initialDataValuesFetch.data?.dataValues

return (
<FormWrapper key={formKey} dataValueSet={dataValueSet}>
<FormWrapper
key={formKey}
dataValueSet={dataValueSet}
validFormKey={validFormKey}
>
<div className={styles.wrapper}>
<main id="data-workspace" className={styles.formWrapper}>
<div className={styles.formArea}>
Expand Down
20 changes: 14 additions & 6 deletions src/data-workspace/form-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PropTypes from 'prop-types'
import React, { useEffect, useState } from 'react'
import React, { useLayoutEffect, useState } from 'react'
import { useValueStore } from '../shared/index.js'

function mapObject(input, callback) {
Expand All @@ -20,7 +20,7 @@ function createInitialValues(dataValueSet) {
])
}

export function FormWrapper({ children, dataValueSet }) {
export function FormWrapper({ children, dataValueSet, validFormKey }) {
// This prevents rerendering on every data value update, but also prevents
// reinitializing (and populating) an empty form when going back online.
// TODO: Reinitialize form `onSuccess` of dataValueSets query
Expand All @@ -31,15 +31,23 @@ export function FormWrapper({ children, dataValueSet }) {
(state) => state.setInitialDataValues
)

useEffect(() => {
if (setInitialDataValues && initialValues) {
setInitialDataValues(initialValues)
const initialValuesInStore =
useValueStore((state) => state.getInitialDataValues())?.formKey ===
validFormKey

useLayoutEffect(() => {
if (setInitialDataValues && initialValues && validFormKey) {
setInitialDataValues(initialValues, validFormKey)
}
}, [initialValues, setInitialDataValues])
}, [initialValues, setInitialDataValues, validFormKey])

if (!initialValuesInStore) {
return null
}
return <>{children}</>
}
FormWrapper.propTypes = {
children: PropTypes.node,
dataValueSet: PropTypes.object,
validFormKey: PropTypes.string,
}
8 changes: 5 additions & 3 deletions src/shared/stores/data-value-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ export const useValueStore = create((set, get) => ({
dataValueSet: values ?? initialState.dataValueSet,
}),
getInitialDataValue: ({ dataElementId, categoryOptionComboId }) => {
return get().initialDataValues?.[dataElementId]?.[categoryOptionComboId]
return get().initialDataValues?.values?.[dataElementId]?.[
categoryOptionComboId
]
},
getInitialDataValues: () => get().initialDataValues,
setInitialDataValues: (values) => {
set({ initialDataValues: values })
setInitialDataValues: (values, formKey) => {
set({ initialDataValues: { values, formKey } })
},
getMinMaxValues: ({ dataElementId, categoryOptionComboId }) => {
return get().dataValueSet?.minMaxValues?.find(
Expand Down

0 comments on commit ef9051b

Please sign in to comment.