frequi_origin/src/components/ftbot/TimeRangeSelect.vue

90 lines
2.4 KiB
Vue
Raw Normal View History

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>
<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>
2022-12-10 12:39:53 +00:00
<script setup lang="ts">
2020-12-05 18:32:37 +00:00
import { dateFromString, dateStringToTimeRange, timestampToDateString } from '@/shared/formatters';
2022-12-10 12:39:53 +00:00
import { ref, computed, onMounted, watch } from 'vue';
2020-07-11 17:55:47 +00:00
const now = new Date();
2022-12-10 12:39:53 +00:00
const props = defineProps({
modelValue: { required: true, type: String },
});
const emit = defineEmits(['update:modelValue']);
const dateFrom = ref<string>('');
const dateTo = ref<string>('');
2020-12-05 18:32:37 +00:00
2022-12-10 12:39:53 +00:00
const timeRange = computed(() => {
if (dateFrom.value !== '' || dateTo.value !== '') {
return `${dateStringToTimeRange(dateFrom.value)}-${dateStringToTimeRange(dateTo.value)}`;
}
return '';
});
2020-07-11 17:55:47 +00:00
2022-12-10 12:39:53 +00:00
const updated = () => {
emit('update:modelValue', timeRange.value);
};
2021-07-04 14:25:41 +00:00
2022-12-10 12:39:53 +00:00
const updateInput = () => {
const tr = props.modelValue.split('-');
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-12-10 12:39:53 +00:00
watch(
() => timeRange.value,
() => updated(),
);
2020-07-11 17:55:47 +00:00
2022-12-10 12:39:53 +00:00
onMounted(() => {
if (!props.modelValue) {
dateFrom.value = timestampToDateString(new Date(now.getFullYear(), now.getMonth() - 1, 1));
} else {
updateInput();
}
emit('update:modelValue', timeRange.value);
});
2020-07-11 17:55:47 +00:00
</script>
<style scoped></style>