Skip to content

Commit

Permalink
feat: allow song vote canceling
Browse files Browse the repository at this point in the history
  • Loading branch information
miksuh-dev committed Oct 19, 2023
1 parent dd85742 commit 43551bd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
16 changes: 13 additions & 3 deletions client/src/view/Room/Footer/Playing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Props = {
showVideo: Accessor<boolean>;
setShowVideo: Setter<boolean>;
onSkip: (song: Song) => void;
onVote: (songId: number, contentId: string, vote: VoteType) => void;
onVote: (song: Song, vote: VoteType) => void;
};

const PlayingComponent: Component<Props> = (props) => {
Expand Down Expand Up @@ -98,7 +98,12 @@ const PlayingComponent: Component<Props> = (props) => {
<div class="space-x-2 w-max flex items-center ">
<button
onClick={() =>
props.onVote(song().id, song().contentId, VoteType.UP)
props.onVote(
song(),
song()?.vote === VoteType.UP
? VoteType.NONE
: VoteType.UP,
)
}
classList={{
"text-green-500 dark:text-green-500":
Expand All @@ -122,7 +127,12 @@ const PlayingComponent: Component<Props> = (props) => {
</span>
<button
onClick={() =>
props.onVote(song().id, song().contentId, VoteType.DOWN)
props.onVote(
song(),
song()?.vote === VoteType.DOWN
? VoteType.NONE
: VoteType.DOWN,
)
}
classList={{
"text-red-500 dark:text-red-500":
Expand Down
10 changes: 3 additions & 7 deletions client/src/view/Room/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,11 @@ const PlayingComponent: Component<Props> = (props) => {
}
};

const handleVote = async (
songId: number,
contentId: string,
vote: VoteType,
) => {
const handleVote = async (song: Song, vote: VoteType) => {
try {
await trpcClient.room.voteSong.mutate({
songId,
contentId,
songId: song.id,
contentId: song.contentId,
vote,
});

Expand Down
29 changes: 12 additions & 17 deletions server/common/playlist/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TRPCError } from "@trpc/server";
import { OnlineUser } from "types/auth";
import { Song } from "types/prisma";
import {
Expand Down Expand Up @@ -135,28 +134,24 @@ export const removeSong = async (id: number, user: OnlineUser) => {
return song;
};

const getVoteValue = (vote: VoteType) => {
switch (vote) {
case VoteType.UP:
return 1;
case VoteType.DOWN:
return -1;
default:
return 0;
}
};

export const voteSong = async (
songId: number,
contentId: string,
vote: VoteType,
user: OnlineUser
) => {
const voteValue = vote === VoteType.UP ? 1 : -1;

const existingVote = await prisma.songRating.findFirst({
where: {
songId,
contentId,
voter: user.name,
},
});

if (existingVote?.vote === voteValue) {
throw new TRPCError({
code: "CONFLICT",
message: "error.alreadyVoted",
});
}
const voteValue = getVoteValue(vote);

const data = {
contentId,
Expand Down
2 changes: 1 addition & 1 deletion server/router/room/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const roomRouter = t.router({
z.object({
songId: z.number().min(1),
contentId: z.string().min(1),
vote: z.enum([VoteType.UP, VoteType.DOWN]),
vote: z.enum([VoteType.UP, VoteType.DOWN, VoteType.NONE]),
})
)
.mutation(async ({ input, ctx }) => {
Expand Down
1 change: 1 addition & 0 deletions server/types/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export type PlayingSong = Song & {
export enum VoteType {
UP = "UP",
DOWN = "DOWN",
NONE = "NONE",
}

0 comments on commit 43551bd

Please sign in to comment.