complate dynamic form
This commit is contained in:
parent
84e9b2beb7
commit
47ea7f34d2
|
|
@ -21,6 +21,8 @@ const StepFormPage: FC = () => {
|
|||
const stageID = params.get("stageID");
|
||||
const processID = params.get("processID");
|
||||
|
||||
|
||||
|
||||
const { data, isLoading, error } = useQuery<WorkflowResponse>({
|
||||
queryKey: ["dynamic-field", stageID, processID],
|
||||
queryFn: () => {
|
||||
|
|
|
|||
|
|
@ -6,8 +6,7 @@ import { useMemo, type FC } from "react";
|
|||
import { useNavigate, useParams } from "react-router-dom";
|
||||
import type {
|
||||
CampaignProcess,
|
||||
GroupedCampaign,
|
||||
StepItems,
|
||||
GroupedCampaign
|
||||
} from "./step.type";
|
||||
|
||||
const StepsPage: FC = () => {
|
||||
|
|
@ -18,82 +17,74 @@ const StepsPage: FC = () => {
|
|||
queryFn: () => getCampaignStepsService(),
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const steps = useMemo(() => {
|
||||
if (!Array.isArray(data) || data.length === 0) return [];
|
||||
|
||||
const row = data[0];
|
||||
if (!row || Object.keys(row).length === 0) return [];
|
||||
|
||||
const processes = Object.keys(row)
|
||||
.filter(
|
||||
(key) =>
|
||||
key.startsWith("process") &&
|
||||
!key.endsWith("_id") &&
|
||||
key.includes("_")
|
||||
)
|
||||
.map((key) => {
|
||||
const index = key.match(/process(\d+)_/)?.[1];
|
||||
if (!index) return null;
|
||||
// 1) جمعآوری فرآیندها
|
||||
const temp: CampaignProcess[] = [];
|
||||
|
||||
return {
|
||||
processId: row[`process${index}`],
|
||||
category: row[`process${index}_category`],
|
||||
score: row[`process${index}_score`],
|
||||
stageId: row[`process${index}_stage_id`],
|
||||
status: row[`process${index}_status`],
|
||||
};
|
||||
})
|
||||
.filter(Boolean);
|
||||
Object.keys(row).forEach(key => {
|
||||
const match = key.match(/^process(\d+)_category$/);
|
||||
if (match) {
|
||||
const i = match[1];
|
||||
temp.push({
|
||||
processId: String(row[`process${i}`]),
|
||||
category: String(row[`process${i}_category`]),
|
||||
score: String(row[`process${i}_score`]),
|
||||
stageId: String(row[`process${i}_stage_id`]),
|
||||
status: String(row[`process${i}_status`]),
|
||||
// title: String(row[`process${i}_title`]),
|
||||
// selected: false,
|
||||
// groupIndex: 0, // placeholder
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Group + Dedupe
|
||||
const grouped = Object.values(
|
||||
processes.reduce(
|
||||
(
|
||||
acc: Record<
|
||||
string,
|
||||
{
|
||||
category: string;
|
||||
processes: CampaignProcess[];
|
||||
stageID: number;
|
||||
processId: number;
|
||||
}
|
||||
>,
|
||||
item
|
||||
) => {
|
||||
if (!item || !item.category) return acc;
|
||||
// 2) گروهبندی + حذف تکراری
|
||||
const grouped: GroupedCampaign[] = Object.values(
|
||||
temp.reduce((acc: Record<string, GroupedCampaign>, item) => {
|
||||
if (!item?.category) return acc;
|
||||
|
||||
if (!acc[item.category]) {
|
||||
acc[item.category] = {
|
||||
category: item.category,
|
||||
processes: [],
|
||||
stageID: Number(item.stageId),
|
||||
processId: Number(item.processId),
|
||||
};
|
||||
}
|
||||
if (!acc[item.category]) {
|
||||
acc[item.category] = {
|
||||
category: item.category,
|
||||
processes: [],
|
||||
// groupIndex: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// حذف تکراریها بر اساس processId
|
||||
const exists = acc[item.category].processes.some(
|
||||
(p) => p.processId === item.processId
|
||||
);
|
||||
const exists = acc[item.category].processes.some(
|
||||
p => p.processId === item.processId
|
||||
);
|
||||
|
||||
if (!exists) {
|
||||
acc[item.category].processes.push({
|
||||
processId: String(item.processId),
|
||||
category: String(item.category),
|
||||
score: String(item.score),
|
||||
stageId: String(item.stageId),
|
||||
status: String(item.status),
|
||||
});
|
||||
}
|
||||
if (!exists) {
|
||||
acc[item.category].processes.push(item);
|
||||
}
|
||||
|
||||
return acc;
|
||||
},
|
||||
{}
|
||||
)
|
||||
return acc;
|
||||
}, {})
|
||||
);
|
||||
return grouped as Array<StepItems>;
|
||||
|
||||
// 3) ست کردن groupIndex روی گروهها و process های داخلش
|
||||
// grouped.forEach((group, index) => {
|
||||
// group.groupIndex = index;
|
||||
// group.processes.forEach(proc => {
|
||||
// proc.groupIndex = index;
|
||||
// });
|
||||
// });
|
||||
|
||||
return grouped;
|
||||
}, [data]);
|
||||
|
||||
|
||||
|
||||
const handleBack = () => {
|
||||
navigate(
|
||||
`${DASHBOARD_ROUTE.sub}/${DASHBOARD_ROUTE.campaigns}/${campaignId}`
|
||||
|
|
@ -103,50 +94,30 @@ const StepsPage: FC = () => {
|
|||
|
||||
|
||||
const handleStepClick = (step: GroupedCampaign) => {
|
||||
if (
|
||||
step.stageID !== null &&
|
||||
step.stageID !== 0 &&
|
||||
step.processId !== null &&
|
||||
step.processId !== 0
|
||||
) {
|
||||
navigate(
|
||||
`${DASHBOARD_ROUTE.sub}/${DASHBOARD_ROUTE.dynamicForm}?stageID=${step.stageID}`,
|
||||
{
|
||||
replace: true,
|
||||
}
|
||||
);
|
||||
const filteIncompleteProcess = step.processes.filter(el => el.status === 'انجام نشده')
|
||||
|
||||
if (filteIncompleteProcess.length > 0) {
|
||||
const firstItem = filteIncompleteProcess[0]
|
||||
if (firstItem.stageId) {
|
||||
navigate(
|
||||
`${DASHBOARD_ROUTE.sub}/${DASHBOARD_ROUTE.dynamicForm}?stageID=${firstItem.stageId}`,
|
||||
{
|
||||
replace: true,
|
||||
})
|
||||
}
|
||||
if (firstItem.processId) {
|
||||
navigate(
|
||||
`${DASHBOARD_ROUTE.sub}/${DASHBOARD_ROUTE.dynamicForm}?processID=${firstItem.processId}`,
|
||||
{
|
||||
replace: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
(step.processId != 0 &&
|
||||
step.processId != null &&
|
||||
step.processId != undefined &&
|
||||
step.stageID === null) ||
|
||||
step.stageID === undefined
|
||||
) {
|
||||
navigate(
|
||||
`${DASHBOARD_ROUTE.sub}/${DASHBOARD_ROUTE.dynamicForm}?processID=${step.processId}`,
|
||||
{
|
||||
replace: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
(step.stageID != 0 &&
|
||||
step.stageID != null &&
|
||||
step.stageID != undefined &&
|
||||
step.processId === null) ||
|
||||
step.processId === undefined
|
||||
) {
|
||||
navigate(
|
||||
`${DASHBOARD_ROUTE.sub}/${DASHBOARD_ROUTE.dynamicForm}?stageID=${step.stageID}`,
|
||||
{
|
||||
replace: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user