diff --git a/app/components/Calendar.module.css b/app/components/Calendar.module.css new file mode 100644 index 0000000..d8d7632 --- /dev/null +++ b/app/components/Calendar.module.css @@ -0,0 +1,41 @@ +.calendarGrid { + display: grid; + gap: 1rem; + grid-template-columns: auto auto auto auto auto auto auto; + text-align: center; + margin-bottom: 2rem; +} + +.monthNavigation { + display: flex; + justify-content: center; + gap: 1rem; + margin-bottom: 2rem; +} + +.monthTitle { + text-align: center; +} + +.monthNavBtn { + background-color: #d4f0dd; +} + +.emptyBtn { + background-color: #d4f0dd; +} + +.calendarCell { + background-color: #d4f0dd; + border: 1px solid #ccc; + padding: 0.5rem; + cursor: pointer; + height: 3rem; + width: 3rem; + border-radius: 0.5rem; +} + +.firstWeekPaddingCell { + height: 3rem; + width: 3rem; +} \ No newline at end of file diff --git a/app/components/Calendar.tsx b/app/components/Calendar.tsx new file mode 100644 index 0000000..903c08b --- /dev/null +++ b/app/components/Calendar.tsx @@ -0,0 +1,72 @@ +import { JSX, useEffect, useState } from "react"; +import styles from "./Calendar.module.css"; +import { getMonthName, daysInMonth } from "../utils/dateUtils"; + + + +const generateFirstWeekPadding = (month: number) => { + const firstDay = new Date(new Date().getFullYear(), month, 1).getDay(); + const padding = Array.from({ length: firstDay === 0 ? 6 : firstDay - 1 }, (_, index) => ( +
+   +
+ )); + return padding; +} + +const generateCalendarCells = (month: number) => { + const monthDays = daysInMonth[month]; + + const calendarCells = Array.from({ length: monthDays }, (_, index) => { + const day = index + 1; + const date = new Date(new Date().getFullYear(), month, day); + + return ( +
+ {date.toLocaleDateString("fi-FI", { + day: "2-digit", + month: "2-digit", + })} +
+ ); + }); + return calendarCells; +} + +export default function Calendar() { + const [selectedMonth, setSelectedMonth] = useState(new Date().getMonth()); + const [calendarCells, setCalendarCells] = useState([]); + const [firstWeekPadding, setFirstWeekPadding] = useState([]); + + useEffect(() => { + setCalendarCells(generateCalendarCells(selectedMonth)); + setFirstWeekPadding(generateFirstWeekPadding(selectedMonth)); + }, [selectedMonth]); + + return ( + <> +

+ {getMonthName(selectedMonth)} {new Date().getFullYear()} +

+
+ + +
+
+ Ma + Ti + Ke + To + Pe + La + Su + {firstWeekPadding} + {calendarCells} +
+ + ); +} \ No newline at end of file diff --git a/app/page.tsx b/app/page.tsx index f7b0fdc..9fddf56 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,3 +1,6 @@ +'use client' +import { use } from "react"; +import Calendar from "./components/Calendar"; import styles from "./page.module.css"; export default function Home() { @@ -5,6 +8,7 @@ export default function Home() {
Livonsaaren Osuuspuutarhan

Satolaatikko kalenteri

+
); } diff --git a/app/utils/dateUtils.ts b/app/utils/dateUtils.ts new file mode 100644 index 0000000..a20ab78 --- /dev/null +++ b/app/utils/dateUtils.ts @@ -0,0 +1,10 @@ +export const getMonthName = (month: number): string => { + const monthNames = [ + "Tammikuu", "Helmikuu", "Maaliskuu", "Huhtikuu", + "Toukokuu", "Kesäkuu", "Heinäkuu", "Elokuu", + "Syyskuu", "Lokakuu", "Marraskuu", "Joulukuu" + ]; + return monthNames[month]; +} + +export const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; \ No newline at end of file