1
1
import type React from "react"
2
- import { useEffect , useState } from "react"
2
+ import { useState } from "react"
3
3
import moment from "moment"
4
- import { useAppDispatch , useAppSelector } from "@/core/redux/hooks"
4
+ import { useAppDispatch } from "@/core/redux/hooks"
5
5
import { useToast } from "@/components/ui/use-toast"
6
6
import { Button } from "@/components/ui/button"
7
7
import { performRefresh } from "@/core/redux/slices/clientsSlice"
@@ -11,99 +11,51 @@ import DialogWrapper from "./DialogWrapper"
11
11
import { Table , TableBody , TableCell , TableHead , TableHeader , TableRow } from "@/components/ui/table"
12
12
import { Skeleton } from "@/components/ui/skeleton"
13
13
import { Edit , Copy , Info , Plus } from "lucide-react"
14
- import { users } from "@/core/apis/users"
15
14
import type { Client } from "@/app/types/clients"
16
15
import { settings } from "@/core/apis/settings"
16
+ import { useUserClients } from "@/core/hooks/useUserClients"
17
17
18
18
19
19
const UserClientsTable = ( ) => {
20
20
const dispatch = useAppDispatch ( )
21
21
const { toast } = useToast ( )
22
22
const [ showInfoModal , setShowInfoModal ] = useState ( false )
23
- const [ isLoading , setIsLoading ] = useState ( false )
24
23
const [ isLoadingToken , setIsLoadingToken ] = useState ( false )
25
24
const [ isLoadingActivationRequest , setIsLoadingActivationRequest ] = useState ( false )
26
25
const [ openEditForm , setOpenEditForm ] = useState ( false )
27
26
const [ openCreateForm , setOpenCreateForm ] = useState ( false )
28
27
const [ selectedClient , setSelectedClient ] = useState ( { } as Client )
29
- const userInfo = useAppSelector ( ( state ) => state . user . userDetails )
30
- const [ clients , setClients ] = useState < Client [ ] > ( [ ] )
31
- const [ clientsDetails , setClientsDetails ] = useState < Client [ ] > ( [ ] )
32
- const [ currentPage , setCurrentPage ] = useState ( 1 )
33
- const itemsPerPage = 4
28
+ const { clients, isLoading, error } = useUserClients ( ) ;
29
+ console . log ( clients )
34
30
35
- const fetchClients = async ( ) => {
36
- setIsLoading ( true )
37
- try {
38
- const res = await users . getUserDetails ( userInfo ?. _id || "" )
39
- if ( res ) {
40
- dispatch ( { type : "ADD_CLIENTS" , payload : res . users [ 0 ] . clients } )
41
- setCurrentPage ( 1 )
42
- }
43
- setClients ( res . users [ 0 ] . clients )
44
- } catch ( error ) {
45
- toast ( {
46
- title : "Error" ,
47
- description : "Failed to fetch user details" ,
48
- variant : "destructive" ,
49
- } )
50
- } finally {
51
- setIsLoading ( false )
52
- }
53
- }
54
-
55
- const fetchClientDetails = async ( ) => {
56
- setIsLoading ( true )
57
- try {
58
- const response = await settings . getUserClientsApi ( userInfo ?. _id || "" )
59
- if ( response ) {
60
- dispatch ( { type : "ADD_CLIENTS_DETAILS" , payload : response } )
61
- }
62
- setClientsDetails ( response . clients )
63
- } catch ( error ) {
64
- console . error ( error )
65
- toast ( {
66
- title : "Error" ,
67
- description : "Failed to fetch client details" ,
68
- variant : "destructive" ,
69
- } )
70
- } finally {
71
- setIsLoading ( false )
72
- }
73
- }
74
-
75
- useEffect ( ( ) => {
76
- fetchClients ( )
77
- fetchClientDetails ( )
78
- } , [ userInfo ?. _id , dispatch ] )
79
31
80
32
const hasAccessToken = ( clientId : string ) : boolean => {
81
33
const client =
82
- Array . isArray ( clientsDetails ) && clientsDetails
83
- ? clientsDetails ?. find ( ( client : Client ) => client . _id === clientId )
34
+ Array . isArray ( clients ) && clients
35
+ ? clients ?. find ( ( client : Client ) => client . _id === clientId )
84
36
: undefined
85
37
return client ?. access_token ?. token ? true : false
86
38
}
87
39
88
40
const getClientToken = ( clientID : string ) => {
89
41
const client =
90
- Array . isArray ( clientsDetails ) && clientsDetails
91
- ? clientsDetails ?. find ( ( client : Client ) => client . _id === clientID )
42
+ Array . isArray ( clients ) && clients
43
+ ? clients ?. find ( ( client : Client ) => client . _id === clientID )
92
44
: undefined
93
45
return client && client . access_token && client . access_token . token
94
46
}
95
47
const getClientTokenExpiryDate = ( clientID : string ) => {
96
48
const client =
97
- Array . isArray ( clientsDetails ) && clientsDetails
98
- ? clientsDetails ?. find ( ( client : Client ) => client . _id === clientID )
49
+ Array . isArray ( clients ) && clients
50
+ ? clients ?. find ( ( client : Client ) => client . _id === clientID )
99
51
: undefined
100
52
return client && client . access_token && client . access_token . expires
101
53
}
102
54
103
55
const getClientTokenCreateAt = ( clientID : string ) => {
104
56
const client =
105
- Array . isArray ( clientsDetails ) && clientsDetails
106
- ? clientsDetails ?. find ( ( client : Client ) => client . _id === clientID )
57
+ Array . isArray ( clients ) && clients
58
+ ? clients ?. find ( ( client : Client ) => client . _id === clientID )
107
59
: undefined
108
60
return client && client . access_token && client . access_token . createdAt
109
61
}
@@ -117,6 +69,7 @@ const UserClientsTable = () => {
117
69
} else {
118
70
try {
119
71
const response = await settings . generateTokenApi ( res )
72
+ await queryClient . invalidateQueries ( { queryKey : [ "clients" ] } ) ;
120
73
if ( response ) {
121
74
toast ( {
122
75
title : "Success" ,
@@ -168,10 +121,6 @@ const UserClientsTable = () => {
168
121
return Array . isArray ( client . ip_addresses ) ? client . ip_addresses . join ( ", " ) : client . ip_addresses
169
122
}
170
123
171
- const handleClientCreated = ( ) => {
172
- fetchClients ( )
173
- fetchClientDetails ( )
174
- }
175
124
176
125
return (
177
126
< div className = "overflow-x-auto" >
@@ -202,7 +151,6 @@ const UserClientsTable = () => {
202
151
</ TableRow >
203
152
) : clients ?. length > 0 ? (
204
153
clients
205
- . slice ( ( currentPage - 1 ) * itemsPerPage , currentPage * itemsPerPage )
206
154
. map ( ( client : Client , index : number ) => (
207
155
< TableRow key = { index } >
208
156
< TableCell className = "font-medium" > { client ?. name } </ TableCell >
@@ -292,12 +240,10 @@ const UserClientsTable = () => {
292
240
open = { openEditForm }
293
241
onClose = { ( ) => setOpenEditForm ( false ) }
294
242
data = { selectedClient }
295
- onClientUpdated = { handleClientCreated }
296
243
/>
297
244
< CreateClientForm
298
245
open = { openCreateForm }
299
246
onClose = { ( ) => setOpenCreateForm ( false ) }
300
- onClientCreated = { handleClientCreated }
301
247
/>
302
248
< DialogWrapper
303
249
open = { showInfoModal }
0 commit comments