Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ _Link to the backend repo:_ https://github.com/chertik77/TaskPro-backend

## Languages and Tools

![Languages and Tools](https://skills.syvixor.com/api/icons?i=ts,react,dndkit,radixui,tanstack,stanjs,axios,datefns,reactdatepicker,reacthookform,valibot,tailwind,tailwindmerge,commitlint,eslint,prettier,githubactions,fsd,yarn,vercel,vite,vscode,figma&perline=8)
![Languages and Tools](https://skills.syvixor.com/api/icons?i=ts,react,dndkit,radixui,tanstack,axios,datefns,reactdatepicker,reacthookform,valibot,tailwind,tailwindmerge,commitlint,eslint,prettier,githubactions,fsd,yarn,vercel,vite,vscode,figma&perline=8)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@radix-ui/react-select": "^2.2.5",
"@radix-ui/react-slot": "^1.2.3",
"@tanstack/react-query": "^5.85.0",
"@tanstack/react-query-devtools": "^5.85.3",
"@tanstack/react-router": "1.131.8",
"@vercel/analytics": "^1.5.0",
"@vercel/speed-insights": "^1.2.0",
Expand Down
21 changes: 12 additions & 9 deletions src/app/providers/RouteProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { RouterProvider as TanStackRouterProvider } from '@tanstack/react-router'
import { useQueryClient } from '@tanstack/react-query'
import { RouterProvider as TanstackRouterProvider } from '@tanstack/react-router'

import { useSessionStore } from '@/entities/session'
import { sessionService } from '@/entities/session'
import { userQueries } from '@/entities/user'

import { attachInternalApiMemoryStorage } from '@/shared/api'
import { router } from '@/shared/lib'

export const RouterProvider = () => {
const session = useSessionStore()
const queryClient = useQueryClient()

return (
<TanStackRouterProvider
router={router}
context={{ session }}
/>
)
attachInternalApiMemoryStorage({
refreshTokens: sessionService.refreshTokens,
logout: () => queryClient.resetQueries({ queryKey: userQueries.current() })
})

return <TanstackRouterProvider router={router} />
}
10 changes: 3 additions & 7 deletions src/app/providers/ToastProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { Toaster } from 'sonner'

import { useSessionStore } from '@/entities/session'

import { useTabletAndBelowMediaQuery } from '@/shared/lib'

export const ToastProvider = () => {
const {
user: { theme }
} = useSessionStore()

const isTabletAndBelow = useTabletAndBelowMediaQuery()

// const { data: user } = useQuery(userQueries.me())

return (
<Toaster
position={isTabletAndBelow ? 'top-center' : 'bottom-right'}
richColors
closeButton
duration={5000}
theme={theme === 'dark' ? 'dark' : 'light'}
// theme={user?.theme === 'dark' ? 'dark' : 'light'}
className='text-balance'
/>
)
Expand Down
51 changes: 27 additions & 24 deletions src/app/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
import { createRootRouteWithContext, Outlet } from '@tanstack/react-router'
import type { QueryClient } from '@tanstack/react-query'

import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import {
createRootRouteWithContext,
Outlet,
redirect
} from '@tanstack/react-router'
import { Analytics } from '@vercel/analytics/react'
import { SpeedInsights } from '@vercel/speed-insights/react'

import { sessionService, useSessionStore } from '@/entities/session'

import { attachInternalApiMemoryStorage } from '@/shared/api'
import { userQueries } from '@/entities/user'

type RouterContext = {
session: ReturnType<typeof useSessionStore>
queryClient: QueryClient
}

const RootRoute = () => {
const { tokens, setTokens, logout } = useSessionStore()

attachInternalApiMemoryStorage({
accessToken: tokens.accessToken,
refreshToken: tokens.refreshToken,
refreshTokens: sessionService.refreshTokens,
setTokens,
logout
})

return (
<>
<Analytics />
<SpeedInsights />
<Outlet />
</>
)
}
const RootRoute = () => (
<>
<Analytics />
<SpeedInsights />
<ReactQueryDevtools />
<Outlet />
</>
)

export const Route = createRootRouteWithContext<RouterContext>()({
beforeLoad: async ({ context: { queryClient } }) => {
try {
const user = await queryClient.fetchQuery(userQueries.me())

if (user) throw redirect({ to: '/dashboard' })
} catch {
return
}
},
component: RootRoute
})
8 changes: 6 additions & 2 deletions src/app/routes/auth/_auth-layout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { createFileRoute, Outlet, redirect } from '@tanstack/react-router'

import { userQueries } from '@/entities/user'

import { AuthNavigation } from '@/widgets/auth-navigation'

export const Route = createFileRoute('/auth/_auth-layout')({
beforeLoad: ({ context: { session } }) => {
if (session.isAuthenticated) throw redirect({ to: '/dashboard' })
beforeLoad: ({ context: { queryClient } }) => {
const isAuthenticated = queryClient.getQueryData(userQueries.current())

if (isAuthenticated) throw redirect({ to: '/dashboard' })
},
component: () => (
<div className='bg-soft-green flex h-dvh items-center justify-center'>
Expand Down
12 changes: 0 additions & 12 deletions src/app/routes/auth/google.callback.tsx

This file was deleted.

8 changes: 6 additions & 2 deletions src/app/routes/dashboard/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { DashboardPage } from '@/pages/dashboard'
import { createFileRoute, redirect } from '@tanstack/react-router'

import { userQueries } from '@/entities/user'

export const Route = createFileRoute('/dashboard')({
beforeLoad: ({ context: { session } }) => {
if (!session.isAuthenticated) throw redirect({ to: '/' })
beforeLoad: ({ context: { queryClient } }) => {
const isAuthenticated = queryClient.getQueryData(userQueries.current())

if (!isAuthenticated) throw redirect({ to: '/' })
},
component: DashboardPage
})
8 changes: 6 additions & 2 deletions src/app/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { WelcomePage } from '@/pages/welcome'
import { createFileRoute, redirect } from '@tanstack/react-router'

import { userQueries } from '@/entities/user'

export const Route = createFileRoute('/')({
beforeLoad: ({ context: { session } }) => {
if (session.isAuthenticated) throw redirect({ to: '/dashboard' })
beforeLoad: ({ context: { queryClient } }) => {
const isAuthenticated = queryClient.getQueryData(userQueries.current())

if (isAuthenticated) throw redirect({ to: '/dashboard' })
},
component: WelcomePage
})
6 changes: 2 additions & 4 deletions src/entities/board/ui/FormBgImageSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ControllerRenderProps, FieldValues } from 'react-hook-form'

import { RadioGroup, RadioGroupItem } from '@radix-ui/react-radio-group'

import { useSessionStore } from '@/entities/session/@x/board'
import { useMe } from '@/entities/user/@x/board'

import { FormControl, FormItem } from '@/shared/ui'

Expand All @@ -12,9 +12,7 @@ export const FormBgImageSelector = <T extends FieldValues>({
value,
onChange
}: ControllerRenderProps<T>) => {
const {
user: { theme }
} = useSessionStore()
const { theme } = useMe()

return (
<FormControl>
Expand Down
1 change: 0 additions & 1 deletion src/entities/session/@x/board.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/entities/session/api/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@ export const InitiateGoogleResponseDtoSchema = v.object({
redirectUrl: v.pipe(v.string(), v.url())
})

export const GoogleSigninDtoSchema = v.object({
code: v.pipe(v.string(), v.minLength(1)),
state: v.pipe(v.string(), v.minLength(1))
})

export const RefreshTokenDtoSchema = v.object({
refreshToken: v.pipe(v.string(), v.minLength(1))
})

export const SessionResponseDtoSchema = v.object({
accessToken: v.string(),
refreshToken: v.string(),
user: v.lazy(() => UserDtoSchema)
})

export const TokensDtoSchema = v.omit(SessionResponseDtoSchema, ['user'])
1 change: 0 additions & 1 deletion src/entities/session/api/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class SessionApiEndpoints {
refresh = `${this.baseUrl}/refresh`
logout = `${this.baseUrl}/logout`
googleInitiate = `${this.baseUrl}/google/initiate`
googleCallback = `${this.baseUrl}/google/callback`
}

export const sessionApiEndpoints = new SessionApiEndpoints()
40 changes: 6 additions & 34 deletions src/entities/session/api/service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import type {
GoogleSigninDto,
RefreshTokenDto,
SigninDto,
SignupDto
} from './types'
import type { SigninDto, SignupDto } from './types'

import { parse } from 'valibot'

import { axiosInstance } from '@/shared/api'

import {
GoogleSigninDtoSchema,
InitiateGoogleResponseDtoSchema,
RefreshTokenDtoSchema,
SessionResponseDtoSchema,
SigninDtoSchema,
SignupDtoSchema,
TokensDtoSchema
SignupDtoSchema
} from './contracts'
import { sessionApiEndpoints } from './endpoints'

Expand Down Expand Up @@ -49,37 +41,17 @@ export const sessionService = {
},

async initiateGoogleSignin() {
const response = await axiosInstance.get(sessionApiEndpoints.googleInitiate)

const parsedData = parse(InitiateGoogleResponseDtoSchema, response.data)

return parsedData
},

async signinWithGoogle(data: GoogleSigninDto) {
const googleSigninDto = parse(GoogleSigninDtoSchema, data)

const response = await axiosInstance.post(
sessionApiEndpoints.googleCallback,
googleSigninDto
sessionApiEndpoints.googleInitiate
)

const parsedData = parse(SessionResponseDtoSchema, response.data)
const parsedData = parse(InitiateGoogleResponseDtoSchema, response.data)

return parsedData
},

async refreshTokens(data: RefreshTokenDto) {
const refreshTokenDto = parse(RefreshTokenDtoSchema, data)

const response = await axiosInstance.post(
sessionApiEndpoints.refresh,
refreshTokenDto
)

const parsedData = parse(TokensDtoSchema, response.data)

return parsedData
async refreshTokens() {
await axiosInstance.post(sessionApiEndpoints.refresh)
},

async logout() {
Expand Down
8 changes: 1 addition & 7 deletions src/entities/session/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import type { InferOutput } from 'valibot'
import type {
GoogleSigninDtoSchema,
InitiateGoogleResponseDtoSchema,
RefreshTokenDtoSchema,
SessionResponseDtoSchema,
SigninDtoSchema,
SignupDtoSchema,
TokensDtoSchema
SignupDtoSchema
} from './contracts'

export type SessionResponseDto = InferOutput<typeof SessionResponseDtoSchema>
export type TokensDto = InferOutput<typeof TokensDtoSchema>
export type SigninDto = InferOutput<typeof SigninDtoSchema>
export type SignupDto = InferOutput<typeof SignupDtoSchema>
export type GoogleSigninDto = InferOutput<typeof GoogleSigninDtoSchema>
export type RefreshTokenDto = InferOutput<typeof RefreshTokenDtoSchema>
export type InitiateGoogleResponseDto = InferOutput<
typeof InitiateGoogleResponseDtoSchema
>
3 changes: 0 additions & 3 deletions src/entities/session/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export * as SessionTypes from './model/types'
export * as SessionDtoTypes from './api/types'
export * as SessionContracts from './model/contracts'
export * as SessionDtoContracts from './api/contracts'
export { sessionService } from './api/service'
export { useSessionStore, getSessionStore, sessionActions } from './model/store'
6 changes: 0 additions & 6 deletions src/entities/session/model/contracts.ts

This file was deleted.

41 changes: 0 additions & 41 deletions src/entities/session/model/store.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/entities/session/model/types.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/entities/user/@x/board.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useMe } from '../model/useMe'
Loading