1
1
import {
2
- Badge ,
3
- Box ,
4
2
Button ,
5
3
Container ,
6
4
Flex ,
@@ -19,53 +17,52 @@ import { createFileRoute, useNavigate } from "@tanstack/react-router"
19
17
import { useEffect } from "react"
20
18
import { z } from "zod"
21
19
22
- import { type UserPublic , UsersService } from "../../client"
23
- import AddUser from "../../components/Admin/AddUser"
20
+ import { ItemsService } from "../../client"
24
21
import ActionsMenu from "../../components/Common/ActionsMenu"
25
22
import Navbar from "../../components/Common/Navbar"
23
+ import AddItem from "../../components/Items/AddItem"
26
24
27
- const usersSearchSchema = z . object ( {
25
+ const itemsSearchSchema = z . object ( {
28
26
page : z . number ( ) . catch ( 1 ) ,
29
27
} )
30
28
31
- export const Route = createFileRoute ( "/_layout/admin " ) ( {
32
- component : Admin ,
33
- validateSearch : ( search ) => usersSearchSchema . parse ( search ) ,
29
+ export const Route = createFileRoute ( "/_layout/items " ) ( {
30
+ component : Items ,
31
+ validateSearch : ( search ) => itemsSearchSchema . parse ( search ) ,
34
32
} )
35
33
36
34
const PER_PAGE = 5
37
35
38
- function getUsersQueryOptions ( { page } : { page : number } ) {
36
+ function getItemsQueryOptions ( { page } : { page : number } ) {
39
37
return {
40
38
queryFn : ( ) =>
41
- UsersService . readUsers ( { skip : ( page - 1 ) * PER_PAGE , limit : PER_PAGE } ) ,
42
- queryKey : [ "users " , { page } ] ,
39
+ ItemsService . readItems ( { skip : ( page - 1 ) * PER_PAGE , limit : PER_PAGE } ) ,
40
+ queryKey : [ "items " , { page } ] ,
43
41
}
44
42
}
45
43
46
- function UsersTable ( ) {
44
+ function ItemsTable ( ) {
47
45
const queryClient = useQueryClient ( )
48
- const currentUser = queryClient . getQueryData < UserPublic > ( [ "currentUser" ] )
49
46
const { page } = Route . useSearch ( )
50
47
const navigate = useNavigate ( { from : Route . fullPath } )
51
48
const setPage = ( page : number ) =>
52
49
navigate ( { search : ( prev ) => ( { ...prev , page } ) } )
53
50
54
51
const {
55
- data : users ,
52
+ data : items ,
56
53
isPending,
57
54
isPlaceholderData,
58
55
} = useQuery ( {
59
- ...getUsersQueryOptions ( { page } ) ,
56
+ ...getItemsQueryOptions ( { page } ) ,
60
57
placeholderData : ( prevData ) => prevData ,
61
58
} )
62
59
63
- const hasNextPage = ! isPlaceholderData && users ?. data . length === PER_PAGE
60
+ const hasNextPage = ! isPlaceholderData && items ?. data . length === PER_PAGE
64
61
const hasPreviousPage = page > 1
65
62
66
63
useEffect ( ( ) => {
67
64
if ( hasNextPage ) {
68
- queryClient . prefetchQuery ( getUsersQueryOptions ( { page : page + 1 } ) )
65
+ queryClient . prefetchQuery ( getItemsQueryOptions ( { page : page + 1 } ) )
69
66
}
70
67
} , [ page , queryClient , hasNextPage ] )
71
68
@@ -75,11 +72,10 @@ function UsersTable() {
75
72
< Table size = { { base : "sm" , md : "md" } } >
76
73
< Thead >
77
74
< Tr >
78
- < Th width = "20%" > Full name</ Th >
79
- < Th width = "50%" > Email</ Th >
80
- < Th width = "10%" > Role</ Th >
81
- < Th width = "10%" > Status</ Th >
82
- < Th width = "10%" > Actions</ Th >
75
+ < Th > ID</ Th >
76
+ < Th > Title</ Th >
77
+ < Th > Description</ Th >
78
+ < Th > Actions</ Th >
83
79
</ Tr >
84
80
</ Thead >
85
81
{ isPending ? (
@@ -94,42 +90,21 @@ function UsersTable() {
94
90
</ Tbody >
95
91
) : (
96
92
< Tbody >
97
- { users ?. data . map ( ( user ) => (
98
- < Tr key = { user . id } >
93
+ { items ?. data . map ( ( item ) => (
94
+ < Tr key = { item . id } opacity = { isPlaceholderData ? 0.5 : 1 } >
95
+ < Td > { item . id } </ Td >
96
+ < Td isTruncated maxWidth = "150px" >
97
+ { item . title }
98
+ </ Td >
99
99
< Td
100
- color = { ! user . full_name ? "ui.dim" : "inherit" }
100
+ color = { ! item . description ? "ui.dim" : "inherit" }
101
101
isTruncated
102
102
maxWidth = "150px"
103
103
>
104
- { user . full_name || "N/A" }
105
- { currentUser ?. id === user . id && (
106
- < Badge ml = "1" colorScheme = "teal" >
107
- You
108
- </ Badge >
109
- ) }
110
- </ Td >
111
- < Td isTruncated maxWidth = "150px" >
112
- { user . email }
104
+ { item . description || "N/A" }
113
105
</ Td >
114
- < Td > { user . is_superuser ? "Superuser" : "User" } </ Td >
115
106
< Td >
116
- < Flex gap = { 2 } >
117
- < Box
118
- w = "2"
119
- h = "2"
120
- borderRadius = "50%"
121
- bg = { user . is_active ? "ui.success" : "ui.danger" }
122
- alignSelf = "center"
123
- />
124
- { user . is_active ? "Active" : "Inactive" }
125
- </ Flex >
126
- </ Td >
127
- < Td >
128
- < ActionsMenu
129
- type = "User"
130
- value = { user }
131
- disabled = { currentUser ?. id === user . id ? true : false }
132
- />
107
+ < ActionsMenu type = { "Item" } value = { item } />
133
108
</ Td >
134
109
</ Tr >
135
110
) ) }
@@ -156,15 +131,15 @@ function UsersTable() {
156
131
)
157
132
}
158
133
159
- function Admin ( ) {
134
+ function Items ( ) {
160
135
return (
161
136
< Container maxW = "full" >
162
137
< Heading size = "lg" textAlign = { { base : "center" , md : "left" } } pt = { 12 } >
163
- Users Management
138
+ Items Management
164
139
</ Heading >
165
140
166
- < Navbar type = { "User " } addModalAs = { AddUser } />
167
- < UsersTable />
141
+ < Navbar type = { "Item " } addModalAs = { AddItem } />
142
+ < ItemsTable />
168
143
</ Container >
169
144
)
170
- }
145
+ }
0 commit comments