-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathDateSelection.tsx
More file actions
131 lines (118 loc) · 3.72 KB
/
DateSelection.tsx
File metadata and controls
131 lines (118 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import styled, { css } from 'styled-components'
import { Typography } from '@ensdomains/thorin'
import { Calendar } from '@app/components/@atoms/Calendar/Calendar'
import { PlusMinusControl } from '@app/components/@atoms/PlusMinusControl/PlusMinusControl'
import { roundDurationWithDay, secondsFromDateDiff } from '@app/utils/date'
import { isInsideSafe } from '@app/utils/safe'
import { formatDurationOfDates, secondsToYears } from '@app/utils/utils'
const YearsViewSwitch = styled.button(
({ theme }) => css`
color: ${theme.colors.bluePrimary};
cursor: pointer;
font-size: ${theme.fontSizes.small};
font-weight: ${theme.fontWeights.bold};
`,
)
const Container = styled.div(
({ theme }) => css`
display: flex;
flex-direction: column;
gap: ${theme.space['2']};
align-items: center;
width: ${theme.space.full};
`,
)
const now = Math.floor(Date.now() / 1000)
export const DateSelection = ({
seconds,
setSeconds,
durationType,
onChangeDurationType,
name,
minSeconds,
mode = 'register',
expiry,
}: {
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 { t } = useTranslation()
useEffect(() => {
if (minSeconds > seconds) setSeconds(minSeconds)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [minSeconds, seconds])
const dateInYears = Math.floor(secondsToYears(seconds))
// When the duration type is years, normalise the seconds to a year value
useEffect(() => {
if (durationType === 'years' && currentTime) {
setSeconds(
secondsFromDateDiff({
startDate: new Date(currentTime * 1000),
additionalYears: Math.max(1, dateInYears),
}),
)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dateInYears, durationType])
const isSafeApp = isInsideSafe()
return (
<Container>
{durationType === 'date' && !isSafeApp ? (
<Calendar
value={currentTime + seconds}
onChange={(e) => {
const { valueAsDate } = e.currentTarget
if (valueAsDate) {
setSeconds(roundDurationWithDay(valueAsDate, currentTime))
}
}}
highlighted
name={name}
min={currentTime + minSeconds}
/>
) : (
<PlusMinusControl
highlighted
minValue={1}
value={dateInYears}
onChange={(e) => {
const newYears = parseInt(e.target.value)
if (!Number.isNaN(newYears))
setSeconds(
secondsFromDateDiff({
startDate: new Date(currentTime * 1000),
additionalYears: newYears,
}),
)
}}
/>
)}
<Typography color="greyPrimary" fontVariant="smallBold" data-testid="date-selection-info">
{formatDurationOfDates({
startDate: new Date(currentTime * 1000),
endDate: new Date((currentTime + seconds) * 1000),
postFix: mode === 'register' ? ' registration. ' : ' extension. ',
t,
})}
{!isSafeApp && (
<YearsViewSwitch
type="button"
data-testid="date-selection"
onClick={() => onChangeDurationType?.(durationType === 'years' ? 'date' : 'years')}
>
{t(`calendar.pick_by_${durationType === 'date' ? 'years' : 'date'}`, { ns: 'common' })}
</YearsViewSwitch>
)}
</Typography>
</Container>
)
}