Skip to content

Commit 3020e58

Browse files
committed
Fix empty parent removal in travel plan example
1 parent 44b0b5f commit 3020e58

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

‎src/content/learn/choosing-the-state-structure.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,21 @@ export default function TravelPlan() {
11511151
// ...so that it has the updated parent.
11521152
[parentId]: nextParent
11531153
});
1154+
// If the parent has no children, remove it from its parent.
1155+
if(nextParent.childIds.length === 0 && parentId !== 0){
1156+
const grandParent = findParent(parentId);
1157+
handleComplete(grandParent, parentId);
1158+
}
1159+
1160+
// Find the parent of a place.
1161+
function findParent(Id){
1162+
for(const parentId in plan){
1163+
if(plan[parentId].childIds.includes(Id)){
1164+
return Number(parentId);
1165+
}
1166+
}
1167+
return null;
1168+
}
11541169
}
11551170
11561171
const root = plan[0];

0 commit comments

Comments
 (0)