From a4bd65d6fedae88ac1a9ffea9d16c34eb110352a Mon Sep 17 00:00:00 2001 From: wbglaeser Date: Fri, 7 Jun 2024 15:42:29 +0100 Subject: [PATCH] manage sections state --- .../OnboardingSectionsScreen.js | 12 +++++++++-- .../components/OnboardingSectionsItem.js | 8 ++++---- .../components/OnboardingSectionsList.js | 20 ++++++++++++++----- .../hooks/useInitializeProfileSections.js | 9 ++++----- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/screens/onboarding-sections/OnboardingSectionsScreen.js b/src/screens/onboarding-sections/OnboardingSectionsScreen.js index f113d69d..7a7f92d8 100644 --- a/src/screens/onboarding-sections/OnboardingSectionsScreen.js +++ b/src/screens/onboarding-sections/OnboardingSectionsScreen.js @@ -7,7 +7,8 @@ import useInitializeProfileSections from "./hooks/useInitializeProfileSections"; const OnboardingSectionsScreen = () => { const [onboardingSectionsData, setOnboardingSectionsData] = useState(); - useInitializeProfileSections(onboardingSectionsData); + const [sectionStatusInitialized, setSectionStatusInitialized] = useState(false); + const initializeProfileSections = useInitializeProfileSections(onboardingSectionsData); useEffect(() => { const fetchData = async () => { @@ -23,10 +24,17 @@ const OnboardingSectionsScreen = () => { fetchData(); }, []); + useEffect(() => { + if (onboardingSectionsData && !sectionStatusInitialized) { + initializeProfileSections(onboardingSectionsData); + setSectionStatusInitialized(true); + } + }, [initializeProfileSections, onboardingSectionsData, sectionStatusInitialized]); + return ( - {onboardingSectionsData ? ( + {onboardingSectionsData && sectionStatusInitialized? ( ) : null diff --git a/src/screens/onboarding-sections/components/OnboardingSectionsItem.js b/src/screens/onboarding-sections/components/OnboardingSectionsItem.js index a131819f..4d534622 100644 --- a/src/screens/onboarding-sections/components/OnboardingSectionsItem.js +++ b/src/screens/onboarding-sections/components/OnboardingSectionsItem.js @@ -2,13 +2,13 @@ import React from "react"; import { CardContent, Typography, Card, ButtonBase } from "@mui/material"; import ArrowForwardIosOutlinedIcon from '@mui/icons-material/ArrowForwardIosOutlined'; import SentimentSatisfiedOutlinedIcon from '@mui/icons-material/SentimentSatisfiedOutlined'; -import { yellow } from '@mui/material/colors'; +import { yellow, green } from '@mui/material/colors'; import { Link } from "react-router-dom"; import VStack from "../../../components/VStack"; import HStack from "../../../components/HStack"; -const OnboardingSectionsItem = ({section, entityData, active, first}) => { - const backgroundColor = active ? yellow[600] : 'rgba(0, 0, 0, 0.1)' +const OnboardingSectionsItem = ({section, entityData, active, completed, first}) => { + const backgroundColor = active ? yellow[600] : (completed ? green[500] : 'rgba(0, 0, 0, 0.1)') return ( @@ -22,7 +22,7 @@ const OnboardingSectionsItem = ({section, entityData, active, first}) => { {section.title} { - active ? ( + active || completed ? ( { const activeSection = useProfileInputSectionStore( (state) => state.activeSection ); + const sections = useProfileInputSectionStore((state) => state.sections); + //const allCompleted = sections.every((obj) => obj.completed); - console.log('entity data', entityData); + console.log('sections:', sections); + console.log('activeSection:', activeSection); return ( @@ -21,10 +24,17 @@ const OnboardingSectionsList = ({onboardingSectionsData}) => { Profilbereiche - {onboardingSectionsData.sections.map((section, index) => ( - - ))} + {onboardingSectionsData.sections.map((section, index) => { + const sectionStatus = sections.find((obj) => obj.id === section.id); + return ( + + ) + })} ); } diff --git a/src/screens/onboarding-sections/hooks/useInitializeProfileSections.js b/src/screens/onboarding-sections/hooks/useInitializeProfileSections.js index 4f59365b..caba34cf 100644 --- a/src/screens/onboarding-sections/hooks/useInitializeProfileSections.js +++ b/src/screens/onboarding-sections/hooks/useInitializeProfileSections.js @@ -1,20 +1,19 @@ -import { useEffect } from 'react'; import { useProfileInputSectionStore } from '../../../storage/zustand'; -const useInitializeProfileSections = (profileSections) => { - useEffect(() => { +const useInitializeProfileSections = () => { + return (profileSections) => { if (!profileSections || profileSections.length === 0) return; profileSections.sections.forEach((currentItem, i) => { const newDictEntry = { id: currentItem.id, - nextId: profileSections[i + 1]?.id, + nextId: profileSections.sections[i + 1]?.id, }; useProfileInputSectionStore .getState() .initialiseSection(newDictEntry.id, newDictEntry.nextId, i === 0); }); - }, [profileSections]); + }; }; export default useInitializeProfileSections;