-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce inheritance to the Parameter structure (#4049)
* Start draft for new Parameter structure * Add the rest of the methods * EnumParameter * QueryBasedDropdownParameter * DateParameter * DateRangeParameter * Update Parameter usage on code * Merge dynamicValue into normalizedValue * Add updateLocals and omit unwanted props * Allow null NumberParameter and omit parentQueryId * Rename parameter getValue to getExecutionValue * Update $$value to normalizedValue + omit on save * Add a few comments * Remove ngModel property from Parameter * Use value directly in DateRangeParameter * Use simpler separator for DateRange url param * Add backward compatibility * Use normalizeValue null value for isEmpty * Start creating jest tests * Add more tests * Normalize null value for multi mode in Enum * Use saved value for param isEmpty
- Loading branch information
1 parent
246eca1
commit 9f78446
Showing
23 changed files
with
1,023 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { findKey, startsWith, has, includes, isNull, values } from 'lodash'; | ||
import moment from 'moment'; | ||
import PropTypes from 'prop-types'; | ||
import { Parameter } from '.'; | ||
|
||
const DATETIME_FORMATS = { | ||
// eslint-disable-next-line quote-props | ||
'date': 'YYYY-MM-DD', | ||
'datetime-local': 'YYYY-MM-DD HH:mm', | ||
'datetime-with-seconds': 'YYYY-MM-DD HH:mm:ss', | ||
}; | ||
|
||
const DYNAMIC_PREFIX = 'd_'; | ||
|
||
const DYNAMIC_DATES = { | ||
now: { | ||
name: 'Today/Now', | ||
value: () => moment(), | ||
}, | ||
yesterday: { | ||
name: 'Yesterday', | ||
value: () => moment().subtract(1, 'day'), | ||
}, | ||
}; | ||
|
||
export const DynamicDateType = PropTypes.oneOf(values(DYNAMIC_DATES)); | ||
|
||
function isDynamicDateString(value) { | ||
return startsWith(value, DYNAMIC_PREFIX) && has(DYNAMIC_DATES, value.substring(DYNAMIC_PREFIX.length)); | ||
} | ||
|
||
export function isDynamicDate(value) { | ||
return includes(DYNAMIC_DATES, value); | ||
} | ||
|
||
export function getDynamicDateFromString(value) { | ||
if (!isDynamicDateString(value)) { | ||
return null; | ||
} | ||
return DYNAMIC_DATES[value.substring(DYNAMIC_PREFIX.length)]; | ||
} | ||
|
||
class DateParameter extends Parameter { | ||
constructor(parameter, parentQueryId) { | ||
super(parameter, parentQueryId); | ||
this.useCurrentDateTime = parameter.useCurrentDateTime; | ||
this.setValue(parameter.value); | ||
} | ||
|
||
get hasDynamicValue() { | ||
return isDynamicDate(this.normalizedValue); | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
normalizeValue(value) { | ||
if (isDynamicDateString(value)) { | ||
return getDynamicDateFromString(value); | ||
} | ||
|
||
if (isDynamicDate(value)) { | ||
return value; | ||
} | ||
|
||
const normalizedValue = moment(value); | ||
return normalizedValue.isValid() ? normalizedValue : null; | ||
} | ||
|
||
setValue(value) { | ||
const normalizedValue = this.normalizeValue(value); | ||
if (isDynamicDate(normalizedValue)) { | ||
this.value = DYNAMIC_PREFIX + findKey(DYNAMIC_DATES, normalizedValue); | ||
} else if (moment.isMoment(normalizedValue)) { | ||
this.value = normalizedValue.format(DATETIME_FORMATS[this.type]); | ||
} else { | ||
this.value = normalizedValue; | ||
} | ||
this.$$value = normalizedValue; | ||
|
||
this.updateLocals(); | ||
this.clearPendingValue(); | ||
return this; | ||
} | ||
|
||
getExecutionValue() { | ||
if (this.hasDynamicValue) { | ||
return this.normalizedValue.value().format(DATETIME_FORMATS[this.type]); | ||
} | ||
if (isNull(this.value) && this.useCurrentDateTime) { | ||
return moment().format(DATETIME_FORMATS[this.type]); | ||
} | ||
return this.value; | ||
} | ||
} | ||
|
||
export default DateParameter; |
Oops, something went wrong.