Skip to content

Commit

Permalink
feature:OH2-320 | Types / Medical CRUD (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverD3 authored Jun 18, 2024
1 parent 7b98744 commit e6f7e27
Show file tree
Hide file tree
Showing 32 changed files with 1,089 additions and 2 deletions.
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 @@ -34,6 +34,7 @@ const TypesAdmin = () => {
{ label: t("types.admissions"), value: "admissions" },
{ label: t("types.deliveryResultType"), value: "deliveryresulttypes" },
{ label: t("types.discharges"), value: "discharges" },
{ label: t("types.medicals"), value: "medicals" },
{ label: t("types.pregnantTreatment"), value: "pregnanttreatmenttypes" },
];

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 {
deleteMedicalType,
deleteMedicalTypeReset,
getMedicalTypes,
} from "../../../../../../state/types/medicals/actions";
import { MedicalTypeDTO } from "../../../../../../generated";
import { PATHS } from "../../../../../../consts";
import Button from "../../../../button/Button";
import "./styles.scss";
import { setTypeMode } from "../../../../../../state/types/config";
import MedicalTypesTable from "./medicalTypesTable";

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

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

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

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

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

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

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

export default MedicalTypes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useTranslation } from "react-i18next";
import React, { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Navigate, useLocation, useParams } from "react-router";
import { MedicalTypeDTO } from "../../../../../../../generated";
import { IState } from "../../../../../../../types";
import { ApiResponse } from "../../../../../../../state/types";
import { updateMedicalType } from "../../../../../../../state/types/medicals/actions";
import { PATHS } from "../../../../../../../consts";
import { getInitialFields } from "../medicalTypesForm/consts";
import MedicalTypeForm from "../medicalTypesForm/MedicalTypeForm";
import { setTypeMode } from "../../../../../../../state/types/config";
import "./styles.scss";

export const EditMedicalType = () => {
const dispatch = useDispatch();
const { t } = useTranslation();
const { state }: { state: MedicalTypeDTO | undefined } = useLocation();
const { code } = useParams();
const update = useSelector<IState, ApiResponse<MedicalTypeDTO>>(
(state) => state.types.medicals.update
);

const handleSubmit = (value: MedicalTypeDTO) => {
dispatch(updateMedicalType(value));
};

useEffect(() => {
dispatch(setTypeMode("edit"));
});

if (state?.code !== code) {
return <Navigate to={PATHS.admin_medicals_types} />;
}

return (
<div className="editMedicalType">
<h3 className="title">{t("medicalTypes.editMedicalType")}</h3>
<MedicalTypeForm
creationMode={false}
onSubmit={handleSubmit}
isLoading={!!update.isLoading}
resetButtonLabel={t("common.cancel")}
submitButtonLabel={t("medicalTypes.updateMedicalType")}
fields={getInitialFields(state)}
/>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./EditMedicalType";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.editMedicalType {
.title {
margin-bottom: 10px;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import MedicalTypes from "./MedicalTypes";

export default MedicalTypes;
export * from "./editMedicalType";
export * from "./newMedicalType";
export * from "./medicalTypesForm";
export * from "./medicalTypesTable";
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 { IMedicalTypeFormProps } from "./types";
import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router";
import { IState } from "../../../../../../../types";
import { IMedicalTypesState } from "../../../../../../../state/types/medicals/types";
import {
formatAllFieldValues,
getFromFields,
} from "../../../../../../../libraries/formDataHandling/functions";
import {
createMedicalTypeReset,
updateMedicalTypeReset,
} from "../../../../../../../state/types/medicals/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 MedicalTypeForm: FC<IMedicalTypeFormProps> = ({
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 medicalTypesStore = useSelector<IState, IMedicalTypesState>(
(state) => state.types.medicals
);

const errorMessage = useMemo(
() =>
(creationMode
? medicalTypesStore.create.error?.message
: medicalTypesStore.update.error?.message) ??
t("common.somethingwrong"),
[
creationMode,
t,
medicalTypesStore.create.error?.message,
medicalTypesStore.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(createMedicalTypeReset());
} else {
dispatch(updateMedicalTypeReset());
}
}, [creationMode, dispatch]);

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

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

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

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

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

0 comments on commit e6f7e27

Please sign in to comment.