code refactor
This commit is contained in:
@@ -1,17 +1,22 @@
|
||||
import { ref, computed } from 'vue';
|
||||
|
||||
import type { YearMonth } from '@/core/datetime.ts';
|
||||
import type { Year0BasedMonth } from '@/core/datetime.ts';
|
||||
|
||||
import {
|
||||
getYearMonthObjectFromUnixTime,
|
||||
getYearMonthObjectFromString,
|
||||
getYearMonthStringFromObject,
|
||||
getYear0BasedMonthObjectFromUnixTime,
|
||||
getYear0BasedMonthObjectFromString,
|
||||
getYearMonthStringFromYear0BasedMonthObject,
|
||||
getCurrentYear,
|
||||
getThisMonthFirstUnixTime
|
||||
} from '@/lib/datetime.ts';
|
||||
|
||||
import { useI18n } from '@/locales/helpers.ts';
|
||||
|
||||
export interface MonthSelectionValue {
|
||||
year: number;
|
||||
month: number; // 0-based month (0 = January, 11 = December)
|
||||
}
|
||||
|
||||
export interface CommonMonthSelectionProps {
|
||||
modelValue?: string;
|
||||
title?: string;
|
||||
@@ -19,18 +24,21 @@ export interface CommonMonthSelectionProps {
|
||||
show: boolean;
|
||||
}
|
||||
|
||||
function getYearMonthValueFromProps(props: CommonMonthSelectionProps): YearMonth {
|
||||
let value: YearMonth = getYearMonthObjectFromUnixTime(getThisMonthFirstUnixTime());
|
||||
function getYearMonthValueFromProps(props: CommonMonthSelectionProps): MonthSelectionValue {
|
||||
let value: Year0BasedMonth = getYear0BasedMonthObjectFromUnixTime(getThisMonthFirstUnixTime());
|
||||
|
||||
if (props.modelValue) {
|
||||
const yearMonth = getYearMonthObjectFromString(props.modelValue);
|
||||
const yearMonth = getYear0BasedMonthObjectFromString(props.modelValue);
|
||||
|
||||
if (yearMonth) {
|
||||
value = yearMonth;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
return {
|
||||
year: value.year,
|
||||
month: value.month0base
|
||||
};
|
||||
}
|
||||
|
||||
export function useMonthSelectionBase(props: CommonMonthSelectionProps) {
|
||||
@@ -41,10 +49,23 @@ export function useMonthSelectionBase(props: CommonMonthSelectionProps) {
|
||||
getCurrentYear() + 1
|
||||
]);
|
||||
|
||||
const monthValue = ref<YearMonth>(getYearMonthValueFromProps(props));
|
||||
const monthValue = ref<MonthSelectionValue>(getYearMonthValueFromProps(props));
|
||||
|
||||
const isYearFirst = computed<boolean>(() => isLongDateMonthAfterYear());
|
||||
|
||||
function getMonthSelectionValue(yearMonth: string): MonthSelectionValue | null {
|
||||
const yearMonthObj = getYear0BasedMonthObjectFromString(yearMonth);
|
||||
|
||||
if (!yearMonthObj) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
year: yearMonthObj.year,
|
||||
month: yearMonthObj.month0base
|
||||
};
|
||||
}
|
||||
|
||||
function getTextualYearMonth(): string | null {
|
||||
if (!monthValue.value) {
|
||||
return null;
|
||||
@@ -54,7 +75,10 @@ export function useMonthSelectionBase(props: CommonMonthSelectionProps) {
|
||||
throw new Error('Date is too early');
|
||||
}
|
||||
|
||||
return getYearMonthStringFromObject(monthValue.value);
|
||||
return getYearMonthStringFromYear0BasedMonthObject({
|
||||
year: monthValue.value.year,
|
||||
month0base: monthValue.value.month
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -64,6 +88,7 @@ export function useMonthSelectionBase(props: CommonMonthSelectionProps) {
|
||||
// computed states
|
||||
isYearFirst,
|
||||
// functions
|
||||
getMonthSelectionValue,
|
||||
getTextualYearMonth
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user