Skip to content

Commit

Permalink
fix: rename discount collection to offer
Browse files Browse the repository at this point in the history
  • Loading branch information
HoreKk committed Jan 11, 2024
1 parent 631a6cd commit 2ed6244
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Flex, Icon, Text } from "@chakra-ui/react";
import { type Discount } from "~/payload/payload-types";
import { type Offer } from "~/payload/payload-types";
import { BsCart2 } from "react-icons/bs";
import { OnlineIcon } from "~/components/icons/online";

export const DiscountKindBadge = ({
export const OfferKindBadge = ({
kind,
variant,
}: {
kind: Discount["kind"];
kind: Offer["kind"];
variant: "light" | "dark";
}) => {
return (
Expand Down
37 changes: 18 additions & 19 deletions webapp/src/pages/dashboard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Badge, Box, Flex, Heading, Text } from "@chakra-ui/react";
import { Box, Flex, Heading, Text } from "@chakra-ui/react";
import Image from "next/image";
import Link from "next/link";
import { Category, Discount } from "~/payload/payload-types";
import { api } from "~/utils/api";
import { DiscountKindBadge } from "~/components/DiscountKindBadge";
import { OfferKindBadge } from "~/components/OfferKindBadge";

export default function Dashboard() {
const { data: resultCategories, isLoading: isLoadingCategories } =
Expand All @@ -13,15 +12,15 @@ export default function Dashboard() {
sort: "createdAt",
});

const { data: resultDiscounts, isLoading: isLoadingDiscounts } =
api.discount.getList.useQuery({
const { data: resultOffers, isLoading: isLoadingOffers } =
api.offer.getList.useQuery({
page: 1,
perPage: 50,
});

const { data: categories } = resultCategories || {};

const { data: discounts } = resultDiscounts || {};
const { data: offers } = resultOffers || {};

return (
<Box>
Expand Down Expand Up @@ -97,31 +96,31 @@ export default function Dashboard() {
},
}}
>
{isLoadingDiscounts ? (
{isLoadingOffers ? (
<Text>Loading...</Text>
) : (
discounts?.map((discount) => (
<Flex key={discount.id} flexDir="column" minW="250px">
offers?.map((offer) => (
<Flex key={offer.id} flexDir="column" minW="250px">
<Flex
flexDir="column"
bgColor={discount.category.color}
bgColor={offer.category.color}
gap={2}
p={3}
borderTopRadius={12}
>
<Box alignSelf="start">
<DiscountKindBadge kind={discount.kind} variant="dark" />
<OfferKindBadge kind={offer.kind} variant="dark" />
</Box>
<Flex alignItems="center" gap={4}>
<Box bgColor="white" borderRadius={6} p={1}>
<Image
src={discount.partner.icon.url ?? ""}
alt={discount.partner.icon.alt ?? ""}
src={offer.partner.icon.url ?? ""}
alt={offer.partner.icon.alt ?? ""}
width={40}
height={40}
/>
</Box>
<Text fontWeight="bold">{discount.partner.name}</Text>
<Text fontWeight="bold">{offer.partner.name}</Text>
</Flex>
</Flex>
<Flex
Expand All @@ -132,21 +131,21 @@ export default function Dashboard() {
>
<Flex alignItems="center" gap={2}>
<Box
bgColor={discount.category.color}
bgColor={offer.category.color}
borderRadius="full"
p={1}
>
<Image
src={discount.category.icon.url ?? ""}
alt={discount.partner.icon.alt ?? ""}
src={offer.category.icon.url ?? ""}
alt={offer.partner.icon.alt ?? ""}
width={20}
height={20}
/>
</Box>
<Text fontSize="sm">{discount.category.label}</Text>
<Text fontSize="sm">{offer.category.label}</Text>
</Flex>
<Text fontWeight="bold" fontSize="sm" noOfLines={2} mt={2}>
{discount.title}
{offer.title}
</Text>
</Flex>
</Flex>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type CollectionConfig } from "payload/types";

export const Discounts: CollectionConfig = {
slug: "discounts",
export const Offers: CollectionConfig = {
slug: "offers",
labels: {
singular: "Réduction",
plural: "Réductions",
singular: "Offre",
plural: "Offres",
},
fields: [
{
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/payload/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Config {
media: Media;
categories: Category;
partners: Partner;
discounts: Discount;
offers: Offer;
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
};
Expand Down Expand Up @@ -104,7 +104,7 @@ export interface Partner {
updatedAt: string;
createdAt: string;
}
export interface Discount {
export interface Offer {
id: number;
title: string;
partner: number | Partner;
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/payload/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Users } from "./collections/User";
import { Media } from "./collections/Media";
import { Categories } from "./collections/Categorie";
import { Partners } from "./collections/Partner";
import { Discounts } from "./collections/Discount";
import { Offers } from "./collections/Offer";

export default buildConfig({
db: postgresAdapter({
Expand All @@ -22,7 +22,7 @@ export default buildConfig({
bundler: webpackBundler(),
user: "admins",
},
collections: [Admins, Users, Media, Categories, Partners, Discounts],
collections: [Admins, Users, Media, Categories, Partners, Offers],
localization: {
locales: ["fr"],
defaultLocale: "fr",
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/payload/seed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getPayloadClient } from "../payloadClient";

import { seedCategories } from "./categories";
import { seedPartners } from "./partners";
import { seedDiscounts } from "./discounts";
import { seedOffers } from "./offers";

export const seedData = async () => {
try {
Expand Down Expand Up @@ -36,7 +36,7 @@ export const seedData = async () => {

await seedPartners(payload);

await seedDiscounts(payload);
await seedOffers(payload);
} catch (e) {
console.error(e);
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type Payload } from "payload";
import { Discount } from "../payload-types";
import { Offer } from "../payload-types";
import { partners } from "./partners";
import { categories } from "./categories";

export async function seedDiscounts(payload: Payload) {
const discounts: Omit<Discount, "id" | "createdAt" | "updatedAt">[] = [
export async function seedOffers(payload: Payload) {
const offers: Omit<Offer, "id" | "createdAt" | "updatedAt">[] = [
{
title: "15% de réduction sur les produits alimentaire",
partner: partners.findIndex((partner) => partner.name === "Leclerc") + 1,
Expand Down Expand Up @@ -35,10 +35,10 @@ export async function seedDiscounts(payload: Payload) {
},
];

for (const discount of discounts) {
for (const offer of offers) {
await payload.create({
collection: "discounts",
data: discount,
collection: "offers",
data: offer,
});
}
}
4 changes: 2 additions & 2 deletions webapp/src/server/api/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createTRPCRouter } from "~/server/api/trpc";

import { userRouter } from "./routers/user";
import { categoryRouter } from "./routers/category";
import { discountRouter } from "./routers/discount";
import { offerRouter } from "./routers/offer";

/**
* This is the primary router for your server.
Expand All @@ -12,7 +12,7 @@ import { discountRouter } from "./routers/discount";
export const appRouter = createTRPCRouter({
user: userRouter,
category: categoryRouter,
discount: discountRouter,
offer: offerRouter,
});

// export type definition of API
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { Category, Discount, Media, Partner } from "~/payload/payload-types";
import { Category, Offer, Media, Partner } from "~/payload/payload-types";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { ZGetListParams } from "~/server/types";

interface DiscountIncluded extends Discount {
interface OfferIncluded extends Offer {
partner: Partner & { icon: Media };
category: Category & { icon: Media };
}

export const discountRouter = createTRPCRouter({
export const offerRouter = createTRPCRouter({
getList: publicProcedure
.input(ZGetListParams)
.query(async ({ ctx, input }) => {
const { perPage, page, sort } = input;

const discounts = await ctx.payload.find({
collection: "discounts",
const offers = await ctx.payload.find({
collection: "offers",
limit: perPage,
page: page,
sort,
});

return {
data: discounts.docs as DiscountIncluded[],
metadata: { page, count: discounts.docs.length },
data: offers.docs as OfferIncluded[],
metadata: { page, count: offers.docs.length },
};
}),
});

0 comments on commit 2ed6244

Please sign in to comment.