Skip to content

Commit

Permalink
replaced useDispatch with request in UserAdd component
Browse files Browse the repository at this point in the history
  • Loading branch information
thedevildude committed Nov 19, 2023
1 parent 46711b3 commit 01a21c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
44 changes: 19 additions & 25 deletions src/Components/Users/UserAdd.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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";
Expand Down Expand Up @@ -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<UserForm>(
Expand Down Expand Up @@ -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([
{
Expand All @@ -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([
{
Expand All @@ -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([
{
Expand All @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions src/Redux/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
DistrictModel,
FacilityModel,
LocationModel,
StateModel,
WardModel,
} from "../Components/Facility/models";
import {
Expand Down Expand Up @@ -150,6 +151,8 @@ const routes = {

userListFacility: {
path: "/api/v1/users/{username}/get_facilities/",
method: "GET",
TRes: Type<FacilityModel[]>(),
},

addUserFacility: {
Expand Down Expand Up @@ -613,6 +616,8 @@ const routes = {
// States
statesList: {
path: "/api/v1/state/",
method: "GET",
TRes: Type<PaginatedResponse<StateModel>>(),
},

getState: {
Expand All @@ -628,6 +633,8 @@ const routes = {
},
getDistrictByState: {
path: "/api/v1/state/{id}/districts/",
method: "GET",
TRes: Type<DistrictModel[]>(),
},
getDistrictByName: {
path: "/api/v1/district/",
Expand Down

0 comments on commit 01a21c5

Please sign in to comment.