From 6f51867b87178fd2998630fc0548ab9992155bb2 Mon Sep 17 00:00:00 2001 From: Oussama El Benney Date: Wed, 10 Jul 2024 14:19:38 +0100 Subject: [PATCH] fixed reinitialisation in planning interface --- .../(dashboard)/planning/PlanningTable.jsx | 33 +++++++++++-------- src/app/lib/fetchRequest.js | 28 +++++++++++----- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/app/(dashboard)/planning/PlanningTable.jsx b/src/app/(dashboard)/planning/PlanningTable.jsx index 2a95805..7b2fea1 100644 --- a/src/app/(dashboard)/planning/PlanningTable.jsx +++ b/src/app/(dashboard)/planning/PlanningTable.jsx @@ -13,9 +13,8 @@ const PlanningTable = ({ project, typePresences}) => { const [loading, setLoading] = useState(false); const {toggleNotification} = useNotification() const [selectedPresence, setSelectedPresence] = useState(''); - const [planningData, setPlanningData] = useState(project.planning ? project.planning : blankData); + const [planningData, setPlanningData] = useState(project.planning ? structuredClone(project.planning) : blankData); const [isModalOpen, setModalOpen] = useState(false); - const handleSave = async () => { // verify if planing data has an empty value const emptyValue = planningData.planning_data.some(week => week.days.some(day => day.nom === '')); @@ -111,25 +110,31 @@ const PlanningTable = ({ project, typePresences}) => { // get the presence type by id (value is a string convert it to int const typePresence = typePresences.find(typePresence => typePresence.id === parseInt(value)); - // the value I want to add should be {"id": 1, "nom": "Travail à Temps Partiel"} and not just the id - // updatedData.planning_data[weekIndex].days[dayIndex] = typePresence; - // setPlanningData(updatedData); - setPlanningData((prevData) => { // prevData is the previous state of planningData - const updatedData = {...prevData}; - updatedData.planning_data[weekIndex].days[dayIndex] = typePresence; - return updatedData; - }); + // Shallow Copy not working cause i need to get previous data + // const updatedData = {...planningData}; + // Deep Copy working + const updatedData = structuredClone(planningData); + updatedData.planning_data[weekIndex].days[dayIndex] = typePresence; + setPlanningData(updatedData); }; const handleRadioChange = (e) => { const newPresence = e.target.value; setSelectedPresence(newPresence); - console.log('sata',planningData); - planningData?.planning_data.forEach((row, rowIndex) => { + + // Use a deep copy to update the entire planning data + const updatedData = structuredClone(planningData); + + updatedData?.planning_data.forEach((row, rowIndex) => { row.days.forEach((day, dayIndex) => { - handleTypePresenceChange(rowIndex, dayIndex, newPresence); + // Update the presence type directly + const typePresence = typePresences.find(typePresence => typePresence.id === parseInt(newPresence)); + updatedData.planning_data[rowIndex].days[dayIndex] = typePresence; }); }); + + // Set the updated planning data + setPlanningData(updatedData); }; return ( @@ -190,6 +195,8 @@ const PlanningTable = ({ project, typePresences}) => { onClick={() => { // set data to the previous state // setPlanningData(project.planning ? project.planning : blankData); + setPlanningData(project.planning ? structuredClone(project.planning) : blankData); + }} > diff --git a/src/app/lib/fetchRequest.js b/src/app/lib/fetchRequest.js index 6ad19f1..28b498c 100644 --- a/src/app/lib/fetchRequest.js +++ b/src/app/lib/fetchRequest.js @@ -1,32 +1,44 @@ import Cookies from 'js-cookie'; import { decrypt } from "@/app/lib/session"; -const BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000" +const BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000"; + const fetchRequest = async (url, options = {}) => { const jwtCookie = Cookies.get('session'); - console.log('jwtCookie', jwtCookie) + console.log('jwtCookie', jwtCookie); const jwtDecrypted = await decrypt(jwtCookie); - console.log('jwtDecrypted', jwtDecrypted?.sessionData?.token) + console.log('jwtDecrypted', jwtDecrypted?.sessionData?.token); + const headers = { ...options.headers, - // add authorization header with token if there is jwtDecrypted.sessionData.token + // Add authorization header with token if there is jwtDecrypted.sessionData.token Authorization: jwtDecrypted?.sessionData?.token ? `Token ${jwtDecrypted.sessionData.token}` : undefined, }; + // Check if the body is FormData + const isFormData = options.body instanceof FormData; + + // If the body is not FormData, set Content-Type to application/json + if (!isFormData) { + headers['Content-Type'] = 'application/json'; + } + const response = await fetch(`${BASE_URL}${url}`, { ...options, headers, }); - console.log(response.status) + + console.log(response.status); if (!response.ok) { try { const errorData = await response.json(); - return { isSuccess: false, errors: errorData, data: null, status: response.status } + return { isSuccess: false, errors: errorData, data: null, status: response.status }; } catch (error) { - return { isSuccess: false, errors: error, data: null, status: response.status } + return { isSuccess: false, errors: error, data: null, status: response.status }; } } - console.log('response', response) + + console.log('response', response); let data = null; // Check if the response has content before parsing it as JSON const contentType = response.headers.get('content-type'); -- GitLab