mirror of
https://github.com/nikdoof/pocket-id.git
synced 2025-12-14 07:12:19 +00:00
feat: add sorting for tables
This commit is contained in:
@@ -5,26 +5,44 @@
|
||||
import * as Select from '$lib/components/ui/select';
|
||||
import * as Table from '$lib/components/ui/table/index.js';
|
||||
import Empty from '$lib/icons/empty.svelte';
|
||||
import type { Paginated } from '$lib/types/pagination.type';
|
||||
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
|
||||
import { debounced } from '$lib/utils/debounce-util';
|
||||
import { cn } from '$lib/utils/style';
|
||||
import { ChevronDown } from 'lucide-svelte';
|
||||
import type { Snippet } from 'svelte';
|
||||
import Button from './ui/button/button.svelte';
|
||||
|
||||
let {
|
||||
items,
|
||||
requestOptions = $bindable(),
|
||||
selectedIds = $bindable(),
|
||||
withoutSearch = false,
|
||||
fetchItems,
|
||||
defaultSort,
|
||||
onRefresh,
|
||||
columns,
|
||||
rows
|
||||
}: {
|
||||
items: Paginated<T>;
|
||||
requestOptions?: SearchPaginationSortRequest;
|
||||
selectedIds?: string[];
|
||||
withoutSearch?: boolean;
|
||||
fetchItems: (search: string, page: number, limit: number) => Promise<Paginated<T>>;
|
||||
columns: (string | { label: string; hidden?: boolean })[];
|
||||
defaultSort?: { column: string; direction: 'asc' | 'desc' };
|
||||
onRefresh: (requestOptions: SearchPaginationSortRequest) => Promise<Paginated<T>>;
|
||||
columns: { label: string; hidden?: boolean; sortColumn?: string }[];
|
||||
rows: Snippet<[{ item: T }]>;
|
||||
} = $props();
|
||||
|
||||
if (!requestOptions) {
|
||||
requestOptions = {
|
||||
search: '',
|
||||
sort: defaultSort,
|
||||
pagination: {
|
||||
page: items.pagination.currentPage,
|
||||
limit: items.pagination.itemsPerPage
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let availablePageSizes: number[] = [10, 20, 50, 100];
|
||||
|
||||
let allChecked = $derived.by(() => {
|
||||
@@ -38,7 +56,8 @@
|
||||
});
|
||||
|
||||
const onSearch = debounced(async (searchValue: string) => {
|
||||
items = await fetchItems(searchValue, 1, items.pagination.itemsPerPage);
|
||||
requestOptions.search = searchValue;
|
||||
onRefresh(requestOptions);
|
||||
}, 300);
|
||||
|
||||
async function onAllCheck(checked: boolean) {
|
||||
@@ -59,11 +78,20 @@
|
||||
}
|
||||
|
||||
async function onPageChange(page: number) {
|
||||
items = await fetchItems('', page, items.pagination.itemsPerPage);
|
||||
requestOptions!.pagination = { limit: items.pagination.itemsPerPage, page };
|
||||
onRefresh(requestOptions!);
|
||||
}
|
||||
|
||||
async function onPageSizeChange(size: number) {
|
||||
items = await fetchItems('', 1, size);
|
||||
requestOptions!.pagination = { limit: size, page: 1 };
|
||||
onRefresh(requestOptions!);
|
||||
}
|
||||
|
||||
async function onSort(column?: string, direction: 'asc' | 'desc' = 'asc') {
|
||||
if (!column) return;
|
||||
|
||||
requestOptions!.sort = { column, direction };
|
||||
onRefresh(requestOptions!);
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -92,11 +120,31 @@
|
||||
</Table.Head>
|
||||
{/if}
|
||||
{#each columns as column}
|
||||
{#if typeof column === 'string'}
|
||||
<Table.Head>{column}</Table.Head>
|
||||
{:else}
|
||||
<Table.Head class={column.hidden ? 'sr-only' : ''}>{column.label}</Table.Head>
|
||||
{/if}
|
||||
<Table.Head class={cn(column.hidden && 'sr-only', column.sortColumn && 'px-0')}>
|
||||
{#if column.sortColumn}
|
||||
<Button
|
||||
variant="ghost"
|
||||
class="flex items-center"
|
||||
on:click={() =>
|
||||
onSort(
|
||||
column.sortColumn,
|
||||
requestOptions.sort?.direction === 'desc' ? 'asc' : 'desc'
|
||||
)}
|
||||
>
|
||||
{column.label}
|
||||
{#if requestOptions.sort?.column === column.sortColumn}
|
||||
<ChevronDown
|
||||
class={cn(
|
||||
'ml-2 h-4 w-4',
|
||||
requestOptions.sort?.direction === 'asc' ? 'rotate-180' : ''
|
||||
)}
|
||||
/>
|
||||
{/if}
|
||||
</Button>
|
||||
{:else}
|
||||
{column.label}
|
||||
{/if}
|
||||
</Table.Head>
|
||||
{/each}
|
||||
</Table.Row>
|
||||
</Table.Header>
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import type { AuditLog } from '$lib/types/audit-log.type';
|
||||
import type { Paginated, PaginationRequest } from '$lib/types/pagination.type';
|
||||
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
|
||||
import APIService from './api-service';
|
||||
|
||||
class AuditLogService extends APIService {
|
||||
async list(pagination?: PaginationRequest) {
|
||||
async list(options?: SearchPaginationSortRequest) {
|
||||
const res = await this.api.get('/audit-logs', {
|
||||
params: pagination
|
||||
params: options
|
||||
});
|
||||
return res.data as Paginated<AuditLog>;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
import type { AuthorizeResponse, OidcClient, OidcClientCreate } from '$lib/types/oidc.type';
|
||||
import type { Paginated, PaginationRequest } from '$lib/types/pagination.type';
|
||||
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
|
||||
import APIService from './api-service';
|
||||
|
||||
class OidcService extends APIService {
|
||||
async authorize(clientId: string, scope: string, callbackURL: string, nonce?: string, codeChallenge?: string, codeChallengeMethod?: string) {
|
||||
async authorize(
|
||||
clientId: string,
|
||||
scope: string,
|
||||
callbackURL: string,
|
||||
nonce?: string,
|
||||
codeChallenge?: string,
|
||||
codeChallengeMethod?: string
|
||||
) {
|
||||
const res = await this.api.post('/oidc/authorize', {
|
||||
scope,
|
||||
nonce,
|
||||
@@ -16,7 +23,14 @@ class OidcService extends APIService {
|
||||
return res.data as AuthorizeResponse;
|
||||
}
|
||||
|
||||
async authorizeNewClient(clientId: string, scope: string, callbackURL: string, nonce?: string, codeChallenge?: string, codeChallengeMethod?: string) {
|
||||
async authorizeNewClient(
|
||||
clientId: string,
|
||||
scope: string,
|
||||
callbackURL: string,
|
||||
nonce?: string,
|
||||
codeChallenge?: string,
|
||||
codeChallengeMethod?: string
|
||||
) {
|
||||
const res = await this.api.post('/oidc/authorize/new-client', {
|
||||
scope,
|
||||
nonce,
|
||||
@@ -29,12 +43,9 @@ class OidcService extends APIService {
|
||||
return res.data as AuthorizeResponse;
|
||||
}
|
||||
|
||||
async listClients(search?: string, pagination?: PaginationRequest) {
|
||||
async listClients(options?: SearchPaginationSortRequest) {
|
||||
const res = await this.api.get('/oidc/clients', {
|
||||
params: {
|
||||
search,
|
||||
...pagination
|
||||
}
|
||||
params: options
|
||||
});
|
||||
return res.data as Paginated<OidcClient>;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Paginated, PaginationRequest } from '$lib/types/pagination.type';
|
||||
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
|
||||
import type {
|
||||
UserGroupCreate,
|
||||
UserGroupWithUserCount,
|
||||
@@ -7,12 +7,9 @@ import type {
|
||||
import APIService from './api-service';
|
||||
|
||||
export default class UserGroupService extends APIService {
|
||||
async list(search?: string, pagination?: PaginationRequest) {
|
||||
async list(options?: SearchPaginationSortRequest) {
|
||||
const res = await this.api.get('/user-groups', {
|
||||
params: {
|
||||
search,
|
||||
...pagination
|
||||
}
|
||||
params: options
|
||||
});
|
||||
return res.data as Paginated<UserGroupWithUserCount>;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
import type { Paginated, PaginationRequest } from '$lib/types/pagination.type';
|
||||
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
|
||||
import type { User, UserCreate } from '$lib/types/user.type';
|
||||
import APIService from './api-service';
|
||||
|
||||
export default class UserService extends APIService {
|
||||
async list(search?: string, pagination?: PaginationRequest) {
|
||||
async list(options?: SearchPaginationSortRequest) {
|
||||
const res = await this.api.get('/users', {
|
||||
params: {
|
||||
search,
|
||||
...pagination
|
||||
}
|
||||
params: options
|
||||
});
|
||||
return res.data as Paginated<User>;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,17 @@ export type PaginationRequest = {
|
||||
limit: number;
|
||||
};
|
||||
|
||||
export type SortRequest = {
|
||||
column: string;
|
||||
direction: "asc" | "desc";
|
||||
};
|
||||
|
||||
export type SearchPaginationSortRequest = {
|
||||
search?: string,
|
||||
pagination?: PaginationRequest;
|
||||
sort?: SortRequest;
|
||||
}
|
||||
|
||||
export type PaginationResponse = {
|
||||
totalPages: number;
|
||||
totalItems: number;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import * as Table from '$lib/components/ui/table';
|
||||
import OIDCService from '$lib/services/oidc-service';
|
||||
import type { OidcClient } from '$lib/types/oidc.type';
|
||||
import type { Paginated, PaginationRequest } from '$lib/types/pagination.type';
|
||||
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
|
||||
import { axiosErrorToast } from '$lib/utils/error-util';
|
||||
import { LucidePencil, LucideTrash } from 'lucide-svelte';
|
||||
import { toast } from 'svelte-sonner';
|
||||
@@ -14,6 +14,7 @@
|
||||
let { clients: initialClients }: { clients: Paginated<OidcClient> } = $props();
|
||||
let clients = $state<Paginated<OidcClient>>(initialClients);
|
||||
let oneTimeLink = $state<string | null>(null);
|
||||
let requestOptions: SearchPaginationSortRequest | undefined = $state();
|
||||
|
||||
$effect(() => {
|
||||
clients = initialClients;
|
||||
@@ -21,12 +22,6 @@
|
||||
|
||||
const oidcService = new OIDCService();
|
||||
|
||||
let pagination = $state<PaginationRequest>({
|
||||
page: 1,
|
||||
limit: 10
|
||||
});
|
||||
let search = $state('');
|
||||
|
||||
async function deleteClient(client: OidcClient) {
|
||||
openConfirmDialog({
|
||||
title: `Delete ${client.name}`,
|
||||
@@ -37,7 +32,7 @@
|
||||
action: async () => {
|
||||
try {
|
||||
await oidcService.removeClient(client.id);
|
||||
clients = await oidcService.listClients(search, pagination);
|
||||
clients = await oidcService.listClients(requestOptions!);
|
||||
toast.success('OIDC client deleted successfully');
|
||||
} catch (e) {
|
||||
axiosErrorToast(e);
|
||||
@@ -46,16 +41,17 @@
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchItems(search: string, page: number, limit: number) {
|
||||
return oidcService.listClients(search, { page, limit });
|
||||
}
|
||||
</script>
|
||||
|
||||
<AdvancedTable
|
||||
items={clients}
|
||||
{fetchItems}
|
||||
columns={['Logo', 'Name', { label: 'Actions', hidden: true }]}
|
||||
{requestOptions}
|
||||
onRefresh={async(o) => clients = await oidcService.listClients(o)}
|
||||
columns={[
|
||||
{ label: 'Logo' },
|
||||
{ label: 'Name', sortColumn: 'name' },
|
||||
{ label: 'Actions', hidden: true }
|
||||
]}
|
||||
>
|
||||
{#snippet rows({ item })}
|
||||
<Table.Cell class="w-8 font-medium">
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import * as DropdownMenu from '$lib/components/ui/dropdown-menu';
|
||||
import * as Table from '$lib/components/ui/table';
|
||||
import UserGroupService from '$lib/services/user-group-service';
|
||||
import type { Paginated } from '$lib/types/pagination.type';
|
||||
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
|
||||
import type { UserGroup, UserGroupWithUserCount } from '$lib/types/user-group.type';
|
||||
import { axiosErrorToast } from '$lib/utils/error-util';
|
||||
import { LucidePencil, LucideTrash } from 'lucide-svelte';
|
||||
@@ -16,6 +16,7 @@
|
||||
$props();
|
||||
|
||||
let userGroups = $state<Paginated<UserGroupWithUserCount>>(initialUserGroups);
|
||||
let requestOptions: SearchPaginationSortRequest | undefined = $state();
|
||||
|
||||
const userGroupService = new UserGroupService();
|
||||
|
||||
@@ -29,7 +30,7 @@
|
||||
action: async () => {
|
||||
try {
|
||||
await userGroupService.remove(userGroup.id);
|
||||
userGroups = await userGroupService.list();
|
||||
userGroups = await userGroupService.list(requestOptions!);
|
||||
} catch (e) {
|
||||
axiosErrorToast(e);
|
||||
}
|
||||
@@ -38,13 +39,19 @@
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchItems(search: string, page: number, limit: number) {
|
||||
return userGroupService.list(search, { page, limit });
|
||||
}
|
||||
</script>
|
||||
|
||||
<AdvancedTable items={userGroups} {fetchItems} columns={['Friendly Name', 'Name', 'User Count', {label: "Actions", hidden: true}]}>
|
||||
<AdvancedTable
|
||||
items={userGroups}
|
||||
onRefresh={async (o) => (userGroups = await userGroupService.list(o))}
|
||||
{requestOptions}
|
||||
columns={[
|
||||
{ label: 'Friendly Name', sortColumn: 'friendlyName' },
|
||||
{ label: 'Name', sortColumn: 'name' },
|
||||
{ label: 'User Count', sortColumn: 'userCount' },
|
||||
{ label: 'Actions', hidden: true }
|
||||
]}
|
||||
>
|
||||
{#snippet rows({ item })}
|
||||
<Table.Cell>{item.friendlyName}</Table.Cell>
|
||||
<Table.Cell>{item.name}</Table.Cell>
|
||||
|
||||
@@ -13,16 +13,15 @@
|
||||
const userService = new UserService();
|
||||
|
||||
let users = $state(initialUsers);
|
||||
|
||||
function fetchItems(search: string, page: number, limit: number) {
|
||||
return userService.list(search, { page, limit });
|
||||
}
|
||||
</script>
|
||||
|
||||
<AdvancedTable
|
||||
items={users}
|
||||
{fetchItems}
|
||||
columns={['Name', 'Email']}
|
||||
onRefresh={async (o) => (users = await userService.list(o))}
|
||||
columns={[
|
||||
{ label: 'Name', sortColumn: 'name' },
|
||||
{ label: 'Email', sortColumn: 'email' }
|
||||
]}
|
||||
bind:selectedIds={selectedUserIds}
|
||||
>
|
||||
{#snippet rows({ item })}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
import * as DropdownMenu from '$lib/components/ui/dropdown-menu';
|
||||
import * as Table from '$lib/components/ui/table';
|
||||
import UserService from '$lib/services/user-service';
|
||||
import type { Paginated } from '$lib/types/pagination.type';
|
||||
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
|
||||
import type { User } from '$lib/types/user.type';
|
||||
import { axiosErrorToast } from '$lib/utils/error-util';
|
||||
import { LucideLink, LucidePencil, LucideTrash } from 'lucide-svelte';
|
||||
@@ -15,20 +15,13 @@
|
||||
import { toast } from 'svelte-sonner';
|
||||
import OneTimeLinkModal from './one-time-link-modal.svelte';
|
||||
|
||||
let { users: initialUsers }: { users: Paginated<User> } = $props();
|
||||
let users = $state<Paginated<User>>(initialUsers);
|
||||
$effect(() => {
|
||||
users = initialUsers;
|
||||
});
|
||||
let { users = $bindable() }: { users: Paginated<User> } = $props();
|
||||
let requestOptions: SearchPaginationSortRequest | undefined = $state();
|
||||
|
||||
let userIdToCreateOneTimeLink: string | null = $state(null);;
|
||||
let userIdToCreateOneTimeLink: string | null = $state(null);
|
||||
|
||||
const userService = new UserService();
|
||||
|
||||
function fetchItems(search: string, page: number, limit: number) {
|
||||
return userService.list(search, { page, limit });
|
||||
}
|
||||
|
||||
async function deleteUser(user: User) {
|
||||
openConfirmDialog({
|
||||
title: `Delete ${user.firstName} ${user.lastName}`,
|
||||
@@ -39,7 +32,7 @@
|
||||
action: async () => {
|
||||
try {
|
||||
await userService.remove(user.id);
|
||||
users = await userService.list();
|
||||
users = await userService.list(requestOptions!);
|
||||
} catch (e) {
|
||||
axiosErrorToast(e);
|
||||
}
|
||||
@@ -52,16 +45,34 @@
|
||||
|
||||
<AdvancedTable
|
||||
items={users}
|
||||
{fetchItems}
|
||||
{requestOptions}
|
||||
onRefresh={async (options) => (users = await userService.list(options))}
|
||||
columns={[
|
||||
'First name',
|
||||
'Last name',
|
||||
'Email',
|
||||
'Username',
|
||||
'Role',
|
||||
{ label: 'Actions', hidden: true }
|
||||
{
|
||||
label: 'First name',
|
||||
sortColumn: 'firstName'
|
||||
},
|
||||
{
|
||||
label: 'Last name',
|
||||
sortColumn: 'lastName'
|
||||
},
|
||||
{
|
||||
label: 'Email',
|
||||
sortColumn: 'email'
|
||||
},
|
||||
{
|
||||
label: 'Username',
|
||||
sortColumn: 'username'
|
||||
},
|
||||
{
|
||||
label: 'Role',
|
||||
sortColumn: 'isAdmin'
|
||||
},
|
||||
{
|
||||
label: 'Actions',
|
||||
hidden: true
|
||||
}
|
||||
]}
|
||||
withoutSearch
|
||||
>
|
||||
{#snippet rows({ item })}
|
||||
<Table.Cell>{item.firstName}</Table.Cell>
|
||||
|
||||
@@ -4,8 +4,10 @@ import type { PageServerLoad } from './$types';
|
||||
export const load: PageServerLoad = async ({ cookies }) => {
|
||||
const auditLogService = new AuditLogService(cookies.get('access_token'));
|
||||
const auditLogs = await auditLogService.list({
|
||||
limit: 15,
|
||||
page: 1,
|
||||
sort: {
|
||||
column: 'createdAt',
|
||||
direction: 'desc'
|
||||
}
|
||||
});
|
||||
return {
|
||||
auditLogs
|
||||
|
||||
@@ -11,13 +11,6 @@
|
||||
|
||||
const auditLogService = new AuditLogService();
|
||||
|
||||
async function fetchItems(search: string, page: number, limit: number) {
|
||||
return await auditLogService.list({
|
||||
page,
|
||||
limit
|
||||
});
|
||||
}
|
||||
|
||||
function toFriendlyEventString(event: string) {
|
||||
const words = event.split('_');
|
||||
const capitalizedWords = words.map((word) => {
|
||||
@@ -29,8 +22,16 @@
|
||||
|
||||
<AdvancedTable
|
||||
items={auditLogs}
|
||||
{fetchItems}
|
||||
columns={['Time', 'Event', 'Approximate Location', 'IP Address', 'Device', 'Client']}
|
||||
onRefresh={async (options) => (auditLogs = await auditLogService.list(options))}
|
||||
defaultSort={{ column: 'createdAt', direction: 'desc' }}
|
||||
columns={[
|
||||
{ label: 'Time', sortColumn: 'createdAt' },
|
||||
{ label: 'Event', sortColumn: 'event' },
|
||||
{ label: 'Approximate Location', sortColumn: 'city' },
|
||||
{ label: 'IP Address', sortColumn: 'ipAddress' },
|
||||
{ label: 'Device', sortColumn: 'device' },
|
||||
{ label: 'Client' }
|
||||
]}
|
||||
withoutSearch
|
||||
>
|
||||
{#snippet rows({ item })}
|
||||
|
||||
Reference in New Issue
Block a user