feat: make sure date ranges work with date picker and vice-versa

This commit is contained in:
kolaente 2022-01-09 14:05:23 +01:00
parent fe85611286
commit d128c4a71f
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 45 additions and 25 deletions

View File

@ -13,10 +13,10 @@
v-for="(value, text) in dateRanges" v-for="(value, text) in dateRanges"
:key="text" :key="text"
@click="setDateRange(value)" @click="setDateRange(value)"
:class="{'is-active': dateRange === value}"> :class="{'is-active': from === value[0] && to === value[1]}">
{{ $t(text) }} {{ $t(text) }}
</button> </button>
<button @click="setDateRange('')" :class="{'is-active': customRangeActive}"> <button @click="setDateRange(null)" :class="{'is-active': customRangeActive}">
{{ $t('misc.custom') }} {{ $t('misc.custom') }}
</button> </button>
</div> </div>
@ -25,7 +25,7 @@
{{ $t('input.datepickerRange.from') }} {{ $t('input.datepickerRange.from') }}
<div class="field has-addons"> <div class="field has-addons">
<div class="control is-fullwidth"> <div class="control is-fullwidth">
<input class="input" type="text"/> <input class="input" type="text" v-model="from" @change="emitChanged"/>
</div> </div>
<div class="control"> <div class="control">
<x-button icon="calendar" variant="secondary" data-toggle/> <x-button icon="calendar" variant="secondary" data-toggle/>
@ -36,7 +36,7 @@
{{ $t('input.datepickerRange.to') }} {{ $t('input.datepickerRange.to') }}
<div class="field has-addons"> <div class="field has-addons">
<div class="control is-fullwidth"> <div class="control is-fullwidth">
<input class="input" type="text"/> <input class="input" type="text" v-model="to" @change="emitChanged"/>
</div> </div>
<div class="control"> <div class="control">
<x-button icon="calendar" variant="secondary" data-toggle/> <x-button icon="calendar" variant="secondary" data-toggle/>
@ -45,7 +45,7 @@
</label> </label>
<flat-pickr <flat-pickr
:config="flatPickerConfig" :config="flatPickerConfig"
v-model="dateRange" v-model="flatpickrRange"
/> />
</div> </div>
</div> </div>
@ -81,10 +81,20 @@ const flatPickerConfig = computed(() => ({
}, },
})) }))
const dateRange = ref<string>('') const flatpickrRange = ref<string>('')
const from = ref<string>('')
const to = ref<string>('')
function emitChanged() {
emit('dateChanged', {
dateFrom: from.value,
dateTo: to.value,
})
}
watch( watch(
() => dateRange.value, () => flatpickrRange.value,
(newVal: string | null) => { (newVal: string | null) => {
if (newVal === null) { if (newVal === null) {
return return
@ -96,30 +106,40 @@ watch(
return return
} }
emit('dateChanged', { from.value = fromDate
dateFrom: fromDate, to.value = toDate
dateTo: toDate,
}) emitChanged()
}, },
) )
function setDateRange(range: string) { function setDateRange(range: string[] | null) {
dateRange.value = range if (range === null) {
from.value = ''
to.value = ''
emitChanged()
return
}
from.value = range[0]
to.value = range[1]
emitChanged()
} }
const dateRanges = { const dateRanges = {
// Still using strings for the range instead of an array or object to keep it compatible with the dates from flatpickr 'task.show.today': ['now/d', 'now/d+1d'],
'task.show.today': 'now/d to now/d+1d', 'task.show.thisWeek': ['now/w', 'now/w+1w'],
'task.show.thisWeek': 'now/w to now/w+1w', 'task.show.nextWeek': ['now/w+1w', 'now/w+2w'],
'task.show.nextWeek': 'now/w+1w to now/w+2w', 'task.show.next7Days': ['now', 'now+7d'],
'task.show.next7Days': 'now to now+7d', 'task.show.thisMonth': ['now/M', 'now/M+1M'],
'task.show.thisMonth': 'now/M to now/M+1M', 'task.show.nextMonth': ['now/M+1M', 'now/M+2M'],
'task.show.nextMonth': 'now/M+1M to now/M+2M', 'task.show.next30Days': ['now', 'now+30d'],
'task.show.next30Days': 'now to now+30d',
} }
const customRangeActive = computed<Boolean>(() => { const customRangeActive = computed<Boolean>(() => {
return !Object.values(dateRanges).includes(dateRange.value) return !Object.values(dateRanges).some(el => from.value === el[0] && to.value === el[1])
}) })
</script> </script>
@ -166,16 +186,16 @@ const customRangeActive = computed<Boolean>(() => {
padding: 0; padding: 0;
border: 0; border: 0;
} }
.field .control :deep(.button) { .field .control :deep(.button) {
border: 1px solid var(--input-border-color); border: 1px solid var(--input-border-color);
height: 2.25rem; height: 2.25rem;
&:hover { &:hover {
border: 1px solid var(--input-hover-border-color); border: 1px solid var(--input-hover-border-color);
} }
} }
.label, .input, :deep(.button) { .label, .input, :deep(.button) {
font-size: .9rem; font-size: .9rem;
} }