2022-01-11 18:05:38 +00:00
import Component from '@glimmer/component' ;
import layout from '../templates/components/calendar-widget' ;
import { setComponentTemplate } from '@ember/component' ;
import { action } from '@ember/object' ;
import { tracked } from '@glimmer/tracking' ;
/ * *
* @ module CalendarWidget
* CalendarWidget components are used in the client counts metrics . It helps users understand the ranges they can select .
*
* @ example
* ` ` ` js
* < CalendarWidget
2022-02-01 20:45:01 +00:00
* @ param { array } arrayOfMonths - An array of all the months that the calendar widget iterates through .
* @ param { string } endTimeDisplay - The formatted display value of the endTime . Ex : January 2022.
2022-02-17 23:52:06 +00:00
* @ param { array } endTimeFromResponse - The value returned on the counters / activity endpoint , which shows the true endTime not the selected one , which can be different . Ex : [ '2022' , 0 ]
2022-01-18 20:17:24 +00:00
* @ param { function } handleClientActivityQuery - a function passed from parent . This component sends the month and year to the parent via this method which then calculates the new data .
2022-02-01 20:45:01 +00:00
* @ param { function } handleCurrentBillingPeriod - a function passed from parent . This component makes the parent aware that the user selected Current billing period and it handles resetting the data .
* @ param { string } startTimeDisplay - The formatted display value of the endTime . Ex : January 2022. This component is only responsible for modifying the endTime which is sends to the parent to make the network request .
2022-01-11 18:05:38 +00:00
* / >
*
* ` ` `
* /
class CalendarWidget extends Component {
currentDate = new Date ( ) ;
currentYear = this . currentDate . getFullYear ( ) ; // integer
currentMonth = parseInt ( this . currentDate . getMonth ( ) ) ; // integer and zero index
@ tracked allMonthsNodeList = [ ] ;
@ tracked displayYear = this . currentYear ; // init to currentYear and then changes as a user clicks on the chevrons
@ tracked showCalendar = false ;
2022-02-10 16:43:40 +00:00
@ tracked tooltipTarget = null ;
@ tracked tooltipText = null ;
2022-01-11 18:05:38 +00:00
2022-06-03 21:22:50 +00:00
get selectedMonthId ( ) {
if ( ! this . args . endTimeFromResponse ) return '' ;
const [ year , monthIndex ] = this . args . endTimeFromResponse ;
return ` ${ monthIndex } - ${ year } ` ;
}
get disableFutureYear ( ) {
return this . displayYear === this . currentYear ;
2022-01-11 18:05:38 +00:00
}
2022-06-03 21:22:50 +00:00
get disablePastYear ( ) {
2022-11-09 23:15:31 +00:00
const startYear = parseInt ( this . args . startTimeDisplay . split ( ' ' ) [ 1 ] ) ;
2022-06-03 21:22:50 +00:00
return this . displayYear === startYear ; // if on startYear then don't let them click back to the year prior
}
get widgetMonths ( ) {
const displayYear = this . displayYear ;
const currentYear = this . currentYear ;
const currentMonthIdx = this . currentMonth ;
const [ startMonth , startYear ] = this . args . startTimeDisplay . split ( ' ' ) ;
const startMonthIdx = this . args . arrayOfMonths . indexOf ( startMonth ) ;
return this . args . arrayOfMonths . map ( ( month , idx ) => {
const monthId = ` ${ idx } - ${ displayYear } ` ;
let readonly = false ;
// if widget is showing billing start year, disable if month is before start month
if ( parseInt ( startYear ) === displayYear && idx < startMonthIdx ) {
readonly = true ;
}
2022-01-11 18:05:38 +00:00
2022-06-03 21:22:50 +00:00
// if widget showing current year, disable if month is current or later
if ( displayYear === currentYear && idx >= currentMonthIdx ) {
readonly = true ;
}
return {
id : monthId ,
month ,
readonly ,
current : monthId === ` ${ currentMonthIdx } - ${ currentYear } ` ,
} ;
} ) ;
2022-01-11 18:05:38 +00:00
}
2022-06-03 21:22:50 +00:00
// HELPER FUNCTIONS (alphabetically) //
addClass ( element , classString ) {
element . classList . add ( classString ) ;
2022-01-11 18:05:38 +00:00
}
removeClass ( element , classString ) {
element . classList . remove ( classString ) ;
}
2022-06-03 21:22:50 +00:00
resetDisplayYear ( ) {
let setYear = this . currentYear ;
if ( this . args . endTimeDisplay ) {
try {
const year = this . args . endTimeDisplay . split ( ' ' ) [ 1 ] ;
setYear = parseInt ( year ) ;
} catch ( e ) {
2022-10-18 15:46:02 +00:00
console . debug ( 'Error resetting display year' , e ) ; // eslint-disable-line
2022-06-03 21:22:50 +00:00
}
}
this . displayYear = setYear ;
}
2022-01-11 18:05:38 +00:00
// ACTIONS (alphabetically) //
2022-02-10 16:43:40 +00:00
@ action
addTooltip ( ) {
2022-06-03 21:22:50 +00:00
if ( this . disablePastYear ) {
2022-11-09 23:15:31 +00:00
const previousYear = Number ( this . displayYear ) - 1 ;
2022-02-10 16:43:40 +00:00
this . tooltipText = ` ${ previousYear } is unavailable because it is before your billing start month. Change your billing start month to a date in ${ previousYear } to see data for this year. ` ; // set tooltip text
this . tooltipTarget = '#previous-year' ;
}
}
2022-01-11 18:05:38 +00:00
@ action
addYear ( ) {
this . displayYear = this . displayYear + 1 ;
}
2022-02-10 16:43:40 +00:00
@ action removeTooltip ( ) {
this . tooltipTarget = null ;
}
2022-01-11 18:05:38 +00:00
@ action
2022-02-01 20:45:01 +00:00
selectCurrentBillingPeriod ( D ) {
this . args . handleCurrentBillingPeriod ( ) ; // resets the billing startTime and endTime to what it is on init via the parent.
2022-01-11 18:05:38 +00:00
this . showCalendar = false ;
2022-02-01 20:45:01 +00:00
D . actions . close ( ) ; // close the dropdown.
2022-01-11 18:05:38 +00:00
}
@ action
2022-06-03 21:22:50 +00:00
selectEndMonth ( monthId , D ) {
const [ monthIdx , year ] = monthId . split ( '-' ) ;
2022-01-11 18:05:38 +00:00
this . toggleShowCalendar ( ) ;
2022-06-03 21:22:50 +00:00
this . args . handleClientActivityQuery ( parseInt ( monthIdx ) , parseInt ( year ) , 'endTime' ) ;
2022-02-01 20:45:01 +00:00
D . actions . close ( ) ; // close the dropdown.
2022-01-11 18:05:38 +00:00
}
@ action
subYear ( ) {
this . displayYear = this . displayYear - 1 ;
}
@ action
toggleShowCalendar ( ) {
this . showCalendar = ! this . showCalendar ;
2022-06-03 21:22:50 +00:00
this . resetDisplayYear ( ) ;
2022-01-11 18:05:38 +00:00
}
}
export default setComponentTemplate ( layout , CalendarWidget ) ;