added current profile, create a server

This commit is contained in:
Bob Burningham 2023-10-08 18:18:17 -07:00
parent 6ed6687800
commit 4763519f35
3 changed files with 74 additions and 1 deletions

42
app/api/servers/route.ts Normal file
View File

@ -0,0 +1,42 @@
import { v4 as uuidv4 } from "uuid";
import { NextResponse } from "next/server";
import { MemberRole } from "@prisma/client";
import { currentProfile } from "@/lib/current-profile";
import { db } from "@/lib/db";
export async function POST(req: Request) {
try {
const { name, imageUrl } = await req.json();
const profile = await currentProfile();
if (!profile) {
return new NextResponse("Unauthorized", { status: 401 });
}
const server = await db.server.create({
data: {
profileId: profile.id,
name,
imageUrl,
inviteCode: uuidv4(),
channels: {
create: [
{ name: "general", profileId: profile.id }
],
},
members: {
create: [
{ profileId: profile.id, role: MemberRole.ADMIN }
]
}
}
})
return NextResponse.json({ server });
} catch (error) {
console.log("[SERVER POST]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}

View File

@ -1,5 +1,6 @@
"use client"; "use client";
import axios from "axios";
import * as z from "zod"; import * as z from "zod";
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
@ -24,6 +25,7 @@ import{
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { FileUpload } from "@/components/file-upload"; import { FileUpload } from "@/components/file-upload";
import { useRouter } from "next/navigation";
const formSchema = z.object({ const formSchema = z.object({
name: z.string().min(1, { name: z.string().min(1, {
@ -37,6 +39,8 @@ const formSchema = z.object({
export const InitialModal = () => { export const InitialModal = () => {
const [isMounted, setIsMounted] = useState(false); const [isMounted, setIsMounted] = useState(false);
const router = useRouter();
useEffect(() => { useEffect(() => {
setIsMounted(true); setIsMounted(true);
}, []); }, []);
@ -52,7 +56,15 @@ export const InitialModal = () => {
const isLoading = form.formState.isSubmitting; const isLoading = form.formState.isSubmitting;
const onSubmit = async (values: z.infer<typeof formSchema>) => { const onSubmit = async (values: z.infer<typeof formSchema>) => {
console.log(values); try {
await axios.post("/api/servers", values);
form.reset();
router.refresh();
window.location.reload();
} catch (error) {
console.error(error);
}
} }
if (!isMounted) { if (!isMounted) {

19
lib/current-profile.ts Normal file
View File

@ -0,0 +1,19 @@
import { auth } from "@clerk/nextjs";
import { db } from "@/lib/db";
export const currentProfile = async () => {
const { userId } = auth();
if (!userId) {
return null;
}
const profile = await db.profile.findUnique({
where: {
userId
},
});
return profile;
}