From 3d6cd486e16ec5de95ef066a72fa6fa91f62a1a9 Mon Sep 17 00:00:00 2001 From: Norbert Maciaszek Date: Sat, 15 Nov 2025 14:32:46 +0100 Subject: [PATCH] Refactor Home component to display gifts for the current year using YearGifts, update YearOverview to accept a custom heading, and enhance YearList button text for clarity. Introduce new gift status 'ready' across relevant components and adjust GiftCard styling for better status representation. --- src/app/page.tsx | 15 ++--- src/components/atoms/YearList/index.tsx | 2 +- src/components/molecules/GiftCard/index.tsx | 7 ++- src/components/molecules/GiftModal/index.tsx | 3 +- .../molecules/PersonCard/client.tsx | 2 +- src/components/molecules/PersonCard/index.tsx | 2 +- src/components/organisms/YearGifts/index.tsx | 56 +++++++++++++++++++ .../organisms/YearOverview/index.tsx | 5 +- src/helpers/formatStatus/index.ts | 2 + src/lib/db/index.ts | 13 +++-- src/lib/db/types.d.ts | 2 +- src/types/global.d.ts | 2 +- 12 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 src/components/organisms/YearGifts/index.tsx diff --git a/src/app/page.tsx b/src/app/page.tsx index f3726c0..f31f2ae 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,20 +1,21 @@ import { YearOverview } from '@/components/organisms/YearOverview'; import { YearList } from '@/components/atoms/YearList'; import { DB } from '@/lib/db'; -import { GiftCard } from '@/components/molecules/GiftCard'; import { Heading } from '@/components/atoms/Heading'; +import { YearGifts } from '@/components/organisms/YearGifts'; + +export const dynamic = 'force-dynamic'; export default async function Home() { - const recentGifts = await DB.getRecentGifts(); + const year = new Date().getFullYear(); + const gifts = await DB.getGifts(year); return ( <> - + - Ostatnie prezenty - {recentGifts.map((gift) => ( - - ))} + Prezenty tego roku + ); } diff --git a/src/components/atoms/YearList/index.tsx b/src/components/atoms/YearList/index.tsx index 9e75399..8ba1a4b 100644 --- a/src/components/atoms/YearList/index.tsx +++ b/src/components/atoms/YearList/index.tsx @@ -8,7 +8,7 @@ export const YearList = async () => {
{years.map((year) => (
diff --git a/src/components/molecules/PersonCard/client.tsx b/src/components/molecules/PersonCard/client.tsx index ece22d0..6686678 100644 --- a/src/components/molecules/PersonCard/client.tsx +++ b/src/components/molecules/PersonCard/client.tsx @@ -24,7 +24,7 @@ export const PersonCardEdit: FC = ({ person }) => { return ( <> -
+
diff --git a/src/components/molecules/PersonCard/index.tsx b/src/components/molecules/PersonCard/index.tsx index 51a51a2..8a7128c 100644 --- a/src/components/molecules/PersonCard/index.tsx +++ b/src/components/molecules/PersonCard/index.tsx @@ -10,7 +10,7 @@ export const PersonCard: FC = (person) => { return (
-
+
{name} diff --git a/src/components/organisms/YearGifts/index.tsx b/src/components/organisms/YearGifts/index.tsx new file mode 100644 index 0000000..d5c6d7e --- /dev/null +++ b/src/components/organisms/YearGifts/index.tsx @@ -0,0 +1,56 @@ +'use client'; +import { GiftCard } from '@/components/molecules/GiftCard'; +import { FC, useState } from 'react'; + +type Props = { + gifts: Gift[]; +}; + +export const YearGifts: FC = ({ gifts }) => { + const [filter, setFilter] = useState('all'); + const [sort, setSort] = useState>('title'); + let renderGifts = gifts; + + // sort + if (sort === 'cost') { + renderGifts = renderGifts.sort((a, b) => b.cost - a.cost); + } else if (sort === 'person') { + renderGifts = renderGifts.sort((a, b) => a.expand.person.name.localeCompare(b.expand.person.name)); + } else { + renderGifts = renderGifts.sort((a, b) => (a[sort] < b[sort] ? -1 : 1)); + } + + if (filter !== 'all') { + renderGifts = renderGifts.filter((gift) => gift.status === filter); + } + + return ( +
+
+ + +
+ + {renderGifts.map((gift) => ( + + ))} +
+ ); +}; diff --git a/src/components/organisms/YearOverview/index.tsx b/src/components/organisms/YearOverview/index.tsx index 7b00f3b..7484ada 100644 --- a/src/components/organisms/YearOverview/index.tsx +++ b/src/components/organisms/YearOverview/index.tsx @@ -5,10 +5,11 @@ import { FC } from 'react'; import { Heading } from '@/components/atoms/Heading'; type Props = { + heading?: string; year?: number; }; -export const YearOverview: FC = async ({ year }) => { +export const YearOverview: FC = async ({ year, heading }) => { const currentYear = new Date().getFullYear(); const data = await DB.getYear(year ?? currentYear); @@ -36,7 +37,7 @@ export const YearOverview: FC = async ({ year }) => { return (
- Podsumowanie roku {data.year} + {heading ?? `Podsumowanie roku ${data.year}`}
diff --git a/src/helpers/formatStatus/index.ts b/src/helpers/formatStatus/index.ts index a683606..31331c1 100644 --- a/src/helpers/formatStatus/index.ts +++ b/src/helpers/formatStatus/index.ts @@ -6,6 +6,8 @@ export const formatStatus = (status: Gift['status']) => { return 'Do kupienia'; case 'bought': return 'Kupione'; + case 'ready': + return 'Do spakowania'; case 'wrapped': return 'Gotowe'; } diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts index 0975254..70d0ee0 100644 --- a/src/lib/db/index.ts +++ b/src/lib/db/index.ts @@ -46,7 +46,7 @@ export const DB = { link: string; cost: number; imageUrl: string; - status: 'planned' | 'decided' | 'bought' | 'wrapped'; + status: Gift['status']; year: string; person: string; }): Promise => { @@ -71,7 +71,7 @@ export const DB = { link: string; cost: number; imageUrl: string; - status: 'planned' | 'decided' | 'bought' | 'wrapped'; + status: Gift['status']; year: string; person: string; }, @@ -88,9 +88,14 @@ export const DB = { 'gifts-': id, }); }, - getGifts: async (yearId: string): Promise => { + getGifts: async (year: number): Promise => { + const yearRecord = await pb.collection('gifts_year').getFirstListItem(`year = ${year}`); + if (!yearRecord) { + return []; + } + return await pb.collection('gifts_items').getFullList({ - filter: `year = "${yearId}"`, + filter: `year = "${yearRecord.id}"`, expand: 'year,person', fields: '*,expand.year.year,expand.person.name,expand.year.id,expand.person.id', }); diff --git a/src/lib/db/types.d.ts b/src/lib/db/types.d.ts index 3a9bbb5..b0402df 100644 --- a/src/lib/db/types.d.ts +++ b/src/lib/db/types.d.ts @@ -25,7 +25,7 @@ namespace DB { link: string; cost: number; imageUrl: string; - status: 'planned' | 'decided' | 'bought' | 'wrapped'; + status: 'planned' | 'decided' | 'bought' | 'ready' | 'wrapped'; created: Date; updated: Date; diff --git a/src/types/global.d.ts b/src/types/global.d.ts index 9f34226..7fe9495 100644 --- a/src/types/global.d.ts +++ b/src/types/global.d.ts @@ -42,7 +42,7 @@ type Gift = { link: string; cost: number; imageUrl: string; - status: 'planned' | 'decided' | 'bought' | 'wrapped'; + status: 'planned' | 'decided' | 'bought' | 'ready' | 'wrapped'; created: Date; updated: Date;