Skip to content

Commit

Permalink
fix: bug when attempting to create 2 textcard sections before adding …
Browse files Browse the repository at this point in the history
…text cards

Lodash behaviour here is buggy - _.set sets the fields in multiple array items for section data, which was causing errors
  • Loading branch information
alexanderleegs committed Sep 27, 2023
1 parent bd74692 commit 34c3c8f
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/hooks/useDrag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,27 +146,32 @@ const updateTextCardsCardSection = (
sectionIndex: number,
newTextCards: unknown[],
newTextCardErrors: unknown[]
): EditorHomepageState => ({
...homepageState,
frontMatter: {
...homepageState.frontMatter,
sections: _.set(
// NOTE: Deep clone here to avoid mutation
_.cloneDeep(homepageState.frontMatter.sections),
[sectionIndex, "textcards", "cards"],
newTextCards
),
},
errors: {
...homepageState.errors,
textcards: _.set(
// NOTE: Deep clone here to avoid mutation
_.cloneDeep(homepageState.errors.textcards),
[sectionIndex],
newTextCardErrors
),
},
})
): EditorHomepageState => {
// Needs to be done separately - lodash's set seems to be buggy when handling arrays of objects
const modifiedSection = _.set(
_.cloneDeep(homepageState.frontMatter.sections[sectionIndex]),
["textcards", "cards"],
newTextCards
)
const newSections = _.cloneDeep(homepageState.frontMatter.sections)
newSections[sectionIndex] = modifiedSection
return {
...homepageState,
frontMatter: {
...homepageState.frontMatter,
sections: newSections,
},
errors: {
...homepageState.errors,
textcards: _.set(
// NOTE: Deep clone here to avoid mutation
_.cloneDeep(homepageState.errors.textcards),
[sectionIndex],
newTextCardErrors
),
},
}
}

type OnDragEndResponseWrapper = (
state: EditorHomepageState
Expand Down

0 comments on commit 34c3c8f

Please sign in to comment.