Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
CynthiaKamau committed Aug 28, 2024
1 parent 3568f24 commit a24d60c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 52 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"access": "public"
},
"dependencies": {
"@carbon/react": ">1.47.0 <1.50.0",
"@carbon/react": "1.40.0",
"classnames": "^2.5.1",
"lodash-es": "^4.17.21",
"react-error-boundary": "^4.0.13",
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/program-state-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const ProgramStateAdapter: FormFieldValueAdapter = {
.filter((state) => !state.endDate)
.find((state) => state.state.programWorkflow?.uuid === field.questionOptions.workflowUuid)?.state;
field.meta = { ...(field.meta || {}), previousValue: currentState };
return currentState.uuid;
return currentState?.uuid;
}
return null;
},
Expand Down
105 changes: 55 additions & 50 deletions src/components/renderer/field/form-field-renderer.component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import {
type FormField,
type RenderType,
Expand Down Expand Up @@ -39,6 +39,16 @@ export const FormFieldRenderer = ({ field, valueAdapter, repeatOptions }: FormFi
const [warnings, setWarnings] = useState<ValidationResult[]>([]);
const [historicalValue, setHistoricalValue] = useState<ValueAndDisplay>(null);
const context = useFormProviderContext();
const [initialValue, setInitialValue] = useState(null); // State to store initial value
const hasMounted = useRef(false); // Set initial value when the component mounts

useEffect(() => {
if (field.type === 'programState') {
const initialVal = valueAdapter.getInitialValue(field, null, context);
setInitialValue(initialVal);
hasMounted.current = true;
}
}, []);

const {
methods: { control, getValues, getFieldState },
Expand Down Expand Up @@ -67,7 +77,7 @@ export const FormFieldRenderer = ({ field, valueAdapter, repeatOptions }: FormFi
}
});
}
if (sessionMode === 'enter' && (field.historicalExpression || context.previousDomainObjectValue)) {
if (field.historicalExpression || context.previousDomainObjectValue) {
try {
context.processor.getHistoricalValue(field, context).then((value) => {
setHistoricalValue(value);
Expand All @@ -76,16 +86,6 @@ export const FormFieldRenderer = ({ field, valueAdapter, repeatOptions }: FormFi
console.error(error);
}
}

if (field.type === 'programState' && field.questionOptions.enablePreviousValue) {
try {
context.processor.getHistoricalValue(field, context).then((value) => {
setHistoricalValue({value: value.value, display: value.display});
});
} catch (error) {
console.error(error);
}
}
}, []);

useEffect(() => {
Expand Down Expand Up @@ -158,51 +158,56 @@ export const FormFieldRenderer = ({ field, valueAdapter, repeatOptions }: FormFi
/>
);
}

return (
<ErrorBoundary FallbackComponent={ErrorFallback} onReset={noop}>
<Controller
control={control}
name={field.id}
render={({ field: { value, onChange, onBlur } }) => (
<div>
<InputComponent
key={`${field.id}-input-component`}
field={field}
value={value}
errors={errors}
warnings={warnings}
setFieldValue={(val) => {
onChange(val);
onAfterChange(val);
onBlur();
}}
/>
{isUnspecifiedSupported(field) && (
<div className={styles.unspecifiedContainer}>
{field.unspecified && (
<UnspecifiedField
key={`${field.id}-unspecified`}
field={field}
setFieldValue={onChange}
render={({ field: { value, onChange, onBlur } }) => {
const valueToUse = hasMounted.current ? initialValue : value;
return (
<div>
<InputComponent
key={`${field.id}-input-component`}
field={field}
value={valueToUse}
errors={errors}
warnings={warnings}
setFieldValue={(val) => {
onChange(val);
onAfterChange(val);
onBlur();
hasMounted.current = false;
}}
/>
{isUnspecifiedSupported(field) && (
<div className={styles.unspecifiedContainer}>
{field.unspecified && (
<UnspecifiedField
key={`${field.id}-unspecified`}
field={field}
setFieldValue={onChange}
onAfterChange={onAfterChange}
fieldValue={value}
/>
)}
</div>
)}
{historicalValue?.value && (
<div>
<PreviousValueReview
key={`${field.id}-previous-value-review`}
previousValue={historicalValue.value}
displayText={historicalValue.display}
onAfterChange={onAfterChange}
fieldValue={value}
field={field}
/>
)}
</div>
)}
{historicalValue?.value && (
<div>
<PreviousValueReview
key={`${field.id}-previous-value-review`}
previousValue={historicalValue.value}
displayText={historicalValue.display}
onAfterChange={onAfterChange}
field={field}
/>
</div>
)}
</div>
)}
</div>
)}
</div>
);
}}
/>
</ErrorBoundary>
);
Expand Down

0 comments on commit a24d60c

Please sign in to comment.