|
1 | | -import { IMonthYearHook, UseMonthYearPick, VueEmit } from '../../interfaces'; |
| 1 | +import { addMonths, addYears, getMonth, getYear, set, subMonths, subYears } from 'date-fns'; |
| 2 | +import { UseMonthYearPick, VueEmit } from '../../interfaces'; |
2 | 3 |
|
3 | | -export const useMontYearPick = (props: UseMonthYearPick, emit: VueEmit): IMonthYearHook => { |
4 | | - const months = props.months.map((month) => month.value); |
5 | | - const years = props.years.map((year) => year.value); |
6 | | - const excludedMonths = months.filter((month: number) => !props.filters.months.some((mon: number) => mon === month)); |
7 | | - const excludedYears = years.filter((year: number) => !props.filters.years.some((yr: number) => yr === year)); |
| 4 | +export const useMontYearPick = ( |
| 5 | + props: UseMonthYearPick, |
| 6 | + emit: VueEmit, |
| 7 | +): { handleMonthYearChange(isNext: boolean): void } => { |
| 8 | + const recursiveMonthAdjust = (date: Date, increment: boolean): Date => { |
| 9 | + let monthDate = date; |
| 10 | + if (props.filters.months.includes(getMonth(monthDate))) { |
| 11 | + monthDate = increment ? addMonths(date, 1) : subMonths(date, 1); |
| 12 | + return recursiveMonthAdjust(monthDate, increment); |
| 13 | + } |
| 14 | + return monthDate; |
| 15 | + }; |
8 | 16 |
|
9 | | - const onNext = (): void => { |
10 | | - let tempMonth; |
11 | | - let month = props.month; |
12 | | - let year = props.year; |
13 | | - if (props.month === 11) { |
14 | | - month = 0; |
15 | | - year = props.year + 1; |
16 | | - } else { |
17 | | - month += 1; |
18 | | - } |
19 | | - if (props.filters.months.includes(month)) { |
20 | | - if (month === 0) { |
21 | | - month = Math.min(...excludedMonths); |
22 | | - } else { |
23 | | - month = Math.max(...excludedMonths); |
24 | | - } |
25 | | - tempMonth = month; |
26 | | - } |
27 | | - if (month === tempMonth) { |
28 | | - month = Math.min(...excludedMonths); |
29 | | - year = props.year + 1; |
30 | | - } |
31 | | - if (props.filters.years.includes(year)) { |
32 | | - const foundYear = excludedYears.find((availableYear: number) => availableYear > year); |
33 | | - if (foundYear) { |
34 | | - year = foundYear; |
35 | | - } |
36 | | - } |
37 | | - if (year <= years[years.length - 1]) { |
38 | | - updateMonthYear(month, year); |
| 17 | + const recursiveYearAdjust = (date: Date, increment: boolean): Date => { |
| 18 | + let yearDate = date; |
| 19 | + if (props.filters.years.includes(getYear(yearDate))) { |
| 20 | + yearDate = increment ? addYears(date, 1) : subYears(date, 1); |
| 21 | + return recursiveYearAdjust(yearDate, increment); |
39 | 22 | } |
| 23 | + return yearDate; |
40 | 24 | }; |
41 | 25 |
|
42 | | - const onPrev = (): void => { |
43 | | - let tempMonth; |
44 | | - let month = props.month; |
45 | | - let year = props.year; |
46 | | - if (props.month === 0) { |
47 | | - month = 11; |
48 | | - year = props.year - 1; |
49 | | - } else { |
50 | | - month -= 1; |
51 | | - } |
| 26 | + const handleMonthYearChange = (isNext: boolean): void => { |
| 27 | + const initialDate = set(new Date(), { month: props.month, year: props.year }); |
| 28 | + let date = isNext ? addMonths(initialDate, 1) : subMonths(initialDate, 1); |
| 29 | + |
| 30 | + let month = getMonth(date); |
| 31 | + let year = getYear(date); |
| 32 | + |
52 | 33 | if (props.filters.months.includes(month)) { |
53 | | - if (month === 11) { |
54 | | - month = Math.max(...excludedMonths); |
55 | | - } else { |
56 | | - month = Math.min(...excludedMonths); |
57 | | - } |
58 | | - tempMonth = month; |
59 | | - } |
60 | | - if (month === tempMonth) { |
61 | | - month = Math.max(...excludedMonths); |
62 | | - year = props.year - 1; |
| 34 | + date = recursiveMonthAdjust(date, isNext); |
| 35 | + month = getMonth(date); |
| 36 | + year = getYear(date); |
63 | 37 | } |
| 38 | + |
64 | 39 | if (props.filters.years.includes(year)) { |
65 | | - const foundYear = excludedYears.reverse().find((availableYear: number) => availableYear < year); |
66 | | - if (foundYear) { |
67 | | - year = foundYear; |
68 | | - } |
69 | | - } |
70 | | - if (year >= years[0]) { |
71 | | - updateMonthYear(month, year); |
| 40 | + date = recursiveYearAdjust(date, isNext); |
| 41 | + year = getYear(date); |
72 | 42 | } |
| 43 | + updateMonthYear(month, year); |
73 | 44 | }; |
74 | 45 |
|
75 | 46 | const updateMonthYear = (month: number, year: number): void => { |
76 | 47 | emit('update:month', month); |
77 | 48 | emit('update:year', year); |
78 | 49 | }; |
79 | 50 |
|
80 | | - return { onNext, onPrev }; |
| 51 | + return { handleMonthYearChange }; |
81 | 52 | }; |
0 commit comments