-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
2 changed files
with
76 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* eslint-disable complexity */ | ||
import { makeDeepCopy } from "./helpers"; | ||
|
||
const reducer = (state, action) => { | ||
switch(action.type) { | ||
case "CREATE COMMENT": | ||
return { | ||
...state, | ||
comments: [ | ||
...state.comments, | ||
action.newComment | ||
] | ||
}; | ||
case "UPDATE COMMENT": | ||
for (let i = 0; i < state.comments.length; i++) { | ||
// find the comment in state | ||
if (state.comments[i].commentId === action.newComment.commentId) { | ||
let newComment = makeDeepCopy(state.comments[i]); | ||
newComment.htmlCommentText = action.newComment.htmlCommentText; // update comment text | ||
newComment.rawCommentText = action.newComment.rawCommentText; | ||
return { | ||
...state, | ||
comments: Object.assign( | ||
[], | ||
state.comments, | ||
{ [i]: newComment } | ||
) | ||
}; // keep the rest of state.comments, but replace comment at index i with newComment | ||
} | ||
} | ||
break; | ||
case "DELETE COMMENT": | ||
for (let i = 0; i < state.comments.length; i++) { | ||
// find the comment in state by ID | ||
if (state.comments[i].commentId === action.commentId) { | ||
return { | ||
...state, | ||
comments: state.comments.filter(comment => action.commentId !== comment.commentId) | ||
}; | ||
} | ||
} | ||
break; | ||
default: | ||
throw new Error(); // default should never be called | ||
} | ||
} | ||
|
||
export { | ||
reducer | ||
} |