Skip to content

Commit

Permalink
♻️ Update how we store poll status (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella authored Dec 5, 2023
1 parent 7670db6 commit 04211ac
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 186 deletions.
7 changes: 1 addition & 6 deletions apps/web/src/app/[locale]/(admin)/polls/polls-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ export function PollsPage() {
data.length > 0 ? (
<div className="mx-auto grid max-w-3xl gap-3 sm:gap-4">
{data.map((poll) => {
const { title, id: pollId, createdAt, closed: paused } = poll;
const status = poll.event
? "closed"
: paused
? "paused"
: "live";
const { title, id: pollId, createdAt, status } = poll;
return (
<div
key={poll.id}
Expand Down
4 changes: 1 addition & 3 deletions apps/web/src/components/event-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export const EventCard = () => {
),
);

const status = poll?.event ? "closed" : poll?.closed ? "paused" : "live";

if (!poll) {
return null;
}
Expand All @@ -49,7 +47,7 @@ export const EventCard = () => {
/>
<div className="bg-pattern p-4 sm:flex sm:flex-row-reverse sm:justify-between sm:px-6">
<div className="mb-2">
<PollStatusBadge status={status} />
<PollStatusBadge status={poll.status} />
</div>
<div className="flex items-start justify-between">
<div className="flex items-start gap-4 sm:gap-6">
Expand Down
5 changes: 2 additions & 3 deletions apps/web/src/components/layouts/poll-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
import ManagePoll from "@/components/poll/manage-poll";
import NotificationsToggle from "@/components/poll/notifications-toggle";
import { LegacyPollContextProvider } from "@/components/poll/poll-context-provider";
import { PollStatus } from "@/components/poll-status";
import { PollStatusLabel } from "@/components/poll-status";
import { Skeleton } from "@/components/skeleton";
import { Trans } from "@/components/trans";
import { useUser } from "@/components/user-provider";
Expand All @@ -53,7 +53,6 @@ import { NextPageWithLayout } from "../../types";

const StatusControl = () => {
const poll = usePoll();
const state = poll.event ? "closed" : poll.closed ? "paused" : "live";
const queryClient = trpc.useUtils();
const reopen = trpc.polls.reopen.useMutation({
onMutate: () => {
Expand Down Expand Up @@ -110,7 +109,7 @@ const StatusControl = () => {
<DropdownMenu modal={false}>
<DropdownMenuTrigger asChild>
<Button>
<PollStatus status={state} />
<PollStatusLabel status={poll.status} />
<ChevronDownIcon className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
Expand Down
15 changes: 7 additions & 8 deletions apps/web/src/components/poll-status.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { PollStatus } from "@rallly/database";
import { cn } from "@rallly/ui";
import { CheckCircleIcon, PauseCircleIcon, RadioIcon } from "lucide-react";

import { Trans } from "@/components/trans";
import { IconComponent } from "@/types";

export type PollState = "live" | "paused" | "closed";

const LabelWithIcon = ({
icon: Icon,
children,
Expand All @@ -23,11 +22,11 @@ const LabelWithIcon = ({
);
};

export const PollStatus = ({
export const PollStatusLabel = ({
status,
className,
}: {
status: PollState;
status: PollStatus;
className?: string;
}) => {
switch (status) {
Expand All @@ -43,7 +42,7 @@ export const PollStatus = ({
<Trans i18nKey="pollStatusPaused" defaults="Paused" />
</LabelWithIcon>
);
case "closed":
case "finalized":
return (
<LabelWithIcon icon={CheckCircleIcon} className={className}>
<Trans i18nKey="pollStatusClosed" defaults="Finalized" />
Expand All @@ -52,13 +51,13 @@ export const PollStatus = ({
}
};

export const PollStatusBadge = ({ status }: { status: PollState }) => {
export const PollStatusBadge = ({ status }: { status: PollStatus }) => {
return (
<PollStatus
<PollStatusLabel
className={cn("rounded-full border py-0.5 pl-1.5 pr-3 text-sm", {
"border-blue-500 text-blue-500": status === "live",
"border-gray-500 text-gray-500": status === "paused",
"border-green-500 text-green-500": status === "closed",
"border-green-500 text-green-500": status === "finalized",
})}
status={status}
/>
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/poll/notifications-toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const NotificationsToggle: React.FunctionComponent = () => {
loading={watch.isLoading || unwatch.isLoading}
icon={isWatching ? BellRingIcon : BellOffIcon}
data-testid="notifications-toggle"
disabled={poll.demo || user.isGuest}
disabled={user.isGuest}
className="flex items-center gap-2 px-2.5"
onClick={async () => {
if (user.isGuest) {
Expand Down
5 changes: 2 additions & 3 deletions apps/web/src/utils/trpc/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User, VoteType } from "@rallly/database";
import { PollStatus, User, VoteType } from "@rallly/database";

export type GetPollApiResponse = {
id: string;
Expand All @@ -9,10 +9,9 @@ export type GetPollApiResponse = {
user: User | null;
timeZone: string | null;
adminUrlId: string;
status: PollStatus;
participantUrlId: string;
closed: boolean;
legacy: boolean;
demo: boolean;
createdAt: Date;
deleted: boolean;
};
Expand Down
130 changes: 0 additions & 130 deletions apps/web/tests/mocks.ts

This file was deleted.

41 changes: 23 additions & 18 deletions packages/backend/trpc/routers/polls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export const polls = router({
timeZone: input.timeZone,
location: input.location,
description: input.description,
demo: input.demo,
adminUrlId: adminToken,
participantUrlId,
userId: ctx.user.id,
Expand Down Expand Up @@ -355,12 +354,11 @@ export const polls = router({
adminUrlId: true,
participantUrlId: true,
closed: true,
legacy: true,
status: true,
hideParticipants: true,
disableComments: true,
hideScores: true,
requireParticipantEmail: true,
demo: true,
options: {
select: {
id: true,
Expand Down Expand Up @@ -436,6 +434,7 @@ export const polls = router({
timeZone: true,
adminUrlId: true,
participantUrlId: true,
status: true,
event: {
select: {
start: true,
Expand Down Expand Up @@ -551,14 +550,20 @@ export const polls = router({
eventStart = eventStart.tz(poll.timeZone, true);
}

await prisma.event.create({
await prisma.poll.update({
where: {
id: input.pollId,
},
data: {
pollId: poll.id,
optionId: input.optionId,
start: eventStart.toDate(),
duration: option.duration,
title: poll.title,
userId: ctx.user.id,
event: {
create: {
optionId: input.optionId,
start: eventStart.toDate(),
duration: option.duration,
title: poll.title,
userId: ctx.user.id,
},
},
},
});

Expand Down Expand Up @@ -721,18 +726,16 @@ export const polls = router({
)
.mutation(async ({ input }) => {
await prisma.$transaction([
prisma.event.delete({
where: {
pollId: input.pollId,
},
}),
prisma.poll.update({
where: {
id: input.pollId,
},
data: {
eventId: null,
closed: false,
event: {
delete: true,
},
status: "live",
closed: false, // @deprecated
},
}),
]);
Expand All @@ -749,7 +752,8 @@ export const polls = router({
id: input.pollId,
},
data: {
closed: true,
closed: true, // TODO (Luke Vella) [2023-12-05]: Remove this
status: "paused",
},
});
}),
Expand Down Expand Up @@ -827,6 +831,7 @@ export const polls = router({
},
data: {
closed: false,
status: "live",
},
});
}),
Expand Down
Loading

1 comment on commit 04211ac

@vercel
Copy link

@vercel vercel bot commented on 04211ac Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

app – ./

app.rallly.co
app-git-main-rallly.vercel.app
app-rallly.vercel.app

Please sign in to comment.