|
1 |
| -'use client' |
2 |
| - |
| 1 | +import React, { ReactNode, useState, useEffect, useMemo } from 'react' |
| 2 | +import { usePathname, useRouter } from 'next/navigation' |
| 3 | +import { useAppSelector } from '@/store/hooks' |
| 4 | +import { getRouteStatus } from '@/lib/getRouteStatus' |
| 5 | +import { getUserPermissions } from '@/lib/getUserPermissions' |
| 6 | +import { selectToken } from '@/modules/auth/authSlice' |
3 | 7 | import { LoadingSpinner } from '@/components/ui/loadingSpinner'
|
4 | 8 | import {
|
5 | 9 | ValidateRouteContext,
|
6 | 10 | ValidateRouteContextType,
|
7 | 11 | } from '@/contexts/validateRouteContext'
|
8 |
| -import { getRouteStatus } from '@/lib/getRouteStatus' |
9 |
| -import { getUserPermissions } from '@/lib/getUserPermissions' |
10 |
| -import { selectToken } from '@/modules/auth/authSlice' |
11 |
| -import { useAppSelector } from '@/store/hooks' |
12 |
| -import { usePathname, useRouter } from 'next/navigation' |
13 |
| -import { ReactNode, useEffect, useMemo, useState } from 'react' |
14 |
| - |
15 |
| -import { |
16 |
| - Dialog, |
17 |
| - DialogContent, |
18 |
| - DialogDescription, |
19 |
| - DialogHeader, |
20 |
| - DialogTitle, |
21 |
| -} from '@/components/ui/dialog' |
22 |
| -import { useTranslation } from 'react-i18next' |
23 | 12 |
|
24 | 13 | export const ValidateRouteProvider = ({
|
25 | 14 | children,
|
26 | 15 | }: {
|
27 | 16 | children: ReactNode
|
28 | 17 | }): React.JSX.Element => {
|
29 |
| - const { t } = useTranslation() |
30 | 18 | const router = useRouter()
|
31 | 19 | const pathname = usePathname()
|
32 | 20 | const token = useAppSelector(selectToken)
|
33 |
| - |
34 | 21 | const [loading, setLoading] = useState(true)
|
35 |
| - const [showModal, setShowModal] = useState(false) |
36 |
| - const [countdown, setCountdown] = useState(3) |
37 |
| - |
38 | 22 | const { isProtected, route, requiredPermission } = getRouteStatus(pathname)
|
39 | 23 | const userPermissions = token ? getUserPermissions(token) : []
|
40 | 24 |
|
@@ -62,58 +46,17 @@ export const ValidateRouteProvider = ({
|
62 | 46 | setLoading(false)
|
63 | 47 | }
|
64 | 48 | } else if (isProtected) {
|
65 |
| - setShowModal(true) |
| 49 | + router.replace('/login') |
66 | 50 | } else {
|
67 | 51 | setLoading(false)
|
68 | 52 | }
|
69 |
| - }, [token, pathname, isProtected, userHasPermission, router]) |
70 |
| - |
71 |
| - useEffect(() => { |
72 |
| - if (!showModal) return |
| 53 | + }, [token, pathname, isProtected, userHasPermission, router, loading]) |
73 | 54 |
|
74 |
| - if (showModal) { |
75 |
| - setCountdown(3) |
76 |
| - } |
77 |
| - |
78 |
| - const interval = setInterval(() => { |
79 |
| - setCountdown((prev) => { |
80 |
| - if (prev === 1) { |
81 |
| - clearInterval(interval) |
82 |
| - setShowModal(false) |
83 |
| - router.replace('/login') |
84 |
| - } |
85 |
| - return prev - 1 |
86 |
| - }) |
87 |
| - }, 1000) |
88 |
| - |
89 |
| - return () => clearInterval(interval) |
90 |
| - }, [showModal, router]) |
91 |
| - |
92 |
| - if (loading || showModal) { |
| 55 | + if (loading) { |
93 | 56 | return (
|
94 |
| - <> |
95 |
| - <Dialog open={showModal}> |
96 |
| - <DialogContent |
97 |
| - className="sm:max-w-md" |
98 |
| - showClose={false} |
99 |
| - onEscapeKeyDown={(e) => e.preventDefault()} |
100 |
| - onPointerDownOutside={(e) => e.preventDefault()} |
101 |
| - > |
102 |
| - <DialogHeader> |
103 |
| - <DialogTitle>{t('sessionExpired.title')}</DialogTitle> |
104 |
| - <DialogDescription> |
105 |
| - {t('sessionExpired.description')} |
106 |
| - <br /> |
107 |
| - {t('sessionExpired.countdown', { count: countdown })} |
108 |
| - </DialogDescription> |
109 |
| - </DialogHeader> |
110 |
| - </DialogContent> |
111 |
| - </Dialog> |
112 |
| - |
113 |
| - <div className="flex items-center justify-center h-screen"> |
114 |
| - <LoadingSpinner /> |
115 |
| - </div> |
116 |
| - </> |
| 57 | + <div className="flex items-center justify-center h-screen"> |
| 58 | + <LoadingSpinner /> |
| 59 | + </div> |
117 | 60 | )
|
118 | 61 | }
|
119 | 62 |
|
|
0 commit comments