mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 16:03:21 +00:00
30 lines
796 B
JavaScript
30 lines
796 B
JavaScript
import { useState } from 'react';
|
|
import { addDays, addMonths, addYears } from 'date-fns';
|
|
|
|
export function useCalendar() {
|
|
const [currentDate, setCurrentDate] = useState(new Date());
|
|
const [viewType, setViewType] = useState('week');
|
|
|
|
const navigateDate = (direction) => {
|
|
const newDate = new Date(currentDate);
|
|
const operations = {
|
|
week: (date) =>
|
|
direction === 'prev' ? addDays(date, -7) : addDays(date, 7),
|
|
month: (date) =>
|
|
direction === 'prev' ? addMonths(date, -1) : addMonths(date, 1),
|
|
year: (date) =>
|
|
direction === 'prev' ? addYears(date, -1) : addYears(date, 1),
|
|
};
|
|
|
|
setCurrentDate(operations[viewType](newDate));
|
|
};
|
|
|
|
return {
|
|
currentDate,
|
|
setCurrentDate,
|
|
viewType,
|
|
setViewType,
|
|
navigateDate,
|
|
};
|
|
}
|