Skip to content

Commit

Permalink
chore: code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Oct 21, 2023
1 parent aa32ce3 commit 2b4ac90
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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<APITypes.GetBlacklistedWordsData> {
const orderBy = getPrismaModelOrderBy(sorting);

const [totalCount, blacklistedWords] = await prisma.$transaction([
prisma.blacklistedWord.count({
where: query ? { word: { contains: query, mode: "insensitive" } } : undefined,
Expand All @@ -480,6 +484,7 @@ export class CADSettingsController {
where: query ? { word: { contains: query, mode: "insensitive" } } : undefined,
take: includeAll ? undefined : 35,
skip: includeAll ? undefined : skip,
orderBy,
}),
]);

Expand Down
20 changes: 6 additions & 14 deletions apps/api/src/controllers/admin/values/values-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<ValueType, ValuesSelect>> = {
QUALIFICATION: {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions apps/api/src/utils/order-by.ts
Original file line number Diff line number Diff line change
@@ -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);
}, {});
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export function BlacklistedWordsTab() {

const asyncTable = useAsyncTable<BlacklistedWord>({
search,
sortingSchema: {
createdAt: "createdAt",
word: "word",
},
fetchOptions: {
onResponse(data: GetBlacklistedWordsData) {
return { totalCount: data.totalCount, data: data.blacklistedWords };
Expand Down Expand Up @@ -95,7 +99,6 @@ export function BlacklistedWordsTab() {
{ header: common("createdAt"), accessorKey: "createdAt" },
{ header: common("actions"), accessorKey: "actions" },
]}
isLoading={asyncTable.isInitialLoading}
tableState={tableState}
/>

Expand Down
8 changes: 5 additions & 3 deletions apps/client/src/hooks/shared/table/use-async-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function useAsyncTable<T>(options: Options<T>) {
pageIndex: options.fetchOptions.pageIndex ?? 0,
});

const { isInitialLoading, error, refetch } = useQuery({
const { isLoading, error, refetch } = useQuery({
retry: false,
enabled: !options.disabled,
initialData: options.initialData ?? undefined,
Expand Down Expand Up @@ -153,8 +153,10 @@ export function useAsyncTable<T>(options: Options<T>) {
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",
Expand Down

0 comments on commit 2b4ac90

Please sign in to comment.