From 94bdde6ad3a38e2a7e9c3517d08d0ce56042dd11 Mon Sep 17 00:00:00 2001 From: Mihoub Date: Thu, 13 Jun 2024 10:42:24 +0200 Subject: [PATCH] fix(dataList): update datalist context for single add --- .../link-publications/index.tsx | 3 +- .../link-publications/name-selector.tsx | 88 +++++++++++++------ 2 files changed, 65 insertions(+), 26 deletions(-) diff --git a/src/pages/api-operation-page/link-publications/index.tsx b/src/pages/api-operation-page/link-publications/index.tsx index b8efa5f..bb569f5 100644 --- a/src/pages/api-operation-page/link-publications/index.tsx +++ b/src/pages/api-operation-page/link-publications/index.tsx @@ -63,6 +63,7 @@ const ContributionPage: React.FC = () => { useEffect(() => { setData(fetchedData); }, [fetchedData]); + const meta = (fetchedData as { meta: any })?.meta; const maxPage = meta ? Math.ceil(meta?.total / 10) : 1; const contrib: Contribute_Production[] = ( @@ -129,7 +130,7 @@ const ContributionPage: React.FC = () => { refetch={refetch} /> ))} - {dataList.length > 0 && } + {dataList.some((item) => item.export === true) && }{" "} ({ ...provided, @@ -22,29 +17,72 @@ export default function SelectWithNames({ }; const threshold = 7; - const handleChange = (option: { value: any }) => { - const selectedIndex = fullName.indexOf(option.value); - - setDataList((prevState) => [ - ...prevState, - { - fullName: option.value, - person_id: idRef, - publi_id: productionId, - first_name: firstName[selectedIndex], - last_name: lastName[selectedIndex], - }, - ]); - - setSelectedId((prevIds) => [...prevIds, productionId]); - }; - const options = fullName.map((name, index) => ({ value: name, label: name, firstName: firstName[index], + lastName: lastName[index], isColored: levenshteinDistance(name, coloredName) <= threshold, })); + useEffect(() => { + const selectedIndex = fullName.indexOf(coloredName); + if (selectedIndex !== -1 && options[selectedIndex]) { + const selectedOption = options[selectedIndex]; + const newElement = { + fullName: coloredName, + person_id: idRef, + publi_id: productionId, + first_name: selectedOption.firstName, + last_name: selectedOption.lastName, + export: false, + }; + + setDataList((prevState) => { + if ( + !prevState.some( + (e) => + e.person_id === newElement.person_id && + e.publi_id === newElement.publi_id + ) + ) { + return [...prevState, newElement]; + } else { + return prevState; + } + }); + } + }, []); + + const handleChange = (option: { value: any }) => { + const selectedIndex = fullName.indexOf(option.value); + + setDataList((prevState) => { + // const index = prevState.findIndex( + // (item) => item.fullName === option.value + // ); + // console.log(index); + // if (index !== -1) { + // const newState = [...prevState]; + // newState[index] = { + // ...newState[index], + // export: true, + // }; + + // return newState; + // } + return [ + ...prevState, + { + fullName: option.value, + person_id: idRef, + publi_id: productionId, + first_name: firstName[selectedIndex], + last_name: lastName[selectedIndex], + export: true, + }, + ]; + }); + }; return (