diff --git a/src/client/app/components/IntervalControlsComponent.tsx b/src/client/app/components/IntervalControlsComponent.tsx index a7e281708..5f59d893a 100644 --- a/src/client/app/components/IntervalControlsComponent.tsx +++ b/src/client/app/components/IntervalControlsComponent.tsx @@ -14,7 +14,7 @@ import translate from '../utils/translate'; import TooltipMarkerComponent from './TooltipMarkerComponent'; /** - * @returns interval controls for the bar, map, and compare pages + * @returns Interval controls for the bar, map, and compare pages */ export default function IntervalControlsComponent() { const dispatch = useAppDispatch(); @@ -44,7 +44,7 @@ export default function IntervalControlsComponent() { React.useEffect(() => { // Assume value is valid since it is coming from state. // Do not allow bad values in state. - const isCustom = !(['1', '7', '28'].find(days => days == duration.asDays().toString())); + const isCustom = !['1', '7', '28'].includes(duration.asDays().toString()); setShowCustomDuration(isCustom); setDaysCustom(duration.asDays()); setDays(isCustom ? CUSTOM_INPUT : duration.asDays().toString()); @@ -60,8 +60,8 @@ export default function IntervalControlsComponent() { if (value === CUSTOM_INPUT) { // Set menu value from standard value to special value to show custom // and show the custom input area. - setDays(CUSTOM_INPUT); setShowCustomDuration(true); + setDays(CUSTOM_INPUT); } else { // Set the standard menu value, hide the custom duration input // and duration for graphing. @@ -91,28 +91,43 @@ export default function IntervalControlsComponent() { } }; - // Define the initial set of options for the dropdown when the chart type is not 'compare'. - const options = [ - { label: 'day', value: '1' }, - { label: 'week', value: '7' }, - { label: '4.weeks', value: '28' } - ]; - - // If the chart type is 'compare', we change the options to reflect comparison periods. - if (chartType === ChartTypes.compare) { - options.length = 0; // Clear the previous options - Object.entries(ComparePeriod).forEach(([key, value]) => { - options.push({ label: key, value: value.toString() }); - }); - } + // Handles change for compare period dropdown + const handleComparePeriodChange = (value: string) => { + const period = value as unknown as ComparePeriod; + dispatch(graphSlice.actions.updateComparePeriod({ comparePeriod: period, currentTime: moment() })); + }; + + const comparePeriodTranslations: Record = { + Day: 'day', + Week: 'week', + FourWeeks: '4.weeks' + }; + + // Defines the set of options for the dropdown + const options = chartType === ChartTypes.compare + ? Object.entries(ComparePeriod).map(([key, value]) => ( + + )) + : ( + <> + + + + + + ); return (

- {(chartType === ChartTypes.bar && translate('bar.interval')) || - (chartType === ChartTypes.map && translate('map.interval')) || - (chartType === ChartTypes.compare && translate('compare.period'))}: + {translate( + chartType === ChartTypes.bar ? 'bar.interval' : + chartType === ChartTypes.map ? 'map.interval' : + 'compare.period' + )}: handleDaysChange(e.target.value)} + onChange={e => chartType === ChartTypes.compare ? handleComparePeriodChange(e.target.value) : handleDaysChange(e.target.value)} > - {options.map(option => ( - - ))} - + {options} {/* TODO: Compare is currently not ready for a custom option. */} {showCustomDuration && chartType !== ChartTypes.compare && - - {translate('days.enter')}: + handleCustomDaysChange(Number(e.target.value))} // This grabs each key hit and then finishes input when hit enter. - onKeyDown={e => { handleEnter(e.key); }} + onKeyDown={e => handleEnter(e.key)} step='1' min={MIN_DAYS} max={MAX_DAYS} value={daysCustom} - invalid={!daysValid(daysCustom)} /> + invalid={!daysValid(daysCustom)} + /> diff --git a/src/client/app/translations/data.ts b/src/client/app/translations/data.ts index 76a21485f..aaf65d524 100644 --- a/src/client/app/translations/data.ts +++ b/src/client/app/translations/data.ts @@ -29,7 +29,7 @@ const LocaleTranslationData = { "as.meter.unit": "as meter unit", "as.meter.defaultgraphicunit": "as meter default graphic unit", "bar": "Bar", - "bar.days.enter": "Enter in days and then hit enter", + "days.enter": "Enter in days and then hit enter", "bar.interval": "Bar Interval", "bar.raw": "Cannot create bar graph on raw units such as temperature", "bar.stacking": "Bar Stacking",