Skip to content

Commit

Permalink
OH2-293 | Disease Types CRUD (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverD3 authored Jun 13, 2024
1 parent d78ab1c commit 0c4fdce
Show file tree
Hide file tree
Showing 42 changed files with 1,062 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/components/accessories/admin/diseases/Diseases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import DiseaseTable from "./diseaseTable";
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";
import { getDiseaseTypes } from "../../../../state/types/diseases";

export const Diseases = () => {
const navigate = useNavigate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
updateDiseaseReset,
} from "../../../../../state/diseases/actions";
import AutocompleteField from "../../../autocompleteField/AutocompleteField";
import { getDiseaseTypes } from "../../../../../state/diseaseTypes/actions";
import { getDiseaseTypes } from "../../../../../state/types/diseases";

const DiseaseForm: FC<IDiseaseProps> = ({
fields,
Expand All @@ -54,7 +54,7 @@ const DiseaseForm: FC<IDiseaseProps> = ({
);

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

const diseasesTypeOptions = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const DiseaseTable: FunctionComponent<IOwnProps> = ({
{ label: string; value: string }[]
>(
(state) =>
state.diseaseTypes.getDiseaseTypes.data?.map((item) => ({
state.types.diseases.getAll.data?.map((item) => ({
value: item.code,
label: item.description ?? "",
})) ?? []
Expand Down
1 change: 1 addition & 0 deletions src/components/accessories/admin/types/TypesAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const TypesAdmin = () => {
defaultTypeOption,
{ label: t("types.vaccines"), value: "vaccines" },
{ label: t("types.operations"), value: "operations" },
{ label: t("types.diseases"), value: "diseases" },
];

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { useDispatch } from "react-redux";
import { useNavigate } from "react-router";
import {
deleteDiseaseType,
deleteDiseaseTypeReset,
getDiseaseTypes,
} from "../../../../../../state/types/diseases/actions";
import { DiseaseTypeDTO } from "../../../../../../generated";
import { PATHS } from "../../../../../../consts";
import DiseaseTypesTable from "./diseaseTypesTable";
import Button from "../../../../button/Button";
import "./styles.scss";
import { setTypeMode } from "../../../../../../state/types/config";

const DiseaseTypes = () => {
const navigate = useNavigate();
const dispatch = useDispatch();

useEffect(() => {
dispatch(getDiseaseTypes());
dispatch(setTypeMode("manage"));

return () => {
dispatch(deleteDiseaseTypeReset());
};
}, [dispatch]);

const handleEdit = (row: DiseaseTypeDTO) => {
navigate(PATHS.admin_diseases_types_edit.replace(":code", row.code!), {
state: row,
});
};

const handleDelete = (row: DiseaseTypeDTO) => {
dispatch(deleteDiseaseType(row.code ?? ""));
};

const { t } = useTranslation();
return (
<>
<h3>{t("diseaseTypes.title")}</h3>

<div className="diseaseTypes">
<DiseaseTypesTable
onEdit={handleEdit}
onDelete={handleDelete}
headerActions={
<Button
onClick={() => {
navigate("./new");
}}
type="button"
variant="contained"
color="primary"
>
{t("diseaseTypes.addDiseaseType")}
</Button>
}
/>
</div>
</>
);
};

export default DiseaseTypes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
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 checkIcon from "../../../../../../../assets/check-icon.png";
import "./styles.scss";
import { IDiseaseTypeFormProps } from "./types";
import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router";
import { IState } from "../../../../../../../types";
import { IDiseaseTypesState } from "../../../../../../../state/types/diseases/types";
import {
formatAllFieldValues,
getFromFields,
} from "../../../../../../../libraries/formDataHandling/functions";
import {
createDiseaseTypeReset,
updateDiseaseTypeReset,
} from "../../../../../../../state/types/diseases/actions";
import TextField from "../../../../../textField/TextField";
import Button from "../../../../../button/Button";
import ConfirmationDialog from "../../../../../confirmationDialog/ConfirmationDialog";
import InfoBox from "../../../../../infoBox/InfoBox";
import { PATHS } from "../../../../../../../consts";

const DiseaseTypeForm: FC<IDiseaseTypeFormProps> = ({
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 diseaseTypesStore = useSelector<IState, IDiseaseTypesState>(
(state) => state.types.diseases
);

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

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

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

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

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 cleanUp = useCallback(() => {
if (creationMode) {
dispatch(createDiseaseTypeReset());
} else {
dispatch(updateDiseaseTypeReset());
}
}, [creationMode, dispatch]);

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

return (
<div className="diseaseTypesForm">
<form className="diseaseTypesForm__form" onSubmit={formik.handleSubmit}>
<div className="row start-sm center-xs">
<div className="diseaseTypesForm__item halfWidth">
<TextField
field={formik.getFieldProps("code")}
theme="regular"
label={t("diseaseTypes.code")}
isValid={isValid("code")}
errorText={getErrorText("code")}
onBlur={formik.handleBlur}
type="text"
disabled={isLoading || !creationMode}
/>
</div>
<div className="diseaseTypesForm__item halfWidth">
<TextField
field={formik.getFieldProps("description")}
theme="regular"
label={t("diseaseTypes.description")}
isValid={isValid("description")}
errorText={getErrorText("description")}
onBlur={formik.handleBlur}
type="text"
disabled={isLoading}
/>
</div>
</div>

<div className="diseaseTypesForm__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={
creationMode
? t("diseaseTypes.cancelCreation")
: t("diseaseTypes.cancelUpdate")
}
icon={warningIcon}
primaryButtonLabel={t("common.ok")}
secondaryButtonLabel={t("common.discard")}
handlePrimaryButtonClick={handleResetConfirmation}
handleSecondaryButtonClick={() => setOpenResetConfirmation(false)}
/>
{(creationMode
? diseaseTypesStore.create.status === "FAIL"
: diseaseTypesStore.update.status === "FAIL") && (
<div ref={infoBoxRef} className="info-box-container">
<InfoBox type="error" message={errorMessage} />
</div>
)}
<ConfirmationDialog
isOpen={
!!(creationMode
? diseaseTypesStore.create.hasSucceeded
: diseaseTypesStore.update.hasSucceeded)
}
title={
creationMode ? t("diseaseTypes.created") : t("diseaseTypes.updated")
}
icon={checkIcon}
info={
creationMode
? t("diseaseTypes.createSuccess")
: t("diseaseTypes.updateSuccess", { code: formik.values.code })
}
primaryButtonLabel="Ok"
handlePrimaryButtonClick={() => {
navigate(PATHS.admin_diseases_types);
}}
handleSecondaryButtonClick={() => ({})}
/>
</form>
</div>
);
};

export default DiseaseTypeForm;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { DiseaseTypeFormFieldName } from ".";
import { DiseaseTypeDTO } from "../../../../../../../generated";
import { TFields } from "../../../../../../../libraries/formDataHandling/types";

export const getInitialFields: (
diseaseType: DiseaseTypeDTO | undefined
) => TFields<DiseaseTypeFormFieldName> = (diseaseType) => ({
code: { type: "text", value: diseaseType?.code ?? "" },
description: { type: "text", value: diseaseType?.description ?? "" },
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./DiseaseTypeForm";
export * from "./types";
Loading

0 comments on commit 0c4fdce

Please sign in to comment.