Skip to content

Commit

Permalink
feat(proposal): add validation to start time (#3991)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Todmy authored Jun 20, 2023
1 parent 4412ac3 commit abf53c7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
35 changes: 31 additions & 4 deletions src/components/ModalSelectDate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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: '' };
Expand All @@ -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');
}
Expand All @@ -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;
});
</script>

<template>
Expand All @@ -56,11 +78,16 @@ watch(open, () => {
<BaseCalendar v-model="input" class="mx-auto mb-2" />
</div>
</div>
<div v-else class="m-4 mx-auto max-w-[140px]">
<div v-else class="m-4">
<input
v-model="time"
type="time"
class="s-input form-input text-center text-lg"
class="s-input form-input mx-auto max-w-[140px] text-center text-lg"
/>
<TuneErrorInput
v-if="!isTimeValid"
class="mx-auto mt-2 text-center"
:error="$t('create.errorTimeInPast')"
/>
</div>
<template #footer>
Expand All @@ -72,7 +99,7 @@ watch(open, () => {
<div class="float-left w-2/4 pl-2">
<BaseButton
type="submit"
:disabled="!input"
:disabled="!isTimeValid"
class="w-full"
primary
@click="handleSubmit"
Expand Down
3 changes: 2 additions & 1 deletion src/locales/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@
"uploadImageExplainer": "Attach images by dragging & dropping, selecting or pasting them.",
"uploading": "Uploading image",
"markdown": "Styling with Markdown is supported",
"errorGettingSnapshot": "We encountered an error while fetching the snapshot block number which is needed to calculate your voting power. Please try again later."
"errorGettingSnapshot": "We encountered an error while fetching the snapshot block number which is needed to calculate your voting power. Please try again later.",
"errorTimeInPast": "Entered time is in the past. Please select a future time."
},
"delegate": {
"header": "Delegate",
Expand Down
3 changes: 2 additions & 1 deletion src/views/SpaceCreate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ function getFormattedForm() {
.map(choice => choice.text)
.filter(choiceText => choiceText.length > 0);
updateTime();
clonedForm.start = dateStart.value;
const thisMomentTimestamp = parseInt((Date.now() / 1e3).toFixed());
clonedForm.start = Math.max(thisMomentTimestamp, dateStart.value);
clonedForm.end = dateEnd.value;
return clonedForm;
}
Expand Down

1 comment on commit abf53c7

@vercel
Copy link

@vercel vercel bot commented on abf53c7 Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.