From cbac17c05908ba3be33f1e8a50a19317673c58ae Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sat, 28 Jan 2023 13:32:39 -0900 Subject: [PATCH] feat: use native date picker + align date formats (#251) * use native date picker * use YYYY-MM-DD for date formats --- frontend/components/Form/DatePicker.vue | 143 +++++------------------- frontend/composables/use-formatters.ts | 6 +- frontend/lib/api/classes/stats.ts | 4 +- 3 files changed, 31 insertions(+), 122 deletions(-) diff --git a/frontend/components/Form/DatePicker.vue b/frontend/components/Form/DatePicker.vue index d04a28b..f216cc7 100644 --- a/frontend/components/Form/DatePicker.vue +++ b/frontend/components/Form/DatePicker.vue @@ -1,38 +1,15 @@ @@ -51,88 +28,20 @@ }, }); - const selected = useVModel(props, "modelValue", emit); - const dateText = computed(() => { - if (!validDate(selected.value)) { - return ""; - } - - if (selected.value) { - return selected.value.toLocaleDateString(); - } - - return ""; - }); - - const time = ref(new Date()); - function resetTime() { - time.value = new Date(); - } - - const label = ref(); - onClickOutside(label, () => { - resetTime(); - }); - - const month = computed(() => { - return time.value.toLocaleString("default", { month: "long" }); - }); - - const year = computed(() => { - return time.value.getFullYear(); - }); - - function nextMonth() { - time.value.setMonth(time.value.getMonth() + 1); - time.value = new Date(time.value); - } - - function prevMonth() { - time.value.setMonth(time.value.getMonth() - 1); - time.value = new Date(time.value); - } - - const daysIdx = computed(() => { - return ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]; - }); - - function select(e: MouseEvent, day: Date) { - selected.value = day; - // @ts-ignore - this is a vue3 bug - e.target.blur(); - resetTime(); - } - - type DayEntry = { - number: number | string; - date: Date; - }; - - function daysInMonth(month: number, year: number) { - return new Date(year, month, 0).getDate(); - } - - const days = computed(() => { - const days = []; - - const totalDays = daysInMonth(time.value.getMonth() + 1, time.value.getFullYear()); - - const firstDay = new Date(time.value.getFullYear(), time.value.getMonth(), 1).getDay(); - - for (let i = 0; i < firstDay; i++) { - days.push({ - number: "", - date: new Date(), - }); - } - - for (let i = 1; i <= totalDays; i++) { - days.push({ - number: i, - date: new Date(time.value.getFullYear(), time.value.getMonth(), i), - }); - } - - return days; + const selected = computed({ + get() { + // return modelValue as string as YYYY-MM-DD or null + return props.modelValue ? props.modelValue.toISOString().split("T")[0] : null; + }, + set(value: string | null) { + // emit update:modelValue with a Date object or null + emit("update:modelValue", value ? new Date(value) : null); + }, }); + + diff --git a/frontend/composables/use-formatters.ts b/frontend/composables/use-formatters.ts index 58ed22d..4a0b5b5 100644 --- a/frontend/composables/use-formatters.ts +++ b/frontend/composables/use-formatters.ts @@ -63,11 +63,11 @@ export function fmtDate(value: string | Date, fmt: DateTimeFormat = "human"): st switch (fmt) { case "relative": - return useTimeAgo(dt).value + useDateFormat(dt, " (MM-DD-YYYY)").value; + return useTimeAgo(dt).value + useDateFormat(dt, " (YYYY-MM-DD)").value; case "long": - return useDateFormat(dt, "MM-DD-YYYY (dddd)").value; + return useDateFormat(dt, "YYYY-MM-DD (dddd)").value; case "short": - return useDateFormat(dt, "MM-DD-YYYY").value; + return useDateFormat(dt, "YYYY-MM-DD").value; case "human": // January 1st, 2021 return `${months[dt.getMonth()]} ${dt.getDate()}${ordinalIndicator(dt.getDate())}, ${dt.getFullYear()}`; diff --git a/frontend/lib/api/classes/stats.ts b/frontend/lib/api/classes/stats.ts index b605270..7959269 100644 --- a/frontend/lib/api/classes/stats.ts +++ b/frontend/lib/api/classes/stats.ts @@ -1,7 +1,7 @@ import { BaseAPI, route } from "../base"; import { GroupStatistics, TotalsByOrganizer, ValueOverTime } from "../types/data-contracts"; -function YYYY_DD_MM(date?: Date): string { +function YYYY_MM_DD(date?: Date): string { if (!date) { return ""; } @@ -14,7 +14,7 @@ function YYYY_DD_MM(date?: Date): string { export class StatsAPI extends BaseAPI { totalPriceOverTime(start?: Date, end?: Date) { return this.http.get({ - url: route("/groups/statistics/purchase-price", { start: YYYY_DD_MM(start), end: YYYY_DD_MM(end) }), + url: route("/groups/statistics/purchase-price", { start: YYYY_MM_DD(start), end: YYYY_MM_DD(end) }), }); }