-
Notifications
You must be signed in to change notification settings - Fork 14
/
useFundingFormState.tsx
174 lines (147 loc) · 4.59 KB
/
useFundingFormState.tsx
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'
import { AuthContext } from '../context'
import { useBTCConverter } from '../helpers'
import { IRewardCount } from '../interfaces'
import {
ProjectRewardForCreateUpdateFragment,
RewardCurrency,
ShippingDestination,
} from '../types/generated/graphql'
import { Satoshis } from '../types/types'
export interface IFundForm {
donationAmount: number
rewardsCost: number
comment: string
anonymous: boolean
shippingDestination: ShippingDestination
shippingCost: number
email: string
media: string
funderUsername: string
funderAvatarURL: string
rewardsByIDAndCount?: { [key: string]: number } | undefined
rewardCurrency: RewardCurrency
}
type UseFundStateProps = {
rewards?: ProjectRewardForCreateUpdateFragment[]
}
export type UpdateReward = (_: IRewardCount) => void
export interface IFundFormState {
state: IFundForm
setTarget: (event: any) => void
setState: (name: string, value: any) => void
updateReward: UpdateReward
resetForm: () => void
}
export type UseFundingFormStateReturn = ReturnType<typeof useFundingFormState>
export const useFundingFormState = ({ rewards }: UseFundStateProps) => {
const { user, isAnonymous } = useContext(AuthContext)
const { getUSDCentsAmount } = useBTCConverter()
const [needsShipping, setNeedsShipping] = useState(false)
const initialState: IFundForm = useMemo(
() => ({
donationAmount: 0,
rewardsCost: 0,
comment: '',
shippingDestination: ShippingDestination.National,
shippingCost: 0,
anonymous: isAnonymous, // The default user has id 0
funderAvatarURL: user.imageUrl || '',
funderUsername: user.username,
email: '',
media: '',
rewardsByIDAndCount: undefined,
rewardCurrency: RewardCurrency.Usdcent,
}),
[isAnonymous, user.imageUrl, user.username],
)
const [state, _setState] = useState<IFundForm>(initialState)
const setTarget = useCallback((event: any) => {
const { name, value } = event.target
_setState((current) => ({ ...current, [name]: value }))
}, [])
const setState = useCallback((name: string, value: any) => {
_setState((current) => ({ ...current, [name]: value }))
}, [])
useEffect(() => {
if (!user || !user.id) {
setState('anonymous', true)
} else {
setState('anonymous', false)
}
}, [setState, user])
const resetRewards = useCallback(() => {
_setState((current) => ({
...current,
rewardsByIDAndCount: {},
rewardsCost: 0,
totalAmount: current.donationAmount,
}))
setNeedsShipping(false)
}, [])
const updateReward = useCallback(
({ id, count }: IRewardCount) => {
_setState((current) => {
const newRewardsCountInfo = { ...current.rewardsByIDAndCount }
if (count !== 0) {
newRewardsCountInfo[id.toString()] = count
} else if (count === 0) {
delete newRewardsCountInfo[id.toString()]
}
let rewardsCost = 0
if (rewards) {
Object.keys(newRewardsCountInfo).forEach((rewardID: string) => {
const id = parseInt(rewardID, 10)
const reward = rewards.find(
(reward) => reward.id === id || `${reward.id}` === rewardID,
)
if (reward && reward.id) {
if (reward.hasShipping) {
setNeedsShipping((current) => current || reward.hasShipping)
}
const rewardMultiplier = newRewardsCountInfo[rewardID.toString()]
if (!rewardMultiplier) {
return 0
}
const cost =
current.rewardCurrency === RewardCurrency.Usdcent
? reward.cost
: // Assume sats if not USD cents
getUSDCentsAmount(reward.cost as Satoshis)
rewardsCost += cost * rewardMultiplier
}
})
}
return {
...current,
rewardsByIDAndCount: newRewardsCountInfo,
rewardsCost,
totalAmount: rewardsCost + current.donationAmount,
}
})
},
[getUSDCentsAmount, rewards],
)
const resetForm = useCallback(() => {
_setState(initialState)
setNeedsShipping(false)
}, [initialState])
const hasSelectedRewards = useMemo(
() =>
Boolean(
state.rewardsByIDAndCount &&
Object.keys(state.rewardsByIDAndCount).length > 0,
),
[state.rewardsByIDAndCount],
)
return {
state,
setTarget,
setState,
updateReward,
resetForm,
resetRewards,
needsShipping,
hasSelectedRewards,
}
}