Skip to content

Commit

Permalink
🐛 FIX: Error displayed for no facility match
Browse files Browse the repository at this point in the history
  • Loading branch information
sam9111 committed May 24, 2021
1 parent 63a33b2 commit c54de52
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
37 changes: 36 additions & 1 deletion src/Components/Users/UserAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useDispatch, useSelector } from "react-redux";
import { GENDER_TYPES, USER_TYPES } from "../../Common/constants";
import { statusType, useAbortableEffect } from "../../Common/utils";
import { validateEmailAddress, validatePassword, validateUsername } from "../../Common/validation";
import { addUser, getDistrictByState, getLocalbodyByDistrict, getStates } from "../../Redux/actions";
import { addUser, getDistrictByState, getLocalbodyByDistrict, getStates, getUserListFacility } from "../../Redux/actions";
import * as Notification from "../../Utils/Notifications.js";
import { FacilitySelect } from "../Common/FacilitySelect";
import { DateInputField, PhoneNumberField, SelectField, TextInputField } from "../Common/HelperInputFields";
Expand Down Expand Up @@ -92,6 +92,7 @@ export const UserAdd = (props: UserProps) => {
const [isStateLoading, setIsStateLoading] = useState(false);
const [isDistrictLoading, setIsDistrictLoading] = useState(false);
const [isLocalbodyLoading, setIsLocalbodyLoading] = useState(false);
const [current_user_facilities, setFacilities] = useState<Array<String>>([]);
const [states, setStates] = useState(initialStates);
const [districts, setDistricts] = useState(selectStates);
const [localBody, setLocalBody] = useState(selectDistrict);
Expand All @@ -100,7 +101,11 @@ export const UserAdd = (props: UserProps) => {
const rootState: any = useSelector((rootState) => rootState);
const { currentUser } = rootState;
const isSuperuser = currentUser.data.is_superuser;

const username = currentUser.data.username;

const userType = currentUser.data.user_type;

const userIndex = USER_TYPES.indexOf(userType);
const userTypes = isSuperuser ? [...USER_TYPES] : USER_TYPES.slice(0, userIndex + 1)

Expand Down Expand Up @@ -138,6 +143,9 @@ export const UserAdd = (props: UserProps) => {
[dispatchAction]
);




// const fetchData = useCallback(
// async (status: statusType) => {
// if (userId) {
Expand Down Expand Up @@ -184,12 +192,26 @@ export const UserAdd = (props: UserProps) => {
[dispatchAction]
);

const fetchFacilities = useCallback(
async (status: any) => {
setIsStateLoading(true);
const res = await dispatchAction(getUserListFacility({ username }));
if (!status.aborted && res && res.data) {
const facilities = res.data.map((f: any) => f.id);
setFacilities(facilities);
}
setIsStateLoading(false);
}, [dispatchAction]
);


useAbortableEffect(
(status: statusType) => {
// if (userId) {
// fetchData(status);
// }
fetchStates(status);
fetchFacilities(status);
},
[dispatch]
);
Expand Down Expand Up @@ -230,6 +252,18 @@ export const UserAdd = (props: UserProps) => {
let invalidForm = false;
Object.keys(state.form).forEach(field => {
switch (field) {
case "facilities":
if (userType === "Staff" && state.form["user_type"] === "Staff") {
invalidForm = true;
for (const facilityId of state.form[field]) {
if (current_user_facilities.indexOf(facilityId) !== -1) {
invalidForm = false;
return;
}
}
errors[field] = "Please select atleast one of your facilities";
}
return;
case "user_type":
if (!state.form[field]) {
errors[field] = "Please select the User Type";
Expand Down Expand Up @@ -294,6 +328,7 @@ export const UserAdd = (props: UserProps) => {
invalidForm = true;
}
return;

default:
return;
}
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13658,7 +13658,7 @@ react-lifecycles-compat@^3.0.4:
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz#4f1a273afdfc8f3488a8c516bfda78f872352362"
integrity sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA==

react-phone-input-2@^2.13.9:
react-phone-input-2@^2.14.0:
version "2.14.0"
resolved "https://registry.yarnpkg.com/react-phone-input-2/-/react-phone-input-2-2.14.0.tgz#75dff7ba503becc7a19e0e8ff5aa3d878265af77"
integrity sha512-gOY3jUpwO7ulryXPEdqzH7L6DPqI9RQxKfBxZbgqAwXyALGsmwLWFyi2RQwXlBLWN/EPPT4Nv6I9TESVY2YBcg==
Expand Down

0 comments on commit c54de52

Please sign in to comment.