-
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.
- Loading branch information
Showing
42 changed files
with
1,062 additions
and
107 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
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
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
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/diseases/DiseaseTypes.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 { | ||
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; |
205 changes: 205 additions & 0 deletions
205
...mponents/accessories/admin/types/components/diseases/diseaseTypesForm/DiseaseTypeForm.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 { 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; |
10 changes: 10 additions & 0 deletions
10
src/components/accessories/admin/types/components/diseases/diseaseTypesForm/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 { 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 ?? "" }, | ||
}); |
2 changes: 2 additions & 0 deletions
2
src/components/accessories/admin/types/components/diseases/diseaseTypesForm/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 "./DiseaseTypeForm"; | ||
export * from "./types"; |
Oops, something went wrong.