Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(refactor) Use react hook form's isSubmitting state #2141

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const MarkPatientDeceasedForm: React.FC<DefaultPatientWorkspaceProps> = ({ close
const { t } = useTranslation();
const isTablet = useLayoutType() === 'tablet';
const memoizedPatientUuid = useMemo(() => ({ patientUuid }), [patientUuid]);
const [isSubmitting, setIsSubmitting] = useState(false);
const [searchTerm, setSearchTerm] = useState('');
const { causesOfDeath, isLoading: isLoadingCausesOfDeath } = useCausesOfDeath();
const { freeTextFieldConceptUuid } = useConfig<ChartConfig>();
Expand Down Expand Up @@ -74,7 +73,7 @@ const MarkPatientDeceasedForm: React.FC<DefaultPatientWorkspaceProps> = ({ close

const {
control,
formState: { errors },
formState: { errors, isSubmitting },
handleSubmit,
watch,
} = useForm<MarkPatientDeceasedFormSchema>({
Expand All @@ -91,7 +90,6 @@ const MarkPatientDeceasedForm: React.FC<DefaultPatientWorkspaceProps> = ({ close

const onSubmit: SubmitHandler<MarkPatientDeceasedFormSchema> = useCallback(
(data) => {
setIsSubmitting(true);
const { causeOfDeath, deathDate, nonCodedCauseOfDeath } = data;

markPatientDeceased(deathDate, patientUuid, causeOfDeath, nonCodedCauseOfDeath)
Expand All @@ -106,9 +104,6 @@ const MarkPatientDeceasedForm: React.FC<DefaultPatientWorkspaceProps> = ({ close
subtitle: error?.message,
title: t('errorMarkingPatientDeceased', 'Error marking patient deceased'),
});
})
.finally(() => {
setIsSubmitting(false);
});
},
[closeWorkspace, patientUuid, t],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ const StartVisitForm: React.FC<StartVisitFormProps> = ({
const { emrConfiguration } = useEmrConfiguration(isEmrApiModuleInstalled);
const { patientUuid, patient } = usePatient(initialPatientUuid);
const [contentSwitcherIndex, setContentSwitcherIndex] = useState(config.showRecommendedVisitTypeTab ? 0 : 1);
const [isSubmitting, setIsSubmitting] = useState(false);
const visitHeaderSlotState = useMemo(() => ({ patientUuid }), [patientUuid]);
const { activePatientEnrollment, isLoading } = useActivePatientEnrollment(patientUuid);
const { mutate: mutateCurrentVisit } = useVisit(patientUuid);
Expand Down Expand Up @@ -238,7 +237,7 @@ const StartVisitForm: React.FC<StartVisitFormProps> = ({
handleSubmit,
control,
getValues,
formState: { errors, isDirty },
formState: { errors, isDirty, isSubmitting },
setError,
reset,
} = methods;
Expand Down Expand Up @@ -475,7 +474,6 @@ const StartVisitForm: React.FC<StartVisitFormProps> = ({
handleCreateExtraVisitInfo && handleCreateExtraVisitInfo();
}

setIsSubmitting(true);
if (isOnline) {
const visitRequest = visitToEdit?.uuid
? updateVisit(visitToEdit?.uuid, payload, abortController)
Expand Down Expand Up @@ -545,7 +543,6 @@ const StartVisitForm: React.FC<StartVisitFormProps> = ({
// do nothing, this catches any reject promises used for short-circuiting
})
.finally(() => {
setIsSubmitting(false);
mutateCurrentVisit();
mutateVisits();
mutateInfiniteVisits();
Expand All @@ -556,32 +553,28 @@ const StartVisitForm: React.FC<StartVisitFormProps> = ({
visitLocation.uuid,
config.offlineVisitTypeUuid,
payload.startDatetime,
)
.then(
() => {
mutateCurrentVisit();
closeWorkspace({ ignoreChanges: true });
showSnackbar({
isLowContrast: true,
kind: 'success',
subtitle: t('visitStartedSuccessfully', '{{visit}} started successfully', {
visit: t('offlineVisit', 'Offline Visit'),
}),
title: t('visitStarted', 'Visit started'),
});
},
(error: Error) => {
showSnackbar({
title: t('startVisitError', 'Error starting visit'),
kind: 'error',
isLowContrast: false,
subtitle: error?.message,
});
},
)
.finally(() => {
setIsSubmitting(false);
});
).then(
() => {
mutateCurrentVisit();
closeWorkspace({ ignoreChanges: true });
showSnackbar({
isLowContrast: true,
kind: 'success',
subtitle: t('visitStartedSuccessfully', '{{visit}} started successfully', {
visit: t('offlineVisit', 'Offline Visit'),
}),
title: t('visitStarted', 'Visit started'),
});
},
(error: Error) => {
showSnackbar({
title: t('startVisitError', 'Error starting visit'),
kind: 'error',
isLowContrast: false,
subtitle: error?.message,
});
},
);

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ const ImmunizationsForm: React.FC<DefaultPatientWorkspaceProps> = ({
promptBeforeClosing,
}) => {
const { t } = useTranslation();
const [isSubmitting, setIsSubmitting] = useState(false);
const { immunizationsConfig } = useConfig() as ConfigObject;
const { immunizationsConfig } = useConfig<ConfigObject>();
const currentUser = useSession();
const { currentVisit } = useVisit(patientUuid);
const isTablet = useLayoutType() === 'tablet';
Expand Down Expand Up @@ -103,7 +102,7 @@ const ImmunizationsForm: React.FC<DefaultPatientWorkspaceProps> = ({
control,
handleSubmit,
reset,
formState: { errors, isDirty },
formState: { errors, isDirty, isSubmitting },
watch,
} = formProps;

Expand Down Expand Up @@ -140,7 +139,6 @@ const ImmunizationsForm: React.FC<DefaultPatientWorkspaceProps> = ({

const onSubmit = useCallback(
(data: ImmunizationFormInputData) => {
setIsSubmitting(true);
const {
vaccineUuid,
vaccinationDate,
Expand Down Expand Up @@ -188,7 +186,6 @@ const ImmunizationsForm: React.FC<DefaultPatientWorkspaceProps> = ({
abortController,
).then(
() => {
setIsSubmitting(false);
closeWorkspaceWithSavedChanges();
mutate();
showSnackbar({
Expand All @@ -198,7 +195,6 @@ const ImmunizationsForm: React.FC<DefaultPatientWorkspaceProps> = ({
});
},
(err) => {
setIsSubmitting(false);
showSnackbar({
title: t('errorSaving', 'Error saving vaccination'),
kind: 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ const VisitNotesForm: React.FC<DefaultPatientWorkspaceProps> = ({
const memoizedState = useMemo(() => ({ patientUuid }), [patientUuid]);
const { clinicianEncounterRole, encounterNoteTextConceptUuid, encounterTypeUuid, formConceptUuid } =
config.visitNoteConfig;
const [isSubmitting, setIsSubmitting] = useState(false);
const [isLoadingPrimaryDiagnoses, setIsLoadingPrimaryDiagnoses] = useState(false);
const [isLoadingSecondaryDiagnoses, setIsLoadingSecondaryDiagnoses] = useState(false);
const [isSearching, setIsSearching] = useState(false);
Expand Down Expand Up @@ -149,7 +148,14 @@ const VisitNotesForm: React.FC<DefaultPatientWorkspaceProps> = ({
[visitNoteFormSchema, selectedPrimaryDiagnoses, t],
);

const { control, handleSubmit, watch, setValue, formState, clearErrors } = useForm<VisitNotesFormData>({
const {
clearErrors,
control,
formState: { errors, isDirty, isSubmitting },
handleSubmit,
setValue,
watch,
} = useForm<VisitNotesFormData>({
mode: 'onSubmit',
resolver: customResolver,
defaultValues: {
Expand All @@ -158,8 +164,6 @@ const VisitNotesForm: React.FC<DefaultPatientWorkspaceProps> = ({
},
});

const { isDirty } = formState;

useEffect(() => {
promptBeforeClosing(() => isDirty);
}, [isDirty, promptBeforeClosing]);
Expand Down Expand Up @@ -317,10 +321,8 @@ const VisitNotesForm: React.FC<DefaultPatientWorkspaceProps> = ({
const onSubmit = useCallback(
(data: VisitNotesFormData) => {
const { noteDate, clinicalNote, images } = data;
setIsSubmitting(true);

if (!selectedPrimaryDiagnoses.length) {
setIsSubmitting(false);
return;
}

Expand Down Expand Up @@ -413,9 +415,6 @@ const VisitNotesForm: React.FC<DefaultPatientWorkspaceProps> = ({
isLowContrast: false,
subtitle: err?.message,
});
})
.finally(() => {
setIsSubmitting(false);
});
},
[
Expand Down Expand Up @@ -524,7 +523,7 @@ const VisitNotesForm: React.FC<DefaultPatientWorkspaceProps> = ({
labelText={t('enterPrimaryDiagnoses', 'Enter Primary diagnoses')}
placeholder={t('primaryDiagnosisInputPlaceholder', 'Choose a primary diagnosis')}
handleSearch={handleSearch}
error={formState?.errors?.primaryDiagnosisSearch}
error={errors?.primaryDiagnosisSearch}
setIsSearching={setIsSearching}
/>
{error ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const OrderCancellationForm: React.FC<OrderCancellationFormProps> = ({
}) => {
const { t } = useTranslation();
const isTablet = useLayoutType() === 'tablet';
const [isSubmitting, setIsSubmitting] = useState(false);
const [showErrorNotification, setShowErrorNotification] = useState(false);
const { mutate } = usePatientOrders(patientUuid);

Expand All @@ -57,7 +56,7 @@ const OrderCancellationForm: React.FC<OrderCancellationFormProps> = ({
const {
control,
handleSubmit,
formState: { errors, isDirty },
formState: { errors, isDirty, isSubmitting },
} = useForm<CancelOrderFormData>({
mode: 'all',
resolver: zodResolver(cancelOrderSchema),
Expand All @@ -77,7 +76,6 @@ const OrderCancellationForm: React.FC<OrderCancellationFormProps> = ({
(data: CancelOrderFormData) => {
const formData = data;
setShowErrorNotification(false);
setIsSubmitting(true);

const payload = {
fulfillerStatus: 'DECLINED',
Expand All @@ -86,7 +84,6 @@ const OrderCancellationForm: React.FC<OrderCancellationFormProps> = ({

cancelOrder(order, payload).then(
(res) => {
setIsSubmitting(false);
closeWorkspace();
mutate();

Expand All @@ -99,7 +96,6 @@ const OrderCancellationForm: React.FC<OrderCancellationFormProps> = ({
});
},
(err) => {
setIsSubmitting(false);
showSnackbar({
isLowContrast: true,
title: t('errorCancellingOrder', 'Error cancelling order'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ const VitalsAndBiometricsForm: React.FC<DefaultPatientWorkspaceProps> = ({
const { currentVisit } = useVisit(patientUuid);
const { data: conceptUnits, conceptMetadata, conceptRanges, isLoading } = useVitalsConceptMetadata();
const [hasInvalidVitals, setHasInvalidVitals] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [muacColorCode, setMuacColorCode] = useState('');
const [showErrorNotification, setShowErrorNotification] = useState(false);
const [showErrorMessage, setShowErrorMessage] = useState(false);
Expand All @@ -98,7 +97,7 @@ const VitalsAndBiometricsForm: React.FC<DefaultPatientWorkspaceProps> = ({
handleSubmit,
watch,
setValue,
formState: { isDirty },
formState: { isDirty, isSubmitting },
} = useForm<VitalsBiometricsFormData>({
mode: 'all',
resolver: zodResolver(VitalsAndBiometricFormSchema),
Expand Down Expand Up @@ -181,7 +180,6 @@ const VitalsAndBiometricsForm: React.FC<DefaultPatientWorkspaceProps> = ({
.every(([key, value]) => isValueWithinReferenceRange(conceptMetadata, config.concepts[`${key}Uuid`], value));

if (allFieldsAreValid) {
setIsSubmitting(true);
setShowErrorMessage(false);
const abortController = new AbortController();

Expand Down Expand Up @@ -209,8 +207,7 @@ const VitalsAndBiometricsForm: React.FC<DefaultPatientWorkspaceProps> = ({
});
}
})
.catch((err) => {
setIsSubmitting(false);
.catch(() => {
createErrorHandler();
showSnackbar({
title: t('vitalsAndBiometricsSaveError', 'Error saving vitals and biometrics'),
Expand Down
Loading