Skip to content

Commit

Permalink
fix(dataList): update datalist context for single add
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihoub2 committed Jun 13, 2024
1 parent 389a5bb commit 94bdde6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/pages/api-operation-page/link-publications/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const ContributionPage: React.FC<ContributionPageProps> = () => {
useEffect(() => {
setData(fetchedData);
}, [fetchedData]);

const meta = (fetchedData as { meta: any })?.meta;
const maxPage = meta ? Math.ceil(meta?.total / 10) : 1;
const contrib: Contribute_Production[] = (
Expand Down Expand Up @@ -129,7 +130,7 @@ const ContributionPage: React.FC<ContributionPageProps> = () => {
refetch={refetch}
/>
))}
{dataList.length > 0 && <ExcelExportButton />}
{dataList.some((item) => item.export === true) && <ExcelExportButton />}{" "}
<BottomPaginationButtons
page={page}
maxPage={maxPage}
Expand Down
88 changes: 63 additions & 25 deletions src/pages/api-operation-page/link-publications/name-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import NameFromScanr from "../../../api/contribution-api/getNames";
import { levenshteinDistance } from "../utils/compare";
import { Col, Row } from "@dataesr/dsfr-plus";
import { useDataList } from "./data-list-context";
import { useEffect } from "react";

export default function SelectWithNames({
productionId,
idRef,
coloredName,
setSelectedId,
}) {
export default function SelectWithNames({ productionId, idRef, coloredName }) {
const { fullName, firstName, lastName } = NameFromScanr(productionId);
const { dataList, setDataList } = useDataList();
console.log(dataList);
const { setDataList } = useDataList();
const customStyles = {
option: (provided, state) => ({
...provided,
Expand All @@ -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 (
<Row>
Expand Down

0 comments on commit 94bdde6

Please sign in to comment.