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: (
Start Date: {startDate.format('L')}
End Date: {endDate.format('L')}
Comparison Start Date:{' '} {moment(comparisonDates?.startDate).format('L')}
Comparison End Date: {moment(comparisonDates?.endDate).format('L')}
), type: 'info', config: { autoClose: false, }, }) } return ( <> updateCompareWith(value)} /> ) } type Story = StoryObj const Template: Story = { render: ({ ...args }) => { return }, } export const Example = { ...Template, args: { historicalTimeframes: historicalTimeframes, currentTimeframe: true, previousTimeframe: true, quarterlyTimeframe: true, hideCustomDateSearch: true, }, } const meta: Meta = { title: 'Helper Functions/getComparisonDates', component: HandleComparisonDates, parameters: { docs: { page: () => ( <> 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.
, ]} /> ), }, }, } export default meta