revert-upvote
This commit is contained in:
parent
8f6de58f6d
commit
83cb23d424
|
@ -26,43 +26,53 @@ export const ShoutRatingControl = (props: ShoutRatingControlProps) => {
|
||||||
|
|
||||||
const {
|
const {
|
||||||
reactionEntities,
|
reactionEntities,
|
||||||
actions: { createReaction, loadReactionsBy },
|
actions: { createReaction, deleteReaction, loadReactionsBy },
|
||||||
} = useReactions()
|
} = useReactions()
|
||||||
|
|
||||||
|
const [isLoading, setIsLoading] = createSignal(false)
|
||||||
|
|
||||||
const checkReaction = (reactionKind: ReactionKind) =>
|
const checkReaction = (reactionKind: ReactionKind) =>
|
||||||
Object.values(reactionEntities).some(
|
Object.values(reactionEntities).some(
|
||||||
(r) =>
|
(r) =>
|
||||||
r.kind === reactionKind &&
|
r.kind === reactionKind &&
|
||||||
r.created_by.slug === author()?.slug &&
|
r.created_by.id === author()?.id &&
|
||||||
r.shout.id === props.shout.id &&
|
r.shout.id === props.shout.id &&
|
||||||
!r.reply_to,
|
!r.reply_to,
|
||||||
)
|
)
|
||||||
|
|
||||||
const isUpvoted = createMemo(() => checkReaction(ReactionKind.Like))
|
const isUpvoted = createMemo(() => checkReaction(ReactionKind.Like))
|
||||||
|
|
||||||
const isDownvoted = createMemo(() => checkReaction(ReactionKind.Dislike))
|
const isDownvoted = createMemo(() => checkReaction(ReactionKind.Dislike))
|
||||||
|
|
||||||
const shoutRatingReactions = createMemo(() =>
|
const shoutRatingReactions = createMemo(() =>
|
||||||
Object.values(reactionEntities).filter(
|
Object.values(reactionEntities).filter(
|
||||||
(r) =>
|
(r) => ['LIKE', 'DISLIKE'].includes(r.kind) && r.shout.id === props.shout.id && !r.reply_to,
|
||||||
[ReactionKind.Like, ReactionKind.Dislike].includes(r.kind) &&
|
|
||||||
r.shout.id === props.shout.id &&
|
|
||||||
!r.reply_to,
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
const [isLoading, setIsLoading] = createSignal(false)
|
|
||||||
|
const deleteShoutReaction = async (reactionKind: ReactionKind) => {
|
||||||
|
const reactionToDelete = Object.values(reactionEntities).find(
|
||||||
|
(r) =>
|
||||||
|
r.kind === reactionKind &&
|
||||||
|
r.created_by.id === author()?.id &&
|
||||||
|
r.shout.id === props.shout.id &&
|
||||||
|
!r.reply_to,
|
||||||
|
)
|
||||||
|
return deleteReaction(reactionToDelete.id)
|
||||||
|
}
|
||||||
|
|
||||||
const handleRatingChange = async (isUpvote: boolean) => {
|
const handleRatingChange = async (isUpvote: boolean) => {
|
||||||
setIsLoading(true)
|
|
||||||
requireAuthentication(async () => {
|
requireAuthentication(async () => {
|
||||||
try {
|
if (isUpvoted()) {
|
||||||
|
await deleteShoutReaction(ReactionKind.Like)
|
||||||
|
} else if (isDownvoted()) {
|
||||||
|
await deleteShoutReaction(ReactionKind.Dislike)
|
||||||
|
} else {
|
||||||
await createReaction({
|
await createReaction({
|
||||||
kind: isUpvote ? ReactionKind.Like : ReactionKind.Dislike,
|
kind: isUpvote ? ReactionKind.Like : ReactionKind.Dislike,
|
||||||
shout: props.shout.id,
|
shout: props.shout.id,
|
||||||
})
|
})
|
||||||
} catch (error) {
|
|
||||||
console.warn(error)
|
|
||||||
}
|
}
|
||||||
setIsLoading(false)
|
|
||||||
loadShout(props.shout.slug)
|
loadShout(props.shout.slug)
|
||||||
loadReactionsBy({
|
loadReactionsBy({
|
||||||
by: { shout: props.shout.slug },
|
by: { shout: props.shout.slug },
|
||||||
|
|
|
@ -78,10 +78,10 @@ export const ReactionsProvider = (props: { children: JSX.Element }) => {
|
||||||
setReactionEntities(changes)
|
setReactionEntities(changes)
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteReaction = async (id: number): Promise<void> => {
|
const deleteReaction = async (reaction_id: number): Promise<void> => {
|
||||||
const reaction = await apiClient.destroyReaction(id)
|
const _reaction = await apiClient.destroyReaction(reaction_id)
|
||||||
setReactionEntities({
|
setReactionEntities({
|
||||||
[reaction.id]: undefined,
|
[reaction_id]: undefined,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user