Skip to content

Commit

Permalink
feature:OH2-317 | Types / Delivery CRUD (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverD3 authored Jun 13, 2024
1 parent 1ec4454 commit ea47d76
Show file tree
Hide file tree
Showing 32 changed files with 1,084 additions and 0 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 @@ -29,6 +29,7 @@ const TypesAdmin = () => {
{ label: t("types.vaccines"), value: "vaccines" },
{ label: t("types.operations"), value: "operations" },
{ label: t("types.diseases"), value: "diseases" },
{ label: t("types.deliveries"), value: "deliveries" },
{ label: t("types.admissions"), value: "admissions" },
];

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 {
deleteDeliveryType,
deleteDeliveryTypeReset,
getDeliveryTypes,
} from "../../../../../../state/types/deliveries/actions";
import { DeliveryTypeDTO } from "../../../../../../generated";
import { PATHS } from "../../../../../../consts";
import DeliveryTypesTable from "./deliveryTypesTable";
import Button from "../../../../button/Button";
import "./styles.scss";
import { setTypeMode } from "../../../../../../state/types/config";

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

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

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

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

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

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

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

export default DeliveryTypes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
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 { IDeliveryTypeFormProps } from "./types";
import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router";
import { IState } from "../../../../../../../types";
import { IDeliveryTypesState } from "../../../../../../../state/types/deliveries/types";
import {
formatAllFieldValues,
getFromFields,
} from "../../../../../../../libraries/formDataHandling/functions";
import {
createDeliveryTypeReset,
updateDeliveryTypeReset,
} from "../../../../../../../state/types/deliveries/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 DeliveryTypeForm: FC<IDeliveryTypeFormProps> = ({
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 deliveryTypesStore = useSelector<IState, IDeliveryTypesState>(
(state) => state.types.deliveries
);

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

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

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

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

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

export const getInitialFields: (
deliveryType: DeliveryTypeDTO | undefined
) => TFields<DeliveryTypeFormFieldName> = (deliveryType) => ({
code: { type: "text", value: deliveryType?.code ?? "" },
description: { type: "text", value: deliveryType?.description ?? "" },
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./DeliveryTypeForm";
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";

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

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

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

.deliveryTypesForm__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%;
}
}

.deliveryTypesForm__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 { DeliveryTypeDTO } from "../../../../../../../generated";
import { TFields } from "../../../../../../../libraries/formDataHandling/types";

export interface IDeliveryTypeFormProps {
fields: TFields<DeliveryTypeFormFieldName>;
onSubmit: (adm: DeliveryTypeDTO) => void;
creationMode: boolean;
submitButtonLabel: string;
resetButtonLabel: string;
isLoading: boolean;
}

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

0 comments on commit ea47d76

Please sign in to comment.