diff --git a/src/components/@molecules/DateSelection/DateSelection.test.tsx b/src/components/@molecules/DateSelection/DateSelection.test.tsx index e72670922..f64c4f66f 100644 --- a/src/components/@molecules/DateSelection/DateSelection.test.tsx +++ b/src/components/@molecules/DateSelection/DateSelection.test.tsx @@ -13,51 +13,64 @@ describe('DateSelection', () => { vi.resetAllMocks() }) it('should render a plus minus counter if no name was provided', () => { - render( {}} />) + render( + {}} + durationType="years" + />, + ) expect(screen.getByTestId('plus-minus-control-input')).toBeInTheDocument() }) it('should show a calendar if user is picking by date', async () => { - render( {}} />) - - screen.getByTestId('date-selection').click() - + render( + {}} durationType="date" />, + ) expect(screen.getByText('unit.years.1 registration.')).toBeVisible() }) it('should set back to one year when switching to a year toggle if previously was set to less', async () => { const { result } = renderHook(() => useState(ONE_YEAR)) - const { rerender } = render( - , + let { result: durationTypeResult } = renderHook(() => useState<'years' | 'date'>('years')) + + const DateSelectionComponent = () => ( + ) + const { rerender } = render() + const dateSelection = screen.getByTestId('date-selection') await userEvent.click(dateSelection) + rerender() + await waitFor(() => { expect(screen.getByText('calendar.pick_by_years')).toBeVisible() }) - const currentDate = new Date() - act(() => { - result.current[1](secondsFromDateDiff({ startDate: currentDate, additionalMonths: 1 })) - }) + result.current[1](secondsFromDateDiff({ startDate: new Date(), additionalMonths: 1 })) - rerender( - , - ) + rerender() expect(screen.getByText('unit.months.1 registration.')).toBeVisible() await userEvent.click(dateSelection) + rerender() + await waitFor(() => { expect(screen.getByText('calendar.pick_by_date')).toBeVisible() }) - rerender( - , - ) + rerender() expect(screen.getByText('unit.years.1 registration.')).toBeVisible() }) diff --git a/src/components/@molecules/DateSelection/DateSelection.tsx b/src/components/@molecules/DateSelection/DateSelection.tsx index b2f6547db..3c19320e2 100644 --- a/src/components/@molecules/DateSelection/DateSelection.tsx +++ b/src/components/@molecules/DateSelection/DateSelection.tsx @@ -1,4 +1,4 @@ -import { useEffect, useMemo, useState } from 'react' +import { useEffect } from 'react' import { useTranslation } from 'react-i18next' import styled, { css } from 'styled-components' @@ -33,6 +33,8 @@ const now = Math.floor(Date.now() / 1000) export const DateSelection = ({ seconds, setSeconds, + durationType, + onChangeDurationType, name, minSeconds, mode = 'register', @@ -40,14 +42,14 @@ export const DateSelection = ({ }: { seconds: number setSeconds: (seconds: number) => void + durationType: 'years' | 'date' name?: string minSeconds: number mode?: 'register' | 'extend' expiry?: number + onChangeDurationType?: (type: 'years' | 'date') => void }) => { const currentTime = expiry ?? now - const [yearPickView, setYearPickView] = useState<'years' | 'date'>('years') - const toggleYearPickView = () => setYearPickView(yearPickView === 'date' ? 'years' : 'date') const { t } = useTranslation() @@ -57,29 +59,23 @@ export const DateSelection = ({ }, [minSeconds, seconds]) const dateInYears = Math.floor(secondsToYears(seconds)) - const extensionPeriod = useMemo(() => { - return formatDurationOfDates( - new Date(currentTime * 1000), - new Date((currentTime + seconds) * 1000), - t, - ) - }, [currentTime, seconds, t]) + // When the duration type is years, normalise the seconds to a year value useEffect(() => { - if (yearPickView === 'years' && currentTime) { + if (durationType === 'years' && currentTime) { setSeconds( secondsFromDateDiff({ startDate: new Date(currentTime * 1000), - additionalYears: dateInYears < 1 ? 1 : dateInYears, + additionalYears: Math.max(1, dateInYears), }), ) } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [dateInYears, yearPickView]) + }, [dateInYears, durationType]) return ( - {yearPickView === 'date' ? ( + {durationType === 'date' ? ( { @@ -111,15 +107,18 @@ export const DateSelection = ({ /> )} - {extensionPeriod === t('unit.invalid_date', { ns: 'common' }) - ? extensionPeriod - : `${extensionPeriod} ${mode === 'register' ? 'registration.' : 'extension.'}`}{' '} + {formatDurationOfDates({ + startDate: new Date(currentTime * 1000), + endDate: new Date((currentTime + seconds) * 1000), + postFix: mode === 'register' ? ' registration. ' : ' extension. ', + t, + })} toggleYearPickView()} + onClick={() => onChangeDurationType?.(durationType === 'years' ? 'date' : 'years')} > - {t(`calendar.pick_by_${yearPickView === 'date' ? 'years' : 'date'}`, { ns: 'common' })} + {t(`calendar.pick_by_${durationType === 'date' ? 'years' : 'date'}`, { ns: 'common' })} diff --git a/src/components/pages/profile/[name]/registration/FullInvoice.tsx b/src/components/pages/profile/[name]/registration/FullInvoice.tsx index 51016b2b7..da8c90822 100644 --- a/src/components/pages/profile/[name]/registration/FullInvoice.tsx +++ b/src/components/pages/profile/[name]/registration/FullInvoice.tsx @@ -51,7 +51,11 @@ const FullInvoice = ({ return [ { label: t('invoice.timeRegistration', { - time: formatDurationOfDates(new Date(), new Date(now + seconds * 1000), t), + time: formatDurationOfDates({ + startDate: new Date(), + endDate: new Date(now + seconds * 1000), + t, + }), }), bufferPercentage: CURRENCY_FLUCTUATION_BUFFER_PERCENTAGE, value: totalDurationBasedFee, diff --git a/src/components/pages/profile/[name]/registration/Registration.tsx b/src/components/pages/profile/[name]/registration/Registration.tsx index baf650902..5b61385e8 100644 --- a/src/components/pages/profile/[name]/registration/Registration.tsx +++ b/src/components/pages/profile/[name]/registration/Registration.tsx @@ -142,6 +142,7 @@ const Registration = ({ nameDetails, isLoading }: Props) => { seconds, reverseRecord, paymentMethodChoice, + durationType, }: RegistrationStepData['pricing']) => { if (paymentMethodChoice === PaymentMethod.moonpay) { initiateMoonpayRegistrationMutation.mutate(secondsToYears(seconds)) @@ -149,7 +150,7 @@ const Registration = ({ nameDetails, isLoading }: Props) => { } dispatch({ name: 'setPricingData', - payload: { seconds, reverseRecord }, + payload: { seconds, reverseRecord, durationType }, selected, }) if (!item.queue.includes('profile')) { diff --git a/src/components/pages/profile/[name]/registration/steps/Pricing/Pricing.tsx b/src/components/pages/profile/[name]/registration/steps/Pricing/Pricing.tsx index 873ecb57e..dc0413a6c 100644 --- a/src/components/pages/profile/[name]/registration/steps/Pricing/Pricing.tsx +++ b/src/components/pages/profile/[name]/registration/steps/Pricing/Pricing.tsx @@ -380,6 +380,7 @@ export type ActionButtonProps = { seconds: number balance: GetBalanceData | undefined totalRequiredBalance?: bigint + durationType: 'date' | 'years' } export const ActionButton = (props: ActionButtonProps) => { @@ -404,12 +405,13 @@ export const ActionButton = (props: ActionButtonProps) => { reverseRecord, seconds, paymentMethodChoice, + durationType, callback, }) => ( ), ) - .otherwise(({ reverseRecord, seconds, paymentMethodChoice, callback }) => ( + .otherwise(({ reverseRecord, seconds, paymentMethodChoice, durationType, callback }) => (