Render previous select input options when switching between workflows in builder

This commit is contained in:
Joao Correia
2025-01-23 17:08:32 +00:00
parent a0936cb1a4
commit a4f60455b5
4 changed files with 33 additions and 21 deletions

View File

@@ -1,7 +1,7 @@
import { WorkflowStep } from "@/interfaces/approval.workflow";
import Option from "@/interfaces/option";
import { CorporateUser, TeacherUser } from "@/interfaces/user";
import { useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { AiOutlineUserAdd } from "react-icons/ai";
import { BsTrash } from "react-icons/bs";
import { LuGripHorizontal } from "react-icons/lu";
@@ -16,6 +16,7 @@ interface Props extends WorkflowStep {
export default function WorkflowEditableStepComponent({
stepNumber,
assignees = [null],
finalStep,
onDelete,
onSelectChange,
@@ -47,6 +48,19 @@ export default function WorkflowEditableStepComponent({
[entityCorporates]
);
const allOptions = useMemo(() => [...teacherOptions, ...corporateOptions], [teacherOptions, corporateOptions]);
useEffect(() => {
if (assignees && assignees.length > 0) {
const initialSelects = assignees.map((assignee) =>
typeof assignee === 'string' ? allOptions.find(opt => opt.value === assignee) || null : null
);
if (JSON.stringify(initialSelects) !== JSON.stringify(selects)) {
setSelects(initialSelects);
}
}
}, [assignees, allOptions]);
const selectedValues = useMemo(() =>
selects.filter((opt): opt is Option => !!opt).map(opt => opt.value),
[selects]