Skip to content

Commit

Permalink
feat(Frontend): working profile
Browse files Browse the repository at this point in the history
  • Loading branch information
iandday committed Nov 24, 2024
1 parent cc58bde commit a048c10
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 35 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/custom/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const Body = React.forwardRef<
ref={ref}
data-layout='body'
className={cn(
'px-4 py-6 md:overflow-hidden md:px-8',
'px-4 py-0 md:overflow-hidden md:px-0',
contextVal && contextVal.fixed && 'flex-1',
className
)}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/user-nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function UserNav() {
</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuGroup>
<Link to={'/settings'}>
<Link to={'/profile'}>
<DropdownMenuItem>Profile</DropdownMenuItem>
</Link>
<Link to={'/settings'}>
Expand Down
150 changes: 150 additions & 0 deletions frontend/src/pages/profile/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { useForm } from 'react-hook-form'
import { Layout } from '@/components/custom/layout'
import { Search } from '@/components/search'

import { Button } from '@/components/custom/button'
import ThemeSwitch from '@/components/theme-switch'
import { UserNav } from '@/components/user-nav'
import { z } from 'zod'
import { zodResolver } from '@hookform/resolvers/zod'
import { useUserStore } from '../../store/user-store'
import { useState } from 'react'
import { useApiViewsUserUpdateMe } from '@/lib/api'
import { useNavigate } from 'react-router-dom'
import { Card } from '@/components/ui/card'
const formSchema = z.object({
firstName: z.string(),
lastName: z.string(),
})

export default function Profile() {
const firstName = useUserStore.getState().user?.firstName
const lastName = useUserStore.getState().user?.lastName
const accessToken = useUserStore.getState().user?.accessToken
const refreshToken = useUserStore.getState().user?.refreshToken
const email = useUserStore.getState().user?.email
const [isLoading, setIsLoading] = useState(false)
const navigate = useNavigate()
const { setCredentials } = useUserStore()

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
firstName: firstName,
lastName: lastName,
},
})
const {
mutate: updateMutate,
isSuccess: updateMutateIsSuccess,
error: updaterMutateError,
reset: updateMutateReset,
data: updateData,
} = useApiViewsUserUpdateMe()

function onSubmit(data: z.infer<typeof formSchema>) {
setIsLoading(true)
setTimeout(() => {
setIsLoading(false)
}, 3000)
updateMutate(
{
data: {
first_name: data.firstName,
last_name: data.lastName,
},
},
{
onSuccess(data) {
console.log(data)
setCredentials({
accessToken: accessToken,
refreshToken: refreshToken,
firstName: data.first_name,
lastName: data.last_name,
email: email,
})
setIsLoading(false)
navigate('/')
},
onError: (err) => {
console.log('ERROR')
console.log(err)
},
}
)
}

return (
<Layout fixed>
{/* ===== Top Heading ===== */}
<Layout.Header>
<Search />
<div className='ml-auto flex items-center space-x-4'>
<ThemeSwitch />
<UserNav />
</div>
</Layout.Header>

<Layout.Body className='flex flex-col'>
<div className='container grid h-svh flex-col items-center justify-center bg-primary-foreground lg:max-w-none lg:px-0'>
<div className='mx-auto flex w-full flex-col justify-center space-y-2 sm:w-[480px] lg:p-8'>
<Card className='p-6'>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<div className='grid gap-2'>
<FormField
control={form.control}
name='firstName'
render={({ field }) => (
<FormItem className='space-y-1'>
<FormLabel>First Name</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name='lastName'
render={({ field }) => (
<FormItem className='space-y-1'>
<FormLabel>Last Name</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>

<Button className='mt-2' loading={isLoading}>
Save
</Button>

<div className='relative my-2'>
<div className='absolute inset-0 flex items-center'>
<span className='w-full border-t' />
</div>
</div>
</div>
</form>
</Form>
</Card>
</div>
</div>
</Layout.Body>
</Layout>
)
}
File renamed without changes.
7 changes: 1 addition & 6 deletions frontend/src/pages/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,10 @@ export default function Settings() {
}

const sidebarNavItems = [
{
title: 'Profile',
icon: <IconUser size={18} />,
href: '/settings',
},
{
title: 'Account',
icon: <IconTool size={18} />,
href: '/settings/account',
href: '/settings/',
},
{
title: 'Appearance',
Expand Down
13 changes: 0 additions & 13 deletions frontend/src/pages/settings/profile/index.tsx

This file was deleted.

19 changes: 5 additions & 14 deletions frontend/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import UnauthorisedError from './pages/errors/unauthorised-error.tsx'
import { useUserStore } from './store/user-store.ts'
import * as React from 'react'
import Settings from './pages/settings/index.tsx'
import Profile from './pages/profile/index.tsx'
import Index from './pages/index/index.tsx'
const ProtectedRoute: React.FC<{ children: React.ReactNode }> = ({
children,
Expand Down Expand Up @@ -80,7 +81,10 @@ const router = createBrowserRouter([
Component: (await import('@/components/coming-soon')).default,
}),
},

{
path: 'profile',
element: <ProtectedRoute>{<Profile />}</ProtectedRoute>,
},
{
path: 'settings',
element: <ProtectedRoute>{<Settings />}</ProtectedRoute>,
Expand All @@ -91,19 +95,6 @@ const router = createBrowserRouter([
children: [
{
index: true,
lazy: async () => ({
Component: (await import('./pages/settings/profile/index.tsx'))
.default,
}),
},
{
path: 'account',
lazy: async () => ({
Component: (await import('./pages/settings/account/index.tsx'))
.default,
}),
},
{
path: 'appearance',
lazy: async () => ({
Component: (await import('./pages/settings/appearance/index.tsx'))
Expand Down

0 comments on commit a048c10

Please sign in to comment.