diff --git a/apps/api/src/controllers/admin/manage/cad-settings/cad-settings-controller.ts b/apps/api/src/controllers/admin/manage/cad-settings/cad-settings-controller.ts index 3ef507426..017cd1fc3 100644 --- a/apps/api/src/controllers/admin/manage/cad-settings/cad-settings-controller.ts +++ b/apps/api/src/controllers/admin/manage/cad-settings/cad-settings-controller.ts @@ -36,6 +36,7 @@ import type { MiscCadSettings } from "@snailycad/types"; import { createFeaturesObject } from "middlewares/is-enabled"; import { hasPermission } from "@snailycad/permissions"; import { parseImportFile } from "~/utils/file"; +import { getPrismaModelOrderBy } from "~/utils/order-by"; @Controller("/admin/manage/cad-settings") @ContentType("application/json") @@ -471,7 +472,10 @@ export class CADSettingsController { @QueryParams("query", String) query?: string, @QueryParams("skip", Number) skip = 0, @QueryParams("includeAll", Boolean) includeAll = false, + @QueryParams("sorting") sorting: string = "", ): Promise { + const orderBy = getPrismaModelOrderBy(sorting); + const [totalCount, blacklistedWords] = await prisma.$transaction([ prisma.blacklistedWord.count({ where: query ? { word: { contains: query, mode: "insensitive" } } : undefined, @@ -480,6 +484,7 @@ export class CADSettingsController { where: query ? { word: { contains: query, mode: "insensitive" } } : undefined, take: includeAll ? undefined : 35, skip: includeAll ? undefined : skip, + orderBy, }), ]); diff --git a/apps/api/src/controllers/admin/values/values-controller.ts b/apps/api/src/controllers/admin/values/values-controller.ts index 9899b3696..78f096747 100644 --- a/apps/api/src/controllers/admin/values/values-controller.ts +++ b/apps/api/src/controllers/admin/values/values-controller.ts @@ -32,7 +32,7 @@ import { validateSchema } from "lib/data/validate-schema"; import { createSearchWhereObject } from "lib/values/create-where-object"; import generateBlurPlaceholder from "lib/images/generate-image-blur-data"; import { AuditLogActionType, createAuditLogEntry } from "@snailycad/audit-logger/server"; -import set from "lodash.set"; +import { getPrismaModelOrderBy } from "~/utils/order-by"; export const GET_VALUES: Partial> = { QUALIFICATION: { @@ -85,15 +85,7 @@ export class ValuesController { paths = validValuePaths.filter((v) => v !== "penal_code_group"); } - const orderBy = sorting.split(",").reduce((obj, cv) => { - const [key, sortOrder] = cv.split(":") as [string, "asc" | "desc"]; - - return set(obj, key, sortOrder); - }, {}); - - console.log({ - orderBy, - }); + const orderBy = getPrismaModelOrderBy(sorting); const values = await Promise.all( paths.map(async (path) => { @@ -117,7 +109,7 @@ export class ValuesController { if (data) { const [totalCount, values] = await prisma.$transaction([ // @ts-expect-error ignore - prisma[data.name].count({ orderBy: { value: { position: "asc" } }, where }), + prisma[data.name].count({ where }), // @ts-expect-error ignore prisma[data.name].findMany({ where, @@ -143,13 +135,13 @@ export class ValuesController { const [totalCount, penalCodes] = await prisma.$transaction([ prisma.penalCode.count({ where, - orderBy: { title: "asc" }, + orderBy: sorting ? orderBy : { title: "asc" }, }), prisma.penalCode.findMany({ take: includeAll ? undefined : 35, skip: includeAll ? undefined : skip, where, - orderBy: { title: "asc" }, + orderBy: sorting ? orderBy : { title: "asc" }, include: { warningApplicable: true, warningNotApplicable: true, @@ -171,7 +163,7 @@ export class ValuesController { where, take: includeAll ? undefined : 35, skip: includeAll ? undefined : skip, - orderBy: { position: "asc" }, + orderBy: sorting ? orderBy : { position: "asc" }, include: { _count: true, ...(type === ValueType.OFFICER_RANK diff --git a/apps/api/src/utils/order-by.ts b/apps/api/src/utils/order-by.ts new file mode 100644 index 000000000..92a9c5c00 --- /dev/null +++ b/apps/api/src/utils/order-by.ts @@ -0,0 +1,8 @@ +import set from "lodash.set"; + +export function getPrismaModelOrderBy(sorting: string) { + return sorting.split(",").reduce((obj, cv) => { + const [key, sortOrder] = cv.split(":") as [string, "asc" | "desc"]; + return set(obj, key, sortOrder); + }, {}); +} diff --git a/apps/client/src/components/admin/manage/cad-settings/general/blacklisted-words-tab.tsx b/apps/client/src/components/admin/manage/cad-settings/general/blacklisted-words-tab.tsx index 263a129b1..b9ebce258 100644 --- a/apps/client/src/components/admin/manage/cad-settings/general/blacklisted-words-tab.tsx +++ b/apps/client/src/components/admin/manage/cad-settings/general/blacklisted-words-tab.tsx @@ -24,6 +24,10 @@ export function BlacklistedWordsTab() { const asyncTable = useAsyncTable({ search, + sortingSchema: { + createdAt: "createdAt", + word: "word", + }, fetchOptions: { onResponse(data: GetBlacklistedWordsData) { return { totalCount: data.totalCount, data: data.blacklistedWords }; @@ -95,7 +99,6 @@ export function BlacklistedWordsTab() { { header: common("createdAt"), accessorKey: "createdAt" }, { header: common("actions"), accessorKey: "actions" }, ]} - isLoading={asyncTable.isInitialLoading} tableState={tableState} /> diff --git a/apps/client/src/hooks/shared/table/use-async-table.ts b/apps/client/src/hooks/shared/table/use-async-table.ts index b7f54ed51..49354dbbe 100644 --- a/apps/client/src/hooks/shared/table/use-async-table.ts +++ b/apps/client/src/hooks/shared/table/use-async-table.ts @@ -45,7 +45,7 @@ export function useAsyncTable(options: Options) { pageIndex: options.fetchOptions.pageIndex ?? 0, }); - const { isInitialLoading, error, refetch } = useQuery({ + const { isLoading, error, refetch } = useQuery({ retry: false, enabled: !options.disabled, initialData: options.initialData ?? undefined, @@ -153,8 +153,10 @@ export function useAsyncTable(options: Options) { return { ...list, sorting: sortingState, - noItemsAvailable: !isInitialLoading && !error && list.items.length <= 0, - isInitialLoading, + noItemsAvailable: !isLoading && !error && list.items.length <= 0, + /** @deprecated */ + // eslint-disable-next-line deprecation/deprecation + isInitialLoading: isLoading, filters, setFilters, isLoading: loadingState === "loading",