/
RegisterProposalPopup.svelte
83 lines (74 loc) · 3.01 KB
/
RegisterProposalPopup.svelte
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
<script lang="typescript">
import { Button, TextInput, Text, TextType } from 'shared/components'
import { localize } from '@core/i18n'
import { closePopup } from '@auxiliary/popup'
import { registerParticipationEvent } from '@core/profile-manager/api'
import { isValidUrl } from '@core/utils'
import { handleError } from '@core/error/handlers/handleError'
import { showAppNotification } from '@auxiliary/notification'
let eventId: string
let nodeUrl: string
let eventIdError: string
let nodeUrlError: string
$: disabled = !eventId || !nodeUrl
function handleCancel(): void {
closePopup()
}
async function handleConfirm(): Promise<void> {
try {
await Promise.all([validateEventId(), validateNodeUrl()])
await registerParticipationEvent(eventId, [{ url: nodeUrl }])
showAppNotification({
type: 'success',
message: localize('views.governance.proposals.successRegister'),
alert: true,
})
closePopup()
} catch (err) {
if (!nodeUrlError && !eventIdError) {
handleError(err)
}
}
}
async function validateNodeUrl(): Promise<void> {
if (!isValidUrl(nodeUrl)) {
nodeUrlError = localize('error.node.invalid')
return Promise.reject(nodeUrlError)
}
}
async function validateEventId(): Promise<void> {
const startsWith0x = eventId?.substring(0, 2) === '0x'
if (!startsWith0x) {
eventIdError = localize('error.eventId.doesNotStartWith0x')
return Promise.reject(eventIdError)
}
const hexLength = eventId?.substring(2)?.length
const has64Length = hexLength === 64
if (!has64Length) {
eventIdError = localize('error.eventId.insufficientLength')
return Promise.reject(eventIdError)
}
}
</script>
<register-proposal>
<Text type={TextType.h3} classes="mb-6">{localize('popups.registerProposal.title')}</Text>
<Text fontSize="15">{localize('popups.registerProposal.body')}</Text>
<div class="flex flex-col w-full space-y-4 mt-4">
<TextInput
bind:value={eventId}
bind:error={eventIdError}
placeholder={localize('views.governance.details.proposalInformation.eventId')}
label={localize('views.governance.details.proposalInformation.eventId')}
/>
<TextInput
bind:value={nodeUrl}
bind:error={nodeUrlError}
placeholder={localize('views.governance.details.proposalInformation.nodeUrl')}
label={localize('views.governance.details.proposalInformation.nodeUrl')}
/>
</div>
<div class="flex w-full space-x-4 mt-6">
<Button outline classes="w-full" onClick={handleCancel}>{localize('actions.cancel')}</Button>
<Button {disabled} classes="w-full" onClick={handleConfirm}>{localize('actions.confirm')}</Button>
</div>
</register-proposal>