Skip to content

Commit

Permalink
Merge pull request DannyFeliz#6 from vyzaldysanchez/feature/to-from-v…
Browse files Browse the repository at this point in the history
…alidation

Validates the to/from dates using computed properties
  • Loading branch information
DannyFeliz authored Apr 5, 2019
2 parents 04ddf87 + 402502f commit 4ee1dda
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/components/TimedContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
},
Expand Down

0 comments on commit 4ee1dda

Please sign in to comment.