From 402502fe1d8bb1d3987593a2e0ddc7d5c3c33040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vyzaldy=20Andr=C3=A9s=20Sanchez?= Date: Thu, 4 Apr 2019 23:46:36 -0400 Subject: [PATCH] Validates the to/from dates using computed properties --- src/components/TimedContent.vue | 35 +++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/components/TimedContent.vue b/src/components/TimedContent.vue index 5a0c82e..91d7d23 100644 --- a/src/components/TimedContent.vue +++ b/src/components/TimedContent.vue @@ -39,6 +39,37 @@ export default { countdown: null }; }, + computed: { + toDate() { + const momentTo = moment(this.to); + const momentFrom = moment(this.from); + + if (momentTo.isBefore(momentFrom)) { + throw new Error( + `The 'to' date is before the 'from' date. Please check: FROM ${ + this.from + } TO ${this.to}` + ); + } + + return this.to; + }, + + fromDate() { + const momentTo = moment(this.to); + const momentFrom = moment(this.from); + + if (momentFrom.isAfter(momentTo)) { + throw new Error( + `The 'from' date is after the 'to' date. Please check: FROM ${ + this.from + } TO ${this.to}` + ); + } + + return this.from; + } + }, created() { this.toggleContent(); @@ -50,8 +81,8 @@ export default { methods: { shouldShowContent() { const dateTimeFormat = "YYYY-MM-DD HH:mm:ss"; - const from = moment.tz(this.from, dateTimeFormat, this.timezone); - const to = moment.tz(this.to, dateTimeFormat, this.timezone); + const from = moment.tz(this.fromDate, dateTimeFormat, this.timezone); + const to = moment.tz(this.toDate, dateTimeFormat, this.timezone); return moment.tz(this.timezone).isBetween(from, to); },