frontPanel/src/pages/IntegrationsPage/IntegrationsModal/AmoQuestions/AmoQuestions.tsx

168 lines
5.2 KiB
TypeScript
Raw Normal View History

2024-06-30 15:32:16 +00:00
import { FC, useMemo, useState } from "react";
import { ItemsSelectionView } from "./ItemsSelectionView/ItemsSelectionView";
import { ItemDetailsView } from "./ItemDetailsView/ItemDetailsView";
import { Box } from "@mui/material";
2024-06-30 15:32:16 +00:00
import { MinifiedData, QuestionKeys, SelectedQuestions, TagKeys, TagQuestionHC } from "../types";
2024-06-28 19:34:13 +00:00
import { EntitiesQuestions } from "./EntitiesQuestions";
2024-04-12 15:07:37 +00:00
type Props = {
2024-06-30 15:32:16 +00:00
selectedCurrentFields: MinifiedData[] | [];
questionsItems: MinifiedData[] | [];
2024-06-30 15:32:16 +00:00
fieldsItems: MinifiedData[] | [];
selectedQuestions: SelectedQuestions;
handleAddQuestion: (scope: QuestionKeys | TagKeys, id: string, type: "question" | "tag") => void;
openDelete: (data: TagQuestionHC) => void;
handlePrevStep: () => void;
handleNextStep: () => void;
2024-06-30 15:32:16 +00:00
FieldsAllowedFC: MinifiedData[]
setSelectedCurrentFields: (value: MinifiedData[]) => void;
2024-04-12 15:07:37 +00:00
};
2024-06-30 15:32:16 +00:00
export type QuestionPair = {
question: string,
field: string
}
2024-04-12 15:07:37 +00:00
export const AmoQuestions: FC<Props> = ({
2024-06-30 15:32:16 +00:00
selectedCurrentFields,
questionsItems,
2024-06-30 15:32:16 +00:00
fieldsItems,
selectedQuestions,
handleAddQuestion,
handlePrevStep,
handleNextStep,
openDelete,
2024-06-30 15:32:16 +00:00
FieldsAllowedFC,
setSelectedCurrentFields
2024-04-12 15:07:37 +00:00
}) => {
2024-06-30 15:32:16 +00:00
console.log("selectedCurrentFields")
console.log(selectedCurrentFields)
console.log("selectedQuestions")
console.log(selectedQuestions)
2024-04-16 10:57:38 +00:00
const [isSelection, setIsSelection] = useState<boolean>(false);
const [activeScope, setActiveScope] = useState<QuestionKeys | null>(null);
const [selectedQuestion, setSelectedQuestion] = useState<string | null>(null);
2024-06-30 15:32:16 +00:00
const [selectedField, setSelectedField] = useState<string | null>(null);
2024-06-30 15:32:16 +00:00
const [isCurrentFields, setIsCurrentFields] = useState(true);
const handleAddNewField = () => {
if (activeScope === null || selectedQuestion === null) return;
setActiveScope(null);
handleAddQuestion(activeScope, selectedQuestion, "question");
};
2024-06-30 15:32:16 +00:00
const handleAddCurrentField = () => {
if (activeScope === null || selectedField === null || selectedQuestion === null || selectedCurrentFields.some(e => e.id === selectedQuestion)) return;
setActiveScope(null);
setSelectedCurrentFields((old) => {
console.log(old)
return (
[...old, {
id: selectedQuestion,
title: questionsItems.find(e => e.id === selectedQuestion)?.title,
entity: activeScope,
subTitle: selectedField
}]
)
});
};
const handleAdd = () => {
console.log(activeScope, selectedQuestion)
if (isCurrentFields) {
console.log("добавляю текущее поле")
handleAddCurrentField()
} else {
console.log("добавляю новое поле")
handleAddNewField()
}
}
const handleDelete = (id: string, scope: QuestionKeys) => {
openDelete({
id,
scope,
type: "question",
});
};
2024-06-30 15:32:16 +00:00
const SCFworld = useMemo(() => {
const obj = {
Lead: [],
Company: [],
Customer: [],
Contact: []
}
selectedCurrentFields.forEach((e) => {
if (!obj[e.entity]?.includes(e.id)) {
obj[e.entity].push(e.id)
}
})
return obj
}, [selectedCurrentFields])
2024-04-16 10:57:38 +00:00
2024-04-12 15:07:37 +00:00
return (
<Box
sx={{
display: "flex",
flexDirection: "column",
height: "100%",
flexGrow: 1,
}}
>
{isSelection && activeScope !== null ? (
2024-06-28 19:34:13 +00:00
<EntitiesQuestions
2024-06-30 15:32:16 +00:00
FieldsAllowedFC={FieldsAllowedFC}
fieldsItems={fieldsItems}
items={questionsItems}
selectedItemId={selectedQuestion}
2024-06-30 15:32:16 +00:00
setSelectedQuestion={setSelectedQuestion}
selectedField={selectedField}
setSelectedField={setSelectedField}
2024-04-16 10:57:38 +00:00
onSmallBtnClick={() => {
setActiveScope(null);
2024-04-16 10:57:38 +00:00
setIsSelection(false);
}}
onLargeBtnClick={() => {
handleAdd();
setActiveScope(null);
2024-04-16 10:57:38 +00:00
setIsSelection(false);
}}
2024-06-30 15:32:16 +00:00
activeScope={activeScope}
setIsCurrentFields={setIsCurrentFields}
isCurrentFields={isCurrentFields}
2024-04-16 10:57:38 +00:00
/>
2024-06-28 19:34:13 +00:00
// Здесь выбираем элемент в табличку
// <ItemsSelectionView
// items={questionsItems}
// selectedItemId={selectedQuestion}
// setSelectedItem={setSelectedQuestion}
// onSmallBtnClick={() => {
// setActiveScope(null);
// setIsSelection(false);
// }}
// onLargeBtnClick={() => {
// handleAdd();
// setActiveScope(null);
// setIsSelection(false);
// }}
// />
2024-04-16 10:57:38 +00:00
) : (
// Табличка
<ItemDetailsView
2024-06-30 15:32:16 +00:00
items={[...questionsItems, ...FieldsAllowedFC]}
setActiveScope={setActiveScope}
2024-06-30 15:32:16 +00:00
selectedQuestions={{
Lead: [...selectedQuestions.Lead, ...SCFworld.Lead],
Company: [...selectedQuestions.Company, ...SCFworld.Company],
Customer: [...selectedQuestions.Customer, ...SCFworld.Customer],
Contact: [...selectedQuestions.Contact, ...SCFworld.Contact]
}}
2024-04-16 10:57:38 +00:00
setIsSelection={setIsSelection}
handleLargeBtn={handleNextStep}
handleSmallBtn={handlePrevStep}
deleteHC={handleDelete}
2024-04-12 15:07:37 +00:00
/>
2024-04-16 10:57:38 +00:00
)}
2024-04-12 15:07:37 +00:00
</Box>
);
};