From 010f709e4baa483c1101dbdf1eb835044ba30c2e Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 12 Jun 2022 16:19:43 +0300 Subject: [PATCH] Plus upvote/downvote from constant --- app/constants.ts | 3 +++ app/modules/plus-server/usePlusVoting.ts | 3 ++- app/permissions.ts | 9 --------- app/routes/plus/voting/index.tsx | 8 ++++++-- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/constants.ts b/app/constants.ts index 11ee91fdc..99ad0b66a 100644 --- a/app/constants.ts +++ b/app/constants.ts @@ -6,3 +6,6 @@ export const PlUS_SUGGESTION_FIRST_COMMENT_MAX_LENGTH = 500; export const PlUS_SUGGESTION_COMMENT_MAX_LENGTH = TWEET_LENGTH_MAX_LENGTH; export const PLUS_TIERS = [1, 2, 3]; + +export const PLUS_UPVOTE = 1; +export const PLUS_DOWNVOTE = -1; diff --git a/app/modules/plus-server/usePlusVoting.ts b/app/modules/plus-server/usePlusVoting.ts index 95bc397fb..2dbdf8f82 100644 --- a/app/modules/plus-server/usePlusVoting.ts +++ b/app/modules/plus-server/usePlusVoting.ts @@ -1,5 +1,6 @@ import * as React from "react"; import invariant from "tiny-invariant"; +import { PLUS_DOWNVOTE, PLUS_UPVOTE } from "~/constants"; import type { UsersForVoting } from "~/db/models/plusVotes.server"; import type { PlusVoteFromFE } from "./types"; import { upcomingVoting } from "./voting-time"; @@ -25,7 +26,7 @@ export function usePlusVoting(usersForVotingFromServer: UsersForVoting) { const newVotes = [ ...votes, - { votedId, score: type === "upvote" ? 1 : -1 }, + { votedId, score: type === "upvote" ? PLUS_UPVOTE : PLUS_DOWNVOTE }, ]; votesToLocalStorage({ usersForVoting, votes: newVotes }); diff --git a/app/permissions.ts b/app/permissions.ts index 4af91ea03..fffc9ca6d 100644 --- a/app/permissions.ts +++ b/app/permissions.ts @@ -198,12 +198,3 @@ function hasUserSuggestedThisMonth({ suggestions[0] && suggestions[0].author.id === user?.id ); } - -export function canVoteFE() { - return allTruthy([isVotingActive(), !alreadyVoted()]); -} - -// xxx: implement alreadyVoted() -function alreadyVoted() { - return false; -} diff --git a/app/routes/plus/voting/index.tsx b/app/routes/plus/voting/index.tsx index be34c567d..e8d37d4e8 100644 --- a/app/routes/plus/voting/index.tsx +++ b/app/routes/plus/voting/index.tsx @@ -7,6 +7,7 @@ import { z } from "zod"; import { Avatar } from "~/components/Avatar"; import { Button } from "~/components/Button"; import { RelativeTime } from "~/components/RelativeTime"; +import { PLUS_DOWNVOTE, PLUS_UPVOTE } from "~/constants"; import { db } from "~/db"; import type { UsersForVoting } from "~/db/models/plusVotes.server"; import { getUser, requireUser } from "~/modules/auth"; @@ -25,7 +26,7 @@ import { PlusSuggestionComments } from "../suggestions"; const voteSchema = z.object({ votedId: z.number(), - score: z.number().refine((val) => [-1, 1].includes(val)), + score: z.number().refine((val) => [PLUS_DOWNVOTE, PLUS_UPVOTE].includes(val)), }); assertType, PlusVoteFromFE>(); @@ -48,7 +49,10 @@ export const action: ActionFunction = async ({ request }) => { validateVotes({ votes: data.votes, usersForVoting }); // freebie +1 for yourself if you vote - const votesForDb = [...data.votes].concat({ votedId: user.id, score: 1 }); + const votesForDb = [...data.votes].concat({ + votedId: user.id, + score: PLUS_UPVOTE, + }); const { month, year } = upcomingVoting(new Date()); const { endDate } = monthsVotingRange({ month, year });