vault/ui/app/components/calendar-widget.js
hashicorp-copywrite[bot] 0b12cdcfd1
[COMPLIANCE] License changes (#22290)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License.

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* add missing license headers

* Update copyright file headers to BUS-1.1

* Fix test that expected exact offset on hcl file

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Sarah Thompson <sthompson@hashicorp.com>
Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
2023-08-10 18:14:03 -07:00

120 lines
3.9 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import { ARRAY_OF_MONTHS, parseAPITimestamp } from 'core/utils/date-formatters';
import { addYears, isSameYear, subYears } from 'date-fns';
import timestamp from 'core/utils/timestamp';
/**
* @module CalendarWidget
* CalendarWidget component is used in the client counts dashboard to select a month/year to query the /activity endpoint.
* The component returns an object with selected date info, example: { dateType: 'endDate', monthIdx: 0, monthName: 'January', year: 2022 }
*
* @example
* ```js
* <CalendarWidget @startTimestamp={{this.startTime}} @endTimestamp={{this.endTime}} @selectMonth={{this.handleSelection}} />
*
* @param {string} startTimestamp - ISO timestamp string of the calendar widget's start time, displays in dropdown trigger
* @param {string} endTimestamp - ISO timestamp string for the calendar widget's end time, displays in dropdown trigger
* @param {function} selectMonth - callback function from parent - fires when selecting a month or clicking "Current billing period"
* />
* ```
*/
export default class CalendarWidget extends Component {
currentDate = timestamp.now();
@tracked calendarDisplayDate = this.currentDate; // init to current date, updates when user clicks on calendar chevrons
@tracked showCalendar = false;
@tracked tooltipTarget = null;
@tracked tooltipText = null;
// both date getters return a date object
get startDate() {
return parseAPITimestamp(this.args.startTimestamp);
}
get endDate() {
return parseAPITimestamp(this.args.endTimestamp);
}
get displayYear() {
return this.calendarDisplayDate.getFullYear();
}
get disableFutureYear() {
return isSameYear(this.calendarDisplayDate, this.currentDate);
}
get disablePastYear() {
// calendar widget should only go as far back as the passed in start time
return isSameYear(this.calendarDisplayDate, this.startDate);
}
get widgetMonths() {
const startYear = this.startDate.getFullYear();
const startMonthIdx = this.startDate.getMonth();
return ARRAY_OF_MONTHS.map((month, index) => {
let readonly = false;
// if widget is showing same year as @startTimestamp year, disable if month is before start month
if (startYear === this.displayYear && index < startMonthIdx) {
readonly = true;
}
// if widget showing current year, disable if month is later than current month
if (this.displayYear === this.currentDate.getFullYear() && index > this.currentDate.getMonth()) {
readonly = true;
}
return {
index,
year: this.displayYear,
name: month,
readonly,
};
});
}
@action
addTooltip() {
if (this.disablePastYear) {
const previousYear = this.displayYear - 1;
this.tooltipText = `${previousYear} is unavailable because it is before your start date. Change your start month to a date in ${previousYear} to see data for this year.`;
this.tooltipTarget = '#previous-year';
}
}
@action
removeTooltip() {
this.tooltipTarget = null;
}
@action
addYear() {
this.calendarDisplayDate = addYears(this.calendarDisplayDate, 1);
}
@action
subYear() {
this.calendarDisplayDate = subYears(this.calendarDisplayDate, 1);
}
@action
toggleShowCalendar() {
this.showCalendar = !this.showCalendar;
this.calendarDisplayDate = this.endDate;
}
@action
handleDateShortcut(dropdown, { target }) {
this.args.selectMonth({ dateType: target.name }); // send clicked shortcut to parent callback
this.showCalendar = false;
dropdown.close();
}
@action
selectMonth(month, dropdown) {
const { index, year, name } = month;
this.toggleShowCalendar();
this.args.selectMonth({ monthIdx: index, monthName: name, year, dateType: 'endDate' });
dropdown.close();
}
}