From 5437a6878aabc45b03d8790cf9400a8962bfa319 Mon Sep 17 00:00:00 2001 From: "MSI\\derek" Date: Thu, 13 Jun 2024 15:36:57 +0800 Subject: [PATCH] update edit team --- src/components/EditSkill/EditSkill.tsx | 14 +++++++++++++- src/components/EditTeam/Allocation.tsx | 18 ++++++++---------- src/components/EditTeam/EditTeam.tsx | 1 - 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/components/EditSkill/EditSkill.tsx b/src/components/EditSkill/EditSkill.tsx index a55723e..19c323c 100644 --- a/src/components/EditSkill/EditSkill.tsx +++ b/src/components/EditSkill/EditSkill.tsx @@ -32,7 +32,10 @@ interface Props { skills: SkillResult[]; } -const EditSkill: React.FC = async ({ skills }) => { +const EditSkill: React.FC = async ({ + skills +}) => { + const codeList = skills.map(s => s.code.toLowerCase().trim()) const { t } = useTranslation(); const formProps = useForm(); const [serverError, setServerError] = useState(""); @@ -49,6 +52,15 @@ const EditSkill: React.FC = async ({ skills }) => { async (data) => { try { console.log(data); + let haveError = false + haveError = codeList.includes(data.code.toLowerCase().trim()) + if (haveError) { + formProps.setError("code", { message: t("Duplicated Code"), type: "required" }) + return + } + data.name = data.name.trim() + data.code = data.code.trim() + data.description = data.description.trim() const postData = { ...data, id: id, diff --git a/src/components/EditTeam/Allocation.tsx b/src/components/EditTeam/Allocation.tsx index 6c61d80..4d2274c 100644 --- a/src/components/EditTeam/Allocation.tsx +++ b/src/components/EditTeam/Allocation.tsx @@ -50,9 +50,10 @@ const Allocation: React.FC = ({ allStaffs: staff, teamLead }) => { } = useFormContext(); const initialStaffs = staff.map((s) => ({ ...s })); - const [filteredStaff, setFilteredStaff] = useState(initialStaffs); - const [selectedStaff, setSelectedStaff] = useState(() => { - const rearrangedStaff = filteredStaff.sort((a, b) => { + const noTeamstaffs = initialStaffs.filter((staff) => !staff.team) + const [filteredStaff, setFilteredStaff] = useState(noTeamstaffs); + const [selectedStaff, setSelectedStaff] = useState(() => { + const rearrangedStaff = initialStaffs.sort((a, b) => { if (a.id === teamLead) return -1; if (b.id === teamLead) return 1; return 0; @@ -69,6 +70,7 @@ const Allocation: React.FC = ({ allStaffs: staff, teamLead }) => { const removeStaff = useCallback((staff: StaffResult) => { setSelectedStaff((s) => s.filter((s) => s.id !== staff.id)); + setFilteredStaff((prev) => [...prev, ...initialStaffs.filter((s) => s.id === staff.id)]) setDeletedStaffIds((prevIds) => [...prevIds, staff.id]); }, []); @@ -100,7 +102,7 @@ const Allocation: React.FC = ({ allStaffs: staff, teamLead }) => { if (defaultValues !== undefined) { resetField("addStaffIds"); setSelectedStaff( - initialStaffs.filter((s) => defaultValues.addStaffIds?.includes(s.id)) + noTeamstaffs.filter((s) => defaultValues.addStaffIds?.includes(s.id)) ); } }, [defaultValues]); @@ -165,8 +167,8 @@ const Allocation: React.FC = ({ allStaffs: staff, teamLead }) => { }, []); React.useEffect(() => { - setFilteredStaff( - initialStaffs.filter((i) => { + setFilteredStaff((prev) => + prev.filter((i) => { const q = query.toLowerCase(); return ( i.staffId.toLowerCase().includes(q) @@ -177,10 +179,6 @@ const Allocation: React.FC = ({ allStaffs: staff, teamLead }) => { ); }, [staff, query]); - useEffect(() => { - // console.log(getValues("addStaffIds")) - }, [initialStaffs]); - const resetStaff = React.useCallback(() => { clearQueryInput(); clearValues(); diff --git a/src/components/EditTeam/EditTeam.tsx b/src/components/EditTeam/EditTeam.tsx index 394d3c2..2039335 100644 --- a/src/components/EditTeam/EditTeam.tsx +++ b/src/components/EditTeam/EditTeam.tsx @@ -26,7 +26,6 @@ interface Props { const EditTeam: React.FC = async ({ staff, teamInfo }) => { const { team, staffIds } = teamInfo; - console.log(team.name) // console.log(staffIds) const { t } = useTranslation(); const formProps = useForm();