Plus upvote/downvote from constant

This commit is contained in:
Kalle
2022-06-12 16:19:43 +03:00
parent f5f589a28c
commit 010f709e4b
4 changed files with 11 additions and 12 deletions

View File

@@ -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;

View File

@@ -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 });

View File

@@ -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;
}

View File

@@ -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<z.infer<typeof voteSchema>, 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 });