From 631a6cd60668be88e840a469b224ae6eac01d530 Mon Sep 17 00:00:00 2001 From: horek Date: Thu, 11 Jan 2024 16:18:01 +0100 Subject: [PATCH] feat: enhanced getList for category and discount --- webapp/src/pages/dashboard/index.tsx | 11 ++++++++-- webapp/src/server/api/routers/category.ts | 25 +++++++++++++++-------- webapp/src/server/api/routers/discount.ts | 24 +++++++++++++++------- webapp/src/server/types.ts | 9 ++++++++ 4 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 webapp/src/server/types.ts diff --git a/webapp/src/pages/dashboard/index.tsx b/webapp/src/pages/dashboard/index.tsx index 342439ba..0eb15c1a 100644 --- a/webapp/src/pages/dashboard/index.tsx +++ b/webapp/src/pages/dashboard/index.tsx @@ -7,10 +7,17 @@ import { DiscountKindBadge } from "~/components/DiscountKindBadge"; export default function Dashboard() { const { data: resultCategories, isLoading: isLoadingCategories } = - api.category.getList.useQuery(); + api.category.getList.useQuery({ + page: 1, + perPage: 50, + sort: "createdAt", + }); const { data: resultDiscounts, isLoading: isLoadingDiscounts } = - api.discount.getList.useQuery(); + api.discount.getList.useQuery({ + page: 1, + perPage: 50, + }); const { data: categories } = resultCategories || {}; diff --git a/webapp/src/server/api/routers/category.ts b/webapp/src/server/api/routers/category.ts index 3c8ebd8b..49c44dc1 100644 --- a/webapp/src/server/api/routers/category.ts +++ b/webapp/src/server/api/routers/category.ts @@ -1,18 +1,27 @@ import type { Category, Media } from "~/payload/payload-types"; import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; +import { ZGetListParams } from "~/server/types"; interface CategoryIncluded extends Category { icon: Media; } export const categoryRouter = createTRPCRouter({ - getList: publicProcedure.query(async ({ ctx }) => { - const categories = await ctx.payload.find({ - collection: "categories", - sort: "createdAt", - limit: 100, - }); + getList: publicProcedure + .input(ZGetListParams) + .query(async ({ ctx, input }) => { + const { perPage, page, sort } = input; - return { data: categories.docs as CategoryIncluded[] }; - }), + const categories = await ctx.payload.find({ + collection: "categories", + limit: perPage, + page: page, + sort, + }); + + return { + data: categories.docs as CategoryIncluded[], + metadata: { page, count: categories.docs.length }, + }; + }), }); diff --git a/webapp/src/server/api/routers/discount.ts b/webapp/src/server/api/routers/discount.ts index 0885d05a..d139b9cb 100644 --- a/webapp/src/server/api/routers/discount.ts +++ b/webapp/src/server/api/routers/discount.ts @@ -1,5 +1,6 @@ import { Category, Discount, Media, Partner } from "~/payload/payload-types"; import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; +import { ZGetListParams } from "~/server/types"; interface DiscountIncluded extends Discount { partner: Partner & { icon: Media }; @@ -7,12 +8,21 @@ interface DiscountIncluded extends Discount { } export const discountRouter = createTRPCRouter({ - getList: publicProcedure.query(async ({ ctx }) => { - const discounts = await ctx.payload.find({ - collection: "discounts", - limit: 100, - }); + getList: publicProcedure + .input(ZGetListParams) + .query(async ({ ctx, input }) => { + const { perPage, page, sort } = input; - return { data: discounts.docs as DiscountIncluded[] }; - }), + const discounts = await ctx.payload.find({ + collection: "discounts", + limit: perPage, + page: page, + sort, + }); + + return { + data: discounts.docs as DiscountIncluded[], + metadata: { page, count: discounts.docs.length }, + }; + }), }); diff --git a/webapp/src/server/types.ts b/webapp/src/server/types.ts new file mode 100644 index 00000000..1d832275 --- /dev/null +++ b/webapp/src/server/types.ts @@ -0,0 +1,9 @@ +import { z } from "zod"; + +export const ZGetListParams = z.object({ + page: z.number(), + perPage: z.number(), + sort: z.string().optional(), +}); + +export type TGetListParams = z.infer;