|
| 1 | +import jalaliMoment from 'jalali-moment' |
| 2 | +import type React from 'react' |
| 3 | +import { useState } from 'react' |
| 4 | +import { FaChevronLeft, FaChevronRight } from 'react-icons/fa6' |
| 5 | +import { useGetEvents } from '../../services/getMethodHooks/getEvents.hook' |
| 6 | +import { DayItem } from './components/day' |
| 7 | +import { Events } from './components/events/event' |
| 8 | +import { Todos } from './components/todos/todos' |
| 9 | +import type { Todo } from './interface/todo.interface' |
| 10 | +import { formatDateStr } from './utils' |
| 11 | + |
| 12 | +const PERSIAN_MONTHS = [ |
| 13 | + 'فروردین', |
| 14 | + 'اردیبهشت', |
| 15 | + 'خرداد', |
| 16 | + 'تیر', |
| 17 | + 'مرداد', |
| 18 | + 'شهریور', |
| 19 | + 'مهر', |
| 20 | + 'آبان', |
| 21 | + 'آذر', |
| 22 | + 'دی', |
| 23 | + 'بهمن', |
| 24 | + 'اسفند', |
| 25 | +] |
| 26 | + |
| 27 | +const WEEKDAYS = ['شنبه', 'یکشنبه', 'دوشنبه', 'سهشنبه', 'چهارشنبه', 'پنجشنبه', 'جمعه'] |
| 28 | + |
| 29 | +export const PersianCalendar: React.FC = () => { |
| 30 | + const today = jalaliMoment() |
| 31 | + const [currentDate, setCurrentDate] = useState(today) |
| 32 | + const [selectedDate, setSelectedDate] = useState(today.clone()) |
| 33 | + |
| 34 | + const firstDayOfMonth = currentDate.clone().startOf('jMonth').day() |
| 35 | + |
| 36 | + const { data: events } = useGetEvents() |
| 37 | + |
| 38 | + const daysInMonth = currentDate.clone().endOf('jMonth').jDate() |
| 39 | + |
| 40 | + const emptyDays = (firstDayOfMonth + 1) % 7 |
| 41 | + |
| 42 | + const changeMonth = (delta: number) => { |
| 43 | + setCurrentDate((prev) => prev.clone().add(delta, 'jMonth')) |
| 44 | + } |
| 45 | + |
| 46 | + const selectedDateStr = formatDateStr(selectedDate) |
| 47 | + |
| 48 | + const todos: Todo[] = [] |
| 49 | + return ( |
| 50 | + <div className="grid gap-4 md:grid-cols-5" dir="rtl"> |
| 51 | + <div className="p-4 md:col-span-3 bg-gray-800/50 rounded-xl backdrop-blur-sm lg:h-96"> |
| 52 | + <div className="flex items-center justify-between mb-4"> |
| 53 | + <h3 className="text-xl font-medium text-gray-200"> |
| 54 | + {PERSIAN_MONTHS[currentDate.jMonth()]} {currentDate.jYear()} |
| 55 | + </h3> |
| 56 | + <div className="flex gap-2"> |
| 57 | + <button |
| 58 | + onClick={() => changeMonth(-1)} |
| 59 | + className="flex flex-row-reverse items-center gap-1 p-2 text-gray-400 rounded-lg hover:text-gray-200 hover:bg-gray-700/50" |
| 60 | + > |
| 61 | + ماه قبل |
| 62 | + <FaChevronRight /> |
| 63 | + </button> |
| 64 | + <button |
| 65 | + onClick={() => changeMonth(1)} |
| 66 | + className="flex flex-row-reverse items-center gap-1 p-2 text-gray-400 rounded-lg hover:text-gray-200 hover:bg-gray-700/50" |
| 67 | + > |
| 68 | + <FaChevronLeft /> |
| 69 | + ماه بعد |
| 70 | + </button> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + |
| 74 | + <div className="grid grid-cols-7 gap-2"> |
| 75 | + {WEEKDAYS.map((day) => ( |
| 76 | + <div key={day} className="py-2 text-xs text-center text-gray-400"> |
| 77 | + {day} |
| 78 | + </div> |
| 79 | + ))} |
| 80 | + |
| 81 | + {Array.from({ length: emptyDays }).map((_, i) => ( |
| 82 | + <div key={`empty-${i}`} className="p-2" /> |
| 83 | + ))} |
| 84 | + |
| 85 | + {Array.from({ length: daysInMonth }, (_, i) => { |
| 86 | + return ( |
| 87 | + <DayItem |
| 88 | + currentDate={currentDate} |
| 89 | + day={i + 1} |
| 90 | + events={events} |
| 91 | + selectedDateStr={selectedDateStr} |
| 92 | + setSelectedDate={setSelectedDate} |
| 93 | + todos={todos} |
| 94 | + key={i + 1} |
| 95 | + /> |
| 96 | + ) |
| 97 | + })} |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + |
| 101 | + <div className="p-4 md:col-span-2 bg-gray-800/50 rounded-xl backdrop-blur-sm"> |
| 102 | + <h3 className="mb-4 text-xl font-medium text-gray-200"> |
| 103 | + {PERSIAN_MONTHS[selectedDate.jMonth()]} {selectedDate.jDate()} |
| 104 | + </h3> |
| 105 | + |
| 106 | + {/* todos */} |
| 107 | + <Todos currentDate={selectedDate} /> |
| 108 | + |
| 109 | + {/* events */} |
| 110 | + <Events events={events} currentDate={selectedDate} /> |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +const CalendarLayout = () => { |
| 117 | + return ( |
| 118 | + <section className="p-2 mx-1 overflow-y-auto rounded lg:mx-4 max-h-[calc(100vh-4rem)]"> |
| 119 | + <div className="flex items-center justify-between w-full px-1 mb-4"> |
| 120 | + <h2 className="text-lg font-semibold text-gray-200 font-[balooTamma]"> |
| 121 | + 📅 Calender |
| 122 | + </h2> |
| 123 | + <div |
| 124 | + className="text-xs text-gray-400 font-[balooTamma] font-semibold flex items-center gap-1 |
| 125 | + hover:text-gray-300 cursor-pointer" |
| 126 | + > |
| 127 | + <span>-</span> |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + <PersianCalendar /> |
| 131 | + </section> |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +export default CalendarLayout |
0 commit comments