Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for searching user type in user creation form #7332

Closed
wants to merge 9 commits into from
48 changes: 27 additions & 21 deletions src/Components/Users/UserAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import routes from "../../Redux/api";
import request from "../../Utils/request/request";
import useQuery from "../../Utils/request/useQuery";
import CareIcon from "../../CAREUI/icons/CareIcon";
import AutocompleteFormField from "../Form/FormFields/Autocomplete";

const Loading = lazy(() => import("../Common/Loading"));

Expand Down Expand Up @@ -266,6 +267,20 @@ export const UserAdd = (props: UserProps) => {
},
});

const { loading: isStateLoading } = useQuery(routes.statesList, {
onResponse: ({ data }) => {
if (!data) {
return;
}

if (userIndex <= USER_TYPES.indexOf("StateAdmin")) {
setStates([authUser.state_object!]);
} else {
setStates(data.results);
}
},
});

const { loading: isLocalbodyLoading } = useQuery(
routes.getAllLocalBodyByDistrict,
{
Expand All @@ -282,17 +297,6 @@ export const UserAdd = (props: UserProps) => {
}
);

const { loading: isStateLoading } = useQuery(routes.statesList, {
onResponse: (result) => {
if (!result || !result.res || !result.data) return;
if (userIndex <= USER_TYPES.indexOf("StateAdmin")) {
setStates([authUser.state_object!]);
} else {
setStates(result.data.results);
}
},
});

const handleDateChange = (e: FieldChangeEvent<Date>) => {
if (dayjs(e.value).isValid()) {
dispatch({
Expand Down Expand Up @@ -328,7 +332,7 @@ export const UserAdd = (props: UserProps) => {
setSelectedFacility(selected as FacilityModel[]);
const form = { ...state.form };
form.facilities = selected
? (selected as FacilityModel[]).map((i) => i.id ?? -1)
? (selected as FacilityModel[]).map((i) => parseInt(i.id as string) ?? -1)
: [];
dispatch({ type: "set_form", form });
};
Expand Down Expand Up @@ -583,10 +587,6 @@ export const UserAdd = (props: UserProps) => {
}
};

if (isLoading) {
return <Loading />;
}

const field = (name: string) => {
return {
id: name,
Expand All @@ -597,14 +597,18 @@ export const UserAdd = (props: UserProps) => {
};
};

if (isLoading) {
return <Loading />;
}

return (
<Page
title={headerText}
options={
<Link
href="https://school.coronasafe.network/targets/12953"
href="https://school.coronasafe.network/targets/12953 "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed?

Suggested change
href="https://school.coronasafe.network/targets/12953 "
href="https://school.coronasafe.network/targets/12953"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While saving the file extra space removed.
Is this making any conflict?

className="inline-block rounded border border-gray-600 bg-gray-50 px-4 py-2 text-gray-600 transition hover:bg-gray-100"
target="_blank"
target="_blank "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed?

Suggested change
target="_blank "
target="_blank"

>
<CareIcon icon="l-info-circle" className="text-lg" /> &nbsp;Need Help?
</Link>
Expand All @@ -631,13 +635,15 @@ export const UserAdd = (props: UserProps) => {
showAll={false}
/>
</div>
<SelectFormField

<AutocompleteFormField
{...field("user_type")}
required
label="User Type"
options={userTypes}
placeholder="Search for a user type..."
options={userTypes ?? []}
optionLabel={(o) => o.role + (o.readOnly ? " (Read Only)" : "")}
optionValue={(o) => o.id}
required
/>

{state.form.user_type === "Doctor" && (
Expand Down
Loading