Skip to content

Commit

Permalink
feat: enhanced getList for category and discount
Browse files Browse the repository at this point in the history
  • Loading branch information
HoreKk committed Jan 11, 2024
1 parent 3ebf7a2 commit 631a6cd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
11 changes: 9 additions & 2 deletions webapp/src/pages/dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};

Expand Down
25 changes: 17 additions & 8 deletions webapp/src/server/api/routers/category.ts
Original file line number Diff line number Diff line change
@@ -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 },
};
}),
});
24 changes: 17 additions & 7 deletions webapp/src/server/api/routers/discount.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
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 };
category: Category & { icon: Media };
}

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 },
};
}),
});
9 changes: 9 additions & 0 deletions webapp/src/server/types.ts
Original file line number Diff line number Diff line change
@@ -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<typeof ZGetListParams>;

0 comments on commit 631a6cd

Please sign in to comment.