webapp/src/components/Author/AuthorRatingControl.tsx

60 lines
1.8 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'
import styles from './AuthorRatingControl.module.scss'
2023-12-27 22:50:42 +00:00
import { Show, createSignal } from 'solid-js'
import { useLocalize } from '../../context/localize'
import { apiClient } from '../../graphql/client/core'
interface AuthorRatingControlProps {
author: Author
class?: string
}
export const AuthorRatingControl = (props: AuthorRatingControlProps) => {
const isUpvoted = false
const isDownvoted = false
2023-12-27 22:50:42 +00:00
const { t } = useLocalize()
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) {
await apiClient.rateAuthor({ rated_slug: props.author?.slug, value: isUpvote ? 1 : -1 })
}
}
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>
)
}