Skip to content

Commit

Permalink
Merge pull request #7 from RicardoGEsteves/create-subspreadits
Browse files Browse the repository at this point in the history
feat: ✨ Title: Enhancement of the Create Community Feature.
  • Loading branch information
RicardoGEsteves authored Dec 21, 2023
2 parents 0ced6c5 + e82669f commit b6fb84f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/(main)/r/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,22 @@ const CreatePage = () => {
<Input
value={input}
onChange={(e) => setInput(e.target.value)}
className="pl-6"
className="pl-6 mt-3"
onKeyDown={(e) => {
if (e.key === "Enter") {
createCommunity();
} else if (e.key === "Escape") {
setInput("");
}
}}
/>
</div>
</div>

<div className="flex justify-end gap-4">
<Button
disabled={isLoading}
variant="secondary"
variant="default"
onClick={() => router.back()}
>
Cancel
Expand All @@ -102,6 +109,7 @@ const CreatePage = () => {
isLoading={isLoading}
disabled={input.length === 0}
onClick={() => createCommunity()}
variant={"primary"}
>
SpreadIt
</Button>
Expand Down
52 changes: 52 additions & 0 deletions app/api/subspreadit/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getAuthSession } from "@/lib/auth";
import { db } from "@/lib/db";
import { SubSpreadItValidator } from "@/lib/validators/sub-spreadIt";
import { z } from "zod";

export async function POST(req: Request) {
try {
const session = await getAuthSession();

if (!session?.user) {
return new Response("Unauthorized", { status: 401 });
}

const body = await req.json();
const { name } = SubSpreadItValidator.parse(body);

// check if subSpreadIt already exists
const subSpreadItExists = await db.subSpreadIt.findFirst({
where: {
name,
},
});

if (subSpreadItExists) {
return new Response("SubSpreadIt already exists", { status: 409 });
}

// create subSpreadIt and associate it with the user
const subSpreadIt = await db.subSpreadIt.create({
data: {
name,
creatorId: session.user.id,
},
});

// creator also has to be subSpreadIt
await db.subscription.create({
data: {
userId: session.user.id,
subSpreadItId: subSpreadIt.id,
},
});

return new Response(subSpreadIt.name);
} catch (error) {
if (error instanceof z.ZodError) {
return new Response(error.message, { status: 422 });
}

return new Response("Could not create subSpreadIt", { status: 500 });
}
}
2 changes: 2 additions & 0 deletions components/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const buttonVariants = cva(
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
primary:
"bg-emerald-500 text-secondary transition hover:bg-emerald-600",
},
size: {
default: "h-10 px-4 py-2",
Expand Down

0 comments on commit b6fb84f

Please sign in to comment.