import { type Meta, type StoryObj } from '@storybook/react-vite' import moment from 'moment' import React, { useState } from 'react' import { DocsTemplate } from '../../../.storybook' import { type CompareWith, type TimeframeType, } from '../../components/HeaderMetric/HeaderMetricHelpers' import { getComparisonDates } from '../../services/DateHelpers' import { historicalTimeframes, initialTimeframe, } from '../../services/TimeframeServiceTyped' import Picker from '../../components/Picker/Picker' import Timeframe from '../../components/TimeframeFilter/Timeframe' import { toast } from '../../components/Toast/Toast' const options: { id: number text: string value: CompareWith }[] = [ { id: 1, text: 'Previous Period', value: 'previous_period', }, { id: 2, text: 'Previous Year', value: 'previous_year', }, ] const HandleComparisonDates = (args) => { const [timeframe, setTimeframe] = useState(initialTimeframe) const [compareWith, setCompareWith] = useState(options[0]) const updateCompareWith = (value) => { const activeOption = options.find((item) => item.value === value) setCompareWith(activeOption || options[0]) } const update = ( _: string, startDate: moment.Moment, endDate: moment.Moment, timeframe: typeof initialTimeframe, ) => { setTimeframe(timeframe) const comparisonDates = getComparisonDates({ startDate: startDate.format('YYYY-MM-DD'), endDate: endDate.format('YYYY-MM-DD'), timeframe: timeframe as TimeframeType, compareWith: compareWith.value, }) toast({ message: (
getComparisonDates is used to provide comparison
dates. It takes in the following arguments:
startDate: The start date of the selected
timeframe.
endDate: The end date of the selected
timeframe.
timeframe: The timeframe object.
compareWith: The comparison selection. Either{' '}
previous_period or previous_year.
hasNoCurrentMonthData: A boolean indicating if
the current month has no data.