From e336754226d8ae3cd6023ea6b64b50f40e6f317a Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 15 Feb 2024 15:51:04 +0300 Subject: [PATCH] refactoring-ratings --- public/locales/en/translation.json | 2 +- public/locales/ru/translation.json | 2 +- src/components/Article/Comment/Comment.tsx | 2 +- .../Article/CommentRatingControl.tsx | 118 ------------------ src/components/Article/FullArticle.tsx | 26 ++-- ...outRatingControl.tsx => RatingControl.tsx} | 80 +++++++++--- .../Feed/ArticleCard/ArticleCard.tsx | 2 +- 7 files changed, 83 insertions(+), 149 deletions(-) delete mode 100644 src/components/Article/CommentRatingControl.tsx rename src/components/Article/{ShoutRatingControl.tsx => RatingControl.tsx} (61%) diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 79653771..bef10ff9 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -380,7 +380,7 @@ "This email is already taken. If it's you": "This email is already taken. If it's you", "This functionality is currently not available, we would like to work on this issue. Use the download link.": "This functionality is currently not available, we would like to work on this issue. Use the download link.", "This month": "This month", - "This post has not been rated yet": "This post has not been rated yet", + "No one rated yet": "No one rated yet", "This way we ll realize that you re a real person and ll take your vote into account. And you ll see how others voted": "This way we ll realize that you re a real person and ll take your vote into account. And you ll see how others voted", "This way you ll be able to subscribe to authors, interesting topics and customize your feed": "This way you ll be able to subscribe to authors, interesting topics and customize your feed", "This week": "This week", diff --git a/public/locales/ru/translation.json b/public/locales/ru/translation.json index afb004de..a871580d 100644 --- a/public/locales/ru/translation.json +++ b/public/locales/ru/translation.json @@ -402,7 +402,7 @@ "This email is already taken. If it's you": "Такой email уже зарегистрирован. Если это вы", "This functionality is currently not available, we would like to work on this issue. Use the download link.": "В данный момент этот функционал не доступен, бы работаем над этой проблемой. Воспользуйтесь загрузкой по ссылке.", "This month": "За месяц", - "This post has not been rated yet": "Эту публикацию еще пока никто не оценил", + "No one rated yet": "Ещё никто не оценивал", "This way we ll realize that you re a real person and ll take your vote into account. And you ll see how others voted": "Так мы поймем, что вы реальный человек, и учтем ваш голос. А вы увидите, как проголосовали другие", "This way you ll be able to subscribe to authors, interesting topics and customize your feed": "Так вы сможете подписаться на авторов, интересные темы и настроить свою ленту", "This week": "За неделю", diff --git a/src/components/Article/Comment/Comment.tsx b/src/components/Article/Comment/Comment.tsx index d6af579f..ebbfd98c 100644 --- a/src/components/Article/Comment/Comment.tsx +++ b/src/components/Article/Comment/Comment.tsx @@ -14,7 +14,7 @@ import { Userpic } from '../../Author/Userpic' import { Icon } from '../../_shared/Icon' import { ShowIfAuthenticated } from '../../_shared/ShowIfAuthenticated' import { CommentDate } from '../CommentDate' -import { CommentRatingControl } from '../CommentRatingControl' +import { RatingControl as CommentRatingControl } from '../RatingControl' import styles from './Comment.module.scss' diff --git a/src/components/Article/CommentRatingControl.tsx b/src/components/Article/CommentRatingControl.tsx deleted file mode 100644 index 5ce60d08..00000000 --- a/src/components/Article/CommentRatingControl.tsx +++ /dev/null @@ -1,118 +0,0 @@ -import { clsx } from 'clsx' -import { createMemo } from 'solid-js' - -import { useLocalize } from '../../context/localize' -import { useReactions } from '../../context/reactions' -import { useSession } from '../../context/session' -import { useSnackbar } from '../../context/snackbar' -import { Reaction, ReactionKind } from '../../graphql/schema/core.gen' -import { loadShout } from '../../stores/zine/articles' -import { Popup } from '../_shared/Popup' -import { VotersList } from '../_shared/VotersList' - -import styles from './CommentRatingControl.module.scss' - -type Props = { - comment: Reaction -} - -export const CommentRatingControl = (props: Props) => { - const { t } = useLocalize() - const { author } = useSession() - const { showSnackbar } = useSnackbar() - const { reactionEntities, createReaction, deleteReaction, loadReactionsBy } = useReactions() - - const checkReaction = (reactionKind: ReactionKind) => - Object.values(reactionEntities).some( - (r) => - r.kind === reactionKind && - r.created_by.slug === author()?.slug && - r.shout.id === props.comment.shout.id && - r.reply_to === props.comment.id, - ) - const isUpvoted = createMemo(() => checkReaction(ReactionKind.Like)) - const isDownvoted = createMemo(() => checkReaction(ReactionKind.Dislike)) - const canVote = createMemo(() => author()?.slug !== props.comment.created_by.slug) - - const commentRatingReactions = createMemo(() => - Object.values(reactionEntities).filter( - (r) => - [ReactionKind.Like, ReactionKind.Dislike].includes(r.kind) && - r.shout.id === props.comment.shout.id && - r.reply_to === props.comment.id, - ), - ) - - const deleteCommentReaction = async (reactionKind: ReactionKind) => { - const reactionToDelete = Object.values(reactionEntities).find( - (r) => - r.kind === reactionKind && - r.created_by.slug === author()?.slug && - r.shout.id === props.comment.shout.id && - r.reply_to === props.comment.id, - ) - return deleteReaction(reactionToDelete.id) - } - - const handleRatingChange = async (isUpvote: boolean) => { - try { - if (isUpvoted()) { - await deleteCommentReaction(ReactionKind.Like) - } else if (isDownvoted()) { - await deleteCommentReaction(ReactionKind.Dislike) - } else { - await createReaction({ - kind: isUpvote ? ReactionKind.Like : ReactionKind.Dislike, - shout: props.comment.shout.id, - reply_to: props.comment.id, - }) - } - } catch { - showSnackbar({ type: 'error', body: t('Error') }) - } - - await loadShout(props.comment.shout.slug) - await loadReactionsBy({ - by: { shout: props.comment.shout.slug }, - }) - } - - return ( -
-
- } - variant="tiny" - > - - - - {total()}} variant="tiny"> + - ) diff --git a/src/components/Feed/ArticleCard/ArticleCard.tsx b/src/components/Feed/ArticleCard/ArticleCard.tsx index 31743db2..1743cbb4 100644 --- a/src/components/Feed/ArticleCard/ArticleCard.tsx +++ b/src/components/Feed/ArticleCard/ArticleCard.tsx @@ -10,8 +10,8 @@ import { router, useRouter } from '../../../stores/router' import { capitalize } from '../../../utils/capitalize' import { getDescription } from '../../../utils/meta' import { CoverImage } from '../../Article/CoverImage' +import { RatingControl as ShoutRatingControl } from '../../Article/RatingControl' import { SharePopup, getShareUrl } from '../../Article/SharePopup' -import { ShoutRatingControl } from '../../Article/ShoutRatingControl' import { AuthorLink } from '../../Author/AuthorLink' import { Icon } from '../../_shared/Icon' import { Image } from '../../_shared/Image'