From 01a21c5b775478de16e02f7074ee713d78ea167b Mon Sep 17 00:00:00 2001 From: Devdeep Ghosh Date: Sun, 19 Nov 2023 07:46:21 +0530 Subject: [PATCH] replaced useDispatch with request in UserAdd component --- src/Components/Users/UserAdd.tsx | 44 ++++++++++++++------------------ src/Redux/api.tsx | 7 +++++ 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/Components/Users/UserAdd.tsx b/src/Components/Users/UserAdd.tsx index d31b405694f..3e5c376bfb4 100644 --- a/src/Components/Users/UserAdd.tsx +++ b/src/Components/Users/UserAdd.tsx @@ -1,6 +1,5 @@ import { Link, navigate } from "raviger"; import { lazy, useCallback, useEffect, useState } from "react"; -import { useDispatch } from "react-redux"; import { GENDER_TYPES, USER_TYPES, @@ -13,12 +12,6 @@ import { validatePassword, validateUsername, } from "../../Common/validation"; -import { - getDistrictByState, - getLocalbodyByDistrict, - getStates, - getUserListFacility, -} from "../../Redux/actions"; import * as Notification from "../../Utils/Notifications.js"; import { FacilitySelect } from "../Common/FacilitySelect"; import { FacilityModel } from "../Facility/models"; @@ -163,7 +156,6 @@ export const validateRule = ( export const UserAdd = (props: UserProps) => { const { goBack } = useAppHistory(); - const dispatchAction: any = useDispatch(); const { userId } = props; const [state, dispatch] = useAutoSaveReducer( @@ -258,8 +250,10 @@ export const UserAdd = (props: UserProps) => { async (id: number) => { if (id > 0) { setIsDistrictLoading(true); - const districtList = await dispatchAction(getDistrictByState({ id })); - if (districtList) { + const districtList = await request(routes.getDistrictByState, { + pathParams: { id: id.toString() }, + }); + if (districtList && districtList.data) { if (userIndex <= USER_TYPES.indexOf("DistrictAdmin")) { setDistricts([ { @@ -274,18 +268,18 @@ export const UserAdd = (props: UserProps) => { setIsDistrictLoading(false); } }, - [dispatchAction] + [authUser.district, authUser.district_object?.name, userIndex] ); const fetchLocalBody = useCallback( async (id: number) => { if (id > 0) { setIsLocalbodyLoading(true); - const localBodyList = await dispatchAction( - getLocalbodyByDistrict({ id }) - ); + const localBodyList = await request(routes.getLocalbodyByDistrict, { + pathParams: { id: id.toString() }, + }); setIsLocalbodyLoading(false); - if (localBodyList) { + if (localBodyList && localBodyList.data) { if (userIndex <= USER_TYPES.indexOf("LocalBodyAdmin")) { setLocalBodies([ { @@ -299,14 +293,14 @@ export const UserAdd = (props: UserProps) => { } } }, - [dispatchAction] + [authUser.local_body, authUser.local_body_object?.name, userIndex] ); const fetchStates = useCallback( async (status: statusType) => { setIsStateLoading(true); - const statesRes = await dispatchAction(getStates()); - if (!status.aborted && statesRes.data.results) { + const { data: statesData } = await request(routes.statesList, {}); + if (!status.aborted && statesData && statesData.results) { if (userIndex <= USER_TYPES.indexOf("StateAdmin")) { setStates([ { @@ -315,26 +309,26 @@ export const UserAdd = (props: UserProps) => { }, ]); } else { - setStates(statesRes.data.results); + setStates(statesData.results); } } setIsStateLoading(false); }, - [dispatchAction] + [authUser.state, authUser.state_object?.name, userIndex] ); const fetchFacilities = useCallback( - async (status: any) => { + async (status: statusType) => { setIsStateLoading(true); - const res = await dispatchAction( - getUserListFacility({ username: authUser.username }) - ); + const res = await request(routes.userListFacility, { + pathParams: { username: authUser.username }, + }); if (!status.aborted && res && res.data) { setFacilities(res.data); } setIsStateLoading(false); }, - [dispatchAction, authUser.username] + [authUser.username] ); useAbortableEffect( diff --git a/src/Redux/api.tsx b/src/Redux/api.tsx index 128f134c11c..168f4e05dd3 100644 --- a/src/Redux/api.tsx +++ b/src/Redux/api.tsx @@ -34,6 +34,7 @@ import { DistrictModel, FacilityModel, LocationModel, + StateModel, WardModel, } from "../Components/Facility/models"; import { @@ -150,6 +151,8 @@ const routes = { userListFacility: { path: "/api/v1/users/{username}/get_facilities/", + method: "GET", + TRes: Type(), }, addUserFacility: { @@ -613,6 +616,8 @@ const routes = { // States statesList: { path: "/api/v1/state/", + method: "GET", + TRes: Type>(), }, getState: { @@ -628,6 +633,8 @@ const routes = { }, getDistrictByState: { path: "/api/v1/state/{id}/districts/", + method: "GET", + TRes: Type(), }, getDistrictByName: { path: "/api/v1/district/",