diff --git a/src/Components/Resource/BadgesList.tsx b/src/Components/Resource/BadgesList.tsx
index 31b8f1c17ae..6977861d596 100644
--- a/src/Components/Resource/BadgesList.tsx
+++ b/src/Components/Resource/BadgesList.tsx
@@ -1,63 +1,25 @@
-import { useState, useEffect } from "react";
-import { getAnyFacility } from "../../Redux/actions";
-import { useDispatch } from "react-redux";
import { SHIFTING_FILTER_ORDER } from "../../Common/constants";
+import routes from "../../Redux/api";
+import useQuery from "../../Utils/request/useQuery";
+
+function useFacilityQuery(facilityId: string | undefined) {
+ return useQuery(routes.getAnyFacility, {
+ pathParams: { id: String(facilityId) },
+ prefetch: facilityId !== undefined,
+ });
+}
export default function BadgesList(props: any) {
const { appliedFilters, FilterBadges } = props;
- const [orginFacilityName, setOrginFacilityName] = useState("");
- const [approvingFacilityName, setApprovingFacilityName] = useState("");
- const [assignedFacilityName, setAssignedFacilityName] = useState("");
- const dispatch: any = useDispatch();
-
- useEffect(() => {
- async function fetchData() {
- if (!appliedFilters.origin_facility) return setOrginFacilityName("");
- const res = await dispatch(
- getAnyFacility(appliedFilters.origin_facility, "origin_facility_name")
- );
- setOrginFacilityName(res?.data?.name);
- }
- fetchData();
- }, [dispatch, appliedFilters.origin_facility]);
-
- useEffect(() => {
- async function fetchData() {
- if (!appliedFilters.approving_facility)
- return setApprovingFacilityName("");
- const res = await dispatch(
- getAnyFacility(
- appliedFilters.approving_facility,
- "approving_facility_name"
- )
- );
- setApprovingFacilityName(res?.data?.name);
- }
- fetchData();
- }, [dispatch, appliedFilters.approving_facility]);
-
- useEffect(() => {
- async function fetchData() {
- if (!appliedFilters.assigned_facility) return setAssignedFacilityName("");
- const res = await dispatch(
- getAnyFacility(
- appliedFilters.assigned_facility,
- "assigned_facility_name"
- )
- );
- setAssignedFacilityName(res?.data?.name);
- }
- fetchData();
- }, [dispatch, appliedFilters.assigned_facility]);
+ const originFacility = useFacilityQuery(appliedFilters.origin_facility);
+ const approvingFacility = useFacilityQuery(appliedFilters.approving_facility);
+ const assignedFacility = useFacilityQuery(appliedFilters.assigned_facility);
const getDescShiftingFilterOrder = (ordering: any) => {
- let desc = "";
- SHIFTING_FILTER_ORDER.map((item: any) => {
- if (item.text === ordering) {
- desc = item.desc;
- }
- });
- return desc;
+ const foundItem = SHIFTING_FILTER_ORDER.find(
+ (item) => item.text === ordering
+ );
+ return foundItem ? foundItem.desc : "";
};
return (
@@ -75,13 +37,25 @@ export default function BadgesList(props: any) {
}),
...dateRange("Modified", "modified_date"),
...dateRange("Created", "created_date"),
- value("Origin facility", "origin_facility", orginFacilityName),
+ value(
+ "Origin facility",
+ "origin_facility",
+ appliedFilters.origin_facility ? originFacility?.data?.name || "" : ""
+ ),
value(
"Approving facility",
"approving_facility",
- approvingFacilityName
+ appliedFilters.approving_facility
+ ? approvingFacility?.data?.name || ""
+ : ""
+ ),
+ value(
+ "Assigned facility",
+ "assigned_facility",
+ appliedFilters.assigned_facility
+ ? assignedFacility?.data?.name || ""
+ : ""
),
- value("Assigned facility", "assigned_facility", assignedFacilityName),
]}
/>
);
diff --git a/src/Components/Resource/CommentSection.tsx b/src/Components/Resource/CommentSection.tsx
index 01bd67454f5..25d8142dae7 100644
--- a/src/Components/Resource/CommentSection.tsx
+++ b/src/Components/Resource/CommentSection.tsx
@@ -1,60 +1,26 @@
-import { useCallback, useState } from "react";
-import { useDispatch } from "react-redux";
-import { statusType, useAbortableEffect } from "../../Common/utils";
-import { getResourceComments, addResourceComments } from "../../Redux/actions";
+import { useState } from "react";
import * as Notification from "../../Utils/Notifications.js";
-import Pagination from "../Common/Pagination";
import { formatDateTime } from "../../Utils/utils";
import CircularProgress from "../Common/components/CircularProgress";
import ButtonV2 from "../Common/components/ButtonV2";
import TextAreaFormField from "../Form/FormFields/TextAreaFormField";
+import useQuery from "../../Utils/request/useQuery";
+import routes from "../../Redux/api";
+import PaginatedList from "../../CAREUI/misc/PaginatedList";
+import { IComment } from "./models";
+import request from "../../Utils/request/request";
-interface CommentSectionProps {
- id: string;
-}
-const CommentSection = (props: CommentSectionProps) => {
- const dispatch: any = useDispatch();
- const initialData: any = [];
- const [comments, setComments] = useState(initialData);
+const CommentSection = (props: { id: string }) => {
const [commentBox, setCommentBox] = useState("");
- const [isLoading, setIsLoading] = useState(true);
-
- const [currentPage, setCurrentPage] = useState(1);
- const [totalCount, setTotalCount] = useState(0);
- const [offset, setOffset] = useState(0);
- const limit = 8;
-
- const handlePagination = (page: number, limit: number) => {
- const offset = (page - 1) * limit;
- setCurrentPage(page);
- setOffset(offset);
- };
-
- const fetchData = useCallback(
- async (status: statusType = { aborted: false }) => {
- setIsLoading(true);
- const res = await dispatch(
- getResourceComments(props.id, { limit, offset })
- );
- if (!status.aborted) {
- if (res && res.data) {
- setComments(res.data?.results);
- setTotalCount(res.data.count);
- }
- setIsLoading(false);
- }
- },
- [props.id, dispatch, offset]
- );
-
- useAbortableEffect(
- (status: statusType) => {
- fetchData(status);
- },
- [fetchData]
+ const { loading, refetch: resourceRefetch } = useQuery(
+ routes.getResourceComments,
+ {
+ pathParams: { id: props.id },
+ query: { limit: 8, offset: 0 },
+ }
);
- const onSubmitComment = () => {
+ const onSubmitComment = async () => {
const payload = {
comment: commentBox,
};
@@ -64,13 +30,16 @@ const CommentSection = (props: CommentSectionProps) => {
});
return;
}
- dispatch(addResourceComments(props.id, payload)).then((_: any) => {
- Notification.Success({ msg: "Comment added successfully" });
- fetchData();
+ const { res } = await request(routes.addResourceComments, {
+ pathParams: { id: props.id },
+ body: payload,
});
+ if (res?.ok) {
+ Notification.Success({ msg: "Comment added successfully" });
+ resourceRefetch();
+ }
setCommentBox("");
};
-
return (
{
Post Your Comment
- {isLoading ? (
+ {loading ? (
) : (
- comments.map((comment: any) => (
-
-
-
-
- {formatDateTime(comment.modified_date) || "-"}
-
-
-
-
- {comment.created_by_object?.first_name?.charAt(0) || "U"}
+
+ {() => (
+
+
+ No comments available
+
+
+
+
+
>
+ {(item) => }
+
+
-
- {comment.created_by_object?.first_name || "Unknown"}{" "}
- {comment.created_by_object?.last_name}
-
-
- ))
+ )}
+
)}
- {totalCount > limit && (
-
- )}
);
};
export default CommentSection;
+
+export const Comment = ({
+ comment,
+ created_by_object,
+ modified_date,
+}: IComment) => (
+
+
+
+
+ {formatDateTime(modified_date) || "-"}
+
+
+
+
+ {created_by_object?.first_name?.charAt(0) || "U"}
+
+
+ {created_by_object?.first_name || "Unknown"}{" "}
+ {created_by_object?.last_name}
+
+
+
+);
diff --git a/src/Components/Resource/ListFilter.tsx b/src/Components/Resource/ListFilter.tsx
index 7a47732db64..afe48eedfd0 100644
--- a/src/Components/Resource/ListFilter.tsx
+++ b/src/Components/Resource/ListFilter.tsx
@@ -1,8 +1,5 @@
-import { useEffect, useState } from "react";
import { FacilitySelect } from "../Common/FacilitySelect";
import { RESOURCE_FILTER_ORDER } from "../../Common/constants";
-import { getAnyFacility } from "../../Redux/actions";
-import { useDispatch } from "react-redux";
import { RESOURCE_CHOICES } from "../../Common/constants";
import useMergeState from "../../Common/hooks/useMergeState";
import { navigate } from "raviger";
@@ -15,6 +12,8 @@ import { DateRange } from "../Common/DateRangeInputV2";
import DateRangeFormField from "../Form/FormFields/DateRangeFormField";
import dayjs from "dayjs";
import { dateQueryString } from "../../Utils/utils";
+import useQuery from "../../Utils/request/useQuery";
+import routes from "../../Redux/api";
const clearFilterState = {
origin_facility: "",
@@ -37,9 +36,6 @@ const getDate = (value: any) =>
export default function ListFilter(props: any) {
const { filter, onChange, closeFilter } = props;
- const [isOriginLoading, setOriginLoading] = useState(false);
- const [isResourceLoading, setResourceLoading] = useState(false);
- const [isAssignedLoading, setAssignedLoading] = useState(false);
const [filterState, setFilterState] = useMergeState({
origin_facility: filter.origin_facility || "",
origin_facility_ref: null,
@@ -55,55 +51,42 @@ export default function ListFilter(props: any) {
ordering: filter.ordering || null,
status: filter.status || null,
});
- const dispatch: any = useDispatch();
- useEffect(() => {
- async function fetchData() {
- if (filter.origin_facility) {
- setOriginLoading(true);
- const res = await dispatch(
- getAnyFacility(filter.origin_facility, "origin_facility")
- );
- if (res && res.data) {
- setFilterState({ origin_facility_ref: res.data });
- }
- setOriginLoading(false);
+ const { loading: orginFacilityLoading } = useQuery(routes.getAnyFacility, {
+ prefetch: filter.origin_facility !== undefined,
+ pathParams: { id: filter.origin_facility },
+ onResponse: ({ res, data }) => {
+ if (res && data) {
+ setFilterState({
+ origin_facility_ref: filter.origin_facility === "" ? "" : data,
+ });
}
- }
- fetchData();
- }, [dispatch]);
+ },
+ });
- useEffect(() => {
- async function fetchData() {
- if (filter.approving_facility) {
- setResourceLoading(true);
- const res = await dispatch(
- getAnyFacility(filter.approving_facility, "approving_facility")
- );
- if (res && res.data) {
- setFilterState({ approving_facility_ref: res.data });
- }
- setResourceLoading(false);
+ const { loading: resourceFacilityLoading } = useQuery(routes.getAnyFacility, {
+ prefetch: filter.approving_facility !== undefined,
+ pathParams: { id: filter.approving_facility },
+ onResponse: ({ res, data }) => {
+ if (res && data) {
+ setFilterState({
+ approving_facility_ref: filter.approving_facility === "" ? "" : data,
+ });
}
- }
- fetchData();
- }, [dispatch]);
+ },
+ });
- useEffect(() => {
- async function fetchData() {
- if (filter.assigned_facility) {
- setAssignedLoading(true);
- const res = await dispatch(
- getAnyFacility(filter.assigned_facility, "assigned_facility")
- );
- if (res && res.data) {
- setFilterState({ assigned_facility_ref: res.data });
- }
- setAssignedLoading(false);
+ const { loading: assignedFacilityLoading } = useQuery(routes.getAnyFacility, {
+ pathParams: { id: filter.assigned_facility },
+ prefetch: filter.assigned_facility !== undefined,
+ onResponse: ({ res, data }) => {
+ if (res && data) {
+ setFilterState({
+ assigned_facility_ref: filter.assigned_facility === "" ? "" : data,
+ });
}
- }
- fetchData();
- }, [dispatch]);
+ },
+ });
const setFacility = (selected: any, name: string) => {
setFilterState({
@@ -178,7 +161,7 @@ export default function ListFilter(props: any) {
Origin facility
- {isOriginLoading ? (
+ {orginFacilityLoading && filter.origin_facility ? (
) : (
Resource approving facility
- {isResourceLoading ? (
+ {filter.approving_facility && resourceFacilityLoading ? (
) : (
Assigned facility
- {isAssignedLoading ? (
+ {filter.approving_facility && assignedFacilityLoading ? (
) : (
import("../Common/Loading"));
const PageTitle = lazy(() => import("../Common/PageTitle"));
export default function ListView() {
- const dispatch: any = useDispatch();
const { qParams, Pagination, FilterBadges, advancedFilter, resultsPerPage } =
useFilters({});
- const [data, setData] = useState([]);
- const [totalCount, setTotalCount] = useState(0);
- const [isLoading, setIsLoading] = useState(false);
const { t } = useTranslation();
const onBoardViewBtnClick = () =>
navigate("/resource/board", { query: qParams });
const appliedFilters = formatFilter(qParams);
- const refreshList = () => {
- fetchData();
- };
-
- const fetchData = () => {
- setIsLoading(true);
- dispatch(
- listResourceRequests(
- formatFilter({
- ...qParams,
- offset: (qParams.page ? qParams.page - 1 : 0) * resultsPerPage,
- }),
- "resource-list-call"
- )
- ).then((res: any) => {
- if (res && res.data) {
- setData(res.data.results);
- setTotalCount(res.data.count);
- }
- setIsLoading(false);
- });
- };
-
- useEffect(() => {
- fetchData();
- }, [
- qParams.status,
- qParams.facility,
- qParams.origin_facility,
- qParams.approving_facility,
- qParams.assigned_facility,
- qParams.emergency,
- qParams.created_date_before,
- qParams.created_date_after,
- qParams.modified_date_before,
- qParams.modified_date_after,
- qParams.ordering,
- qParams.page,
- ]);
+ const { loading, data, refetch } = useQuery(routes.listResourceRequests, {
+ query: formatFilter({
+ ...qParams,
+ offset: (qParams.page ? qParams.page - 1 : 0) * resultsPerPage,
+ }),
+ });
const showResourceCardList = (data: any) => {
if (data && !data.length) {
@@ -218,14 +178,14 @@ export default function ListView() {
- {isLoading ? (
+ {loading ? (
) : (
- {showResourceCardList(data)}
+ {data?.results && showResourceCardList(data?.results)}
-
+
)}
diff --git a/src/Components/Resource/ResourceBoard.tsx b/src/Components/Resource/ResourceBoard.tsx
index 66bf559be0d..217f2941e10 100644
--- a/src/Components/Resource/ResourceBoard.tsx
+++ b/src/Components/Resource/ResourceBoard.tsx
@@ -1,15 +1,13 @@
import { useState, useEffect } from "react";
-import { useDispatch } from "react-redux";
-import {
- listResourceRequests,
- downloadResourceRequests,
-} from "../../Redux/actions";
+import { downloadResourceRequests } from "../../Redux/actions";
import { navigate } from "raviger";
import { classNames } from "../../Utils/utils";
import { useDrag, useDrop } from "react-dnd";
import { formatDateTime } from "../../Utils/utils";
import { ExportButton } from "../Common/Export";
import dayjs from "../../Utils/dayjs";
+import useQuery from "../../Utils/request/useQuery";
+import routes from "../../Redux/api";
const limit = 14;
@@ -118,7 +116,6 @@ const ResourceCard = ({ resource }: any) => {
-
{resource.assigned_to_object && (
{
)}
-
@@ -273,13 +258,14 @@ export default function ResourceBoard({
- ) : data?.length > 0 ? (
+ ) : data && data?.results.length > 0 ? (
boardFilter(board)
) : (
No requests to show.
)}
{!isLoading.board &&
- data?.length < (totalCount || 0) &&
+ data &&
+ data?.results.length < (data?.count || 0) &&
(isLoading.more ? (
Loading
diff --git a/src/Components/Resource/ResourceCreate.tsx b/src/Components/Resource/ResourceCreate.tsx
index cf9e5f6f22a..d0a6c36272b 100644
--- a/src/Components/Resource/ResourceCreate.tsx
+++ b/src/Components/Resource/ResourceCreate.tsx
@@ -1,8 +1,7 @@
-import { useReducer, useState, useEffect, lazy } from "react";
+import { useReducer, useState, lazy } from "react";
import { FacilitySelect } from "../Common/FacilitySelect";
import * as Notification from "../../Utils/Notifications.js";
-import { useDispatch } from "react-redux";
import { navigate } from "raviger";
import {
OptionsType,
@@ -11,8 +10,6 @@ import {
} from "../../Common/constants";
import { parsePhoneNumber } from "../../Utils/utils";
import { phonePreg } from "../../Common/validation";
-
-import { createResource, getAnyFacility } from "../../Redux/actions";
import { Cancel, Submit } from "../Common/components/ButtonV2";
import PhoneNumberFormField from "../Form/FormFields/PhoneNumberFormField";
import { FieldChangeEvent } from "../Form/FormFields/Utils";
@@ -26,6 +23,9 @@ import { FieldLabel } from "../Form/FormFields/FormField";
import Card from "../../CAREUI/display/Card";
import Page from "../Common/components/Page";
import { PhoneNumberValidator } from "../Form/FieldValidators";
+import useQuery from "../../Utils/request/useQuery";
+import routes from "../../Redux/api";
+import request from "../../Utils/request/request";
const Loading = lazy(() => import("../Common/Loading"));
@@ -87,10 +87,7 @@ export default function ResourceCreate(props: resourceProps) {
const { goBack } = useAppHistory();
const { facilityId } = props;
const { t } = useTranslation();
-
- const dispatchAction: any = useDispatch();
const [isLoading, setIsLoading] = useState(false);
- const [facilityName, setFacilityName] = useState("");
const resourceFormReducer = (state = initialState, action: any) => {
switch (action.type) {
@@ -113,18 +110,10 @@ export default function ResourceCreate(props: resourceProps) {
const [state, dispatch] = useReducer(resourceFormReducer, initialState);
- useEffect(() => {
- async function fetchFacilityName() {
- if (facilityId) {
- const res = await dispatchAction(getAnyFacility(facilityId));
-
- setFacilityName(res?.data?.name || "");
- } else {
- setFacilityName("");
- }
- }
- fetchFacilityName();
- }, [dispatchAction, facilityId]);
+ const { data: facilityData } = useQuery(routes.getAnyFacility, {
+ prefetch: facilityId !== undefined,
+ pathParams: { id: String(facilityId) },
+ });
const validateForm = () => {
const errors = { ...initError };
@@ -184,11 +173,11 @@ export default function ResourceCreate(props: resourceProps) {
if (validForm) {
setIsLoading(true);
- const data = {
+ const resourceData = {
status: "PENDING",
category: state.form.category,
sub_category: state.form.sub_category,
- origin_facility: props.facilityId,
+ origin_facility: String(props.facilityId),
approving_facility: (state.form.approving_facility || {}).id,
assigned_facility: (state.form.assigned_facility || {}).id,
emergency: state.form.emergency === "true",
@@ -202,16 +191,18 @@ export default function ResourceCreate(props: resourceProps) {
requested_quantity: state.form.requested_quantity || 0,
};
- const res = await dispatchAction(createResource(data));
+ const { res, data } = await request(routes.createResource, {
+ body: resourceData,
+ });
setIsLoading(false);
- if (res && res.data && (res.status == 201 || res.status == 200)) {
+ if (res?.ok && data) {
await dispatch({ type: "set_form", form: initForm });
Notification.Success({
msg: "Resource request created successfully",
});
- navigate(`/resource/${res.data.id}`);
+ navigate(`/resource/${data.id}`);
}
}
};
@@ -224,7 +215,7 @@ export default function ResourceCreate(props: resourceProps) {
import("../Common/Loading"));
export default function ResourceDetails(props: { id: string }) {
- const dispatch: any = useDispatch();
- const initialData: any = {};
- const [data, setData] = useState(initialData);
- const [isLoading, setIsLoading] = useState(true);
const [isPrintMode, setIsPrintMode] = useState(false);
-
const [openDeleteResourceDialog, setOpenDeleteResourceDialog] =
useState(false);
-
- const fetchData = useCallback(
- async (status: statusType) => {
- setIsLoading(true);
- const res = await dispatch(getResourceDetails({ id: props.id }));
- if (!status.aborted) {
- if (res && res.data) {
- setData(res.data);
- } else {
- navigate("/not-found");
- }
- setIsLoading(false);
+ const { data, loading } = useQuery(routes.getResourceDetails, {
+ pathParams: { id: props.id },
+ onResponse: ({ res, data }) => {
+ if (!res && !data) {
+ navigate("/not-found");
}
},
- [props.id, dispatch]
- );
-
- useAbortableEffect(
- (status: statusType) => {
- fetchData(status);
- },
- [fetchData]
- );
+ });
const handleResourceDelete = async () => {
setOpenDeleteResourceDialog(true);
-
- const res = await dispatch(deleteResourceRecord(props.id));
+ const { res, data } = await request(routes.deleteResourceRecord, {
+ pathParams: { id: props.id },
+ });
if (res?.status === 204) {
Notification.Success({
msg: "Resource record has been deleted successfully.",
});
} else {
Notification.Error({
- msg: "Error while deleting Resource: " + (res?.data?.detail || ""),
+ msg: "Error while deleting Resource: " + (data?.detail || ""),
});
}
@@ -223,7 +203,7 @@ export default function ResourceDetails(props: { id: string }) {
);
};
- if (isLoading) {
+ if (loading || !data) {
return ;
}
diff --git a/src/Components/Resource/ResourceDetailsUpdate.tsx b/src/Components/Resource/ResourceDetailsUpdate.tsx
index ce0751e7b2e..c692618a774 100644
--- a/src/Components/Resource/ResourceDetailsUpdate.tsx
+++ b/src/Components/Resource/ResourceDetailsUpdate.tsx
@@ -1,15 +1,7 @@
import * as Notification from "../../Utils/Notifications.js";
-
import { Cancel, Submit } from "../Common/components/ButtonV2";
-import { lazy, useCallback, useEffect, useReducer, useState } from "react";
-import {
- getResourceDetails,
- getUserList,
- updateResource,
-} from "../../Redux/actions";
+import { lazy, useReducer, useState } from "react";
import { navigate, useQueryParams } from "raviger";
-import { statusType, useAbortableEffect } from "../../Common/utils";
-
import Card from "../../CAREUI/display/Card";
import CircularProgress from "../Common/components/CircularProgress";
import { FacilitySelect } from "../Common/FacilitySelect";
@@ -22,9 +14,11 @@ import { SelectFormField } from "../Form/FormFields/SelectFormField";
import TextAreaFormField from "../Form/FormFields/TextAreaFormField";
import TextFormField from "../Form/FormFields/TextFormField";
import UserAutocompleteFormField from "../Common/UserAutocompleteFormField";
-
import useAppHistory from "../../Common/hooks/useAppHistory";
-import { useDispatch } from "react-redux";
+import useQuery from "../../Utils/request/useQuery.js";
+import routes from "../../Redux/api.js";
+import { UserModel } from "../Users/models.js";
+import request from "../../Utils/request/request.js";
const Loading = lazy(() => import("../Common/Loading"));
@@ -67,13 +61,9 @@ const initialState = {
export const ResourceDetailsUpdate = (props: resourceProps) => {
const { goBack } = useAppHistory();
- const dispatchAction: any = useDispatch();
const [qParams, _] = useQueryParams();
const [isLoading, setIsLoading] = useState(true);
- const [assignedQuantity, setAssignedQuantity] = useState(0);
- const [requestTitle, setRequestTitle] = useState("");
- const [assignedUser, SetAssignedUser] = useState(null);
- const [assignedUserLoading, setAssignedUserLoading] = useState(false);
+ const [assignedUser, SetAssignedUser] = useState();
const resourceFormReducer = (state = initialState, action: any) => {
switch (action.type) {
case "set_form": {
@@ -95,23 +85,13 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
const [state, dispatch] = useReducer(resourceFormReducer, initialState);
- useEffect(() => {
- async function fetchData() {
- if (state.form.assigned_to) {
- setAssignedUserLoading(true);
-
- const res = await dispatchAction(
- getUserList({ id: state.form.assigned_to })
- );
-
- if (res && res.data && res.data.count)
- SetAssignedUser(res.data.results[0]);
-
- setAssignedUserLoading(false);
+ const { loading: assignedUserLoading } = useQuery(routes.userList, {
+ onResponse: ({ res, data }) => {
+ if (res?.ok && data && data.count) {
+ SetAssignedUser(data.results[0]);
}
- }
- fetchData();
- }, [dispatchAction, state.form.assigned_to]);
+ },
+ });
const validateForm = () => {
const errors = { ...initError };
@@ -147,13 +127,25 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
dispatch({ type: "set_form", form });
};
+ const { data: resourceDetails } = useQuery(routes.getResourceDetails, {
+ pathParams: { id: props.id },
+ onResponse: ({ res, data }) => {
+ if (res && data) {
+ const d = data;
+ d["status"] = qParams.status || data.status;
+ dispatch({ type: "set_form", form: d });
+ }
+ setIsLoading(false);
+ },
+ });
+
const handleSubmit = async () => {
const validForm = validateForm();
if (validForm) {
setIsLoading(true);
- const data = {
+ const resourceData = {
category: "OXYGEN",
status: state.form.status,
origin_facility: state.form.origin_facility_object?.id,
@@ -167,14 +159,17 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
assigned_quantity:
state.form.status === "PENDING"
? state.form.assigned_quantity
- : assignedQuantity,
+ : resourceDetails?.assigned_quantity || 0,
};
- const res = await dispatchAction(updateResource(props.id, data));
+ const { res, data } = await request(routes.updateResource, {
+ pathParams: { id: props.id },
+ body: resourceData,
+ });
setIsLoading(false);
- if (res && res.status == 200 && res.data) {
- dispatch({ type: "set_form", form: res.data });
+ if (res && res.status == 200 && data) {
+ dispatch({ type: "set_form", form: data });
Notification.Success({
msg: "Resource request updated successfully",
});
@@ -186,31 +181,6 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
}
};
- const fetchData = useCallback(
- async (status: statusType) => {
- setIsLoading(true);
- const res = await dispatchAction(getResourceDetails({ id: props.id }));
- if (!status.aborted) {
- if (res && res.data) {
- setRequestTitle(res.data.title);
- setAssignedQuantity(res.data.assigned_quantity);
- const d = res.data;
- d["status"] = qParams.status || res.data.status;
- dispatch({ type: "set_form", form: d });
- }
- setIsLoading(false);
- }
- },
- [props.id, dispatchAction, qParams.status]
- );
-
- useAbortableEffect(
- (status: statusType) => {
- fetchData(status);
- },
- [fetchData]
- );
-
if (isLoading) {
return ;
}
@@ -219,7 +189,7 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
diff --git a/src/Components/Resource/models.ts b/src/Components/Resource/models.ts
new file mode 100644
index 00000000000..f10ac988552
--- /dev/null
+++ b/src/Components/Resource/models.ts
@@ -0,0 +1,41 @@
+import { PerformedByModel } from "../HCX/misc";
+
+export interface IComment {
+ id: string;
+ created_by_object: PerformedByModel;
+ created_date: string;
+ modified_date: string;
+ comment: string;
+ created_by: number;
+}
+
+export interface IResource {
+ id: string;
+ title: string;
+ emergency: boolean;
+ status?: string;
+ origin_facility_object: {
+ name: string;
+ };
+ approving_facility_object: {
+ name: string;
+ };
+ assigned_facility_object: {
+ name: string;
+ };
+ assigned_quantity: number;
+ modified_date: string;
+ category: any;
+ sub_category: number;
+ origin_facility: string;
+ approving_facility: string;
+ assigned_facility: string;
+ reason: string;
+ refering_facility_contact_name: string;
+ refering_facility_contact_number: string;
+ requested_quantity: number;
+ assigned_to_object: PerformedByModel;
+ created_by_object: PerformedByModel;
+ created_date: string;
+ last_edited_by_object: PerformedByModel;
+}
diff --git a/src/Redux/actions.tsx b/src/Redux/actions.tsx
index 1d2d6f4f7b6..15934d0957d 100644
--- a/src/Redux/actions.tsx
+++ b/src/Redux/actions.tsx
@@ -803,30 +803,9 @@ export const listMedibaseMedicines = (
};
// Resource
-export const createResource = (params: object) => {
- return fireRequest("createResource", [], params);
-};
-export const updateResource = (id: string, params: object) => {
- return fireRequest("updateResource", [id], params);
-};
-export const deleteResourceRecord = (id: string) => {
- return fireRequest("deleteResourceRecord", [id], {});
-};
-export const listResourceRequests = (params: object, key: string) => {
- return fireRequest("listResourceRequests", [], params, null, key);
-};
-export const getResourceDetails = (pathParam: object) => {
- return fireRequest("getResourceDetails", [], {}, pathParam);
-};
export const downloadResourceRequests = (params: object) => {
return fireRequest("downloadResourceRequests", [], params);
};
-export const getResourceComments = (id: string, params: object) => {
- return fireRequest("getResourceComments", [], params, { id });
-};
-export const addResourceComments = (id: string, params: object) => {
- return fireRequest("addResourceComments", [], params, { id });
-};
export const listAssets = (params: object) =>
fireRequest("listAssets", [], params);
diff --git a/src/Redux/api.tsx b/src/Redux/api.tsx
index 104fa0c0c75..01c356f6603 100644
--- a/src/Redux/api.tsx
+++ b/src/Redux/api.tsx
@@ -28,6 +28,7 @@ import {
AssetUpdate,
} from "../Components/Assets/AssetTypes";
import {
+ ConsultationModel,
FacilityModel,
LocationModel,
WardModel,
@@ -40,10 +41,13 @@ import {
ILocalBodyByDistrict,
IPartialUpdateExternalResult,
} from "../Components/ExternalResult/models";
+
import { Prescription } from "../Components/Medicine/models";
-import { PatientModel } from "../Components/Patient/models";
+
import { UserModel } from "../Components/Users/models";
import { PaginatedResponse } from "../Utils/request/types";
+import { PatientModel } from "../Components/Patient/models";
+import { IComment, IResource } from "../Components/Resource/models";
/**
* A fake function that returns an empty object casted to type T
@@ -128,6 +132,8 @@ const routes = {
userList: {
path: "/api/v1/users/",
+ method: "GET",
+ TRes: Type>(),
},
userListSkill: {
@@ -843,21 +849,31 @@ const routes = {
createResource: {
path: "/api/v1/resource/",
method: "POST",
+ TRes: Type(),
+ TBody: Type>(),
},
updateResource: {
- path: "/api/v1/resource",
+ path: "/api/v1/resource/{id}",
method: "PUT",
+ TRes: Type(),
+ TBody: Type>(),
},
deleteResourceRecord: {
- path: "/api/v1/resource",
+ path: "/api/v1/resource/{id}",
method: "DELETE",
+ TRes: Type<{
+ detail?: string;
+ }>(),
},
listResourceRequests: {
path: "/api/v1/resource/",
method: "GET",
+ TRes: Type>(),
},
getResourceDetails: {
path: "/api/v1/resource/{id}/",
+ method: "GET",
+ TRes: Type(),
},
downloadResourceRequests: {
path: "/api/v1/resource/",
@@ -866,10 +882,13 @@ const routes = {
getResourceComments: {
path: "/api/v1/resource/{id}/comment/",
method: "GET",
+ TRes: Type>(),
},
addResourceComments: {
path: "/api/v1/resource/{id}/comment/",
method: "POST",
+ TRes: Type(),
+ TBody: Type>(),
},
// Assets endpoints