|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +export interface Event { |
| 4 | + date: string | Date; |
| 5 | + category: "Evento" | "Produto"; |
| 6 | + name: string; |
| 7 | +} |
| 8 | + |
| 9 | +interface CalendarProps { |
| 10 | + events: Event[]; |
| 11 | +} |
| 12 | + |
| 13 | +export default function Calendar({ events }: CalendarProps) { |
| 14 | + if (!events || events.length === 0) { |
| 15 | + return null; |
| 16 | + } |
| 17 | + |
| 18 | + // 1. Sort events by date |
| 19 | + const sortedEvents = [...events].sort((a, b) => { |
| 20 | + return new Date(a.date).getTime() - new Date(b.date).getTime(); |
| 21 | + }); |
| 22 | + |
| 23 | + const groupedEvents: Record<string, Event[]> = {}; |
| 24 | + |
| 25 | + sortedEvents.forEach((event) => { |
| 26 | + const d = new Date(event.date); |
| 27 | + const monthYear = new Intl.DateTimeFormat('pt-BR', { |
| 28 | + month: 'long', |
| 29 | + }).format(d).replace(/^\w/, (c) => c.toUpperCase()); |
| 30 | + |
| 31 | + if (!groupedEvents[monthYear]) { |
| 32 | + groupedEvents[monthYear] = []; |
| 33 | + } |
| 34 | + groupedEvents[monthYear].push(event); |
| 35 | + }); |
| 36 | + |
| 37 | + return ( |
| 38 | + <div className="w-full border-zinc-900 lg:border-2 p-10 rounded-xl max-w-4xl mx-auto"> |
| 39 | + {Object.keys(groupedEvents).length === 0 ? ( |
| 40 | + <p className="text-zinc-600 text-center py-4">Nenhum evento agendado.</p> |
| 41 | + ) : ( |
| 42 | + Object.entries(groupedEvents).map(([month, monthEvents]) => ( |
| 43 | + <div key={month} className="mb-8"> |
| 44 | + <h2 className="text-2xl font-bold text-zinc-900 border-b-2 border-zinc-900 mb-4 pb-2"> |
| 45 | + {month} |
| 46 | + </h2> |
| 47 | + |
| 48 | + <ul className="space-y-4"> |
| 49 | + {monthEvents.map((event, index) => { |
| 50 | + const eventDate = new Date(event.date); |
| 51 | + const day = eventDate.getDate(); |
| 52 | + const weekday = new Intl.DateTimeFormat('pt-BR', { weekday: 'short' }).format(eventDate).replace('.', ''); |
| 53 | + |
| 54 | + return ( |
| 55 | + <li key={`${event.name}-${index}`} className="flex gap-4"> |
| 56 | + <div className="flex-shrink-0 flex flex-col items-center justify-center bg-zinc-900 text-white rounded-md p-3 min-w-[4rem] text-center"> |
| 57 | + <span className="text-xs font-semibold uppercase">{weekday}</span> |
| 58 | + <span className="text-2xl font-bold leading-none">{day}</span> |
| 59 | + </div> |
| 60 | + |
| 61 | + <div className="flex-grow flex flex-col justify-center"> |
| 62 | + <span className="py-0.5 font-extrabold text-zinc-900"> |
| 63 | + {event.category} |
| 64 | + </span> |
| 65 | + <h3 className="text-lg font-medium text-gray-900">{event.name}</h3> |
| 66 | + </div> |
| 67 | + </li> |
| 68 | + ); |
| 69 | + })} |
| 70 | + </ul> |
| 71 | + </div> |
| 72 | + )) |
| 73 | + )} |
| 74 | + </div> |
| 75 | + ); |
| 76 | +} |
0 commit comments