diff --git a/.secrets.baseline b/.secrets.baseline index 3acc6dd6..e40d679e 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -199,5 +199,5 @@ } ] }, - "version": "0.12.6" + "version": "0.12.5" } diff --git a/js/components/pop_date_range.js b/js/components/pop_date_range.js index 97d4b097..4e19e128 100644 --- a/js/components/pop_date_range.js +++ b/js/components/pop_date_range.js @@ -1,5 +1,10 @@ +import { format } from 'date-fns' + import DateSelector from './date_selector' +const START_DATE = 'start_date' +const END_DATE = 'end_date' + export default { name: 'pop-date-range', @@ -17,14 +22,54 @@ export default { data: function() { return { - startDate: this.initialStartDate, - endDate: this.initialEndDate, + startDate: new Date(this.initialStartDate), + endDate: new Date(this.initialEndDate), startValid: false, endValid: false, - minStartDate: this.initialMinStartDate, - maxStartDate: this.initialMaxEndDate, - minEndDate: this.initialMinStartDate, - maxEndDate: this.initialMaxEndDate, + minStartDate: new Date(this.initialMinStartDate), + maxEndDate: new Date(this.initialMaxEndDate), } }, + + mounted: function() { + this.$root.$on('field-change', this.handleDateChange) + }, + + methods: { + handleDateChange: function(event) { + if (event.name.includes(START_DATE)) { + if (!!event.value) this.startDate = new Date(event.value) + if (!!event.valid) this.startValid = event.valid + } else if (event.name.includes(END_DATE)) { + if (!!event.value) this.endDate = new Date(event.value) + if (!!event.valid) this.endValid = event.valid + } + }, + + maxStartDate: function() { + if (this.endDate < new Date(this.initialMaxEndDate)) { + return this.endDate + } else { + return this.initialMaxEndDate + } + }, + + minEndDate: function() { + if (this.startDate > new Date(this.initialMinStartDate)) { + return this.startDate + } else { + return this.initialMinEndDate + } + }, + }, + + computed: { + maxStartProp: function() { + return format(this.maxStartDate(), 'YYYY-MM-DD') + }, + + minEndProp: function() { + return format(this.minEndDate(), 'YYYY-MM-DD') + } + } } diff --git a/templates/components/pop_date_range.html b/templates/components/pop_date_range.html index a468b8fa..ad1c3fcd 100644 --- a/templates/components/pop_date_range.html +++ b/templates/components/pop_date_range.html @@ -18,8 +18,8 @@