Skip to content

Commit

Permalink
perf: Improve sorting API
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Nov 5, 2024
1 parent 8d1f577 commit fb46de8
Show file tree
Hide file tree
Showing 32 changed files with 121 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class ShowLanguagesAdminService {
last,
cursor,
search = '',
sortBy,
sortDirection,
}: ShowLanguagesAdminQuery): Promise<ShowLanguagesAdminObj> {
const where = ilike(core_languages.name, `%${search}%`);

Expand All @@ -25,6 +27,8 @@ export class ShowLanguagesAdminService {
database: core_languages,
first,
last,
sortBy,
sortDirection,
defaultSortBy: {
direction: SortDirectionEnum.desc,
column: 'created_at',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ export class ShowAdminStaffMembersAdminService {
first,
last,
cursor,
sortBy,
sortDirection,
}: AdminStaffMembersAdminQuery): Promise<AdminStaffMembersAdminObj> {
const pagination = await this.databaseService.paginationCursor({
cursor,
database: core_admin_permissions,
first,
last,
sortBy,
sortDirection,
defaultSortBy: {
direction: SortDirectionEnum.desc,
column: 'updated_at',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class ShowPluginsAdminService {
last,
cursor,
search = '',
sortBy,
sortDirection,
}: ShowPluginsAdminQuery): Promise<ShowPluginsAdminObj> {
const where = ilike(core_plugins.name, `%${search}%`);

Expand All @@ -25,6 +27,8 @@ export class ShowPluginsAdminService {
database: core_plugins,
first,
last,
sortBy,
sortDirection,
defaultSortBy: {
direction: SortDirectionEnum.desc,
column: 'updated_at',
Expand Down
12 changes: 4 additions & 8 deletions packages/frontend/src/components/ui/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import { HeaderDataTable } from '../data-table/header';
import { SkeletonDataTable } from '../data-table/skeleton';
import { ToolbarDataTable } from '../data-table/toolbar/toolbar';
import { Pagination } from './pagination';
// import { HeaderDataTable } from '../data-table/header';
// import { SkeletonDataTable } from '../data-table/skeleton';
// import { ToolbarDataTable } from '../data-table/toolbar/toolbar';
// import { Pagination } from './pagination';
import {
Table,
TableBody,
Expand All @@ -39,7 +35,7 @@ export interface DataTableProps<T extends TMin> {
sortBy: keyof T;
sortDirection: 'asc' | 'desc';
};
page_info?: PageInfo;
pageInfo?: PageInfo;
searchPlaceholder?: string;
}

Expand All @@ -48,7 +44,7 @@ export function DataTable<T extends TMin>({
columns,
defaultSorting,
searchPlaceholder,
page_info,
pageInfo,
defaultPageSize = 10,
}: DataTableProps<T>) {
const t = useTranslations('core.global');
Expand Down Expand Up @@ -146,8 +142,8 @@ export function DataTable<T extends TMin>({
)}
</div>

{page_info && (
<Pagination defaultPageSize={defaultPageSize} page_info={page_info} />
{pageInfo && (
<Pagination defaultPageSize={defaultPageSize} pageInfo={pageInfo} />
)}
</div>
);
Expand Down
22 changes: 11 additions & 11 deletions packages/frontend/src/components/ui/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import {
const PAGE_SIZES = [10, 20, 30, 40, 50];

export const Pagination = ({
page_info,
defaultPageSize,
pageInfo,
defaultPageSize = 10,
}: {
defaultPageSize: 10 | 20 | 30 | 40 | 50;
page_info: PageInfo;
defaultPageSize?: 10 | 20 | 30 | 40 | 50;
pageInfo: PageInfo;
}) => {
const t = useTranslations('core.global');
const { push } = useRouter();
Expand Down Expand Up @@ -56,7 +56,7 @@ export const Pagination = ({
return (
<div className="flex flex-wrap items-center justify-center gap-4 sm:justify-end">
<span className="text-muted-foreground text-sm">
{t('total_count', { count: page_info.total_count })}
{t('total_count', { count: pageInfo.total_count })}
</span>

<div className="flex flex-wrap items-center justify-center gap-4">
Expand Down Expand Up @@ -100,12 +100,12 @@ export const Pagination = ({
<Button
ariaLabel={t('previous')}
className="bg-card size-9"
disabled={!page_info.has_previous_page}
disabled={!pageInfo.has_previous_page}
onClick={() => {
if (!page_info.start_cursor) return;
if (!pageInfo.start_cursor) return;

const params = new URLSearchParams(searchParams.toString());
params.set('cursor', `${page_info.start_cursor}`);
params.set('cursor', `${pageInfo.start_cursor}`);
params.set('last', `${pageSizeValue}`);
params.delete('first');
push(`${pathname}?${params.toString()}`, {
Expand All @@ -120,12 +120,12 @@ export const Pagination = ({
<Button
ariaLabel={t('next')}
className="bg-card size-9"
disabled={!page_info.has_next_page}
disabled={!pageInfo.has_next_page}
onClick={() => {
if (!page_info.end_cursor) return;
if (!pageInfo.end_cursor) return;

const params = new URLSearchParams(searchParams.toString());
params.set('cursor', `${page_info.end_cursor}`);
params.set('cursor', `${pageInfo.end_cursor}`);
params.set('first', `${pageSizeValue}`);
params.delete('last');
push(`${pathname}?${params.toString()}`, {
Expand Down
10 changes: 5 additions & 5 deletions packages/frontend/src/helpers/get-pagination-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export interface SearchParamsPagination {
}

export async function getPaginationTool({
defaultPageSize,
defaultPageSize = 10,
searchParams: searchParamsPromise,
columnsSortByEnum,
sortEnum,
}: {
columnsSortByEnum?: Record<string, unknown>;
defaultPageSize: 10 | 20 | 30 | 40 | 50;
defaultPageSize?: 10 | 20 | 30 | 40 | 50;
search?: boolean;
searchParams: Promise<SearchParamsPagination>;
sortEnum?: Record<string, unknown>;
}): Promise<{
cursor?: number;
first: number;
Expand All @@ -28,7 +28,7 @@ export async function getPaginationTool({
const searchParams = await searchParamsPromise;

const sortBy = getGetSortByParamsAPI({
constEnum: columnsSortByEnum,
constEnum: sortEnum,
searchParams,
});
const pagination = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ShowLanguagesAdminObj,
ShowLanguagesAdminQuery,
} from 'vitnode-shared/admin/language.dto';
import { ShowLanguagesAdminSortEnum } from 'vitnode-shared/admin/language.enum';

import { CreateActionLangAdmin } from './actions/create';
import { ContentLangsCoreAdminView } from './table/content';
Expand Down Expand Up @@ -43,7 +44,7 @@ export const LangsCoreAdminView = async ({
}) => {
const variables = await getPaginationTool({
searchParams,
defaultPageSize: 10,
sortEnum: ShowLanguagesAdminSortEnum,
});

const [t, data] = await Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const ContentLangsCoreAdminView = ({
sortBy: 'created_at',
sortDirection: 'desc',
}}
page_info={page_info}
pageInfo={page_info}
searchPlaceholder={t('search_placeholder')}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ShowPluginsAdminObj,
ShowPluginsAdminQuery,
} from 'vitnode-shared/admin/plugins.dto';
import { ShowPluginsAdminSortEnum } from 'vitnode-shared/admin/plugins.enum';

import { WarnReqRestartServer } from '../warn-req-restart-server';
import { ActionsPluginsAdmin } from './actions/actions';
Expand Down Expand Up @@ -39,7 +40,7 @@ export const PluginsAdminView = async ({
}) => {
const variables = await getPaginationTool({
searchParams,
defaultPageSize: 10,
sortEnum: ShowPluginsAdminSortEnum,
});
const [t, data] = await Promise.all([
getTranslations('admin.core.plugins'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import { SubmitContentDeletePluginActionsAdmin } from './submit';

export const ContentDeletePluginActionsAdmin = ({
id,
name,
}: Pick<ShowPluginAdmin, 'author' | 'id' | 'name'>) => {
const t = useTranslations('admin.core.plugins.delete');
const tCore = useTranslations('core.global');
const { onSubmit } = useDeletePluginAdmin({ id, name });
const { onSubmit } = useDeletePluginAdmin({ id });

return (
<form action={onSubmit}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const ContentPluginsCoreAdmin = ({
sortBy: 'updated_at',
sortDirection: 'desc',
}}
page_info={page_info}
pageInfo={page_info}
searchPlaceholder={t('search_placeholder')}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const ContentLogsEmailSettingsAdmin = ({
sortBy: 'created_at',
sortDirection: 'desc',
}}
page_info={page_info}
pageInfo={page_info}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export const LogsEmailSettingsAdminView = async ({
}) => {
const variables = await getPaginationTool({
searchParams,
defaultPageSize: 10,
});

const [t, data] = await Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const LegalSettingsAdminView = async ({
if (perm) return perm;
const variables = await getPaginationTool({
searchParams,
defaultPageSize: 10,
});
const [t, data] = await Promise.all([
getTranslations('admin.core.settings.legal'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const TableLegalSettingsAdmin = ({ edges, page_info }: LegalsObj) => {
sortBy: 'updated_at',
sortDirection: 'desc',
}}
page_info={page_info}
pageInfo={page_info}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
GroupsMembersAdminObj,
GroupsMembersAdminQuery,
} from 'vitnode-shared/admin/members/groups.dto';
import { ColumnsSortGroupsMembersAdmin } from 'vitnode-shared/admin/members/groups.enum';
import { GroupsMembersAdminSortEnum } from 'vitnode-shared/admin/members/groups.enum';

import { ActionsGroupsMembersAdmin } from './actions/actions-groups-members-admin';
import { TableGroupsMembersAdmin } from './table/table';
Expand Down Expand Up @@ -46,8 +46,7 @@ export const GroupsMembersAdminView = async ({
}: GroupsMembersAdminViewProps) => {
const variables = await getPaginationTool({
searchParams,
columnsSortByEnum: ColumnsSortGroupsMembersAdmin,
defaultPageSize: 10,
sortEnum: GroupsMembersAdminSortEnum,
});

const [data, t] = await Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const TableGroupsMembersAdmin = ({
sortBy: 'updated_at',
sortDirection: 'desc',
}}
page_info={page_info}
pageInfo={page_info}
searchPlaceholder={t('search_placeholder')}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
AdminStaffMembersAdminObj,
AdminStaffMembersAdminQuery,
} from 'vitnode-shared/admin/members/staff/admin.dto';
import { ShowStaffMembersAdminSortEnum } from 'vitnode-shared/admin/members/staff/admin.enum';

import { ActionsAdministratorsStaffAdmin } from './actions/actions';
import { TableAdministratorsStaffAdmin } from './table/table';
Expand Down Expand Up @@ -43,7 +44,7 @@ export const AdminStaffAdminView = async ({
}) => {
const variables = await getPaginationTool({
searchParams,
defaultPageSize: 10,
sortEnum: ShowStaffMembersAdminSortEnum,
});

const [t, middleware, data] = await Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const TableAdministratorsStaffAdmin = ({
sortBy: 'updated_at',
sortDirection: 'desc',
}}
page_info={page_info}
pageInfo={page_info}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const TableUsersMembersAdmin = ({
sortBy: 'joined_at',
sortDirection: 'desc',
}}
page_info={page_info}
pageInfo={page_info}
// filters={<GroupsFiltersUsersMembersAdmin />}
searchPlaceholder={t('search_placeholder')}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
UsersMembersAdminObj,
UsersMembersAdminQuery,
} from 'vitnode-shared/admin/members/users.dto';
import { ColumnsSortUsersMembersAdminEnum } from 'vitnode-shared/admin/members/users.enum';
import { UsersMembersAdminSortEnum } from 'vitnode-shared/admin/members/users.enum';

import { CreateUserUsersMembersAdmin } from '../create/create';
import { TableUsersMembersAdmin } from './table';
Expand Down Expand Up @@ -47,8 +47,7 @@ export const UsersMembersAdminView = async ({
const { group_id } = await searchParams;
const variables = await getPaginationTool({
searchParams,
defaultPageSize: 10,
columnsSortByEnum: ColumnsSortUsersMembersAdminEnum,
sortEnum: UsersMembersAdminSortEnum,
});

const query: UsersMembersAdminQuery = {
Expand Down
16 changes: 15 additions & 1 deletion packages/shared/src/admin/language.dto.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { ApiProperty, ApiPropertyOptional, OmitType } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsBoolean, IsOptional, IsString } from 'class-validator';
import { IsBoolean, IsEnum, IsOptional, IsString } from 'class-validator';

import { PageInfoObj, PaginationQuery } from '../utils/pagination.dto';
import { SortDirectionEnum } from '../utils/pagination.enum';
import { TransformString } from '../utils/text-language';
import { ShowLanguagesAdminSortEnum } from './language.enum';

export class ShowLanguagesAdminQuery extends PaginationQuery {
@ApiPropertyOptional()
@IsString()
@IsOptional()
search?: string;

@ApiPropertyOptional({
enum: ShowLanguagesAdminSortEnum,
})
@IsEnum(ShowLanguagesAdminSortEnum)
@IsOptional()
sortBy?: ShowLanguagesAdminSortEnum;

@ApiPropertyOptional({ enum: SortDirectionEnum })
@IsEnum(SortDirectionEnum)
@IsOptional()
sortDirection?: SortDirectionEnum;
}

export class LanguagesAdminObj {
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/admin/language.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum ShowLanguagesAdminSortEnum {
created_at = 'created_at',
updated_at = 'updated_at',
}
Loading

0 comments on commit fb46de8

Please sign in to comment.