Skip to content

Commit 5a496c0

Browse files
authored
BN-84 | Use Zustand Store for DiagnosesForm (#24)
* Add. Zustand package for state management * BN-84 | Refactor. Use Zustand store for diagnoses form * BN-84 | Refactor. Remove additional id attribute from diagnosis input entry * BN-84 | Refactor. Rename title as display in diagnosisinput store
1 parent c35b618 commit 5a496c0

22 files changed

+1024
-498
lines changed

.storybook/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const config: StorybookConfig = {
5454
'@styles': path.resolve(__dirname, '../src/styles'),
5555
'@types': path.resolve(__dirname, '../src/types'),
5656
'@utils': path.resolve(__dirname, '../src/utils'),
57+
'@stores': path.resolve(__dirname, '../src/stores'),
5758
};
5859
}
5960

jest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const config: Config.InitialOptions = {
2323
'@services/(.*)$': ['<rootDir>/src/services/$1'],
2424
'@types/(.*)$': ['<rootDir>/src/types/$1'],
2525
'@utils/(.*)$': ['<rootDir>/src/utils/$1'],
26+
'@stores/(.*)$': ['<rootDir>/src/stores/$1'],
2627
'@__mocks__/(.*)$': ['<rootDir>/src/__mocks__/$1'],
2728
},
2829
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"workbox-expiration": "^7.0.0",
3838
"workbox-precaching": "^7.0.0",
3939
"workbox-routing": "^7.0.0",
40-
"workbox-strategies": "^7.0.0"
40+
"workbox-strategies": "^7.0.0",
41+
"zustand": "^5.0.5"
4142
},
4243
"devDependencies": {
4344
"@babel/core": "^7.22.15",

public/locales/locale_en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"DIAGNOSES_CERTAINTY_ARIA_LABEL": "Diagnoses Certainty",
4848
"DIAGNOSES_DUPLICATE_ERROR": "This diagnosis has already been added. Please select a different diagnosis.",
4949
"DIAGNOSES_FORM_TITLE": "Diagnoses",
50+
"DROPDOWN_VALUE_REQUIRED": "Please select a value",
5051
"DIAGNOSES_SEARCH_ARIA_LABEL": "Search for diagnoses",
5152
"DIAGNOSES_SEARCH_PLACEHOLDER": "Search to add new diagnosis",
5253
"DIAGNOSES_SELECT_CERTAINTY": "Select Certainty",

public/locales/locale_es.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"DIAGNOSES_CERTAINTY_ARIA_LABEL": "Certeza del Diagnóstico",
4848
"DIAGNOSES_DUPLICATE_ERROR": "Este diagnóstico ya ha sido agregado. Por favor seleccione un diagnóstico diferente.",
4949
"DIAGNOSES_FORM_TITLE": "Diagnósticos",
50+
"DROPDOWN_VALUE_REQUIRED": "Por favor seleccione un valor",
5051
"DIAGNOSES_SEARCH_ARIA_LABEL": "Buscar diagnósticos",
5152
"DIAGNOSES_SEARCH_PLACEHOLDER": "Buscar para agregar un diagnóstico",
5253
"DIAGNOSES_SELECT_CERTAINTY": "Seleccionar Certeza",

src/components/clinical/consultationPad/ConsultationPad.tsx

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import { Column, Grid, Loading } from '@carbon/react';
99
import * as styles from './styles/ConsultationPad.module.scss';
1010
import BasicForm from '@components/clinical/basicForm/BasicForm';
1111
import DiagnosesForm from '@components/clinical/diagnosesForm/DiagnosesForm';
12-
import { SelectedDiagnosisItemProps } from '@components/clinical/diagnosesForm/SelectedDiagnosisItem';
1312
import { Concept } from '@types/encounterConcepts';
14-
import { ConceptSearch } from '@types/concepts';
1513
import { ConsultationBundle } from '@types/consultationBundle';
1614
import {
1715
postConsultationBundle,
@@ -24,9 +22,8 @@ import {
2422
createBundleEntry,
2523
createConsultationBundle,
2624
} from '@utils/fhir/consultationBundleCreator';
27-
import { CERTAINITY_CONCEPTS } from '@constants/concepts';
2825
import { ERROR_TITLES } from '@constants/errors';
29-
import { Coding } from 'fhir/r4';
26+
import { useDiagnosisStore } from '@stores/diagnosisStore';
3027

3128
interface ConsultationPadProps {
3229
patientUUID: string;
@@ -38,46 +35,13 @@ const ConsultationPad: React.FC<ConsultationPadProps> = ({
3835
onClose,
3936
}) => {
4037
const [isSubmitting, setIsSubmitting] = React.useState(false);
41-
const [selectedDiagnoses, setSelectedDiagnoses] = React.useState<
42-
SelectedDiagnosisItemProps[]
43-
>([]);
4438

4539
const { t } = useTranslation();
4640
const { addNotification } = useNotification();
4741

48-
const handleResultSelection = (selectedItem: ConceptSearch) => {
49-
// Create new diagnosis with certainty handler
50-
const newDiagnosis: SelectedDiagnosisItemProps = {
51-
id: selectedItem.conceptUuid,
52-
title: selectedItem.conceptName,
53-
certaintyConcepts: CERTAINITY_CONCEPTS,
54-
selectedCertainty: null,
55-
handleCertaintyChange: (data) => {
56-
handleCertaintyChange(selectedItem.conceptUuid, data.selectedItem);
57-
},
58-
};
59-
60-
setSelectedDiagnoses([...selectedDiagnoses, newDiagnosis]);
61-
};
62-
63-
const handleRemoveDiagnosis = (index: number) => {
64-
setSelectedDiagnoses((prevDiagnoses) =>
65-
prevDiagnoses.filter((_, i) => i !== index),
66-
);
67-
};
68-
69-
const handleCertaintyChange = (
70-
diagnosisId: string,
71-
selectedCertainty: Coding | null | undefined,
72-
) => {
73-
setSelectedDiagnoses((prevDiagnoses) =>
74-
prevDiagnoses.map((diagnosis) =>
75-
diagnosis.id === diagnosisId
76-
? { ...diagnosis, selectedCertainty: selectedCertainty || null }
77-
: diagnosis,
78-
),
79-
);
80-
};
42+
// Use the diagnosis store
43+
const { selectedDiagnoses, validateAllDiagnoses, reset } =
44+
useDiagnosisStore();
8145

8246
const {
8347
locations,
@@ -158,10 +122,16 @@ const ConsultationPad: React.FC<ConsultationPadProps> = ({
158122

159123
const handleOnPrimaryButtonClick = async () => {
160124
if (!isSubmitting && canSubmitConsultation) {
125+
const isDiagnosesValid = validateAllDiagnoses();
126+
if (!isDiagnosesValid) {
127+
return;
128+
}
129+
161130
try {
162131
setIsSubmitting(true);
163132
await submitConsultation();
164133
setIsSubmitting(false);
134+
reset();
165135
addNotification({
166136
title: t('CONSULTATION_SUBMITTED_SUCCESS_TITLE'),
167137
message: t('CONSULTATION_SUBMITTED_SUCCESS_MESSAGE'),
@@ -269,11 +239,7 @@ const ConsultationPad: React.FC<ConsultationPadProps> = ({
269239
locationSelected={locations[0]}
270240
defaultDate={formattedDate.formattedResult}
271241
/>
272-
<DiagnosesForm
273-
handleResultSelection={handleResultSelection}
274-
selectedDiagnoses={selectedDiagnoses}
275-
handleRemoveDiagnosis={handleRemoveDiagnosis}
276-
/>
242+
<DiagnosesForm />
277243
</>
278244
}
279245
/>

0 commit comments

Comments
 (0)