Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort wards by ward number in filters + adds utility method: compareBy #6852

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Components/Diagnosis/LegacyDiagnosesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "./types";
import { useTranslation } from "react-i18next";
import CareIcon from "../../CAREUI/icons/CareIcon";
import { compareBy } from "../../Utils/utils";

interface Props {
diagnoses: ConsultationDiagnosis[];
Expand All @@ -22,7 +23,7 @@ function groupDiagnoses(diagnoses: ConsultationDiagnosis[]) {
for (const status of ActiveConditionVerificationStatuses) {
groupedDiagnoses[status] = diagnoses
.filter((d) => d.verification_status === status)
.sort((a, b) => Number(b.is_principal) - Number(a.is_principal));
.sort(compareBy("is_principal"));
}

return groupedDiagnoses;
Expand Down
100 changes: 43 additions & 57 deletions src/Components/ExternalResult/ListFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import TextFormField from "../Form/FormFields/TextFormField";
import { MultiSelectFormField } from "../Form/FormFields/SelectFormField";
import DateRangeFormField from "../Form/FormFields/DateRangeFormField";
import dayjs from "dayjs";
import { dateQueryString } from "../../Utils/utils";
import { dateQueryString, compareBy } from "../../Utils/utils";
import useAuthUser from "../../Common/hooks/useAuthUser";
import useQuery from "../../Utils/request/useQuery";
import routes from "../../Redux/api";
import Loading from "../Common/Loading";
import { LocalBodyModel, WardModel } from "../Facility/models";

const clearFilterState = {
created_date_before: "",
Expand All @@ -27,10 +28,10 @@ const getDate = (value: any) =>

export default function ListFilter(props: any) {
const { filter, onChange, closeFilter, dataList, removeFilters } = props;
const [wardList, setWardList] = useState<any[]>([]);
const [lsgList, setLsgList] = useState<any[]>([]);
const [wards, setWards] = useState<any[]>([]);
const [selectedLsgs, setSelectedLsgs] = useState<any[]>([]);
const [wardList, setWardList] = useState<WardModel[]>([]);
const [lsgList, setLsgList] = useState<LocalBodyModel[]>([]);
const [wards, setWards] = useState<WardModel[]>([]);
const [selectedLsgs, setSelectedLsgs] = useState<LocalBodyModel[]>([]);
const authUser = useAuthUser();
const [filterState, setFilterState] = useMergeState({
created_date_before: filter.created_date_before || null,
Expand All @@ -47,32 +48,32 @@ export default function ListFilter(props: any) {
pathParams: { id: String(authUser.district) },
onResponse: ({ res, data }) => {
if (res && data) {
let allWards: any[] = [];
let allLsgs: any[] = [];
const allWards: any[] = [];
const allLsgs: any[] = [];

if (res && data) {
data.forEach((local: any) => {
allLsgs = [...allLsgs, { id: local.id, name: local.name }];
allLsgs.push({ id: local.id, name: local.name });
if (local.wards) {
local.wards.forEach((ward: any) => {
allWards = [
...allWards,
{
id: ward.id,
name: ward.number + ": " + ward.name,
panchayath: local.name,
number: ward.number,
local_body_id: local.id,
},
];
allWards.push({
id: ward.id,
name: ward.number + ": " + ward.name,
panchayath: local.name,
number: ward.number,
local_body_id: local.id,
});
});
}
});
}

sortByName(allWards);
sortByName(allLsgs);
setWardList(allWards || []);
setLsgList(allLsgs || []);
allWards.sort(compareBy("number"));
allLsgs.sort(compareBy("name"));

setWardList(allWards);
setLsgList(allLsgs);

const filteredWard = filter?.wards?.split(",").map(Number);
const selectedWards: any =
filteredWard && allWards
Expand Down Expand Up @@ -106,13 +107,6 @@ export default function ListFilter(props: any) {
setFilterState(filterData);
};

const handleWardChange = (value: any) => {
setWards(value);
};
const handleLsgChange = (value: any) => {
setSelectedLsgs(value);
};

const field = (name: string) => ({
name,
label: t(name),
Expand Down Expand Up @@ -161,12 +155,6 @@ export default function ListFilter(props: any) {
dataList(selectedLsgs, wards);
};

const sortByName = (items: any) => {
items.sort(function (a: any, b: any) {
return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
});
};

const filterWards = () => {
const selectedLsgIds: any = selectedLsgs.map((e) => {
return e.id;
Expand Down Expand Up @@ -205,29 +193,27 @@ export default function ListFilter(props: any) {
closeFilter();
}}
>
<div>
<MultiSelectFormField
name="local_bodies"
options={lsgList}
label={t("Local Body")}
placeholder={t("select_local_body")}
value={selectedLsgs}
optionLabel={(option: any) => option.name}
onChange={(e: any) => handleLsgChange(e.value)}
/>
</div>
<MultiSelectFormField
name="local_bodies"
options={lsgList}
label={t("Local Body")}
placeholder={t("select_local_body")}
value={selectedLsgs}
optionLabel={(option) => option.name}
optionDescription={(option) => option.localbody_code}
onChange={({ value }) => setSelectedLsgs(value)}
/>

<div>
<MultiSelectFormField
name="wards"
options={filterWards()}
label={t("Ward")}
placeholder={t("select_wards")}
value={wards}
optionLabel={(option: any) => option.name}
onChange={(e: any) => handleWardChange(e.value)}
/>
</div>
<MultiSelectFormField
name="wards"
options={filterWards() as WardModel[]}
label={t("Ward")}
placeholder={t("select_wards")}
value={wards}
optionLabel={(option) => option.name}
optionDescription={(option) => option.panchayath}
onChange={({ value }) => setWards(value)}
/>
<DateRangeFormField
name="created_date"
id="created_date"
Expand Down
9 changes: 4 additions & 5 deletions src/Components/ExternalResult/ResultUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Page from "../Common/components/Page.js";
import useQuery from "../../Utils/request/useQuery.js";
import routes from "../../Redux/api.js";
import request from "../../Utils/request/request.js";
import { compareBy } from "../../Utils/utils.js";

const Loading = lazy(() => import("../Common/Loading"));

Expand Down Expand Up @@ -265,11 +266,9 @@ export default function UpdateResult(props: any) {
name="ward"
label="Ward/Division of respective LSGI"
required
options={ward
.sort((a, b) => a.number - b.number)
.map((e) => {
return { id: e.id, name: e.number + ": " + e.name };
})}
options={ward.sort(compareBy("number")).map((e) => {
return { id: e.id, name: e.number + ": " + e.name };
})}
value={state.form.ward}
optionLabel={(ward) => ward.name}
optionValue={(ward) => ward.id}
Expand Down
41 changes: 19 additions & 22 deletions src/Components/Facility/FacilityCreate.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import * as Notification from "../../Utils/Notifications.js";

import ButtonV2, { Cancel, Submit } from "../Common/components/ButtonV2";
import { CapacityModal, DoctorModal } from "./models";
import {
CapacityModal,
DistrictModel,
DoctorModal,
LocalBodyModel,
StateModel,
WardModel,
} from "./models";
import { DraftSection, useAutoSaveReducer } from "../../Utils/AutoSave.js";
import {
FACILITY_FEATURE_TYPES,
Expand Down Expand Up @@ -30,6 +37,7 @@ import {
getPincodeDetails,
includesIgnoreCase,
parsePhoneNumber,
compareBy,
} from "../../Utils/utils";
import {
phonePreg,
Expand Down Expand Up @@ -67,15 +75,6 @@ interface FacilityProps {
facilityId?: string;
}

interface StateObj {
id: number;
name: string;
}

interface WardObj extends StateObj {
number: number;
}

type FacilityForm = {
facility_type: string;
name: string;
Expand Down Expand Up @@ -162,10 +161,10 @@ export const FacilityCreate = (props: FacilityProps) => {
const [isDistrictLoading, setIsDistrictLoading] = useState(false);
const [isLocalbodyLoading, setIsLocalbodyLoading] = useState(false);
const [isWardLoading, setIsWardLoading] = useState(false);
const [states, setStates] = useState<StateObj[]>([]);
const [districts, setDistricts] = useState<StateObj[]>([]);
const [localBodies, setLocalBodies] = useState<StateObj[]>([]);
const [ward, setWard] = useState<WardObj[]>([]);
const [states, setStates] = useState<StateModel[]>([]);
const [districts, setDistricts] = useState<DistrictModel[]>([]);
const [localBodies, setLocalBodies] = useState<LocalBodyModel[]>([]);
const [ward, setWard] = useState<WardModel[]>([]);
const [currentStep, setCurrentStep] = useState(1);
const [createdFacilityId, setCreatedFacilityId] = useState("");
const [showAutoFilledPincode, setShowAutoFilledPincode] = useState(false);
Expand Down Expand Up @@ -855,14 +854,12 @@ export const FacilityCreate = (props: FacilityProps) => {
className={isWardLoading ? "animate-pulse" : ""}
disabled={isWardLoading}
placeholder="Choose Ward"
options={ward
.sort((a, b) => a.number - b.number)
.map((e) => {
return {
id: e.id,
name: e.number + ": " + e.name,
};
})}
options={ward.sort(compareBy("number")).map((e) => {
return {
id: e.id,
name: e.number + ": " + e.name,
};
})}
optionLabel={(o) => o.name}
optionValue={(o) => o.id}
/>
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Facility/models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export interface WardModel {
id: number;
name: string;
number: number;
local_body: number;
panchayath: string;
local_body_id: LocalBodyModel["id"];
}

export interface FacilityModel {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo } from "react";
import AutocompleteFormField from "./Autocomplete";
import { FormFieldBaseProps } from "./Utils";
import { classNames } from "../../../Utils/utils";
import { classNames, compareBy } from "../../../Utils/utils";
import ButtonV2 from "../../Common/components/ButtonV2";

interface Threshold {
Expand All @@ -23,8 +23,7 @@ type Props = FormFieldBaseProps<number> & {

export default function RangeAutocompleteFormField(props: Props) {
const options = useMemo(() => {
const sortedThresholds =
props.thresholds?.sort((a, b) => a.value - b.value) || [];
const sortedThresholds = props.thresholds?.sort(compareBy("value")) || [];

const getThreshold = (value: number) => {
const threshold = sortedThresholds.findLast(
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Patient/PatientRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
includesIgnoreCase,
parsePhoneNumber,
scrollTo,
compareBy,
} from "../../Utils/utils";
import { navigate, useQueryParams } from "raviger";
import { statusType, useAbortableEffect } from "../../Common/utils";
Expand Down Expand Up @@ -1507,7 +1508,7 @@ export const PatientRegister = (props: PatientRegisterProps) => {
{...field("ward")}
label="Ward"
options={ward
.sort((a, b) => a.number - b.number)
.sort(compareBy("number"))
.map((e) => {
return {
id: e.id,
Expand Down
6 changes: 6 additions & 0 deletions src/Utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,9 @@ export const invalidateFiltersCache = () => {
}
}
};

export const compareBy = <T extends object>(key: keyof T) => {
return (a: T, b: T) => {
return a[key] < b[key] ? -1 : a[key] > b[key] ? 1 : 0;
};
};
Loading