terra-clinical-onset-picker
Version:
The terra-clinical-onset-picker component provides users a way to enter or select an approximate date for use in onset scenarios.
318 lines (310 loc) • 11.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _moment = _interopRequireDefault(require("moment"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var maxWeeks = 8;
var maxMonths = 24;
var PrecisionOptions = {
ONAT: 'on/at',
ABOUT: 'about',
BEFORE: 'before',
AFTER: 'after',
UNKNOWN: 'unknown'
};
var DATE_FORMAT = 'YYYY-MM-DD';
var OnsetUtils = /*#__PURE__*/function () {
function OnsetUtils() {
_classCallCheck(this, OnsetUtils);
}
return _createClass(OnsetUtils, null, [{
key: "allowedMonths",
value:
/*
* Create object to pass to SelectField for month options
* Filters out months before birth or after the current date.
*/
function allowedMonths(intl, birthdate, year) {
var start = (0, _moment.default)(birthdate);
var end = (0, _moment.default)();
var possibleMonths = [{
value: '0',
display: intl.formatMessage({
id: 'Terra.onsetPicker.january'
})
}, {
value: '1',
display: intl.formatMessage({
id: 'Terra.onsetPicker.february'
})
}, {
value: '2',
display: intl.formatMessage({
id: 'Terra.onsetPicker.march'
})
}, {
value: '3',
display: intl.formatMessage({
id: 'Terra.onsetPicker.april'
})
}, {
value: '4',
display: intl.formatMessage({
id: 'Terra.onsetPicker.may'
})
}, {
value: '5',
display: intl.formatMessage({
id: 'Terra.onsetPicker.june'
})
}, {
value: '6',
display: intl.formatMessage({
id: 'Terra.onsetPicker.july'
})
}, {
value: '7',
display: intl.formatMessage({
id: 'Terra.onsetPicker.august'
})
}, {
value: '8',
display: intl.formatMessage({
id: 'Terra.onsetPicker.september'
})
}, {
value: '9',
display: intl.formatMessage({
id: 'Terra.onsetPicker.october'
})
}, {
value: '10',
display: intl.formatMessage({
id: 'Terra.onsetPicker.november'
})
}, {
value: '11',
display: intl.formatMessage({
id: 'Terra.onsetPicker.december'
})
}];
if (start.year().toString() === year || start.year() === end.year()) {
possibleMonths = possibleMonths.filter(function (month) {
return month.value > start.month();
});
}
if (end.year().toString() === year || start.year() === end.year()) {
possibleMonths = possibleMonths.filter(function (month) {
return month.value <= end.month();
});
}
return possibleMonths;
}
/*
* Create object to pass to SelectField for year options.
* Populates birth year to current year.
*/
}, {
key: "allowedYears",
value: function allowedYears(granularity, month, birthdate) {
var start = (0, _moment.default)(birthdate).year();
if (granularity === 'month' && month <= (0, _moment.default)(birthdate).month() || granularity === 'year') {
start += 1;
}
var end = (0, _moment.default)().year();
if (granularity === 'month' && month > (0, _moment.default)().month()) {
end -= 1;
}
if (month > (0, _moment.default)().month() && start === end && granularity !== 'month') {
start = '';
end = '';
}
return Array(end - start + 1).fill(undefined).map(function (x, idx) {
var year = start + idx;
return {
value: start !== '' ? year.toString() : '',
display: start !== '' ? year.toString() : ''
};
});
}
/**
* Calulcates maximum number allowed for each age duration.
* Will use the lesser of power of two or the difference between birth for week/month.
* Power of two for maximum weeks/months (8 weeks = 2 months, 24 months = 2 years)
*/
}, {
key: "allowedAge",
value: function allowedAge(birthdate, ageUnit) {
var birthMoment = (0, _moment.default)(birthdate).startOf('day'); // startOf to clear time from values
var currentMoment = (0, _moment.default)().startOf('day');
switch (ageUnit) {
case 'years':
return currentMoment.diff(birthMoment, 'years');
case 'months':
{
var monthDiff = currentMoment.diff(birthMoment, 'months');
return monthDiff > maxMonths ? maxMonths : monthDiff;
}
default:
{
var weekDiff = currentMoment.diff(birthMoment, 'weeks');
return weekDiff > maxWeeks ? maxWeeks : weekDiff;
}
}
}
/*
* Create object to pass to SelectField for age unit options.
* Does not add durations that are not possible based on birth.
*/
}, {
key: "allowedAgeUnits",
value: function allowedAgeUnits(birthdate, intl) {
var ageMoment = (0, _moment.default)(birthdate).startOf('day'); // startOf to clear time from values
var currentMoment = (0, _moment.default)().startOf('day');
if (currentMoment.diff(ageMoment, 'weeks') === 0) {
return [];
}
var ageUnits = [{
value: 'weeks',
display: intl.formatMessage({
id: 'Terra.onsetPicker.agePrecisionWeek'
})
}];
// Do not add month option if less than a month old
if (currentMoment.diff(ageMoment, 'months') > 0) {
ageUnits.push({
value: 'months',
display: intl.formatMessage({
id: 'Terra.onsetPicker.agePrecisionMonth'
})
});
}
// Do not add year option if less than a year old
if (currentMoment.diff(ageMoment, 'years') > 0) {
ageUnits.push({
value: 'years',
display: intl.formatMessage({
id: 'Terra.onsetPicker.agePrecisionYear'
})
});
}
return ageUnits;
}
/**
* Converts onset date to a age value with lowest possible age unit (weeks, then months, then years).
*/
}, {
key: "onsetToAge",
value: function onsetToAge(birthdate, onsetDate) {
if (onsetDate === undefined) {
return {
age: undefined,
ageUnit: undefined
};
}
var birthMoment = (0, _moment.default)(birthdate).startOf('day'); // startOf to clear time from values
var onsetMoment = onsetDate.startOf('day');
var yearDiff = onsetMoment.diff(birthMoment, 'years');
var monthDiff = onsetMoment.diff(birthMoment, 'months');
var weekDiff = onsetMoment.diff(birthMoment, 'weeks');
var dayDiff = onsetMoment.diff(birthMoment, 'days');
var onset = onsetMoment.format(DATE_FORMAT);
var addYear = '';
var addMonth = '';
if (birthMoment.format(DATE_FORMAT) === (0, _moment.default)().year(birthMoment.year()).month(birthMoment.month()).endOf('month').format(DATE_FORMAT) && onsetMoment.subtract(1, 'days').daysInMonth() < birthMoment.daysInMonth()) {
addYear = (0, _moment.default)(birthMoment).add(yearDiff, 'years').add(1, 'days').format(DATE_FORMAT);
addMonth = (0, _moment.default)(birthMoment).add(monthDiff, 'months').add(1, 'days').format(DATE_FORMAT);
} else {
addYear = (0, _moment.default)(birthMoment).add(yearDiff, 'years').format(DATE_FORMAT);
addMonth = (0, _moment.default)(birthMoment).add(monthDiff, 'months').format(DATE_FORMAT);
}
if (monthDiff > 24 && monthDiff % 12 === 0 && addYear === onset) {
return {
age: yearDiff,
ageUnit: 'years'
};
}
if (monthDiff <= 24 && addMonth === onset) {
return {
age: monthDiff,
ageUnit: 'months'
};
}
if (weekDiff <= 8 && dayDiff % 7 === 0) {
return {
age: weekDiff,
ageUnit: 'weeks'
};
}
return {
age: undefined,
ageUnit: undefined
};
}
/**
* Returns a translated set of precisions to be used in the precision SelectField.
*/
}, {
key: "allowedPrecisions",
value: function allowedPrecisions(intl, precisionSet) {
var precisions = [];
for (var i = 0; i < precisionSet.length; i += 1) {
switch (precisionSet[i]) {
case PrecisionOptions.ONAT:
precisions.push({
value: PrecisionOptions.ONAT,
display: intl.formatMessage({
id: 'Terra.onsetPicker.precisionOnAt'
})
});
break;
case PrecisionOptions.ABOUT:
precisions.push({
value: PrecisionOptions.ABOUT,
display: intl.formatMessage({
id: 'Terra.onsetPicker.precisionAbout'
})
});
break;
case PrecisionOptions.BEFORE:
precisions.push({
value: PrecisionOptions.BEFORE,
display: intl.formatMessage({
id: 'Terra.onsetPicker.precisionBefore'
})
});
break;
case PrecisionOptions.AFTER:
precisions.push({
value: PrecisionOptions.AFTER,
display: intl.formatMessage({
id: 'Terra.onsetPicker.precisionAfter'
})
});
break;
case PrecisionOptions.UNKNOWN:
precisions.push({
value: PrecisionOptions.UNKNOWN,
display: intl.formatMessage({
id: 'Terra.onsetPicker.precisionUnknown'
})
});
break;
default:
break;
}
}
return precisions;
}
}]);
}();
OnsetUtils.PrecisionOptions = PrecisionOptions;
var _default = exports.default = OnsetUtils;