-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix(#266): displayed reaction tyoes with their counts
- Loading branch information
1 parent
728e1e3
commit c0fbf24
Showing
5 changed files
with
150 additions
and
102 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
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
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 |
---|---|---|
@@ -1,94 +1,97 @@ | ||
import { | ||
fetchReactions, | ||
addReaction, | ||
removeReaction, | ||
} from "../actiontypes/reactionTypes"; | ||
|
||
|
||
interface ReactionState { | ||
isReactionLoading: boolean; | ||
reactions: Record<string, number>; | ||
errors: null | string; | ||
} | ||
|
||
const initialState: ReactionState = { | ||
isReactionLoading: false, | ||
reactions: {}, | ||
errors: null, | ||
}; | ||
|
||
export default ( | ||
state = initialState, | ||
{ type, payload }: { type: string; payload: any } | ||
): ReactionState => { | ||
switch (type) { | ||
case fetchReactions.FETCH_REACTIONS_LOADING: | ||
case addReaction.ADD_REACTION_LOADING: | ||
case removeReaction.REMOVE_REACTION_LOADING: | ||
return { | ||
...state, | ||
isReactionLoading: true, | ||
}; | ||
|
||
case fetchReactions.FETCH_REACTIONS_SUCCESS: | ||
|
||
const transformedReactions = | ||
typeof payload === "number" | ||
? { TOTAL: payload } | ||
: Array.isArray(payload) | ||
? payload.reduce((acc: Record<string, number>, reaction: { type: string }) => { | ||
if (reaction.type) { | ||
acc[reaction.type] = (acc[reaction.type] || 0) + 1; | ||
} | ||
return acc; | ||
}, {}) | ||
: {}; | ||
|
||
fetchReactions, | ||
addReaction, | ||
removeReaction, | ||
} from "../actiontypes/reactionTypes"; | ||
|
||
interface ReactionState { | ||
isReactionLoading: boolean; | ||
reactions: Record<string, number>; | ||
errors: null | string; | ||
} | ||
|
||
const initialState: ReactionState = { | ||
isReactionLoading: false, | ||
reactions: {}, | ||
errors: null, | ||
}; | ||
|
||
export default ( | ||
state = initialState, | ||
{ type, payload }: { type: string; payload: any } | ||
): ReactionState => { | ||
switch (type) { | ||
case fetchReactions.FETCH_REACTIONS_LOADING: | ||
return { | ||
...state, | ||
isReactionLoading: true, | ||
errors: null, | ||
}; | ||
|
||
case fetchReactions.FETCH_REACTIONS_SUCCESS: | ||
return { | ||
...state, | ||
isReactionLoading: false, | ||
reactions: payload, | ||
errors: null, | ||
}; | ||
|
||
case fetchReactions.FETCH_REACTIONS_FAILURE: | ||
return { | ||
...state, | ||
isReactionLoading: false, | ||
errors: payload, | ||
}; | ||
|
||
case addReaction.ADD_REACTION_LOADING: | ||
case removeReaction.REMOVE_REACTION_LOADING: | ||
return { | ||
...state, | ||
isReactionLoading: true, | ||
errors: null, | ||
}; | ||
|
||
case addReaction.ADD_REACTION_SUCCESS: { | ||
const updatedReactions = { ...state.reactions }; | ||
const { type } = payload; | ||
|
||
if (type) { | ||
updatedReactions[type] = (updatedReactions[type] || 0) + 1; | ||
} | ||
|
||
return { | ||
...state, | ||
isReactionLoading: false, | ||
reactions: transformedReactions, | ||
reactions: updatedReactions, | ||
errors: null, | ||
}; | ||
|
||
case addReaction.ADD_REACTION_SUCCESS: | ||
const updatedReactions = (payload || []).reduce( | ||
(acc: Record<string, number>, reaction: { type: string; count: number }) => { | ||
acc[reaction.type] = reaction.count; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
return { | ||
...state, | ||
isReactionLoading: false, | ||
reactions: updatedReactions, | ||
}; | ||
|
||
case removeReaction.REMOVE_REACTION_SUCCESS: | ||
const reactionsAfterRemoval = (payload || []).reduce( | ||
(acc: Record<string, number>, reaction: { type: string; count: number }) => { | ||
acc[reaction.type] = reaction.count; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
return { | ||
...state, | ||
isReactionLoading: false, | ||
reactions: reactionsAfterRemoval, | ||
}; | ||
|
||
case fetchReactions.FETCH_REACTIONS_FAIL: | ||
case addReaction.ADD_REACTION_FAIL: | ||
case removeReaction.REMOVE_REACTION_FAIL: | ||
return { | ||
...state, | ||
isReactionLoading: false, | ||
errors: payload, | ||
}; | ||
|
||
default: | ||
return state; | ||
} | ||
|
||
case removeReaction.REMOVE_REACTION_SUCCESS: { | ||
const updatedReactions = { ...state.reactions }; | ||
const { type } = payload; | ||
|
||
if (type && updatedReactions[type] > 0) { | ||
updatedReactions[type] -= 1; | ||
} | ||
}; | ||
|
||
|
||
return { | ||
...state, | ||
isReactionLoading: false, | ||
reactions: updatedReactions, | ||
errors: null, | ||
}; | ||
} | ||
|
||
case addReaction.ADD_REACTION_FAIL: | ||
case removeReaction.REMOVE_REACTION_FAIL: | ||
return { | ||
...state, | ||
isReactionLoading: false, | ||
errors: payload, | ||
}; | ||
|
||
default: | ||
return state; | ||
} | ||
}; |