Skip to content

Commit

Permalink
feat: wish pausing
Browse files Browse the repository at this point in the history
  • Loading branch information
flowergardn committed Dec 14, 2023
1 parent 380810a commit 8582ebe
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 15 deletions.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ model Cards {
description String @default("")
createdAt DateTime @default(now())
birthday DateTime
paused Boolean @default(false)
User User @relation(fields: [creatorId], references: [id])
Expand Down
1 change: 1 addition & 0 deletions src/components/EditCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type CardDataValues = {
description: string;
birthday: string;
showAge: boolean;
paused?: boolean;
};
// used for type-checking in our textbox component
type CardDataTypes = "title" | "description" | "birthday";
Expand Down
35 changes: 27 additions & 8 deletions src/pages/c/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PropsWithChildren } from "react";

const CardPage: NextPage<{ id: string }> = ({ id }) => {
const {
data,
data: cardData,
isLoading: cardLoading,
isError: cardError,
} = api.cards.fetch.useQuery({
Expand All @@ -23,7 +23,7 @@ const CardPage: NextPage<{ id: string }> = ({ id }) => {
<>
<Meta
title="Sign this card | bday.quest (beta)"
description={data ? data.description : ""}
description={cardData ? cardData.description : ""}
/>
<Navbar />
{props.children}
Expand All @@ -40,7 +40,7 @@ const CardPage: NextPage<{ id: string }> = ({ id }) => {
</BasePage>
);

if (cardError || !data)
if (cardError || !cardData)
return (
<BasePage>
<main className="flex min-h-screen items-center justify-center bg-base-100">
Expand All @@ -55,8 +55,8 @@ const CardPage: NextPage<{ id: string }> = ({ id }) => {
const CardInfo = () => {
return (
<article className="prose">
<h1>{data.title}</h1>
<p>{data.description}</p>
<h1>{cardData.title}</h1>
<p>{cardData.description}</p>
</article>
);
};
Expand All @@ -73,15 +73,34 @@ const CardPage: NextPage<{ id: string }> = ({ id }) => {
if (wishError) return <></>;
const canShow = !wishesLoading && !wishError && data;

const SignCard = () => {
if (!canShow) return <></>;

if (cardData.paused) {
return (
<div
className="tooltip tooltip-bottom"
data-tip="Wishes are disabled"
>
<button className="btn btn-disabled">Sign</button>
</div>
);
}

return (
<Link href={`/c/${id}/sign`}>
<button className="btn btn-primary">Sign</button>
</Link>
);
};

return (
<>
<article className="prose">
<h2>Wishes</h2>
</article>
{canShow ? <Signatures signatures={data} /> : <Loader />}
<Link href={`/c/${id}/sign`}>
<button className="btn btn-primary">Sign</button>
</Link>
<SignCard />
</>
);
};
Expand Down
25 changes: 21 additions & 4 deletions src/pages/manage/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { api } from "~/utils/api";

const ManageCard: NextPage<{ id: string }> = ({ id }) => {
const {
data,
data: cardData,
isLoading: cardLoading,
isError: isCardError,
error: cardError,
Expand Down Expand Up @@ -44,7 +44,7 @@ const ManageCard: NextPage<{ id: string }> = ({ id }) => {
</BasePage>
);

if (cardError ?? !data) {
if (cardError ?? !cardData) {
toast.error(cardError?.message ?? "");
return (
<BasePage>
Expand Down Expand Up @@ -100,14 +100,31 @@ const ManageCard: NextPage<{ id: string }> = ({ id }) => {
title="Title"
type="title"
register={register}
placeholder={data.title}
placeholder={cardData.title}
/>
<Textbox
title="Description"
type="description"
register={register}
placeholder={data.description}
placeholder={cardData.description}
/>
<Textbox
title="Description"
type="description"
register={register}
placeholder={cardData.description}
/>
{/* note: perhaps we should convert this into a component, since this is copied from our create code. */}
<div className="form-control">
<label className="label cursor-pointer">
<span className="label-text">Pause wishes?</span>
<input
type="checkbox"
className="checkbox"
{...register("paused")}
/>
</label>
</div>
<div className="form-control mt-6">
<button className="btn btn-primary">Edit</button>
</div>
Expand Down
32 changes: 29 additions & 3 deletions src/server/api/routers/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
import type CardWish from "~/interfaces/CardWish";
import { TRPCError } from "@trpc/server";

const checkFalsy = (...values: (string | boolean | null | undefined)[]) =>
values.every((value) => value === null);

export const cardRouter = createTRPCRouter({
create: privateProcedure
.input(
Expand Down Expand Up @@ -58,6 +61,7 @@ export const cardRouter = createTRPCRouter({
cardId: z.string().cuid(),
title: z.string().nullish(),
description: z.string().nullish(),
paused: z.boolean().nullish(),
}),
)
.mutation(async ({ ctx, input }) => {
Expand All @@ -73,19 +77,21 @@ export const cardRouter = createTRPCRouter({
code: "NOT_FOUND",
});

if (!input.title && !input.description)
if (checkFalsy(input.title, input.description, input.paused))
throw new TRPCError({
code: "BAD_REQUEST",
cause: "Either a title or description should be specified.",
cause: "No valid options specified.",
});

const update: {
title?: string;
description?: string;
paused?: boolean;
} = {};

if (input.title) update.title = input.title;
if (input.description) update.description = input.description;
if (input.paused !== null) update.paused = input.paused;

return await ctx.db.cards.update({
where: {
Expand Down Expand Up @@ -182,18 +188,38 @@ export const cardRouter = createTRPCRouter({
}),
)
.mutation(async ({ ctx, input }) => {
const cardInfo = await ctx.db.cards.findUnique({
where: {
id: input.cardId,
},
});

if (!cardInfo) {
throw new TRPCError({
code: "NOT_FOUND",
cause: "You cannot send a wish to an inexistent card!",
});
}
const hasWish = await ctx.db.wishes.findFirst({
where: {
cardId: input.cardId,
creatorId: ctx.userId,
},
});

if (hasWish)
if (hasWish) {
throw new TRPCError({
code: "BAD_REQUEST",
cause: "You've already submitted a wish to this card!",
});
}

if (cardInfo.paused) {
throw new TRPCError({
code: "BAD_REQUEST",
cause: "Wishes can no longer be submitted to this card!",
});
}

return await ctx.db.wishes.create({
data: {
Expand Down

0 comments on commit 8582ebe

Please sign in to comment.