Skip to content

Commit

Permalink
Add: functionality to add/remove organizations from provider when edi…
Browse files Browse the repository at this point in the history
…ting
  • Loading branch information
georgipavlov-7DIGIT committed Sep 2, 2024
1 parent 43fd0ef commit 7af5bb0
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 5 deletions.
40 changes: 40 additions & 0 deletions src/blocks/EditProvider/EditProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import Joi from "joi";

import "./edit-provider.scss";
import { useGetAllOrganizations } from "../../hooks";

const fetchCountryMinPrice = async () => {
const { data } = await countrySvc.getActiveCountries();
Expand Down Expand Up @@ -77,6 +78,9 @@ export const EditProvider = ({
const localizationQuery = useGetCountryAndLanguages();
const workWithQuery = useGetWorkWithCategories();

const { data: organizations, isLoading: organizationsLoading } =
useGetAllOrganizations();

const [errors, setErrors] = useState({});

const specializationOptions = [
Expand Down Expand Up @@ -118,6 +122,7 @@ export const EditProvider = ({
totalConsultations: Joi.any(),
earliestAvailableSlot: Joi.any(),
videoLink: Joi.string().uri().allow("", null),
organizations: Joi.array().min(1).label(t("organizations_error")),
});

const sexOptions = [
Expand Down Expand Up @@ -193,6 +198,30 @@ export const EditProvider = ({
return workWithOptions;
}, [workWithQuery.data, providerData]);

const getOrganizationOptions = useCallback(() => {
const organizationOptions = [];

if (organizations && providerData) {
const providerOrganizations = providerData.organizations.map(
(x) => x.organization_id || x
);
for (let i = 0; i < organizations.length; i++) {
const newOrganization = {};
const organization = organizations[i];
newOrganization.value = organization.organization_id;
newOrganization.label = organization.name;
newOrganization.selected = providerOrganizations.includes(
organization.organization_id
);
newOrganization.selectedIndex = providerOrganizations.indexOf(
organization.organization_id
);
organizationOptions.push(newOrganization);
}
}
return organizationOptions;
}, [organizations, providerData]);

const handleChange = (field, value) => {
const data = { ...providerData };
data[field] = value;
Expand Down Expand Up @@ -421,6 +450,17 @@ export const EditProvider = ({
addMoreText={t("add_more_work_with")}
errorMessage={errors.workWith}
/>
<Select
placeholder={t("select")}
options={getOrganizationOptions()}
handleChange={(organizations) =>
handleWorkWithAndLanguageSelect("organizations", organizations)
}
label={t("organizations_label") + " *"}
maxShown={5}
addMoreText={t("add_more_organizations")}
errorMessage={errors.organizations}
/>
</GridItem>
{errors.submit ? (
<GridItem md={8} lg={12}>
Expand Down
5 changes: 4 additions & 1 deletion src/blocks/EditProvider/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,8 @@
"search": "Search",
"no_data_found": "No data found",
"select": "Select",
"remove": "Remove"
"remove": "Remove",

"organizations_label": "Organizations",
"organizations_error": "Select at least one organization"
}
5 changes: 4 additions & 1 deletion src/blocks/EditProvider/locales/kk.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,8 @@
"search": "Іздеу",
"no_data_found": "Ақпарат табылмады",
"select": "Таңдау",
"remove": "Жою"
"remove": "Жою",

"organizations_label": "Ұйымдар",
"organizations_error": "Кемінде бір ұйымды таңдау қажет"
}
5 changes: 4 additions & 1 deletion src/blocks/EditProvider/locales/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,8 @@
"search": "Wyszukiwanie",
"no_data_found": "Nie znaleziono danych",
"select": "Wybierz",
"remove": "Usuń"
"remove": "Usuń",

"organizations_label": "Organizacje",
"organizations_error": "Wybierz co najmniej jedną organizację"
}
4 changes: 3 additions & 1 deletion src/blocks/EditProvider/locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,7 @@
"search": "Поиск",
"no_data_found": "Данные не найдены",
"select": "Выбрать",
"remove": "Удалить"
"remove": "Удалить",
"organizations_label": "Организации",
"organizations_error": "Выберите хотя бы одну организацию"
}
4 changes: 3 additions & 1 deletion src/blocks/EditProvider/locales/uk.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,7 @@
"search": "Пошук",
"no_data_found": "Дані не знайдено",
"select": "Виберіть",
"remove": "Видалити"
"remove": "Видалити",
"organizations_label": "Організації",
"organizations_error": "Виберіть принаймні одну організацію"
}
1 change: 1 addition & 0 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ export * from "./useActivateQuestion.js";
export * from "./useDeleteQuestion.js";
export * from "./useGetQuestions.js";
export * from "./useDebounce.js";
export * from "./useGetAllOrganizations.js";
10 changes: 10 additions & 0 deletions src/hooks/useGetAllOrganizations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useQuery } from "@tanstack/react-query";
import { organizationSvc } from "@USupport-components-library/services";

export default function useGetAllOrganizations() {
return useQuery(["organizations"], async () => {
const data = await organizationSvc.getAllOrganizations();
return data;
});
}
export { useGetAllOrganizations };
9 changes: 9 additions & 0 deletions src/hooks/useUpdateProviderData.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export default function useUpdateProviderData(onSuccess, onError) {
}
});

newPayload.organizationIds = newPayload.organizations.map((item) => {
if (typeof item === "object") {
return item.organization_id;
} else {
return item;
}
});

newPayload.consultationPrice = Number(newPayload.consultationPrice);
newPayload.providerId = newPayload.providerDetailId;
newPayload.email = newPayload.email.toLowerCase();
Expand All @@ -36,6 +44,7 @@ export default function useUpdateProviderData(onSuccess, onError) {
delete newPayload.image;
delete newPayload.totalConsultations;
delete newPayload.earliestAvailableSlot;
delete newPayload.organizations;
return newPayload;
};

Expand Down

0 comments on commit 7af5bb0

Please sign in to comment.