Skip to content

Commit eb0e1b1

Browse files
committed
feat: save items per page to localStorage and remove i18n cookies
1 parent d21c492 commit eb0e1b1

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

dashboard/src/components/admins/AdminsTable.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent,
1010
import { cn } from '@/lib/utils'
1111
import useDirDetection from '@/hooks/use-dir-detection'
1212
import { Checkbox } from '@/components/ui/checkbox.tsx'
13+
import { getAdminsPerPageLimitSize, setAdminsPerPageLimitSize } from '@/utils/userPreferenceStorage'
1314

1415
interface AdminFilters {
1516
sort: string
@@ -105,7 +106,7 @@ const ResetUsersUsageConfirmationDialog = ({ adminUsername, isOpen, onClose, onC
105106
export default function AdminsTable({ onEdit, onDelete, onToggleStatus, onResetUsage }: AdminsTableProps) {
106107
const { t } = useTranslation()
107108
const [currentPage, setCurrentPage] = useState(0)
108-
const [itemsPerPage, setItemsPerPage] = useState(10)
109+
const [itemsPerPage, setItemsPerPage] = useState(getAdminsPerPageLimitSize())
109110
const [isChangingPage, setIsChangingPage] = useState(false)
110111
const [filters, setFilters] = useState<AdminFilters>({
111112
sort: '-created_at',
@@ -206,6 +207,9 @@ export default function AdminsTable({ onEdit, onDelete, onToggleStatus, onResetU
206207
setIsChangingPage(true)
207208
setItemsPerPage(value)
208209
setCurrentPage(0) // Reset to first page when items per page changes
210+
211+
// Save to localStorage
212+
setAdminsPerPageLimitSize(value.toString())
209213

210214
try {
211215
// Immediate refetch without delay

dashboard/src/components/users/users-table.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import useDirDetection from '@/hooks/use-dir-detection'
55
import { UseEditFormValues } from '@/pages/_dashboard.users'
66
import { useGetUsers, UserResponse } from '@/service/api'
77
import { useAdmin } from '@/hooks/use-admin'
8-
import { getUsersPerPageLimitSize } from '@/utils/userPreferenceStorage'
8+
import { getUsersPerPageLimitSize, setUsersPerPageLimitSize } from '@/utils/userPreferenceStorage'
99
import { useQueryClient } from '@tanstack/react-query'
1010
import { memo, useCallback, useEffect, useState } from 'react'
1111
import { useForm } from 'react-hook-form'
@@ -202,6 +202,9 @@ const UsersTable = memo(() => {
202202
setIsChangingPage(true)
203203
setItemsPerPage(value)
204204
setCurrentPage(0) // Reset to first page when items per page changes
205+
206+
// Save to localStorage
207+
setUsersPerPageLimitSize(value.toString())
205208

206209
// Remove async/await and setTimeout for instant response
207210
setIsChangingPage(false)

dashboard/src/locales/i18n.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ i18n
2121
},
2222
load: 'languageOnly',
2323
detection: {
24-
caches: ['localStorage', 'sessionStorage', 'cookie'],
24+
caches: ['localStorage', 'sessionStorage'],
2525
},
2626
backend: {
2727
loadPath: joinURL(import.meta.env.BASE_URL, `statics/locales/{{lng}}.json`),
2828
},
2929
},
30-
function (err, t) {
30+
function (err) {
3131
if (err) {
3232
console.error('i18next initialization error:', err)
3333
}
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
const NUM_USERS_PER_PAGE_LOCAL_STORAGE_KEY = 'pasargaurd-num-users-per-page'
2-
const NUM_USERS_PER_PAGE_DEFAULT = 10
3-
export const getUsersPerPageLimitSize = () => {
4-
const numUsersPerPage = (typeof localStorage !== 'undefined' && localStorage.getItem(NUM_USERS_PER_PAGE_LOCAL_STORAGE_KEY)) || NUM_USERS_PER_PAGE_DEFAULT.toString() // this catches `null` values
5-
return parseInt(numUsersPerPage) || NUM_USERS_PER_PAGE_DEFAULT // this catches NaN values
2+
const NUM_ADMINS_PER_PAGE_LOCAL_STORAGE_KEY = 'pasargaurd-num-admins-per-page'
3+
const NUM_ITEMS_PER_PAGE_DEFAULT = 10
4+
5+
// Generic function for any table type
6+
export const getItemsPerPageLimitSize = (tableType: 'users' | 'admins' = 'users') => {
7+
const storageKey = tableType === 'users' ? NUM_USERS_PER_PAGE_LOCAL_STORAGE_KEY : NUM_ADMINS_PER_PAGE_LOCAL_STORAGE_KEY
8+
const numItemsPerPage = (typeof localStorage !== 'undefined' && localStorage.getItem(storageKey)) || NUM_ITEMS_PER_PAGE_DEFAULT.toString() // this catches `null` values
9+
return parseInt(numItemsPerPage) || NUM_ITEMS_PER_PAGE_DEFAULT // this catches NaN values
610
}
711

8-
export const setUsersPerPageLimitSize = (value: string) => {
9-
return typeof localStorage !== 'undefined' && localStorage.setItem(NUM_USERS_PER_PAGE_LOCAL_STORAGE_KEY, value)
12+
export const setItemsPerPageLimitSize = (value: string, tableType: 'users' | 'admins' = 'users') => {
13+
const storageKey = tableType === 'users' ? NUM_USERS_PER_PAGE_LOCAL_STORAGE_KEY : NUM_ADMINS_PER_PAGE_LOCAL_STORAGE_KEY
14+
return typeof localStorage !== 'undefined' && localStorage.setItem(storageKey, value)
1015
}
16+
17+
// Legacy functions for backward compatibility
18+
export const getUsersPerPageLimitSize = () => getItemsPerPageLimitSize('users')
19+
export const setUsersPerPageLimitSize = (value: string) => setItemsPerPageLimitSize(value, 'users')
20+
21+
export const getAdminsPerPageLimitSize = () => getItemsPerPageLimitSize('admins')
22+
export const setAdminsPerPageLimitSize = (value: string) => setItemsPerPageLimitSize(value, 'admins')

0 commit comments

Comments
 (0)