Skip to content

Commit

Permalink
fix: agency update and modal issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Soarecos committed Aug 11, 2022
1 parent dc1bb0c commit aab6479
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/api/agency/hasAgencyConsultants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const hasAgencyConsultants = (agencyId: string) => {
skipAuth: false,
responseHandling: [FETCH_ERRORS.CATCH_ALL],
}).then((data) => {
return data.total > 0;
return data.length > 0;
});
};

Expand Down
6 changes: 5 additions & 1 deletion src/components/Agency/AgencyAddAllgemeines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,11 @@ export default function AgencieAddAllgemeines() {
name="online"
>
<div className="flex">
<Switch size="default" defaultChecked={false} />
<Switch
size="default"
defaultChecked={false}
disabled
/>
<Paragraph className="desc__toggleText">
{t("yes")}
</Paragraph>
Expand Down
10 changes: 9 additions & 1 deletion src/components/Agency/AgencyEditAllgemeines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { UserRole } from "../../enums/UserRole";
import { SliderFormField } from "../SliderFormField";
import { useFeatureContext } from "../../context/FeatureContext";
import { useUserRoles } from "../../hooks/useUserRoles.hook";
import { hasAgencyConsultants } from "../../api/agency/hasAgencyConsultants";

const { Paragraph } = Typography;
const { Item } = Form;
Expand Down Expand Up @@ -63,6 +64,8 @@ export default function AgencieEditAllgemeines() {
const [readOnlyAgencyGender, setReadOnlyAgencyGender] =
useState<boolean>(true);
const [readOnlyAgencyAge, setReadOnlyAgencyAge] = useState<boolean>(true);
const [agencyHasConsultants, setAgencyHasConsultants] =
useState<boolean>(true);
const currentPath = useLocation().pathname;
const [, agencyID] = currentPath.match(/.*\/([^/]+)\/[^/]+/);
const [, hasRole] = useUserRoles();
Expand Down Expand Up @@ -212,10 +215,12 @@ export default function AgencieEditAllgemeines() {
Promise.all([
getAgencyPostCodeRange(agencyId),
getAgencyDataAgencyId(agencyId),
hasAgencyConsultants(agencyId),
]).then((values) => {
const agencyPostCodeRangesResponse = values[0];
/* eslint no-underscore-dangle: ["error", { "allow": ["_embedded"] }] */
const agencyData = values[1]._embedded;
setAgencyHasConsultants(values[2]);
setAgencyPostCodeRanges(agencyPostCodeRangesResponse);
setPostCodeRangesSwitchState(agencyPostCodeRangesResponse);
setAgencyModel(agencyData);
Expand Down Expand Up @@ -469,7 +474,10 @@ export default function AgencieEditAllgemeines() {
<Switch
size="default"
defaultChecked={false}
disabled={readOnlyOnline}
disabled={
readOnlyOnline ||
(!readOnlyOnline && !agencyHasConsultants)
}
/>
<Paragraph className="desc__toggleText">
{t("yes")}
Expand Down
19 changes: 11 additions & 8 deletions src/components/Agency/AgencyEditErstberatung.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ import ErstberatungEditModal from "./ErstberatungEditModal";
import getAgencyEventTypes from "../../api/agency/getAgencyEventTypes";
import getConsultantForAgencyEventTypes from "../../api/agency/getConsultantForAgencyEventTypes";
import EventTypeDeletionModal from "./EventTypeDeletionModal";
import { agencyConsultants } from "../../api/agency/hasAgencyConsultants";

const { Paragraph } = Typography;

export default function AgencieEditErstberatung() {
const { t } = useTranslation();
const [topics, setTopics] = useState([]);
const [apiAgencyEventTypes, setApiAgencyEventTypes] = useState<
AgencyEventTypes[]
>([]);
const [showEditModal, setShowEditModal] = useState(false);
const [showDeleteModal, setShowDeleteModal] = useState(false);
const [showNewModal, setShowNewModal] = useState(false);
const [editableData, setEditableData] = useState(undefined);
const [eventTypeDelete, setEventTypeDelete] = useState(undefined);
const [allAgencyConsultants, setAllAgencyConsultants] =
useState<ConsultantInterface[]>(undefined);
const [isLoading, setIsLoading] = useState(true);
const [tableState] = useState<TableState>({
current: 1,
Expand All @@ -57,11 +57,14 @@ export default function AgencieEditErstberatung() {
};

const getAgencyData = () => {
getAgencyEventTypes(agencyId).then((resp: AgencyEventTypes[]) => {
setApiAgencyEventTypes(resp);
const agencyEventTypes = transformData(resp);
Promise.all([
getAgencyEventTypes(agencyId),
agencyConsultants(agencyId),
]).then((resp) => {
const agencyEventTypes = transformData(resp[0]);
setTopics(agencyEventTypes);
setIsLoading(false);
setAllAgencyConsultants(resp[1]);
});
};

Expand Down Expand Up @@ -235,14 +238,14 @@ export default function AgencieEditErstberatung() {
showEditModal={showNewModal}
handleCancel={handleCancelNew}
handleSave={handleSaveNew}
apiData={apiAgencyEventTypes}
allAgencyConsultants={allAgencyConsultants}
/>
<ErstberatungEditModal
showEditModal={showEditModal}
handleCancel={handleCancelEdit}
handleSave={handleSaveEdit}
editableData={editableData}
apiData={apiAgencyEventTypes}
allAgencyConsultants={allAgencyConsultants}
/>
<EventTypeDeletionModal
showDeleteModal={showDeleteModal}
Expand Down
55 changes: 20 additions & 35 deletions src/components/Agency/ErstberatungEditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ import { Form, Input, message, Modal, Typography } from "antd";
import { useTranslation } from "react-i18next";
import Title from "antd/es/typography/Title";
import TextArea from "antd/lib/input/TextArea";
import { useEffect, useState } from "react";
import { useLocation } from "react-router";
import { useEffect } from "react";
import { SelectFormField } from "../SelectFormField";
import {
AgencyEditData,
AgencyEventTypes,
ConsultantInterface,
} from "../../types/agencyEdit";
import { AgencyEditData, ConsultantInterface } from "../../types/agencyEdit";
import putConsultantForAgencyEventTypes from "../../api/agency/putConsultantForAgencyEventTypes";

const { Paragraph } = Typography;
Expand All @@ -20,24 +16,28 @@ export default function ErstberatungEditModal(props: {
handleCancel?: (callback: Function) => void;
handleSave?: (callback: Function) => void;
editableData: AgencyEditData;
apiData: AgencyEventTypes[];
allAgencyConsultants: ConsultantInterface[];
}) {
const { t } = useTranslation();
const [formInstance] = Form.useForm();
const currentPath = useLocation().pathname;
const [, agencyId] = currentPath.match(/.*\/([^/]+)\/[^/]+/);
const [advisors, setAdvisors] = useState([]);

useEffect(() => {
const newConsultants = [];
props?.apiData[0]?.consultants?.map((consultant: ConsultantInterface) => {
return newConsultants.push({
id: consultant.consultantId,
name: consultant.consultantName,
});
formInstance.setFieldsValue({
name: props?.editableData?.name,
description: props.editableData?.description,
url: props.editableData?.url,
duration: props.editableData?.duration,
advisor: props.editableData?.advisor?.map((advisor) => {
return {
label: advisor.name,
value: advisor.id,
};
}),
location: props.editableData?.location,
});
setAdvisors(newConsultants);
}, [props.apiData]);
}, [props.editableData]);

return (
<Modal
Expand Down Expand Up @@ -87,9 +87,7 @@ export default function ErstberatungEditModal(props: {
});
});
}}
onCancel={() => {
props.handleCancel(() => {});
}}
onCancel={() => props.handleCancel(() => {})}
destroyOnClose
cancelText={t(
"agency.edit.erstberatung.modal_edit_consultation_type.cancel"
Expand All @@ -104,19 +102,6 @@ export default function ErstberatungEditModal(props: {
labelAlign="left"
labelWrap
layout="vertical"
initialValues={{
name: props?.editableData?.name,
description: props?.editableData?.description,
url: props?.editableData?.url,
duration: props?.editableData?.duration,
advisor: props?.editableData?.advisor?.map((advisor) => {
return {
label: advisor.name,
value: advisor.id,
};
}),
location: props?.editableData?.location,
}}
>
<Item
label={t(
Expand Down Expand Up @@ -186,10 +171,10 @@ export default function ErstberatungEditModal(props: {
isMulti
allowClear
placeholder="agency.edit.erstberatung.modal_new_consultation_type.advisor"
options={advisors?.map((advisor) => {
options={props.allAgencyConsultants?.map((consultant) => {
return {
label: advisor.name,
value: advisor.id,
label: consultant.consultantName,
value: consultant.consultantId,
};
})}
/>
Expand Down
24 changes: 6 additions & 18 deletions src/components/Agency/ErstberatungNewModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { useTranslation } from "react-i18next";
import Title from "antd/es/typography/Title";
import TextArea from "antd/lib/input/TextArea";
import { useLocation } from "react-router";
import { useEffect, useState } from "react";
import { SelectFormField } from "../SelectFormField";
import postConsultantForAgencyEventTypes from "../../api/agency/postConsultantForAgencyEventTypes";
import { AgencyEventTypes, ConsultantInterface } from "../../types/agencyEdit";
import { ConsultantInterface } from "../../types/agencyEdit";

const { Paragraph } = Typography;
const { Item } = Form;
Expand All @@ -15,24 +14,12 @@ export default function ErstberatungNewModal(props: {
showEditModal: boolean;
handleCancel?: (callback: Function) => void;
handleSave?: (callback: Function) => void;
apiData: AgencyEventTypes[];
allAgencyConsultants: ConsultantInterface[];
}) {
const { t } = useTranslation();
const [formInstance] = Form.useForm();
const currentPath = useLocation().pathname;
const [, agencyId] = currentPath.match(/.*\/([^/]+)\/[^/]+/);
const [advisors, setAdvisors] = useState([]);

useEffect(() => {
const newConsultants = [];
props?.apiData[0]?.consultants?.map((consultant: ConsultantInterface) => {
return newConsultants.push({
id: consultant.consultantId,
name: consultant.consultantName,
});
});
setAdvisors(newConsultants);
}, [props?.apiData]);

return (
<Modal
Expand Down Expand Up @@ -79,6 +66,7 @@ export default function ErstberatungNewModal(props: {
});
}}
onCancel={() => {
formInstance.resetFields();
props.handleCancel(() => {});
}}
destroyOnClose
Expand Down Expand Up @@ -151,10 +139,10 @@ export default function ErstberatungNewModal(props: {
isMulti
allowClear
placeholder="agency.edit.erstberatung.modal_new_consultation_type.advisor"
options={advisors?.map((advisor) => {
options={props.allAgencyConsultants?.map((consultant) => {
return {
label: advisor.name,
value: advisor.id,
label: consultant.consultantName,
value: consultant.consultantId,
};
})}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/types/agencyEdit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface AdvisorInterface {
id: string;
}

interface ConsultantInterface {
export interface ConsultantInterface {
consultantId: string;
consultantName: string;
}
Expand Down

0 comments on commit aab6479

Please sign in to comment.