From a8635e8c8bc4f25a7ea1d1685c3627014598e549 Mon Sep 17 00:00:00 2001 From: leigh-mil Date: Thu, 26 Sep 2019 15:23:49 -0400 Subject: [PATCH] Add explanation about maxStartDate and minEndDate, Refactor calc max and min functions --- .../__tests__/pop_date_range.test.js | 6 +++ js/components/pop_date_range.js | 50 +++++++++++-------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/js/components/__tests__/pop_date_range.test.js b/js/components/__tests__/pop_date_range.test.js index ada35c15..17273bff 100644 --- a/js/components/__tests__/pop_date_range.test.js +++ b/js/components/__tests__/pop_date_range.test.js @@ -8,6 +8,12 @@ import { makeTestWrapper } from '../../test_utils/component_test_helpers' const PopDateRangeWrapper = makeTestWrapper({ components: { PopDateRange }, templatePath: 'pop_date_range.html', + data: function() { + return { + initialMinStartDate: '2019-09-14', + initialMaxEndDate: '2022-09-14', + } + }, }) describe('PopDateRange Test', () => { diff --git a/js/components/pop_date_range.js b/js/components/pop_date_range.js index 7abac268..0804066c 100644 --- a/js/components/pop_date_range.js +++ b/js/components/pop_date_range.js @@ -27,45 +27,51 @@ export default { }, data: function() { - var start = !!this.initialStartDate ? new Date(this.initialStartDate) : null - var end = !!this.initialEndDate ? new Date(this.initialEndDate) : null - var contractStart = new Date(this.initialMinStartDate) - var contractEnd = new Date(this.initialMaxEndDate) + let start = !!this.initialStartDate + ? new Date(this.initialStartDate) + : false + let contractStart = new Date(this.initialMinStartDate) + let minEndDate = start && start > contractStart ? start : contractStart + let end = !!this.initialEndDate ? new Date(this.initialEndDate) : false + let contractEnd = new Date(this.initialMaxEndDate) + let maxStartDate = end && end < contractEnd ? end : contractEnd + + // the maxStartDate and minEndDate change based on user input: + // the latest date the start can be is the PoP end date + // the earliest date the end can be is the PoP start date + // if the form is initialized with out a PoP, the maxStartDate and minEndDate + // default to the contract dates return { - maxStartDate: this.calcMaxStartDate(end, contractEnd), - minEndDate: this.calcMinEndDate(start, contractStart), + maxStartDate: maxStartDate, + minEndDate: minEndDate, } }, methods: { handleDateChange: function(event) { - if (event.name.includes(START_DATE)) { - if (event.valid != undefined && event.valid) { - var date = new Date(event.value) - this.minEndDate = this.calcMinEndDate(date) - } - } else if (event.name.includes(END_DATE)) { - if (event.valid != undefined && event.valid) { - var date = new Date(event.value) - this.maxStartDate = this.calcMaxStartDate(date) - } + if (event.name.includes(START_DATE) && event.valid) { + let date = new Date(event.value) + this.minEndDate = this.calcMinEndDate(date) + } else if (event.name.includes(END_DATE) && event.valid) { + let date = new Date(event.value) + this.maxStartDate = this.calcMaxStartDate(date) } }, - calcMaxStartDate: function(date, end = this.maxStartDate) { - if (!!date && date < end) { + calcMaxStartDate: function(date) { + if (!!date && date < this.maxStartDate) { return date } else { - return end + return this.maxStartDate } }, - calcMinEndDate: function(date, start = this.minEndDate) { - if (!!date && date > start) { + calcMinEndDate: function(date) { + if (!!date && date > this.minEndDate) { return date } else { - return start + return this.minEndDate } }, },