-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
633 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
/** | ||
* This is server side action. | ||
* I may use it in the future again. | ||
*/ | ||
|
||
/* import { auth, signOut } from '@/auth'; | ||
const SettingsPage = async () => { | ||
const session = await auth(); | ||
return ( | ||
<div> | ||
{JSON.stringify(session)} | ||
<form | ||
action={async () => { | ||
'use server'; | ||
await signOut(); // this signOut function was made exclusively for server actions | ||
}} | ||
> | ||
<button type="submit">Sign out</button> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
export default SettingsPage; | ||
*/ | ||
|
||
// ------------------------------------- | ||
|
||
/** | ||
* This is a combination of server action and a client component! | ||
*/ | ||
// 'use client'; | ||
// import { logout } from '@/actions/logout'; | ||
// import { useCurrentUser } from '@/hooks/use-current-user'; | ||
|
||
// const SettingsPage = () => { | ||
// const user = useCurrentUser(); | ||
// const onClick = () => { | ||
// logout(); | ||
// }; | ||
|
||
// return ( | ||
// <div className=' bg-white p-10 rounded-xl'> | ||
// <button onClick={onClick}>Sign out</button> | ||
// </div> | ||
// ); | ||
// }; | ||
|
||
// export default SettingsPage; | ||
|
||
"use client"; | ||
|
||
import * as z from "zod"; | ||
import { useForm } from "react-hook-form"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { useTransition, useState } from "react"; | ||
import { useSession } from "next-auth/react"; | ||
|
||
import { Switch } from "@/components/ui/switch"; | ||
// import { | ||
// Select, | ||
// SelectContent, | ||
// SelectItem, | ||
// SelectTrigger, | ||
// SelectValue, | ||
// } from "@/components/ui/select"; | ||
import { SettingsSchema } from "@/schemas"; | ||
import { Card, CardHeader, CardContent } from "@/components/ui/card"; | ||
import { Button } from "@/components/ui/button"; | ||
import { settings } from "@/actions/settings"; | ||
import { | ||
Form, | ||
FormField, | ||
FormControl, | ||
FormItem, | ||
FormLabel, | ||
FormDescription, | ||
FormMessage, | ||
} from "@/components/ui/form"; | ||
import { Input } from "@/components/ui/input"; | ||
import { useCurrentUser } from "@/hooks/use-current-user"; | ||
import FormError from "@/components/form-error"; | ||
import FormSuccess from "@/components/form-success"; | ||
import { UserRole } from "@prisma/client"; | ||
import { UserInfo } from "@/components/user-info"; | ||
import Link from "next/link"; | ||
|
||
const SettingsPage = () => { | ||
const user = useCurrentUser(); | ||
|
||
const [error, setError] = useState<string | undefined>(); | ||
const [success, setSuccess] = useState<string | undefined>(); | ||
const { update } = useSession(); | ||
const [isPending, startTransition] = useTransition(); | ||
|
||
const form = useForm<z.infer<typeof SettingsSchema>>({ | ||
resolver: zodResolver(SettingsSchema), | ||
defaultValues: { | ||
password: undefined, | ||
newPassword: undefined, | ||
name: user?.name || undefined, | ||
email: user?.email || undefined, | ||
role: user?.role || undefined, | ||
isTwoFactorEnabled: user?.isTwoFactorEnabled || undefined, | ||
}, | ||
}); | ||
|
||
const onSubmit = (values: z.infer<typeof SettingsSchema>) => { | ||
startTransition(() => { | ||
settings(values) | ||
.then((data) => { | ||
if (data.error) { | ||
setError(data.error); | ||
} | ||
|
||
if (data.success) { | ||
update(); | ||
setSuccess(data.success); | ||
} | ||
}) | ||
.catch(() => setError("Something went wrong!")); | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className=" flex flex-col"> | ||
<UserInfo label="Profile details" user={user} /> | ||
<Card className="min-w-[350px] max-w-[600px] md:max-w-full shadow-none border-none md:shadow-lg"> | ||
<CardHeader> | ||
<p className="text-2xl font-semibold text-center">⚙️ Settings</p> | ||
</CardHeader> | ||
<CardContent> | ||
<Form {...form}> | ||
<form className="space-y-6" onSubmit={form.handleSubmit(onSubmit)}> | ||
<div className="space-y-4"> | ||
<FormField | ||
control={form.control} | ||
name="name" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Name</FormLabel> | ||
<FormControl> | ||
<Input {...field} placeholder="John Doe" disabled={isPending} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
{user?.isOAuth === false && ( | ||
<> | ||
<FormField | ||
control={form.control} | ||
name="email" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Email</FormLabel> | ||
<FormControl> | ||
<Input | ||
{...field} | ||
placeholder="[email protected]" | ||
type="email" | ||
disabled={isPending} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="password" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Password</FormLabel> | ||
<FormControl> | ||
<Input | ||
{...field} | ||
placeholder="******" | ||
type="password" | ||
disabled={isPending} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="newPassword" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>New Password</FormLabel> | ||
<FormControl> | ||
<Input | ||
{...field} | ||
placeholder="******" | ||
type="password" | ||
disabled={isPending} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</> | ||
)} | ||
{/* <FormField | ||
control={form.control} | ||
name="role" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Role</FormLabel> | ||
<Select | ||
disabled={isPending} | ||
onValueChange={field.onChange} | ||
defaultValue={field.value} | ||
> | ||
<FormControl> | ||
<SelectTrigger> | ||
<SelectValue placeholder="Select a role" /> | ||
</SelectTrigger> | ||
</FormControl> | ||
<SelectContent> | ||
<SelectItem value={UserRole.ADMIN}>Admin</SelectItem> | ||
<SelectItem value={UserRole.USER}>User</SelectItem> | ||
</SelectContent> | ||
</Select> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> */} | ||
{user?.isOAuth === false && ( | ||
<FormField | ||
control={form.control} | ||
name="isTwoFactorEnabled" | ||
render={({ field }) => ( | ||
<FormItem className="flex flex-row items-center justify-between rounded-lg border p-3 shadow-sm"> | ||
<div className="space-y-0.5"> | ||
<FormLabel>Two Factor Authentication</FormLabel> | ||
<FormDescription> | ||
Enable two factor authentication for your account | ||
</FormDescription> | ||
</div> | ||
<FormControl> | ||
<Switch | ||
disabled={isPending} | ||
checked={field.value} | ||
onCheckedChange={field.onChange} | ||
/> | ||
</FormControl> | ||
</FormItem> | ||
)} | ||
/> | ||
)} | ||
</div> | ||
<FormError message={error} /> | ||
<FormSuccess message={success} /> | ||
<Button disabled={isPending} type="submit"> | ||
Save | ||
</Button> | ||
</form> | ||
</Form> | ||
</CardContent> | ||
</Card> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SettingsPage; |
Oops, something went wrong.