Skip to content

Commit

Permalink
Replace useDispatch with useQuery in Doctor Capacity and Counts
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad committed Jan 11, 2024
1 parent 468527f commit 50f11d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
33 changes: 13 additions & 20 deletions src/Components/Facility/DoctorCapacity.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useCallback, useEffect, useReducer, useState } from "react";
import { useDispatch } from "react-redux";
import { DOCTOR_SPECIALIZATION } from "../../Common/constants";
import { statusType, useAbortableEffect } from "../../Common/utils";
import { createDoctor, getDoctor, listDoctor } from "../../Redux/actions";
Expand All @@ -9,7 +8,7 @@ import { FieldErrorText, FieldLabel } from "../Form/FormFields/FormField";
import TextFormField from "../Form/FormFields/TextFormField";
import { FieldChangeEventHandler } from "../Form/FormFields/Utils";
import SelectMenuV2 from "../Form/SelectMenuV2";
import { DoctorModal, OptionsType } from "./models";
import { DoctorModal } from "./models";

interface DoctorCapacityProps extends DoctorModal {
facilityId: string;
Expand All @@ -19,8 +18,6 @@ interface DoctorCapacityProps extends DoctorModal {
id?: number;
}

const initDoctorTypes: Array<OptionsType> = [...DOCTOR_SPECIALIZATION];

const initForm: any = {
area: "",
count: "",
Expand Down Expand Up @@ -51,13 +48,11 @@ const doctorCapacityReducer = (state = initialState, action: any) => {
};

export const DoctorCapacity = (props: DoctorCapacityProps) => {
const dispatchAction: any = useDispatch();
const { facilityId, handleClose, handleUpdate, className, id } = props;
const [state, dispatch] = useReducer(doctorCapacityReducer, initialState);
const [isLoading, setIsLoading] = useState(false);
const [isLastOptionType, setIsLastOptionType] = useState(false);
const [doctorTypes, setDoctorTypes] =
useState<Array<OptionsType>>(initDoctorTypes);
const [doctorTypes, setDoctorTypes] = useState([...DOCTOR_SPECIALIZATION]);

const headerText = !id ? "Add Doctor Capacity" : "Edit Doctor Capacity";
const buttonText = !id
Expand All @@ -78,17 +73,15 @@ export const DoctorCapacity = (props: DoctorCapacityProps) => {
return;
}
// disable existing doctor types
const updatedDoctorTypes = initDoctorTypes.map(
(type: OptionsType) => {
const isExisting = existingData.find(
(i: DoctorModal) => i.area === type.id
);
return {
...type,
disabled: !!isExisting,
};
}
);
const updatedDoctorTypes = DOCTOR_SPECIALIZATION.map((type) => {
const isExisting = existingData.find(
(i: DoctorModal) => i.area === type.id
);
return {
...type,
disabled: !!isExisting,
};
});
setDoctorTypes(updatedDoctorTypes);
}
}
Expand Down Expand Up @@ -118,7 +111,7 @@ export const DoctorCapacity = (props: DoctorCapacityProps) => {

useEffect(() => {
const lastDoctorType =
doctorTypes.filter((i: OptionsType) => i.disabled).length ===
doctorTypes.filter((i) => i.disabled).length ===
DOCTOR_SPECIALIZATION.length - 1;
setIsLastOptionType(lastDoctorType);
}, [doctorTypes]);
Expand Down Expand Up @@ -163,7 +156,7 @@ export const DoctorCapacity = (props: DoctorCapacityProps) => {
setIsLoading(false);
if (res && res.data) {
// disable last added bed type
const updatedDoctorTypes = doctorTypes.map((type: OptionsType) => {
const updatedDoctorTypes = doctorTypes.map((type) => {
return {
...type,
disabled: res.data.area !== type.id ? type.disabled : true,
Expand Down
32 changes: 13 additions & 19 deletions src/Components/Facility/DoctorsCountCard.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { useState } from "react";
import { DoctorModal } from "./models";
import { DOCTOR_SPECIALIZATION } from "../../Common/constants";
import { useDispatch } from "react-redux";
import { deleteDoctor } from "../../Redux/actions";
import * as Notification from "../../Utils/Notifications";
import { DoctorIcon } from "../TeleIcu/Icons/DoctorIcon";
import { DoctorCapacity } from "./DoctorCapacity";
import DialogModal from "../Common/Dialog";
import ConfirmDialog from "../Common/ConfirmDialog";
import ButtonV2 from "../Common/components/ButtonV2";
import { NonReadOnlyUsers } from "../../Utils/AuthorizeFor";
import request from "../../Utils/request/request";
import routes from "../../Redux/api";

interface DoctorsCountProps extends DoctorModal {
facilityId: string;
Expand All @@ -19,28 +19,22 @@ interface DoctorsCountProps extends DoctorModal {

const DoctorsCountCard = (props: DoctorsCountProps) => {
const specialization = DOCTOR_SPECIALIZATION.find((i) => i.id === props.area);
const dispatchAction: any = useDispatch();
const [openDeleteDialog, setOpenDeleteDialog] = useState(false);
const [open, setOpen] = useState(false);
const [selectedId, setSelectedId] = useState<number>(-1);

const handleDeleteSubmit = async () => {
if (props.area) {
const res = await dispatchAction(
deleteDoctor(props.area, { facilityId: props.facilityId })
);
if (res?.status === 204) {
Notification.Success({
msg: "Doctor specialization type deleted successfully",
});
props.removeDoctor(props.id);
} else {
Notification.Error({
msg:
"Error while deleting Doctor specialization: " +
(res?.data?.detail || ""),
});
}
if (!props.area) return;

const { res } = await request(routes.deleteAsset, {
pathParams: { facilityId: props.facilityId, area: `${props.area}` },
});

if (res?.ok) {
props.removeDoctor(props.id);
Notification.Success({
msg: "Doctor specialization type deleted successfully",
});
}
};

Expand Down

0 comments on commit 50f11d9

Please sign in to comment.