Skip to content

Commit 19f94cc

Browse files
committed
feat: add department to user
1 parent 00a5848 commit 19f94cc

File tree

15 files changed

+200
-256
lines changed

15 files changed

+200
-256
lines changed

actions/register.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const register = async (values: z.infer<typeof RegisterSchema>) => {
1414
return { error: "Invalid fields!" };
1515
}
1616

17-
const {name , password } = validatedFields.data;
17+
const {name , password,departmentId } = validatedFields.data;
1818
const hashedPassword = await bcrypt.hash(password, 10);
1919

2020
const existingUser = await getUserByName(name);
@@ -27,6 +27,7 @@ export const register = async (values: z.infer<typeof RegisterSchema>) => {
2727
data: {
2828
name,
2929
password: hashedPassword,
30+
departmentId,
3031
imageUrl: "",
3132
}
3233
});

app/(main)/me/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use client"
22

33
import {clsx} from "clsx";
4-
import useConversation from "@/hooks/use-conversation";
54
import EmptyState from "@/components/conversation/empty-state";
5+
import {useConversation} from "@/hooks/use-conversation";
66

77
const ServerMe = () => {
88
const { isOpen } = useConversation();

app/api/departments/route.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {currentUser} from "@/lib/auth";
2+
import {NextResponse} from "next/server";
3+
import {Message} from "@prisma/client";
4+
import {db} from "@/lib/db";
5+
import {getDepartments} from "@/lib/departments";
6+
7+
export async function GET()
8+
{
9+
try {
10+
const departments = await getDepartments();
11+
return NextResponse.json(departments);
12+
} catch (error) {
13+
console.log("[DEPARTMENTS_GET]", error);
14+
return new NextResponse("Internal Error", { status: 500 });
15+
}
16+
}

app/page.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import {LoginButton} from "@/components/auth/login-button";
2-
import React from "react";
3-
import {InitialModal} from "@/components/modals/initial-modal";
41
import {currentUser} from "@/lib/auth";
52
import {db} from "@/lib/db";
63
import {redirect} from "next/navigation";
4+
import {InitialModal} from "@/components/modals/initial-modal";
75

86
export default async function Home() {
97
const user = await currentUser();
@@ -21,7 +19,11 @@ export default async function Home() {
2119

2220
if (server) {
2321
return redirect(`/servers/${server.id}`);
22+
} else {
23+
return redirect(`/me`);
2424
}
25+
} else {
26+
return redirect('/login');
2527
}
2628

2729
return <InitialModal/>;

components/auth/login-form.tsx

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { CardWrapper } from "@/components/auth/card-wrapper"
3+
import {CardWrapper} from "@/components/auth/card-wrapper"
44
import {useForm} from "react-hook-form";
55
import {LoginSchema} from "@/schemas";
66
import {zodResolver} from "@hookform/resolvers/zod";
@@ -10,17 +10,18 @@ import {Input} from "@/components/ui/input";
1010
import {Button} from "@/components/ui/button";
1111
import {FormError} from "@/components/form-error";
1212
import {login} from "@/actions/login";
13-
import {useState, useTransition} from "react";
13+
import {useEffect, useState, useTransition} from "react";
14+
import {Department} from "@prisma/client";
15+
import axios from "axios";
16+
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue} from "@/components/ui/select";
1417

1518
export const LoginForm = () => {
1619
const [error, setError] = useState<string | undefined>("");
1720
const [isPending, startTransition] = useTransition();
1821

1922
const form = useForm<z.infer<typeof LoginSchema>>({
20-
resolver: zodResolver(LoginSchema),
21-
defaultValues: {
22-
name: "",
23-
password: "",
23+
resolver: zodResolver(LoginSchema), defaultValues: {
24+
name: "", password: "",
2425
},
2526
});
2627

@@ -35,64 +36,58 @@ export const LoginForm = () => {
3536
});
3637
}
3738

38-
return (
39-
<CardWrapper
40-
headerLabel="Welcome back"
41-
backButtonLabel="Don't have an account?"
42-
backButtonHref="/register"
43-
>
44-
<Form {...form}>
45-
<form
46-
onSubmit={form.handleSubmit(onSubmit)}
47-
className="space-y-6"
39+
return (<CardWrapper
40+
headerLabel="Welcome back"
41+
backButtonLabel="Don't have an account?"
42+
backButtonHref="/register"
43+
>
44+
<Form {...form}>
45+
<form
46+
onSubmit={form.handleSubmit(onSubmit)}
47+
className="space-y-6"
48+
>
49+
<div className="space-y-4">
50+
<FormField
51+
control={form.control}
52+
name="name"
53+
render={({field}) => (<FormItem>
54+
<FormLabel>Name</FormLabel>
55+
<FormControl>
56+
<Input
57+
{...field}
58+
disabled={isPending}
59+
placeholder="John Doe"
60+
/>
61+
</FormControl>
62+
<FormMessage/>
63+
</FormItem>)}
64+
/>
65+
<FormField
66+
control={form.control}
67+
name="password"
68+
render={({field}) => (<FormItem>
69+
<FormLabel>Password</FormLabel>
70+
<FormControl>
71+
<Input
72+
{...field}
73+
disabled={isPending}
74+
placeholder="******"
75+
type="password"
76+
/>
77+
</FormControl>
78+
<FormMessage/>
79+
</FormItem>)}
80+
/>
81+
</div>
82+
<FormError message={error}/>
83+
<Button
84+
disabled={isPending}
85+
type="submit"
86+
className="w-full"
4887
>
49-
<div className="space-y-4">
50-
<FormField
51-
control={form.control}
52-
name="name"
53-
render={({ field }) => (
54-
<FormItem>
55-
<FormLabel>Name</FormLabel>
56-
<FormControl>
57-
<Input
58-
{...field}
59-
disabled={isPending}
60-
placeholder="John Doe"
61-
/>
62-
</FormControl>
63-
<FormMessage />
64-
</FormItem>
65-
)}
66-
/>
67-
<FormField
68-
control={form.control}
69-
name="password"
70-
render={({ field }) => (
71-
<FormItem>
72-
<FormLabel>Password</FormLabel>
73-
<FormControl>
74-
<Input
75-
{...field}
76-
disabled={isPending}
77-
placeholder="******"
78-
type="password"
79-
/>
80-
</FormControl>
81-
<FormMessage />
82-
</FormItem>
83-
)}
84-
/>
85-
</div>
86-
<FormError message={error} />
87-
<Button
88-
disabled={isPending}
89-
type="submit"
90-
className="w-full"
91-
>
92-
Login
93-
</Button>
94-
</form>
95-
</Form>
96-
</CardWrapper>
97-
);
88+
Login
89+
</Button>
90+
</form>
91+
</Form>
92+
</CardWrapper>);
9893
};

components/auth/register-form.tsx

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import * as z from "zod";
4-
import { useState, useTransition } from "react";
4+
import {useEffect, useState, useTransition} from "react";
55
import { useForm } from "react-hook-form";
66
import { zodResolver } from "@hookform/resolvers/zod";
77

@@ -20,17 +20,22 @@ import { Button } from "@/components/ui/button";
2020
import { FormError } from "@/components/form-error";
2121
import { FormSuccess } from "@/components/form-success";
2222
import { register } from "@/actions/register";
23+
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue} from "@/components/ui/select";
24+
import {Department} from "@prisma/client";
25+
import axios from "axios";
2326

2427
export const RegisterForm = () => {
2528
const [error, setError] = useState<string | undefined>("");
2629
const [success, setSuccess] = useState<string | undefined>("");
30+
const [departments, setDepartments] = useState<Department[]>([]);
2731
const [isPending, startTransition] = useTransition();
2832

2933
const form = useForm<z.infer<typeof RegisterSchema>>({
3034
resolver: zodResolver(RegisterSchema),
3135
defaultValues: {
3236
name: "",
3337
password: "",
38+
departmentId: "",
3439
},
3540
});
3641

@@ -47,6 +52,19 @@ export const RegisterForm = () => {
4752
});
4853
};
4954

55+
useEffect(() => {
56+
const fetchDepartments = async () => {
57+
try {
58+
const response = await axios.get('/api/departments');
59+
console.log(response);
60+
setDepartments(response.data);
61+
} catch (error) {
62+
console.log(error);
63+
}
64+
};
65+
fetchDepartments();
66+
}, []);
67+
5068
return (
5169
<CardWrapper
5270
headerLabel="Create an account"
@@ -94,6 +112,30 @@ export const RegisterForm = () => {
94112
</FormItem>
95113
)}
96114
/>
115+
<FormField
116+
control={form.control}
117+
name="departmentId"
118+
render={({field}) => (<FormItem>
119+
<FormLabel
120+
className="uppercase text-xs font-bold text-zinc-500 dark:text-secondary/70">
121+
Select department
122+
</FormLabel>
123+
<Select onValueChange={field.onChange} defaultValue={field.value}>
124+
<FormControl>
125+
<SelectTrigger className="w-[180px]">
126+
<SelectValue placeholder="Select department"/>
127+
</SelectTrigger>
128+
</FormControl>
129+
<SelectContent>
130+
{departments.map((department) => (
131+
<SelectItem key={department.id} value={department.id}>
132+
{department.department}
133+
</SelectItem>))}
134+
</SelectContent>
135+
</Select>
136+
<FormMessage/>
137+
</FormItem>)}
138+
/>
97139
</div>
98140
<FormError message={error} />
99141
<FormSuccess message={success} />

components/conversation/conversation-list.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {find} from "lodash";
88
import {FullConversationType} from "@/types";
99
import {User} from "@prisma/client";
1010
import {useSession} from "next-auth/react";
11-
import useConversation from "@/hooks/use-conversation";
11+
import {useConversation} from "@/hooks/use-conversation";
1212
import {pusherClient} from "@/lib/pusher";
1313
import {Group} from "lucide-react";
1414
import ConversationBox from "@/components/conversation/conversation-box";

0 commit comments

Comments
 (0)