-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
32 changed files
with
1,089 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/components/accessories/admin/types/components/medicals/MedicalTypes.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
49 changes: 49 additions & 0 deletions
49
...omponents/accessories/admin/types/components/medicals/editMedicalType/EditMedicalType.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
src/components/accessories/admin/types/components/medicals/editMedicalType/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./EditMedicalType"; |
5 changes: 5 additions & 0 deletions
5
src/components/accessories/admin/types/components/medicals/editMedicalType/styles.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.editMedicalType { | ||
.title { | ||
margin-bottom: 10px; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/components/accessories/admin/types/components/medicals/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
205 changes: 205 additions & 0 deletions
205
...mponents/accessories/admin/types/components/medicals/medicalTypesForm/MedicalTypeForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
10 changes: 10 additions & 0 deletions
10
src/components/accessories/admin/types/components/medicals/medicalTypesForm/consts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?? "" }, | ||
}); |
2 changes: 2 additions & 0 deletions
2
src/components/accessories/admin/types/components/medicals/medicalTypesForm/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./MedicalTypeForm"; | ||
export * from "./types"; |
Oops, something went wrong.