Skip to content

Commit

Permalink
OH2-220 | Add reset button in visit and laboratory filters (#498)
Browse files Browse the repository at this point in the history
* update(OH2-220): Add reset button in visit and laboratory filters

* update(Laboratory Filter): Fix disease field not being reset on disease type changed

* update: Fix laboratory and visits default filter date values

* update: Show patient full name in patient input and fix field being reset after filter submitted

* refactor: Remove console log

* fix: Fix visit form filter
  • Loading branch information
SteveGT96 authored Oct 4, 2023
1 parent a60afc0 commit 41ba7ab
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 64 deletions.
12 changes: 8 additions & 4 deletions src/components/accessories/laboratory/Exams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export const Exams: FC = () => {
const labStore = useSelector<IState, ILaboratoriesState>(
(state: IState) => state.laboratories
);
const handleResetFilter = () => {
setFilter(initialFilter as TFilterValues);
};

useEffect(() => {
setFilter((previous) => ({ ...previous, page: page }));
Expand All @@ -80,9 +83,6 @@ export const Exams: FC = () => {
}, []);

useEffect(() => {
if (!isEmpty(filter.patientCode)) {
dispatch(getPatientThunk(filter.patientCode?.toString() ?? "0"));
}
dispatch(searchLabs({ ...filter, paged: true }));
}, [filter]);

Expand Down Expand Up @@ -181,7 +181,11 @@ export const Exams: FC = () => {
)}
{status !== "LOADING" && (
<Permission require="exam.read">
<ExamFilterForm onSubmit={onSubmit} fields={fields} />
<ExamFilterForm
onSubmit={onSubmit}
fields={fields}
handleResetFilter={handleResetFilter}
/>
{status === "SUCCESS_EMPTY" && (
<InfoBox type="info" message={t("common.emptydata")} />
)}
Expand Down
41 changes: 33 additions & 8 deletions src/components/accessories/laboratory/filter/ExamFilterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ import { IState } from "../../../../types";
import { useSelector } from "react-redux";
import moment from "moment";
import { Permission } from "../../../../libraries/permissionUtils/Permission";
import ConfirmationDialog from "../../confirmationDialog/ConfirmationDialog";
import warningIcon from "../../../../assets/warning-icon.png";
import SelectField from "../../selectField/SelectField";

export const ExamFilterForm: FC<IExamFilterProps> = ({ fields, onSubmit }) => {
export const ExamFilterForm: FC<IExamFilterProps> = ({
fields,
onSubmit,
handleResetFilter,
}) => {
const { t } = useTranslation();

const [expanded, setExpanded] = useState(true);
const patient = useSelector(
(state: IState) => state.patients.selectedPatient.data
);
const [openResetConfirmation, setOpenResetConfirmation] = useState(false);

const validationSchema = object({
examName: string(),
Expand Down Expand Up @@ -137,6 +141,12 @@ export const ExamFilterForm: FC<IExamFilterProps> = ({ fields, onSubmit }) => {

const { setFieldValue, handleBlur } = formik;

const handleResetConfirmation = () => {
setOpenResetConfirmation(false);
handleResetFilter();
formik.resetForm();
};

const mapToOptions = (value: ExamDTO) => ({
value: value.description ?? "",
label: value.description ?? "",
Expand Down Expand Up @@ -252,9 +262,6 @@ export const ExamFilterForm: FC<IExamFilterProps> = ({ fields, onSubmit }) => {
isValid={isValid("patientCode")}
errorText={getErrorText("patientCode")}
onBlur={onBlurCallback("patientCode")}
initialValue={
isEmpty(formik.values.patientCode) ? undefined : patient
}
/>
</div>
</Permission>
Expand Down Expand Up @@ -338,13 +345,31 @@ export const ExamFilterForm: FC<IExamFilterProps> = ({ fields, onSubmit }) => {
</div>
</div>
<div className="filterForm__buttonSet">
<Button type="submit" color="primary" variant="contained">
<Button
type="reset"
variant="text"
onClick={() => setOpenResetConfirmation(true)}
className="reset_button"
>
{t("common.reset")}
</Button>
<Button variant="contained" color="primary" type="submit">
{t("lab.filter")}
</Button>
</div>
</form>
</AccordionDetails>
</Accordion>
<ConfirmationDialog
isOpen={openResetConfirmation}
title={t("common.reset").toUpperCase()}
info={t("common.resetform")}
icon={warningIcon}
primaryButtonLabel={t("common.reset")}
secondaryButtonLabel={t("common.discard")}
handlePrimaryButtonClick={handleResetConfirmation}
handleSecondaryButtonClick={() => setOpenResetConfirmation(false)}
/>
</div>
);
};
1 change: 1 addition & 0 deletions src/components/accessories/laboratory/filter/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
flex-wrap: wrap;
gap: 8px;
width: 100%;
justify-content: flex-end;

.MuiButton-label {
font-size: smaller;
Expand Down
1 change: 1 addition & 0 deletions src/components/accessories/laboratory/filter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TFields } from "../../../../libraries/formDataHandling/types";
export interface IExamFilterProps {
fields: TFields<ExamFilterFormFieldName>;
onSubmit: (values: any) => void;
handleResetFilter: () => void;
}

export type TFilterValues = {
Expand Down
37 changes: 22 additions & 15 deletions src/components/accessories/opds/Opds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useTranslation } from "react-i18next";
import { useDispatch } from "react-redux";
import { useNavigate } from "react-router";
import InfoBox from "../infoBox/InfoBox";
import { initialFilterFields } from "./consts";
import { initialFilter, initialFilterFields } from "./consts";
import { OpdFilterForm } from "./filter/OpdFilterForm";
import "./styles.scss";
import { OpdTable } from "./table/OpdTable";
Expand All @@ -24,14 +24,13 @@ export const Opds: FC = () => {
const fields = initialFilterFields;
const { t } = useTranslation();
const dispatch = useDispatch();
const navigate = useNavigate();

const [filter, setFilter] = useState({} as TFilterValues);
const [filter, setFilter] = useState(initialFilter as TFilterValues);

const { data, status, error, page, pageInfo, handlePageChange } = useOpds();

useEffect(() => {
dispatch(searchOpds({ ...filter, paged: true }));
dispatch(searchOpds({ ...filter, paged: false }));
}, [filter]);

useEffect(() => {
Expand All @@ -42,19 +41,15 @@ export const Opds: FC = () => {
setFilter({ ...values, page: 0, size: filter.size });
};

const handleResetFilter = () => {
setFilter(initialFilter as TFilterValues);
};

const onPageChange = (e: any, page: number) => handlePageChange(e, page - 1);

const errorMessage = error || t("common.somethingwrong");

useEffect(() => {
dispatch(
searchOpds({
...getFromFields(fields, "value"),
page: 0,
size: 80,
paged: true,
})
);
dispatch(getDiseasesOpd());
dispatch(getDiseaseTypes());
dispatch(getWards());
Expand All @@ -71,7 +66,11 @@ export const Opds: FC = () => {
case "FAIL":
return (
<Permission require="opd.read">
<OpdFilterForm onSubmit={onSubmit} fields={fields} />
<OpdFilterForm
onSubmit={onSubmit}
fields={fields}
handleResetFilter={handleResetFilter}
/>
<InfoBox type="error" message={errorMessage} />
</Permission>
);
Expand All @@ -86,15 +85,23 @@ export const Opds: FC = () => {
case "SUCCESS_EMPTY":
return (
<Permission require="opd.read">
<OpdFilterForm onSubmit={onSubmit} fields={fields} />
<OpdFilterForm
onSubmit={onSubmit}
fields={fields}
handleResetFilter={handleResetFilter}
/>
<InfoBox type="info" message={t("common.emptydata")} />
</Permission>
);

case "SUCCESS":
return (
<Permission require="opd.read">
<OpdFilterForm onSubmit={onSubmit} fields={fields} />
<OpdFilterForm
onSubmit={onSubmit}
fields={fields}
handleResetFilter={handleResetFilter}
/>
<OpdTable data={data ?? []} />
<Pagination
page={(pageInfo?.page ?? 0) + 1}
Expand Down
13 changes: 12 additions & 1 deletion src/components/accessories/opds/consts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import moment from "moment";
import {
fixFilterDateFrom,
fixFilterDateTo,
} from "../../../libraries/formDataHandling/functions";
import { TFields } from "../../../libraries/formDataHandling/types";
import { OpdFilterFormFieldName } from "./filter/types";
import { OpdFilterFormFieldName, TFilterValues } from "./filter/types";

export const initialFilterFields: TFields<OpdFilterFormFieldName> = {
ageFrom: { type: "number", value: "" },
Expand All @@ -17,3 +21,10 @@ export const initialFilterFields: TFields<OpdFilterFormFieldName> = {
patientCode: { type: "number", value: "" },
wardCode: { type: "text", value: "" },
};

export const initialFilter: TFilterValues = {
dateFrom: fixFilterDateFrom(moment().subtract(1, "years").toISOString()),
dateTo: fixFilterDateTo(moment().toISOString()),
page: 0,
size: 80,
};
38 changes: 33 additions & 5 deletions src/components/accessories/opds/filter/OpdFilterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { differenceInSeconds } from "date-fns";
import { useFormik } from "formik";
import get from "lodash.get";
import has from "lodash.has";
import React, { useCallback, useState } from "react";
import React, { useCallback, useEffect, useState } from "react";
import { FC, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { date, number, object, string } from "yup";
Expand All @@ -35,12 +35,19 @@ import { IState } from "../../../../types";
import { useSelector } from "react-redux";
import moment from "moment";
import { Permission } from "../../../../libraries/permissionUtils/Permission";
import ConfirmationDialog from "../../confirmationDialog/ConfirmationDialog";
import warningIcon from "../../../../assets/warning-icon.png";

export const OpdFilterForm: FC<IOpdFilterProps> = ({ fields, onSubmit }) => {
export const OpdFilterForm: FC<IOpdFilterProps> = ({
fields,
onSubmit,
handleResetFilter,
}) => {
const { t } = useTranslation();

const [expanded, setExpanded] = useState(true);
const [diseaseTypeCode, setDiseaseTypeCode] = useState("");
const [openResetConfirmation, setOpenResetConfirmation] = useState(false);

const validationSchema = object({
diseaseCode: string(),
Expand Down Expand Up @@ -175,6 +182,11 @@ export const OpdFilterForm: FC<IOpdFilterProps> = ({ fields, onSubmit }) => {

const { setFieldValue, handleBlur } = formik;

const handleResetConfirmation = () => {
setOpenResetConfirmation(false);
handleResetFilter();
formik.resetForm();
};
const mapToOptions = (value: DiseaseTypeDTO | DiseaseDTO | WardDTO) => ({
value: value.code ?? "",
label: value.description ?? "",
Expand Down Expand Up @@ -290,6 +302,7 @@ export const OpdFilterForm: FC<IOpdFilterProps> = ({ fields, onSubmit }) => {
}
if (fieldName === "diseaseTypeCode") {
setDiseaseTypeCode((value ?? "") as string);
formik.setFieldValue("diseaseCode", "");
}
},
[setFieldValue, handleBlur]
Expand Down Expand Up @@ -505,16 +518,31 @@ export const OpdFilterForm: FC<IOpdFilterProps> = ({ fields, onSubmit }) => {
</div>
</div>
<div className="filterForm__buttonSet">
<Button
type="reset"
variant="text"
onClick={() => setOpenResetConfirmation(true)}
className="reset_button"
>
{t("common.reset")}
</Button>
<Button variant="contained" color="primary" type="submit">
{t("opd.filter")}
</Button>
</div>
</form>
</AccordionDetails>
</Accordion>
<ConfirmationDialog
isOpen={openResetConfirmation}
title={t("common.reset").toUpperCase()}
info={t("common.resetform")}
icon={warningIcon}
primaryButtonLabel={t("common.reset")}
secondaryButtonLabel={t("common.discard")}
handlePrimaryButtonClick={handleResetConfirmation}
handleSecondaryButtonClick={() => setOpenResetConfirmation(false)}
/>
</div>
);
};
function useEffect(arg0: () => void, arg1: never[]) {
throw new Error("Function not implemented.");
}
11 changes: 7 additions & 4 deletions src/components/accessories/opds/filter/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@
}
.filterForm__buttonSet {
display: flex;
justify-content: flex-end;
width: 100%;
.MuiButton-contained {
width: 24%;
gap: 8px;

.MuiButton-root {
width: 16%;
@include susy-media($tablet_land) {
width: 32%;
width: 26%;
}
@include susy-media($phone) {
width: 100%;
width: 50%;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/components/accessories/opds/filter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TFields } from "../../../../libraries/formDataHandling/types";
export interface IOpdFilterProps {
fields: TFields<OpdFilterFormFieldName>;
onSubmit: (values: any) => void;
handleResetFilter: () => void;
}
export type IStatus = "" | "A" | "R";
export type ISex = "" | "F" | "M" | "U";
Expand Down
2 changes: 1 addition & 1 deletion src/components/accessories/patientPicker/PatientPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ const PatientPicker: FC<IProps> = ({
label={label}
name={fieldName}
variant="outlined"
value={value.firstName ?? ""}
value={value.name ?? ""}
type={"text"}
onBlur={handleOnBlur}
onMouseDown={handleCriteriaChange}
Expand Down
Loading

0 comments on commit 41ba7ab

Please sign in to comment.