webapp/src/components/Article/ShoutRatingControl.tsx

96 lines
2.9 KiB
TypeScript
Raw Normal View History

import { clsx } from 'clsx'
2024-01-22 23:16:22 +00:00
import { createMemo, createSignal, Show } from 'solid-js'
import { useLocalize } from '../../context/localize'
import { useReactions } from '../../context/reactions'
import { useSession } from '../../context/session'
2023-11-28 13:18:25 +00:00
import { ReactionKind, Shout } from '../../graphql/schema/core.gen'
import { loadShout } from '../../stores/zine/articles'
import { Icon } from '../_shared/Icon'
import { Popup } from '../_shared/Popup'
2023-03-09 23:56:19 +00:00
import { VotersList } from '../_shared/VotersList'
import styles from './ShoutRatingControl.module.scss'
interface ShoutRatingControlProps {
shout: Shout
class?: string
}
export const ShoutRatingControl = (props: ShoutRatingControlProps) => {
2023-03-09 23:39:07 +00:00
const { t } = useLocalize()
const {
2023-11-30 08:07:31 +00:00
author,
actions: { requireAuthentication },
} = useSession()
const {
reactionEntities,
2024-01-22 22:12:25 +00:00
actions: { createReaction, loadReactionsBy },
} = useReactions()
const checkReaction = (reactionKind: ReactionKind) =>
Object.values(reactionEntities).some(
(r) =>
r.kind === reactionKind &&
2023-11-30 08:07:31 +00:00
r.created_by.slug === author()?.slug &&
r.shout.id === props.shout.id &&
2023-11-28 13:18:25 +00:00
!r.reply_to,
)
const isUpvoted = createMemo(() => checkReaction(ReactionKind.Like))
const isDownvoted = createMemo(() => checkReaction(ReactionKind.Dislike))
const shoutRatingReactions = createMemo(() =>
Object.values(reactionEntities).filter(
2023-03-08 16:35:13 +00:00
(r) =>
[ReactionKind.Like, ReactionKind.Dislike].includes(r.kind) &&
r.shout.id === props.shout.id &&
2023-11-28 13:18:25 +00:00
!r.reply_to,
),
)
2024-01-22 23:16:22 +00:00
const [isLoading, setIsLoading] = createSignal(false)
const handleRatingChange = async (isUpvote: boolean) => {
2024-01-22 23:16:22 +00:00
setIsLoading(true)
requireAuthentication(async () => {
2024-01-23 00:25:00 +00:00
try {
await createReaction({
kind: isUpvote ? ReactionKind.Like : ReactionKind.Dislike,
shout: props.shout.id,
})
} catch (error) {
console.warn(error)
}
2024-01-22 23:16:22 +00:00
setIsLoading(false)
loadShout(props.shout.slug)
loadReactionsBy({
by: { shout: props.shout.slug },
})
}, 'vote')
}
return (
2023-07-09 18:34:59 +00:00
<div class={clsx(styles.rating, props.class)}>
2024-01-22 23:16:22 +00:00
<button onClick={() => handleRatingChange(false)} disabled={isLoading()}>
2024-01-23 02:18:17 +00:00
<Show when={!isDownvoted()} fallback={<Icon name="rating-control-checked" />}>
2023-07-09 18:34:59 +00:00
<Icon name="rating-control-less" />
</Show>
</button>
<Popup trigger={<span class={styles.ratingValue}>{props.shout.stat.rating}</span>} variant="tiny">
2023-03-09 23:39:07 +00:00
<VotersList
reactions={shoutRatingReactions()}
fallbackMessage={t('This post has not been rated yet')}
/>
</Popup>
2024-01-22 23:16:22 +00:00
<button onClick={() => handleRatingChange(true)} disabled={isLoading()}>
2024-01-23 02:18:17 +00:00
<Show when={!isUpvoted()} fallback={<Icon name="rating-control-checked" />}>
2023-07-09 18:34:59 +00:00
<Icon name="rating-control-more" />
</Show>
</button>
</div>
)
}