Skip to content

Commit

Permalink
return to previous position in parent
Browse files Browse the repository at this point in the history
  • Loading branch information
wbglaeser committed Jun 25, 2024
1 parent e47aad8 commit f0f4660
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/screens/profile-section/ProfileSectionScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ const ProfileSectionScreen = () => {
setCompleted(false);
}, [id]);

console.log("we are back here: ", profileSectionData)

return (
<Layout>
{profileSectionData ? (
Expand Down
13 changes: 11 additions & 2 deletions src/screens/profile-section/components/ProfileSectionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ProfileSectionList = ({profileSectionData, mode, setCompleted}) => {
const [currentIndex, setCurrentIndex] = useState(0);
const [entityData, setEntityData] = useState({});
const retrieveCurrentEntityData = useProfileSectionStore((state) => state.retrieveCurrentEntityData);
const retrieveCurrentDatafield = useProfileSectionStore((state) => state.retrieveCurrentDatafield);
const {
handleConfirm,
handleBack,
Expand All @@ -17,8 +18,16 @@ const ProfileSectionList = ({profileSectionData, mode, setCompleted}) => {

useEffect(() => {
setEntityData(retrieveCurrentEntityData());
setCurrentIndex(0);
}, [profileSectionData, retrieveCurrentEntityData]);
const datafield = retrieveCurrentDatafield();
if (datafield) {
const index = profileSectionData.fields.findIndex((field) => field.datafield === datafield);
if (index !== -1) {
setCurrentIndex(index);
}
} else {
setCurrentIndex(0);
}
}, [profileSectionData, retrieveCurrentDatafield, retrieveCurrentEntityData]);

const currentField = profileSectionData.fields[currentIndex];
return (
Expand Down
12 changes: 12 additions & 0 deletions src/storage/useProfileSectionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export const useProfileSectionStore = create((set, get) => ({
console.log(`STATE UPDATE: We are retrieving the current profile section`);
return findDeepestProfileSection(data);
},
retrieveCurrentDatafield() {
const data = get().sectionStore;
console.log(`STATE UPDATE: We are retrieving the current datafield`);
return findDeepestDatafield(data);
},
updateDeepestDatafield: (newDatafield) =>
console.log('STATE UPDATE: We are updating the deepest datafield') ||
set((state) => ({
Expand Down Expand Up @@ -55,6 +60,13 @@ const findDeepestEntityData = (section) => {
return findDeepestEntityData(section.nestedSection);
};

const findDeepestDatafield = (section) => {
if (!section.nestedSection) {
return section.datafield;
}
return findDeepestDatafield(section.nestedSection);
};

const findDeepestProfileSection = (section) => {
if (!section.nestedSection) {
return section.profileSection;
Expand Down

0 comments on commit f0f4660

Please sign in to comment.