Add explanation about maxStartDate and minEndDate,

Refactor calc max and min functions
This commit is contained in:
leigh-mil 2019-09-26 15:23:49 -04:00
parent f840bbb941
commit a8635e8c8b
2 changed files with 34 additions and 22 deletions

View File

@ -8,6 +8,12 @@ import { makeTestWrapper } from '../../test_utils/component_test_helpers'
const PopDateRangeWrapper = makeTestWrapper({ const PopDateRangeWrapper = makeTestWrapper({
components: { PopDateRange }, components: { PopDateRange },
templatePath: 'pop_date_range.html', templatePath: 'pop_date_range.html',
data: function() {
return {
initialMinStartDate: '2019-09-14',
initialMaxEndDate: '2022-09-14',
}
},
}) })
describe('PopDateRange Test', () => { describe('PopDateRange Test', () => {

View File

@ -27,45 +27,51 @@ export default {
}, },
data: function() { data: function() {
var start = !!this.initialStartDate ? new Date(this.initialStartDate) : null let start = !!this.initialStartDate
var end = !!this.initialEndDate ? new Date(this.initialEndDate) : null ? new Date(this.initialStartDate)
var contractStart = new Date(this.initialMinStartDate) : false
var contractEnd = new Date(this.initialMaxEndDate) 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 { return {
maxStartDate: this.calcMaxStartDate(end, contractEnd), maxStartDate: maxStartDate,
minEndDate: this.calcMinEndDate(start, contractStart), minEndDate: minEndDate,
} }
}, },
methods: { methods: {
handleDateChange: function(event) { handleDateChange: function(event) {
if (event.name.includes(START_DATE)) { if (event.name.includes(START_DATE) && event.valid) {
if (event.valid != undefined && event.valid) { let date = new Date(event.value)
var date = new Date(event.value) this.minEndDate = this.calcMinEndDate(date)
this.minEndDate = this.calcMinEndDate(date) } else if (event.name.includes(END_DATE) && event.valid) {
} let date = new Date(event.value)
} else if (event.name.includes(END_DATE)) { this.maxStartDate = this.calcMaxStartDate(date)
if (event.valid != undefined && event.valid) {
var date = new Date(event.value)
this.maxStartDate = this.calcMaxStartDate(date)
}
} }
}, },
calcMaxStartDate: function(date, end = this.maxStartDate) { calcMaxStartDate: function(date) {
if (!!date && date < end) { if (!!date && date < this.maxStartDate) {
return date return date
} else { } else {
return end return this.maxStartDate
} }
}, },
calcMinEndDate: function(date, start = this.minEndDate) { calcMinEndDate: function(date) {
if (!!date && date > start) { if (!!date && date > this.minEndDate) {
return date return date
} else { } else {
return start return this.minEndDate
} }
}, },
}, },