Skip to content

Commit

Permalink
OH2-304 | Implement disease create and update (#595)
Browse files Browse the repository at this point in the history
* feature(OH2-304): Implement disease create and update operations

* fix: Fix mock server diseases fixtures import

* chore: Rename admin pages paths

* update: Use consts for deseases/operations/wards path values and update diseases routes data

---------

Co-authored-by: SteveGT96 <[email protected]>
  • Loading branch information
SteveGT96 and SteveGT96 authored Jun 5, 2024
1 parent bc434ff commit 774128e
Show file tree
Hide file tree
Showing 24 changed files with 740 additions and 54 deletions.
24 changes: 21 additions & 3 deletions src/components/accessories/admin/diseases/Diseases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,42 @@ import { useNavigate } from "react-router";
import { PATHS } from "../../../../consts";
import { getAllDiseases } from "../../../../state/diseases/actions";
import { getDiseaseTypes } from "../../../../state/diseaseTypes/actions";
import Button from "../../button/Button";
import { useTranslation } from "react-i18next";

export const Diseases = () => {
const navigate = useNavigate();
const dispatch = useDispatch();
const { t } = useTranslation();

useEffect(() => {
dispatch(getAllDiseases());
dispatch(getDiseaseTypes());
}, [dispatch]);

const handleEdit = (row: DiseaseDTO) => {
navigate(PATHS.diseases_edit.replace("#id", row.code!), { state: row });
navigate(PATHS.admin_diseases_edit.replace("#id", row.code!), {
state: row,
});
};

return (
<div className={classes.diseases}>
<div className={classes.actions}></div>
<DiseaseTable onEdit={handleEdit} />
<DiseaseTable
onEdit={handleEdit}
headerActions={
<Button
onClick={() => {
navigate(PATHS.admin_diseases_new);
}}
type="button"
variant="contained"
color="primary"
>
{t("disease.addDisease")}
</Button>
}
/>
</div>
);
};
277 changes: 277 additions & 0 deletions src/components/accessories/admin/diseases/diseaseForm/DiseaseForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
import { useFormik } from "formik";
import { get, has } from "lodash";
import React, {
FC,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { useTranslation } from "react-i18next";
import { object, string } from "yup";
import warningIcon from "../../../../../assets/warning-icon.png";
import {
formatAllFieldValues,
getFromFields,
} from "../../../../../libraries/formDataHandling/functions";
import checkIcon from "../../../../../assets/check-icon.png";
import Button from "../../../button/Button";
import ConfirmationDialog from "../../../confirmationDialog/ConfirmationDialog";
import TextField from "../../../textField/TextField";
import "./styles.scss";
import { IDiseaseProps } from "./types";
import CheckboxField from "../../../checkboxField/CheckboxField";
import { useDispatch, useSelector } from "react-redux";
import { IState } from "../../../../../types";
import InfoBox from "../../../infoBox/InfoBox";
import { useNavigate } from "react-router";
import { IDiseaseState } from "../../../../../state/diseases/types";
import { PATHS } from "../../../../../consts";
import {
createDiseaseReset,
updateDiseaseReset,
} from "../../../../../state/diseases/actions";
import AutocompleteField from "../../../autocompleteField/AutocompleteField";
import { getDiseaseTypes } from "../../../../../state/diseaseTypes/actions";

const DiseaseForm: FC<IDiseaseProps> = ({
fields,
onSubmit,
creationMode,
submitButtonLabel,
resetButtonLabel,
isLoading,
}) => {
const dispatch = useDispatch();
const { t } = useTranslation();
const navigate = useNavigate();
const infoBoxRef = useRef<HTMLDivElement>(null);
const [openResetConfirmation, setOpenResetConfirmation] = useState(false);

const diseaseStore = useSelector<IState, IDiseaseState>(
(state) => state.diseases
);

const diseasesTypeState = useSelector(
(state: IState) => state.diseaseTypes.getDiseaseTypes
);

const diseasesTypeOptions = useMemo(
() =>
diseasesTypeState.data?.map((item) => ({
value: item.code,
label: item.description,
})) ?? [],
[diseasesTypeState.data]
);

const errorMessage = useMemo(
() =>
(creationMode
? diseaseStore.create.error?.message
: diseaseStore.update.error?.message) ?? t("common.somethingwrong"),
[
creationMode,
t,
diseaseStore.create.error?.message,
diseaseStore.update.error?.message,
]
);

const initialValues = getFromFields(fields, "value");

const validationSchema = object({
code: string().required(t("common.required")),
description: string().required(t("common.required")),
diseaseType: string().required(t("common.required")),
});

const formik = useFormik({
initialValues,
validationSchema,
enableReinitialize: true,
onSubmit: (values) => {
const formattedValues = formatAllFieldValues(fields, values);
onSubmit(formattedValues as any);
},
});

const { setFieldValue, handleBlur } = formik;

const isValid = (fieldName: string): boolean => {
return has(formik.touched, fieldName) && has(formik.errors, fieldName);
};

const getErrorText = (fieldName: string): string => {
return has(formik.touched, fieldName)
? (get(formik.errors, fieldName) as string)
: "";
};

const handleResetConfirmation = () => {
setOpenResetConfirmation(false);
navigate(-1);
};

const handleCheckboxChange = useCallback(
(fieldName: string) => (value: boolean) => {
setFieldValue(fieldName, value ? "true" : "false");
},
[setFieldValue]
);

const onBlurCallback = useCallback(
(fieldName: string) =>
(e: React.FocusEvent<HTMLDivElement>, value: string) => {
handleBlur(e);
setFieldValue(fieldName, value);
},
[handleBlur, setFieldValue]
);

const cleanUp = useCallback(() => {
if (creationMode) {
dispatch(createDiseaseReset());
} else {
dispatch(updateDiseaseReset());
}
}, [creationMode, dispatch]);

useEffect(() => {
dispatch(getDiseaseTypes());
}, [dispatch]);

useEffect(() => {
return cleanUp;
}, [cleanUp]);

return (
<div className="diseaseForm">
<form className="diseaseForm__form" onSubmit={formik.handleSubmit}>
<div className="row start-sm center-xs">
<div className="diseaseForm__item">
<TextField
field={formik.getFieldProps("code")}
theme="regular"
label={t("disease.code")}
isValid={isValid("code")}
errorText={getErrorText("code")}
onBlur={formik.handleBlur}
type="text"
disabled={isLoading || !creationMode}
/>
</div>
<div className="diseaseForm__item">
<AutocompleteField
fieldName="diseaseType"
fieldValue={formik.values.diseaseType}
label={t("disease.diseaseType")}
isValid={isValid("diseaseType")}
errorText={getErrorText("diseaseType")}
onBlur={onBlurCallback("diseaseType")}
options={diseasesTypeOptions}
loading={diseasesTypeState.status === "LOADING"}
disabled={isLoading}
/>
</div>
<div className="diseaseForm__item">
<TextField
field={formik.getFieldProps("description")}
theme="regular"
label={t("disease.name")}
isValid={isValid("description")}
errorText={getErrorText("description")}
onBlur={formik.handleBlur}
type="text"
disabled={isLoading}
/>
</div>
</div>

<div className="row start-sm center-xs">
<div className="diseaseForm__item">
<CheckboxField
fieldName={"opdInclude"}
checked={formik.values.opdInclude === "true"}
label={t("disease.opdInclude")}
onChange={handleCheckboxChange("opdInclude")}
/>
</div>
<div className="diseaseForm__item">
<CheckboxField
fieldName={"ipdInInclude"}
checked={formik.values.ipdInInclude === "true"}
label={t("disease.ipdInInclude")}
onChange={handleCheckboxChange("ipdInInclude")}
/>
</div>
<div className="diseaseForm__item">
<CheckboxField
fieldName={"ipdOutInclude"}
checked={formik.values.ipdOutInclude === "true"}
label={t("disease.ipdOutInclude")}
onChange={handleCheckboxChange("ipdOutInclude")}
/>
</div>
</div>

<div className="diseaseForm__buttonSet">
<div className="submit_button">
<Button type="submit" variant="contained" disabled={isLoading}>
{submitButtonLabel}
</Button>
</div>
<div className="reset_button">
<Button
type="reset"
variant="text"
disabled={isLoading}
onClick={() => setOpenResetConfirmation(true)}
>
{resetButtonLabel}
</Button>
</div>
</div>
<ConfirmationDialog
isOpen={openResetConfirmation}
title={resetButtonLabel.toUpperCase()}
info={t("common.resetform")}
icon={warningIcon}
primaryButtonLabel={t("common.ok")}
secondaryButtonLabel={t("common.discard")}
handlePrimaryButtonClick={handleResetConfirmation}
handleSecondaryButtonClick={() => setOpenResetConfirmation(false)}
/>
{(creationMode
? diseaseStore.create.status === "FAIL"
: diseaseStore.update.status === "FAIL") && (
<div ref={infoBoxRef} className="info-box-container">
<InfoBox type="error" message={errorMessage} />
</div>
)}
<ConfirmationDialog
isOpen={
!!(creationMode
? diseaseStore.create.hasSucceeded
: diseaseStore.update.hasSucceeded)
}
title={creationMode ? t("disease.created") : t("disease.updated")}
icon={checkIcon}
info={
creationMode
? t("disease.createSuccess")
: t("disease.updateSuccess", { code: formik.values.code })
}
primaryButtonLabel="Ok"
handlePrimaryButtonClick={() => {
navigate(PATHS.admin_diseases);
}}
handleSecondaryButtonClick={() => ({})}
/>
</form>
</div>
);
};

export default DiseaseForm;
23 changes: 23 additions & 0 deletions src/components/accessories/admin/diseases/diseaseForm/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TFields } from "../../../../../libraries/formDataHandling/types";
import { DiseaseFormFieldName } from ".";
import { DiseaseDTO } from "../../../../../generated";

export const getInitialFields: (
disease: DiseaseDTO | undefined
) => TFields<DiseaseFormFieldName> = (disease) => ({
code: { type: "text", value: disease?.code ?? "" },
description: { type: "text", value: disease?.description ?? "" },
diseaseType: { type: "text", value: disease?.diseaseType?.code ?? "" },
opdInclude: {
type: "boolean",
value: disease?.opdInclude ? "true" : "false",
},
ipdInInclude: {
type: "boolean",
value: disease?.ipdInInclude ? "true" : "false",
},
ipdOutInclude: {
type: "boolean",
value: disease?.ipdOutInclude ? "true" : "false",
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./DiseaseForm";
export * from "./types";
Loading

0 comments on commit 774128e

Please sign in to comment.