Skip to content

Commit

Permalink
feat: select a new random person to play
Browse files Browse the repository at this point in the history
  • Loading branch information
fdaciuk committed Jul 20, 2023
1 parent 9485e07 commit cf3123f
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { NextRequest, NextResponse } from "next/server"
import { getRandomInteger } from "@/lib"
import { prisma } from "@/prisma"

type SelectNewRandomPlayerInput = {
params: {
event: string
participantId: string
}
}
export async function POST(
request: NextRequest,
{ params }: SelectNewRandomPlayerInput,
) {
const { participantId, event } = params
// TODO: Validar com Zod
const { groupId }: { groupId: number } = await request.json()

return prisma.$transaction(async (prisma) => {
const usersThatWannaPlay = await prisma.user.findMany({
select: {
id: true,
},
where: {
play: {
every: {
wannaPlay: true,
gonnaPlay: false,
},
},
},
})

if (usersThatWannaPlay.length === 0) {
return NextResponse.json(
{
message:
"Selecione mais de 16 pessoas para participar antes de tentar trocar alguém.",
},
{
status: 400,
},
)
}

const rnd = getRandomInteger(usersThatWannaPlay.length - 1)
const randomUserId = usersThatWannaPlay[rnd].id

await prisma.play.update({
where: {
userId_eventSlug: {
userId: participantId,
eventSlug: event,
},
},
data: {
wannaPlay: false,
gonnaPlay: false,
groupId: null,
},
})

await prisma.play.update({
where: {
userId_eventSlug: {
userId: randomUserId,
eventSlug: event,
},
},
data: {
groupId,
gonnaPlay: true,
},
})

return NextResponse.json({
status: 204,
})
})
}
8 changes: 4 additions & 4 deletions src/app/events/[event]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
registerParticipant,
} from "./participants/data-participants"
import { upload } from "./participants/upload"
import { getRandomInteger } from "./get-random-integer"
import { RegisterParticipant } from "@/shared/types"
import { getRandomInteger } from "@/lib/get-random-integer"
import { Participant, RegisterParticipant } from "@/shared/types"

type EventProps = {
params: {
Expand Down Expand Up @@ -152,7 +152,7 @@ export default function Event({ params }: EventProps) {
/>
<input type="hidden" name="event" value={event} />
</form>
<Button variant="file" >Importar CSV</Button>
<Button variant="file">Importar CSV</Button>
</li>
<li>
<Modal>
Expand Down Expand Up @@ -192,7 +192,7 @@ export default function Event({ params }: EventProps) {
<Checkbox
id="raffle-participants"
checked={isOnlyRaffle}
onCheckedChange={() => handleSetOnlyRaffle()}
onCheckedChange={handleSetOnlyRaffle}
/>
<label
htmlFor="raffle-participants"
Expand Down
22 changes: 22 additions & 0 deletions src/app/events/[event]/participants/data-participants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,25 @@ export async function setWinner({ userId, event, groupId }: SetWinnerInput) {
},
})
}

type SelectNewRandomPlayerInput = {
userId: string
event: string
groupId: number
}
export async function selectNewRandomPlayer({
userId,
event,
groupId,
}: SelectNewRandomPlayerInput) {
await fetch(
`/api/events/${event}/participants/${userId}/select-new-random-player`,
{
method: "POST",
body: JSON.stringify({ groupId }),
headers: {
"content-type": "application/json",
},
},
)
}
37 changes: 35 additions & 2 deletions src/app/events/[event]/participants/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
} from "@/components"
import { EventProps, Round } from "@/shared/types"
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
import { getSelectedParticipants, setWinner } from "./data-participants"
import {
getSelectedParticipants,
selectNewRandomPlayer,
setWinner,
} from "./data-participants"

export default function Participants({ params }: EventProps) {
const { event } = params
Expand All @@ -31,6 +35,15 @@ export default function Participants({ params }: EventProps) {
},
})

const selectNewRandomPlayerMutation = useMutation({
mutationFn: selectNewRandomPlayer,
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ["selected-participants", { event }],
})
},
})

const selectedParticipants = query.data ?? []

const rounds = selectedParticipants.reduce<Round[]>((acc, participant) => {
Expand Down Expand Up @@ -60,12 +73,24 @@ export default function Participants({ params }: EventProps) {
event: string
groupId: number
}

const handleSetWinner =
({ userId, event, groupId }: HandleSetWinnerInput) =>
() => {
winnerMutation.mutate({ userId, event, groupId })
}

type HandleSelectNewRandomPlayerInput = {
userId: string
event: string
groupId: number
}
const handleSelectNewRandomPlayer =
({ userId, event, groupId }: HandleSelectNewRandomPlayerInput) =>
() => {
selectNewRandomPlayerMutation.mutate({ userId, event, groupId })
}

return (
<section className="font-sans">
<p className="text-body-xs leading-normal text-neutral-500 underline">
Expand Down Expand Up @@ -100,7 +125,15 @@ export default function Participants({ params }: EventProps) {
</button>
</p>
<p className="text-primary-100">
<button>Selecionar outro</button>
<button
onClick={handleSelectNewRandomPlayer({
userId: participant.id,
event,
groupId: participant.groupId,
})}
>
Selecionar outro
</button>
</p>
</ParticipantItem>
))}
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./get-random-integer"
export * from "./utils"

0 comments on commit cf3123f

Please sign in to comment.