From abf53c7d9561d4f5f05bdd7bb592d346cb4ea1b3 Mon Sep 17 00:00:00 2001 From: Dmytro Tolok Date: Wed, 21 Jun 2023 01:55:13 +0200 Subject: [PATCH] feat(proposal): add validation to start time (#3991) * feat(proposal): add validation to start time * refactor(math): use Math.max instead of condition * fix(validation): add validation instead of forcing proper time entering * fix(error): add error message for wrong time --- src/components/ModalSelectDate.vue | 35 ++++++++++++++++++++++++++---- src/locales/default.json | 3 ++- src/views/SpaceCreate.vue | 3 ++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/components/ModalSelectDate.vue b/src/components/ModalSelectDate.vue index 2650b185357..61bfab329ea 100644 --- a/src/components/ModalSelectDate.vue +++ b/src/components/ModalSelectDate.vue @@ -11,6 +11,16 @@ const { open } = toRefs(props); const step = ref(0); const input = ref(''); const time = ref('12:00'); +const isTimeValid = computed(() => { + const isTimeEnteringStep = step.value === 1; + if (!isTimeEnteringStep) return true; + if (!input.value) return false; + + const startDateString = `${input.value} ${time.value}:59`; + const startTimestamp = new Date(startDateString).getTime(); + + return startTimestamp >= Date.now(); +}); function formatDate(date) { const output = { h: '12', m: '00', dateString: '' }; @@ -28,7 +38,8 @@ function handleSubmit() { if (step.value === 0) return (step.value = 1); const dateString = `${input.value} ${time.value}:00`; const timestamp = new Date(dateString).getTime() / 1000; - emit('input', timestamp); + const now = parseInt((Date.now() / 1e3).toFixed()); + emit('input', Math.max(timestamp, now)); emit('close'); } @@ -39,6 +50,17 @@ watch(open, () => { time.value = `${h}:${m}`; input.value = dateString; }); + +watch(step, () => { + if (step.value === 0) return; + const timestamp = Math.max( + props.value, + parseInt((Date.now() / 1e3 + 10).toFixed()) + ); + const { dateString, h, m } = formatDate(timestamp); + time.value = `${h}:${m}`; + input.value = dateString; +});