forked from KappaSigmaMu/kappasigmamu.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextStep.tsx
202 lines (179 loc) · 5.53 KB
/
NextStep.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* eslint-disable react/no-unescaped-entities */
import { ApiPromise } from '@polkadot/api'
import { u32 } from '@polkadot/types'
import { ReactElement, useEffect, useState } from 'react'
import { Button } from 'react-bootstrap'
import { useLocation } from 'react-router-dom'
import styled from 'styled-components'
import { LinkWithQuery } from './LinkWithQuery'
import { useAccount } from '../account/AccountContext'
import { StatusChangeHandler, doTx } from '../helpers/extrinsics'
import { useKusama } from '../kusama/KusamaContext'
import { isVotingPeriod } from './rotation-bar/helpers/periods'
import { LoadingSpinner } from '../pages/explore/components/LoadingSpinner'
import { toastByStatus } from '../pages/explore/helpers'
const StyledP = styled.p`
color: ${(props) => props.theme.colors.lightGrey};
`
interface LevelsType {
[key: string]: ReactElement
}
const HumanNextStep = (
<>
<h5 className="mb-4">
To become a Candidate you need to level up;
<br />
To level up you must first Submit a Bid.
</h5>
<LinkWithQuery to="/explore/bidders" className="ml-5 btn btn-primary">
Submit a Bid
</LinkWithQuery>
</>
)
const BidderNextStep = (
<>
<h5 className="mb-4">To become a Candidate your bid must be accepted.</h5>
<LinkWithQuery to="/explore/bidders" className="ml-5 btn btn-primary">
Check Bids
</LinkWithQuery>
</>
)
const CandidateNextStep = (
<>
<h3 className="mb-4">To become a Cyborg you need to submit the Proof of Ink.</h3>
<a
href="https://hackmd.io/@laurogripa/SkahoUpIT"
target="_blank"
className="btn btn-outline-light-grey"
rel="noreferrer"
>
Proof of Ink (PoI) Rules
</a>
<LinkWithQuery to="/explore/poi" className="btn btn-outline-light-grey">
Ink Art
</LinkWithQuery>
<a
href="https://matrix.to/#/!BUmiAAnAYSRGarqwOt:matrix.parity.io?via=matrix.parity.io"
target="_blank"
className="btn btn-primary"
rel="noreferrer"
>
Submit Proof of Ink
</a>
<br />
<br />
<LinkWithQuery to="/journey?claim=true">I've already submitted Proof of Ink</LinkWithQuery>
</>
)
const ClaimMembershipStep = ({
api,
showMessage,
handleUpdate
}: {
api: ApiPromise
showMessage: (args: ExtrinsicResult) => any
handleUpdate: () => void
}) => (
<>
<h5>It's claim time!</h5>
<p>If you were approved, go ahead and claim your membership:</p>
<ClaimMembershipButton
api={api!}
showMessage={showMessage}
successText="Claim request sent."
waitingText="Request sent. Waiting for response..."
handleUpdate={handleUpdate}
></ClaimMembershipButton>
</>
)
const CyborgNextStep = (
<>
<h5 className="mb-4">Welcome to the Kusama Society!</h5>
<LinkWithQuery to="/explore/bidders" className="btn btn-outline-light-grey">
Vouch for someone
</LinkWithQuery>
<LinkWithQuery to="/explore/candidates" className="ml-5 btn btn-primary">
Vote on Candidates
</LinkWithQuery>
</>
)
const LEVELS: LevelsType = {
human: HumanNextStep,
bidder: BidderNextStep,
candidate: CandidateNextStep,
cyborg: CyborgNextStep
}
const NextStep = () => {
const { level, setLevel } = useAccount()
const { api } = useKusama()
const { search } = useLocation()
const [currentBlock, setCurrentBlock] = useState<number>(0)
const [votingPeriod, setVotingPeriod] = useState<number>(0)
const [claimPeriod, setClaimPeriod] = useState<number>(0)
useEffect(() => {
if (api && api.consts && api.consts.society) {
const votingPeriod = (api.consts.society.votingPeriod as u32).toNumber()
setVotingPeriod(votingPeriod)
const claimPeriod = (api.consts.society.claimPeriod as u32).toNumber()
setClaimPeriod(claimPeriod)
api.derive.chain.bestNumber((block) => {
setCurrentBlock(block.toNumber())
})
}
}, [api])
const claim = new URLSearchParams(search).get('claim')
const isClaimPeriod = claim || !isVotingPeriod(votingPeriod, claimPeriod, currentBlock)
const showMessage = (nextResult: ExtrinsicResult) => {
toastByStatus[nextResult.status](nextResult.message, { id: nextResult.message })
}
const handleUpdate = () => {
setLevel('cyborg')
}
return (
<>
<StyledP>{level !== 'cyborg' && 'Next Step'}</StyledP>
{level === 'candidate' && isClaimPeriod ? (
<ClaimMembershipStep api={api!} showMessage={showMessage} handleUpdate={handleUpdate} />
) : (
LEVELS[level]
)}
</>
)
}
type ClaimMembershipButtonProps = {
api: ApiPromise
showMessage: (args: ExtrinsicResult) => any
handleUpdate: () => void
successText: string
waitingText: string
}
function ClaimMembershipButton({
api,
showMessage,
handleUpdate,
successText,
waitingText
}: ClaimMembershipButtonProps) {
const [loading, setLoading] = useState(false)
const { activeAccount } = useAccount()
const onStatusChange: StatusChangeHandler = ({ loading, message, status }) => {
setLoading(loading!)
showMessage({ status, message })
if (!loading && message !== 'Transaction submitted.' && status === 'success') handleUpdate()
}
const handleClaim = async () => {
setLoading(true)
try {
await doTx(api, api.tx.society.claimMembership(), successText, waitingText, activeAccount!, onStatusChange)
} catch (e) {
console.error(e)
}
}
if (loading) return <LoadingSpinner center={false} small={true} />
return <Button onClick={handleClaim}>Claim Membership</Button>
}
export { NextStep }