Skip to content

Open source personal finance application

refactor(date): replace DatePicker with UCalendar and update date utilities

Ilya Komichevf530d3caeb44
diff --git a/app/app.config.ts b/app/app.config.ts
index 3a5c6289..b1e5244a 100644
--- a/app/app.config.ts
+++ b/app/app.config.ts
@@ -10,6 +10,13 @@ export default defineAppConfig({
radius: 0.375,
},
ui: {
+ calendar: {
+ slots: {
+ body: '!flex-col !space-y-8',
+ cellTrigger: 'data-[disabled]:hover:bg-transparent data-today:bg-primary/10',
+ root: 'p-2 pt-0',
+ },
+ },
colors: {
neutral: 'neutral',
primary: 'black',
diff --git a/app/components/date/Picker.vue b/app/components/date/Picker.vue
deleted file mode 100644
index 8631f75e..00000000
--- a/app/components/date/Picker.vue
+++ /dev/null
@@ -1,108 +0,0 @@
-<script setup lang="ts">
-import { DatePicker as VCalendarDatePicker } from 'v-calendar'
-import 'v-calendar/style.css'
-
-type DatePickerDate = number | string | Date | null | {
- end?: number | string | Date | null
- start?: number | string | Date | null
-}
-
-const props = defineProps<{
- mode?: 'date' | 'range'
- rows?: number
- value: DatePickerDate
-}>()
-
-const emit = defineEmits(['update:model-value', 'close'])
-const colorMode = useColorMode()
-
-const date = computed({
- get: () => props.value,
- set: (value) => {
- emit('update:model-value', value)
- emit('close')
- },
-})
-
-const attrs = {
- 'borderless': true,
- 'first-day-of-week': 2,
- 'transparent': true,
-}
-
-const matchTheme = {
- 'dark': {
- color: 'pink',
- isDark: true,
- },
- 'dark-blue': {
- color: 'blue',
- isDark: true,
- },
- 'dark-pink': {
- color: 'pink',
- isDark: true,
- },
- 'light': {
- color: 'pink',
- isDark: false,
- },
- 'light-blue': {
- color: 'blue',
- isDark: false,
- },
- 'light-pink': {
- color: 'pink',
- isDark: false,
- },
- 'system': {
- color: 'pink',
- isDark: colorMode.value === 'dark',
- },
-}
-</script>
-
-<template>
- <VCalendarDatePicker
- v-if="props.mode === 'range'"
- v-model.range="date"
- class="datePicker w-full"
- :rows="props.rows ?? 1"
- style="width: 100%;"
- v-bind="{ ...attrs, ...$attrs, ...matchTheme[colorMode.preference] }"
- />
- <VCalendarDatePicker
- v-else
- v-model="date"
- :rows="1"
- class="datePicker w-full"
- mode="date"
- style="width: 100%;"
- v-bind="{ ...attrs, ...$attrs, ...matchTheme[colorMode.preference] }"
- />
-</template>
-
-<style>
-@import '~/assets/css/main.css';
-
-html .vc-dark,
-html .vc-light {
- --vc-popover-content-bg: var(--item-1);
- --vc-accent-500: var(--ui-primary);
- --vc-focus-ring: var(--ui-primary);
- --vc-nav-item-active-bg: var(--ui-primary);
- --vc-nav-hover-bg: var(--item-5);
- --vc-color: var(--text-1);
- --vc-day-content-disabled-color: var(--text-1/.40);
- --vc-weekday-color: var(--text-muted);
- --vc-header-title-color: var(--text-3);
- --vc-popover-content-color: var(--text-muted);
- --vc-nav-title-color: var(--text-3);
- --vc-nav-item-current-color: var(--ui-primary);
- --vc-highlight-solid-bg: var(--ui-primary);
-}
-
-.vc-popover-content {
- @apply border-2 border-item-4;
-}
-</style>
diff --git a/app/components/date/utils.ts b/app/components/date/utils.ts
index 3e79240e..7cc493b2 100644
--- a/app/components/date/utils.ts
+++ b/app/components/date/utils.ts
@@ -1,4 +1,5 @@
-import { differenceInDays, endOfDay, endOfMonth, endOfWeek, endOfYear, format, startOfDay, startOfMonth, startOfWeek, startOfYear, sub } from 'date-fns'
+import { getLocalTimeZone, parseDate, today } from '@internationalized/date'
+import { differenceInDays, endOfDay, endOfMonth, endOfWeek, endOfYear, format, formatISO, startOfDay, startOfMonth, startOfWeek, startOfYear, sub } from 'date-fns'
import { ru } from 'date-fns/locale'
import type { IntervalsInRangeProps, Range } from '~/components/date/types'
@@ -93,3 +94,17 @@ export function getEndOf(date: Date, intervalType: string): Date {
return endOfDay(date)
}
}
+
+export function getUCalendarToday() {
+ return today(getLocalTimeZone())
+}
+
+export function parseUCalendarDate(date: number) {
+ return parseDate(formatISO(date, { representation: 'date' }))
+}
+
+export function getUCalendarTimedDate(date: any) {
+ const now = new Date()
+ const selectedDate = date.toString()
+ return new Date(selectedDate).setHours(now.getHours(), now.getMinutes())
+}
diff --git a/app/components/stat/date/Selector.vue b/app/components/stat/date/Selector.vue
index 19a81f86..17d86b1c 100644
--- a/app/components/stat/date/Selector.vue
+++ b/app/components/stat/date/Selector.vue
@@ -1,14 +1,12 @@
<script setup lang="ts">
import type { Grouped, Range, StatDateProvider } from '~/components/date/types'
+import { getUCalendarTimedDate, getUCalendarToday, parseUCalendarDate } from '~/components/date/utils'
+
const emit = defineEmits<{
onClose: []
}>()
-function close() {
- emit('onClose')
-}
-
const { t } = useI18n()
const statDate = inject('statDate') as StatDateProvider
@@ -34,6 +32,21 @@ const intervals = computed<Grouped[]>(() => [{
function selectInterval(grouped: Grouped) {
statDate.setInterval(grouped)
}
+
+const dateRange = ref({
+ end: parseUCalendarDate(statDate.range.value.end),
+ start: parseUCalendarDate(statDate.range.value.start),
+})
+
+function onSelectRange(value: unknown) {
+ const range = {
+ end: value.end,
+ start: value.start,
+ } as Range
+
+ statDate.setRangeByCalendar(range)
+ emit('onClose')
+}
</script>
<template>
@@ -46,7 +59,7 @@ function selectInterval(grouped: Grouped) {
:isActive="tabs.selected.value === tab"
@click="tabs.selected.value = tab"
>
- {{ t(tab) }}
+ {{ t(`dates.calendar.${tab}`) }}
</UiTabsItem1>
</UiTabs1>
@@ -83,7 +96,7 @@ function selectInterval(grouped: Grouped) {
<!-- Grouped by -->
<div class="grid gap-3">
<UiTitleOption class="px-1">
- {{ t('intervalsGrouped') }}
+ {{ t('dates.calendar.intervalsGrouped') }}
</UiTitleOption>
<div class="grid gap-2">
@@ -113,45 +126,13 @@ function selectInterval(grouped: Grouped) {
</div>
</div>
- <DatePicker
+ <UCalendar
v-if="tabs.selected.value === 'calendar'"
- :rows="2"
- :value="statDate.range.value"
- mode="range"
- @update:modelValue="(v: Range) => {
- statDate.setRangeByCalendar(v);
- close();
- }"
+ v-model="dateRange"
+ :maxValue="getUCalendarToday()"
+ :numberOfMonths="2"
+ range
+ @update:modelValue="onSelectRange"
/>
</div>
</template>
-
-<i18n lang="yaml">
-en:
- 12Months: 12m
- 14Days: 14d
- 30Days: 30d
- 7Days: 7d
- all: All
- allSkipEmpty: Maximum
- calendar: Calendar
- intervalsGrouped: Grouped by
- last: Last
- mini: mini
- presets: Presets
- ranges: Ranges
-
-ru:
- 12Months: 12м
- 14Days: 14д
- 30Days: 30д
- 7Days: 7д
- all: Все
- allSkipEmpty: Максимально
- calendar: Календарь
- intervalsGrouped: Группировка
- last: Последние
- mini: мини
- presets: Пресеты
- ranges: Диапазоны
-</i18n>
diff --git a/app/components/trnForm/Calendar.vue b/app/components/trnForm/Calendar.vue
index c02a7d9c..153d5ee4 100644
--- a/app/components/trnForm/Calendar.vue
+++ b/app/components/trnForm/Calendar.vue
@@ -1,28 +1,33 @@
<script setup lang="ts">
+/*
+ * UCalendar has bad type definitions
+ */
+import { getUCalendarTimedDate, getUCalendarToday, parseUCalendarDate } from '~/components/date/utils'
import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'
const props = defineProps<{
- hide: () => void
+ onClose: () => void
}>()
const trnsFormStore = useTrnsFormStore()
-const date = ref(new Date(trnsFormStore.values.date).toISOString())
-const maxDate = new Date()
+const date = ref(parseUCalendarDate(trnsFormStore.values.date))
+const maxDate = getUCalendarToday()
-watch(date, (value) => {
- if (value) {
- trnsFormStore.values.date = new Date(+value).getTime()
- props.hide()
- }
-})
+function onUpdate(date: unknown) {
+ props.onClose()
+
+ if (!date)
+ return
+
+ trnsFormStore.values.date = getUCalendarTimedDate(date)
+}
</script>
<template>
- <DatePicker
- :value="date"
- :maxDate="maxDate"
- expanded
- mode="date"
- @update:modelValue="value => date = value"
+ <UCalendar
+ v-model="date"
+ :maxValue="maxDate"
+ class="p-10"
+ @update:modelValue="onUpdate"
/>
</template>
diff --git a/app/components/trnForm/Date.vue b/app/components/trnForm/Date.vue
index ba105a5f..a364da45 100644
--- a/app/components/trnForm/Date.vue
+++ b/app/components/trnForm/Date.vue
@@ -9,10 +9,8 @@ const trnsFormStore = useTrnsFormStore()
const { formatDate } = useDateFormats()
const formattedDate = computed(() => formatDate(trnsFormStore.values.date, 'full'))
-
-const isToday = computed(() => {
- return isSameDay(new Date(trnsFormStore.values.date), new Date())
-})
+const isToday = computed(() => isSameDay(new Date(trnsFormStore.values.date), new Date()))
+const isShow = ref(false)
function changeDate(way: 'prev' | 'next' | 'today') {
let newDate: number = new Date().getTime()
@@ -25,8 +23,6 @@ function changeDate(way: 'prev' | 'next' | 'today') {
trnsFormStore.values.date = newDate
}
-
-const isShow = ref(false)
</script>
<template>
@@ -52,7 +48,7 @@ const isShow = ref(false)
<template #content="{ close }">
<div class="min-w-80">
- <TrnFormCalendar :hide="close" />
+ <TrnFormCalendar :onClose="close" />
</div>
</template>
</BottomSheetOrDropdown>
diff --git a/app/components/trnForm/Selection.vue b/app/components/trnForm/Selection.vue
index 85fa30ba..01271ab7 100644
--- a/app/components/trnForm/Selection.vue
+++ b/app/components/trnForm/Selection.vue
@@ -5,8 +5,8 @@ import { Pagination } from 'swiper/modules'
import type { CategoryId } from '~/components/categories/types'
import type { WalletId } from '~/components/wallets/types'
-import { getParentCategoryId } from '~/components/categories/getCategories'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
+import { getParentCategoryIdOrReturnSame } from '~/components/categories/utils'
import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'
import 'swiper/css'
@@ -49,7 +49,7 @@ onMounted(() => {
}
})
-const parentCategoryId = ref<CategoryId | null>(getParentCategoryId(categoriesStore.items, trnsFormStore.values.categoryId) || trnsFormStore.values.categoryId)
+const parentCategoryId = ref<CategoryId | null>(getParentCategoryIdOrReturnSame(categoriesStore.items, trnsFormStore.values.categoryId))
function onSelectWallet(id: WalletId, close: () => void) {
trnsFormStore.values.walletId = id
diff --git a/app/pages/dev.vue b/app/pages/dev.vue
index c8c585bb..0f344a32 100644
--- a/app/pages/dev.vue
+++ b/app/pages/dev.vue
@@ -3,8 +3,6 @@
<template>
<UiPage>
- <ThemeSwitcherTabs />
-
<div class="grid grid-cols-[1fr_auto] items-start gap-8 p-3">
<div class="grid gap-12">
<div>
@@ -275,19 +273,12 @@
</h1>
<div class="grid gap-4">
- <DatePicker
- :value="new Date().getTime()"
- :maxDate="new Date()"
- mode="date"
- />
- <DatePicker
- :value="{
- end: new Date().getTime() - 1000 * 60 * 60 * 24 * 4,
- start: new Date().getTime() - 1000 * 60 * 60 * 24 * 14,
- }"
- mode="range"
- :maxDate="new Date()"
- :rows="2"
+ <UCalendar />
+ <div class="h-px bg-muted" />
+ <UCalendar
+ class="@container/calendar"
+ :numberOfMonths="2"
+ range
/>
</div>
</div>
diff --git a/i18n/locales/en-US.js b/i18n/locales/en-US.js
index d3e1516e..ac0d6a6d 100644
--- a/i18n/locales/en-US.js
+++ b/i18n/locales/en-US.js
@@ -124,6 +124,11 @@ export default {
},
dates: {
+ calendar: {
+ calendar: 'Calendar',
+ intervalsGrouped: 'Grouped by',
+ presets: 'Presets',
+ },
day: {
current: 'Today',
last: 'Yesterday',
diff --git a/i18n/locales/ru-RU.js b/i18n/locales/ru-RU.js
index beae33fc..13fbc6f4 100644
--- a/i18n/locales/ru-RU.js
+++ b/i18n/locales/ru-RU.js
@@ -122,6 +122,11 @@ export default {
},
dates: {
+ calendar: {
+ calendar: 'Календарь',
+ intervalsGrouped: 'Группировка',
+ presets: 'Пресеты',
+ },
day: {
current: 'Сегодня',
last: 'Вчера',