From 594244db2a271288a0a5f96ed4b155286f8ca548 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Pailharey?= <rpailharey@grandlyon.com>
Date: Tue, 30 May 2023 17:40:53 +0200
Subject: [PATCH] wip: update copy of the state instead of mutating it

---
 .../ProfileTypeFormDateSelection.tsx          |  3 +-
 .../ProfileTypeFormMultiChoice.tsx            |  6 ++--
 .../ProfileType/ProfileTypeFormNumber.tsx     |  3 +-
 .../ProfileTypeFormNumberSelection.tsx        |  3 +-
 .../ProfileTypeFormSingleChoice.tsx           |  3 +-
 .../ProfileType/ProfileTypeView.tsx           | 31 ++++++++++---------
 6 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/src/components/ProfileType/ProfileTypeFormDateSelection.tsx b/src/components/ProfileType/ProfileTypeFormDateSelection.tsx
index 025d29932..59186b342 100644
--- a/src/components/ProfileType/ProfileTypeFormDateSelection.tsx
+++ b/src/components/ProfileType/ProfileTypeFormDateSelection.tsx
@@ -141,8 +141,7 @@ const ProfileTypeFormDateSelection: React.FC<
   }, [profileType, setPreviousStep])
 
   const handleNext = useCallback(() => {
-    profileType[answerType.attribute] = answer
-    setNextStep(profileType)
+    setNextStep({ ...profileType, [answerType.attribute]: answer })
   }, [profileType, setNextStep, answer, answerType.attribute])
 
   function handleSelectMonth(event: any) {
diff --git a/src/components/ProfileType/ProfileTypeFormMultiChoice.tsx b/src/components/ProfileType/ProfileTypeFormMultiChoice.tsx
index f1210185c..46cec7784 100644
--- a/src/components/ProfileType/ProfileTypeFormMultiChoice.tsx
+++ b/src/components/ProfileType/ProfileTypeFormMultiChoice.tsx
@@ -67,8 +67,10 @@ const ProfileTypeFormMultiChoice: React.FC<ProfileTypeFormMultiChoiceProps> = ({
   }, [profileType, setPreviousStep])
 
   const handleNext = useCallback(() => {
-    profileType[answerType.attribute] = answer as IndividualInsulationWork[]
-    setNextStep(profileType)
+    setNextStep({
+      ...profileType,
+      [answerType.attribute]: answer as IndividualInsulationWork[],
+    })
   }, [profileType, setNextStep, answer, answerType.attribute])
 
   useEffect(() => {
diff --git a/src/components/ProfileType/ProfileTypeFormNumber.tsx b/src/components/ProfileType/ProfileTypeFormNumber.tsx
index 66e36f2bb..c46ac1924 100644
--- a/src/components/ProfileType/ProfileTypeFormNumber.tsx
+++ b/src/components/ProfileType/ProfileTypeFormNumber.tsx
@@ -37,8 +37,7 @@ const ProfileTypeFormNumber: React.FC<ProfileTypeFormNumberProps> = ({
   }, [profileType, setPreviousStep])
 
   const handleNext = useCallback(() => {
-    profileType[answerType.attribute] = answer
-    setNextStep(profileType)
+    setNextStep({ ...profileType, [answerType.attribute]: answer })
   }, [profileType, setNextStep, answer, answerType.attribute])
 
   useEffect(() => {
diff --git a/src/components/ProfileType/ProfileTypeFormNumberSelection.tsx b/src/components/ProfileType/ProfileTypeFormNumberSelection.tsx
index 944a82388..d40309fa1 100644
--- a/src/components/ProfileType/ProfileTypeFormNumberSelection.tsx
+++ b/src/components/ProfileType/ProfileTypeFormNumberSelection.tsx
@@ -51,8 +51,7 @@ const ProfileTypeFormNumberSelection: React.FC<
   }, [profileType, setPreviousStep])
 
   const handleNext = useCallback(() => {
-    profileType[answerType.attribute] = answer
-    setNextStep(profileType)
+    setNextStep({ ...profileType, [answerType.attribute]: answer })
   }, [profileType, setNextStep, answer, answerType.attribute])
 
   useEffect(() => {
diff --git a/src/components/ProfileType/ProfileTypeFormSingleChoice.tsx b/src/components/ProfileType/ProfileTypeFormSingleChoice.tsx
index a4fc88272..f0fd6681c 100644
--- a/src/components/ProfileType/ProfileTypeFormSingleChoice.tsx
+++ b/src/components/ProfileType/ProfileTypeFormSingleChoice.tsx
@@ -40,8 +40,7 @@ const ProfileTypeFormSingleChoice: React.FC<
   }, [profileType, setPreviousStep])
 
   const handleNext = useCallback(() => {
-    profileType[answerType.attribute] = answer
-    setNextStep(profileType)
+    setNextStep({ ...profileType, [answerType.attribute]: answer })
   }, [profileType, setNextStep, answer, answerType.attribute])
 
   useEffect(() => {
diff --git a/src/components/ProfileType/ProfileTypeView.tsx b/src/components/ProfileType/ProfileTypeView.tsx
index 5fb0f3b2f..b138a7147 100644
--- a/src/components/ProfileType/ProfileTypeView.tsx
+++ b/src/components/ProfileType/ProfileTypeView.tsx
@@ -86,6 +86,7 @@ const ProfileTypeView: React.FC = () => {
       if (_profileType) {
         setProfileType(_profileType)
         profileTypeFormService = new ProfileTypeFormService(_profileType)
+        console.log(profileTypeFormService)
         curProfileEcogesture.heating = _profileType.heating
         curProfileEcogesture.hotWater = _profileType.hotWater
         curProfileEcogesture.warmingFluid = _profileType.warmingFluid
@@ -115,17 +116,14 @@ const ProfileTypeView: React.FC = () => {
     ]
   )
 
-  const setPreviousStep = useCallback(
-    (_profileType: ProfileType) => {
-      setProfileType(_profileType)
-      const profileTypeFormService = new ProfileTypeFormService(_profileType)
-      const previousStep: ProfileTypeStepForm =
-        profileTypeFormService.getPreviousFormStep(step)
-      setIsLoading(true)
-      setStep(previousStep)
-    },
-    [step]
-  )
+  const setPreviousStep = useCallback(() => {
+    console.log(profileType)
+    const profileTypeFormService = new ProfileTypeFormService(profileType)
+    const previousStep: ProfileTypeStepForm =
+      profileTypeFormService.getPreviousFormStep(step)
+    setIsLoading(true)
+    setStep(previousStep)
+  }, [profileType, step])
 
   const selectForm = () => {
     if (answerType.type === ProfileTypeFormType.SINGLE_CHOICE) {
@@ -201,14 +199,17 @@ const ProfileTypeView: React.FC = () => {
   }
 
   useEffect(() => {
-    if (profile.isProfileTypeCompleted) {
-      setProfileType(curProfileType)
-    }
     const _answerType: ProfileTypeAnswer =
       ProfileTypeFormService.getAnswerForStep(step)
     setAnswerType(_answerType)
     setIsLoading(false)
-  }, [step, profile, curProfileType])
+  }, [step])
+
+  useEffect(() => {
+    if (profile.isProfileTypeCompleted) {
+      setProfileType({ ...curProfileType })
+    }
+  }, [curProfileType, profile])
 
   return (
     <>
-- 
GitLab