Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/staging'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihoub2 committed Nov 6, 2024
2 parents fd5b5f5 + 06d0198 commit d1b24f2
Showing 1 changed file with 94 additions and 94 deletions.
188 changes: 94 additions & 94 deletions client/src/components/items/staff-action.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Col, Text, Toggle } from "@dataesr/dsfr-plus";
import { Col, Text } from "@dataesr/dsfr-plus";
import EmailSender from "../../api/send-mail";
import type { Contribution } from "../../types";
import { useLocation } from "react-router-dom";
import { useMutation, useQueryClient } from "@tanstack/react-query";
// import { useMutation, useQueryClient } from "@tanstack/react-query";
import "./styles.scss";
import { useState, useEffect } from "react";
// import { useState, useEffect } from "react";

const StaffActions = ({
data,
refetch,
url,
}: {
}: // url,
{
data: Contribution;
refetch: () => void;
url: string;
Expand All @@ -32,93 +32,93 @@ const StaffActions = ({
? "staffSide"
: "staffSideContact";

const queryClient = useQueryClient();

const [isAllRead, setIsAllRead] = useState(() => {
return (
data?.threads?.every((thread) =>
thread.responses?.every((response) => response.read)
) || false
);
});

const mutation = useMutation(
async (isRead: boolean) => {
return fetch(`api/${baseUrl}/${data.id}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
threads: data.threads.map((thread) => ({
...thread,
responses: thread.responses.map((response) => ({
...response,
read: isRead,
})),
})),
}),
});
},
{
onMutate: async (isRead: boolean) => {
await queryClient.cancelQueries([url]);

const previousData = queryClient.getQueryData<{ data: Contribution[] }>(
[url]
) || { data: [] };

if (Array.isArray(previousData.data)) {
const updatedThreads = previousData.data.map((thread) => ({
...thread,
responses: Array.isArray(thread.responses)
? thread.responses.map((response) => ({
...response,
read: isRead,
}))
: [],
}));

queryClient.setQueryData([url], {
...previousData,
data: updatedThreads,
});
} else {
console.error("previousData.data n'est pas un tableau", previousData);
}

return { previousData };
},

onSuccess: () => {
queryClient.invalidateQueries([url]);
const updatedData = queryClient.getQueryData<{ data: Contribution[] }>([
url,
]);

if (updatedData?.data) {
const allRead = updatedData.data.every((thread) =>
thread.responses.every((response) => response.read)
);
setIsAllRead(allRead);
}
},
}
);

const handleToggleChange = () => {
const newState = !isAllRead;
setIsAllRead(newState);
mutation.mutate(newState);
};

useEffect(() => {
const allRead =
data?.threads?.every((thread) =>
thread.responses?.every((response) => response.read)
) || false;
setIsAllRead(allRead);
}, [data]);
// const queryClient = useQueryClient();

// const [isAllRead, setIsAllRead] = useState(() => {
// return (
// data?.threads?.every((thread) =>
// thread.responses?.every((response) => response.read)
// ) || false
// );
// });

// const mutation = useMutation(
// async (isRead: boolean) => {
// return fetch(`api/${baseUrl}/${data.id}`, {
// method: "PATCH",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({
// threads: data.threads.map((thread) => ({
// ...thread,
// responses: thread.responses.map((response) => ({
// ...response,
// read: isRead,
// })),
// })),
// }),
// });
// },
// {
// onMutate: async (isRead: boolean) => {
// await queryClient.cancelQueries([url]);

// const previousData = queryClient.getQueryData<{ data: Contribution[] }>(
// [url]
// ) || { data: [] };

// if (Array.isArray(previousData.data)) {
// const updatedThreads = previousData.data.map((thread) => ({
// ...thread,
// responses: Array.isArray(thread.responses)
// ? thread.responses.map((response) => ({
// ...response,
// read: isRead,
// }))
// : [],
// }));

// queryClient.setQueryData([url], {
// ...previousData,
// data: updatedThreads,
// });
// } else {
// console.error("previousData.data n'est pas un tableau", previousData);
// }

// return { previousData };
// },

// onSuccess: () => {
// queryClient.invalidateQueries([url]);
// const updatedData = queryClient.getQueryData<{ data: Contribution[] }>([
// url,
// ]);

// if (updatedData?.data) {
// const allRead = updatedData.data.every((thread) =>
// thread.responses.every((response) => response.read)
// );
// setIsAllRead(allRead);
// }
// },
// }
// );

// const handleToggleChange = () => {
// const newState = !isAllRead;
// setIsAllRead(newState);
// mutation.mutate(newState);
// };

// useEffect(() => {
// const allRead =
// data?.threads?.every((thread) =>
// thread.responses?.every((response) => response.read)
// ) || false;
// setIsAllRead(allRead);
// }, [data]);

const cleanResponseMessage = (message: string) => {
return message
Expand All @@ -139,13 +139,13 @@ const StaffActions = ({
<>
{data?.threads?.length > 0 && (
<Col className={contributorClassName}>
{data.threads.length > 2 && (
{/* {data.threads.length > 2 && (
<Toggle
label="Marquer toutes les réponses comme lues"
checked={isAllRead}
onChange={handleToggleChange}
/>
)}
)} */}

{data.threads.map((thread, threadIndex) =>
thread.responses.map((response, index) => {
Expand Down

0 comments on commit d1b24f2

Please sign in to comment.