Skip to content

Commit

Permalink
rewrite deleteThoughtAtFirstMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
snqb committed Dec 29, 2024
1 parent 9fa99ad commit ce82e0a
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/test-helpers/deleteThoughtAtFirstMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import rootedParentOf from '../selectors/rootedParentOf'
/**
* Get thought and context for the given unranked path.
*/
const getThoughtAndParentPath = (state: State, at: string[]): [Thought | undefined, Path] => {
const getThoughtAndParentPath = (state: State, at: string[]): [Thought, Path] => {
const path = contextToPath(state, at)

if (!path) throw new Error(`Ranked thoughts not found for context: ${at}`)

const thought = pathToThought(state, path)

if (!thought) throw new Error(`Thought not found for path: ${path}`)

const pathParent = rootedParentOf(state, path)

return [thought, pathParent]
Expand All @@ -28,10 +30,7 @@ const getThoughtAndParentPath = (state: State, at: string[]): [Thought | undefin
*/
const deleteThoughtAtFirstMatch = _.curryRight((state: State, at: string[]) => {
const [thought, pathParent] = getThoughtAndParentPath(state, at)
if (!thought) {
console.warn(`Aborting deleteThoughtAtFirstMatch for ${at} because thought not found`)
return state
}

return deleteThought(state, {
pathParent,
thoughtId: thought.id,
Expand All @@ -45,10 +44,7 @@ export const deleteThoughtAtFirstMatchActionCreator =
(at: Context): Thunk =>
(dispatch, getState) => {
const [thought, pathParent] = getThoughtAndParentPath(getState(), at)
if (!thought) {
console.warn(`Aborting deleteThoughtAtFirstMatch for ${at} because thought not found`)
return
}

dispatch(
deleteThoughtActionCreator({
pathParent,
Expand Down

0 comments on commit ce82e0a

Please sign in to comment.