webapp/src/components/Author/AuthorRatingControl.tsx

63 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-11-28 13:18:25 +00:00
import type { Author } from '../../graphql/schema/core.gen'
import { clsx } from 'clsx'
2023-12-27 22:50:42 +00:00
import { Show, createSignal } from 'solid-js'
2024-06-24 17:50:27 +00:00
import { useGraphQL } from '~/context/graphql'
import rateAuthorMutation from '~/graphql/mutation/core/author-rate'
2023-12-27 23:04:12 +00:00
import styles from './AuthorRatingControl.module.scss'
interface AuthorRatingControlProps {
author: Author
class?: string
}
export const AuthorRatingControl = (props: AuthorRatingControlProps) => {
const isUpvoted = false
const isDownvoted = false
2023-05-01 18:32:32 +00:00
// eslint-disable-next-line unicorn/consistent-function-scoping
2023-12-27 22:50:42 +00:00
const handleRatingChange = async (isUpvote: boolean) => {
console.log('handleRatingChange', { isUpvote })
2023-12-27 22:57:24 +00:00
if (props.author?.slug) {
2023-12-27 23:04:12 +00:00
const value = isUpvote ? 1 : -1
2024-06-24 17:50:27 +00:00
const _resp = await mutation(rateAuthorMutation, {
rated_slug: props.author?.slug,
value,
}).toPromise()
setRating((r) => (r || 0) + value)
2023-12-27 22:57:24 +00:00
}
}
2024-06-24 17:50:27 +00:00
const { mutation } = useGraphQL()
2023-12-27 22:57:24 +00:00
const [rating, setRating] = createSignal(props.author?.stat?.rating)
return (
<div
class={clsx(styles.rating, props.class, {
[styles.isUpvoted]: isUpvoted,
[styles.isDownvoted]: isDownvoted,
})}
>
<button
class={clsx(styles.ratingControl, styles.downvoteButton)}
onClick={() => handleRatingChange(false)}
>
&minus;
</button>
{/*TODO*/}
2023-12-27 22:50:42 +00:00
<div>
<div class={styles.ratingValue}>{rating()}</div>
<Show when={props.author?.stat?.rating_shouts}>
<div class={styles.ratingValue}>{props.author?.stat?.rating_shouts}</div>
</Show>
<Show when={props.author?.stat?.rating_comments}>
<div class={styles.ratingValue}>{props.author?.stat?.rating_comments}</div>
</Show>
</div>
<button
class={clsx(styles.ratingControl, styles.upvoteButton)}
onClick={() => handleRatingChange(true)}
>
+
</button>
</div>
)
}