From bf5fef54fa1e76503309d1e045cd55b0090c8ac0 Mon Sep 17 00:00:00 2001 From: Onkar Jadhav Date: Fri, 17 Nov 2023 10:53:23 +0530 Subject: [PATCH] Show fields when copying from previous log --- src/Components/Patient/DailyRounds.tsx | 372 +++++++++++++------------ 1 file changed, 200 insertions(+), 172 deletions(-) diff --git a/src/Components/Patient/DailyRounds.tsx b/src/Components/Patient/DailyRounds.tsx index ff749aed29e..20d9d417ae2 100644 --- a/src/Components/Patient/DailyRounds.tsx +++ b/src/Components/Patient/DailyRounds.tsx @@ -67,8 +67,8 @@ const initError = Object.assign( ); const initialState = { - form: { ...initForm }, - errors: { ...initError }, + form: initForm, + errors: initError, }; const DailyRoundsFormReducer = (state = initialState, action: any) => { @@ -107,7 +107,8 @@ export const DailyRounds = (props: any) => { const [patientName, setPatientName] = useState(""); const [prevReviewInterval, setPreviousReviewInterval] = useState(-1); const [prevAction, setPreviousAction] = useState("NO_ACTION"); - const [hasPreviousLog, setHasPreviousLog] = useState(false); + const [previousLog, setPreviousLog] = useState({}); + const headerText = !id ? "Add Consultation Update" : "Info"; const buttonText = !id ? "Save" : "Continue"; @@ -177,7 +178,7 @@ export const DailyRounds = (props: any) => { const res = await dispatchAction( getDailyReport({ limit: 1, offset: 0 }, { consultationId }) ); - setHasPreviousLog(res.data.count > 0); + setPreviousLog(res.data.results[0]); dispatch({ type: "set_form", form: { @@ -192,7 +193,7 @@ export const DailyRounds = (props: any) => { RHYTHM_CHOICES.find((i) => i.text === res.data.rhythm)?.id) || "0", temperature: parseFloat(res.data.temperature), - clone_last: res.data.count > 0 ? true : false, + clone_last: false, }, }); } @@ -389,6 +390,27 @@ export const DailyRounds = (props: any) => { return ; } + const clonePreviousLog = () => { + const fieldsToNotClone = [ + "taken_at", + "created_date", + "modified_date", + "rounds_type", + "patient_category", + ]; + + const clonedLog = Object.fromEntries( + Object.entries(previousLog).filter( + ([key]) => !fieldsToNotClone.includes(key) + ) + ); + + dispatch({ + type: "set_form", + form: { ...state.form, ...clonedLog, clone_last: true }, + }); + }; + return ( { - {!id && hasPreviousLog && ( + {!id && previousLog && ( { + if (e.value) { + clonePreviousLog(); + } else { + dispatch({ + type: "set_form", + form: initForm, + }); + } + }} label="Copy values from previous log?" /> )} - {(state.form.clone_last === false || id) && ( -
- - - - - {state.form.additional_symptoms?.includes(9) && ( -
- -
- )} +
+ + + - option.desc} - optionValue={(option) => option.text} - value={prevAction} - onChange={(event) => { - handleFormFieldChange(event); - setPreviousAction(event.value); - }} - /> + {state.form.additional_symptoms?.includes(9) && ( +
+ +
+ )} + + option.desc} + optionValue={(option) => option.text} + value={prevAction} + onChange={(event) => { + handleFormFieldChange(event); + setPreviousAction(event.value); + }} + /> - option.text} - optionValue={(option) => option.id} - value={prevReviewInterval} - onChange={(event) => { - handleFormFieldChange(event); - setPreviousReviewInterval(Number(event.value)); - }} - /> + option.text} + optionValue={(option) => option.id} + value={prevReviewInterval} + onChange={(event) => { + handleFormFieldChange(event); + setPreviousReviewInterval(Number(event.value)); + }} + /> - {state.form.rounds_type === "NORMAL" && ( - <> -

Vitals

- - - - - - - - - - - - option.desc} - optionValue={(option) => option.id} - /> - - - - )} -
- )} + {state.form.rounds_type === "NORMAL" && ( + <> +

Vitals

+ + + + + + + + + + + + option.desc} + optionValue={(option) => option.id} + /> + + + + )} +
goBack()} />