2020-07-11 17:55:47 +00:00
|
|
|
<template>
|
2021-05-24 09:34:48 +00:00
|
|
|
<div>
|
|
|
|
<div class="d-flex">
|
|
|
|
<b-form-group class="col-md-6" label="Start date" label-for="dp_dateFrom">
|
|
|
|
<b-input-group>
|
|
|
|
<b-input-group-prepend>
|
|
|
|
<b-form-datepicker v-model="dateFrom" class="mb-1" button-only></b-form-datepicker>
|
|
|
|
</b-input-group-prepend>
|
|
|
|
<b-form-input
|
|
|
|
id="dp_dateFrom"
|
|
|
|
v-model="dateFrom"
|
|
|
|
type="text"
|
|
|
|
placeholder="YYYY-MM-DD"
|
|
|
|
autocomplete="off"
|
|
|
|
></b-form-input>
|
|
|
|
</b-input-group>
|
|
|
|
</b-form-group>
|
2022-11-18 18:48:15 +00:00
|
|
|
<b-form-group class="ms-2 col-md-6" label="End date" label-for="dp_dateTo">
|
2021-05-24 09:34:48 +00:00
|
|
|
<b-input-group>
|
|
|
|
<b-input-group-prepend>
|
|
|
|
<b-form-datepicker v-model="dateTo" class="mb-1" button-only></b-form-datepicker>
|
|
|
|
</b-input-group-prepend>
|
|
|
|
<b-form-input
|
|
|
|
id="dp_dateTo"
|
|
|
|
v-model="dateTo"
|
|
|
|
type="text"
|
|
|
|
placeholder="YYYY-MM-DD"
|
|
|
|
autocomplete="off"
|
|
|
|
></b-form-input>
|
|
|
|
</b-input-group>
|
|
|
|
</b-form-group>
|
|
|
|
</div>
|
2022-10-30 13:26:23 +00:00
|
|
|
<label class="text-start">
|
2021-05-24 09:34:48 +00:00
|
|
|
Timerange: <b>{{ timeRange }}</b>
|
|
|
|
</label>
|
|
|
|
</div>
|
2020-07-11 17:55:47 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2020-12-05 18:32:37 +00:00
|
|
|
import { dateFromString, dateStringToTimeRange, timestampToDateString } from '@/shared/formatters';
|
2022-07-07 18:44:19 +00:00
|
|
|
import { defineComponent, ref, computed, onMounted, watch } from 'vue';
|
2020-07-11 17:55:47 +00:00
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
|
2022-04-21 04:37:57 +00:00
|
|
|
export default defineComponent({
|
|
|
|
name: 'TimeRangeSelect',
|
|
|
|
props: {
|
2022-11-30 18:48:46 +00:00
|
|
|
modelValue: { required: true, type: String },
|
2022-04-21 04:37:57 +00:00
|
|
|
},
|
|
|
|
setup(props, { emit }) {
|
|
|
|
const dateFrom = ref<string>('');
|
|
|
|
const dateTo = ref<string>('');
|
2020-07-11 17:55:47 +00:00
|
|
|
|
2022-04-21 04:37:57 +00:00
|
|
|
const timeRange = computed(() => {
|
|
|
|
if (dateFrom.value !== '' || dateTo.value !== '') {
|
|
|
|
return `${dateStringToTimeRange(dateFrom.value)}-${dateStringToTimeRange(dateTo.value)}`;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
});
|
2020-12-05 18:32:37 +00:00
|
|
|
|
2022-04-21 04:37:57 +00:00
|
|
|
const updated = () => {
|
|
|
|
emit('input', timeRange.value);
|
|
|
|
};
|
2020-07-11 17:55:47 +00:00
|
|
|
|
2022-04-21 04:37:57 +00:00
|
|
|
const updateInput = () => {
|
2022-11-30 18:48:46 +00:00
|
|
|
const tr = props.modelValue.split('-');
|
2022-04-21 04:37:57 +00:00
|
|
|
if (tr[0]) {
|
|
|
|
dateFrom.value = timestampToDateString(dateFromString(tr[0], 'yyyyMMdd'));
|
|
|
|
}
|
|
|
|
if (tr.length > 1 && tr[1]) {
|
|
|
|
dateTo.value = timestampToDateString(dateFromString(tr[1], 'yyyyMMdd'));
|
|
|
|
}
|
|
|
|
updated();
|
|
|
|
};
|
2021-07-04 14:25:41 +00:00
|
|
|
|
2022-04-21 04:37:57 +00:00
|
|
|
watch(
|
|
|
|
() => timeRange.value,
|
|
|
|
() => updated(),
|
|
|
|
);
|
2021-07-04 14:25:41 +00:00
|
|
|
|
2022-04-21 04:37:57 +00:00
|
|
|
onMounted(() => {
|
2022-11-30 18:48:46 +00:00
|
|
|
if (!props.modelValue) {
|
2022-04-21 04:37:57 +00:00
|
|
|
dateFrom.value = timestampToDateString(new Date(now.getFullYear(), now.getMonth() - 1, 1));
|
|
|
|
} else {
|
|
|
|
updateInput();
|
|
|
|
}
|
|
|
|
emit('input', timeRange.value);
|
|
|
|
});
|
2020-07-11 17:55:47 +00:00
|
|
|
|
2022-04-21 04:37:57 +00:00
|
|
|
return {
|
|
|
|
dateFrom,
|
|
|
|
dateTo,
|
|
|
|
timeRange,
|
|
|
|
updated,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
2020-07-11 17:55:47 +00:00
|
|
|
</script>
|
|
|
|
|
2021-05-24 09:03:36 +00:00
|
|
|
<style scoped></style>
|