Skip to content

Commit

Permalink
OH2-318 | Types / Delivery Result CRUD (#612)
Browse files Browse the repository at this point in the history
* Add delivery result type

* correction of description type error

---------

Co-authored-by: Alessandro Domanico <[email protected]>
  • Loading branch information
fogouang and mwithi authored Jun 18, 2024
1 parent 5cbe562 commit 1a6a984
Show file tree
Hide file tree
Showing 33 changed files with 1,111 additions and 1 deletion.
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 @@ -32,6 +32,7 @@ const TypesAdmin = () => {
{ label: t("types.diseases"), value: "diseases" },
{ label: t("types.deliveries"), value: "deliveries" },
{ label: t("types.admissions"), value: "admissions" },
{ label: t("types.deliveryResultType"), value: "deliveryresulttypes" },
{ label: t("types.discharges"), value: "discharges" },
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { useDispatch } from "react-redux";
import { useNavigate } from "react-router";
import {
deleteDeliveryResultType,
deleteDeliveryResultTypeReset,
getDeliveryResultType,
} from "../../../../../../state/types/deliveryResultType/actions";
import { PATHS } from "../../../../../../consts";
import Button from "../../../../button/Button";
import "./styles.scss";
import { setTypeMode } from "../../../../../../state/types/config";
import DeliveryResultTypeTable from "./deliveryResultTypeTable";
import { DeliveryResultTypeDTO } from "../../../../../../generated";

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

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

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

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

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

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

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

export default DelevyResultType;
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
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 { IDeliveryResultTypeFormProps } from "./types";
import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router";
import { IState } from "../../../../../../../types";
import { IDeliveryResultTypeState } from "../../../../../../../state/types/deliveryResultType/types";
import {
formatAllFieldValues,
getFromFields,
} from "../../../../../../../libraries/formDataHandling/functions";
import {
createDeliveryResultTypeReset,
updateDeliveryResultTypeReset,
} from "../../../../../../../state/types/deliveryResultType/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 DeliveryResultTypeForm: FC<IDeliveryResultTypeFormProps> = ({
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 deliveryResultTypesStore = useSelector<
IState,
IDeliveryResultTypeState
>((state) => state.types.deliveryResult);

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

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

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

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

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

export const getInitialFields: (
diseaseType: DeliveryResultTypeDTO | undefined
) => TFields<DeliveryResultTypeFormFieldName> = (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 "./DeliveryResultTypeForm";
export * from "./types";
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
@import "../../../../../../../../node_modules/susy/sass/susy";
@import "../../../../../../../styles/variables";

.deliveryResultTypesForm {
display: inline-block;
flex-direction: column;
align-items: center;
width: 100%;

.formInsertMode {
margin: 0px 0px 20px;
}

.row {
justify-content: space-between;
}

.deliveryResultTypesForm__item {
margin: 7px 0px;
padding: 0px 15px;
width: 50%;
@include susy-media($narrow) {
padding: 0px 10px;
}
@include susy-media($tablet_land) {
padding: 0px 10px;
}
@include susy-media($medium-up) {
width: 25%;
}
@include susy-media($tablet_port) {
width: 50%;
}
@include susy-media($smartphone) {
width: 100%;
}
.textField,
.selectField {
width: 100%;
}

&.halfWidth {
width: 50%;
@include susy-media($smartphone) {
width: 100%;
}
}
&.fullWidth {
width: 100%;
}
}

.deliveryResultTypesForm__buttonSet {
display: flex;
margin-top: 25px;
padding: 0px 15px;
flex-direction: row-reverse;
@include susy-media($smartphone_small) {
display: block;
}

.submit_button,
.reset_button {
.MuiButton-label {
font-size: smaller;
letter-spacing: 1px;
font-weight: 600;
}
button {
@include susy-media($smartphone_small) {
width: 100%;
margin-top: 10px;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { DeliveryResultTypeDTO } from "../../../../../../../generated";
import { TFields } from "../../../../../../../libraries/formDataHandling/types";

export interface IDeliveryResultTypeFormProps {
fields: TFields<DeliveryResultTypeFormFieldName>;
onSubmit: (adm: DeliveryResultTypeDTO) => void;
creationMode: boolean;
submitButtonLabel: string;
resetButtonLabel: string;
isLoading: boolean;
}

export type DeliveryResultTypeFormFieldName = "code" | "description";
Loading

0 comments on commit 1a6a984

Please sign in to comment.