/
MintNftFormPopup.svelte
232 lines (208 loc) · 8.5 KB
/
MintNftFormPopup.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<script lang="typescript">
import { onMount } from 'svelte'
import { BaseError } from '@core/error'
import { localize } from '@core/i18n'
import { setMintNftDetails, mintNftDetails, TokenStandard } from '@core/wallet'
import { handleError } from '@core/error/handlers/handleError'
import { closePopup, openPopup } from '@auxiliary/popup'
import { Button, Dropdown, Error, FontWeight, OptionalInput, Text, TextInput } from 'shared/components'
import { IIrc27Metadata, MimeType, SupportedMimeType } from '@core/nfts'
import { isValidUrl, validateBech32Address } from '@core/utils'
import { networkHrp } from '@core/network'
export let _onMount: (..._: any[]) => Promise<void> = async () => {}
const { standard, version, ...metadata } = $mintNftDetails
type Inputs = { [key in keyof typeof metadata]: string }
const inputs = Object.fromEntries(
Object.entries(metadata).map(([key, value]) => [key, (value as string) ?? ''])
) as Inputs
type OptionalInputErrors = Omit<Inputs, 'type' | 'uri' | 'name'>
const optionalInputErrors = Object.fromEntries(
Object.entries(inputs)
.filter((input) => !['type', 'uri', 'name'].includes(input[0]))
.map(([key]) => [key, ''])
) as OptionalInputErrors
let typeError: string, uriError: string, nameError: string
const error: BaseError = null
const nftTypeOptions = Object.keys(SupportedMimeType)
.filter((key) => Number.isNaN(Number(key)))
.map((type) => ({
label: type as MimeType,
value: type as MimeType,
}))
function handleCancel(): void {
closePopup()
}
function handleContinue(): void {
const valid = validate()
if (valid) {
setMintNftDetails(convertInputsToMetadataType(inputs))
openPopup({
type: 'mintNftConfirmation',
overflow: true,
})
}
}
function handleSelectNftType(item: { label: MimeType; value: MimeType }): void {
inputs.type = item.value
}
function validate(): boolean {
if (!nftTypeOptions.map((e) => e.value).includes(inputs.type as MimeType)) {
typeError = 'Invalid MimeType, check if the file type is supported'
}
if (inputs.name.length === 0) {
nameError = 'Empty name, it is a required field'
}
if (inputs.uri.length === 0) {
uriError = 'Empty URI, please provide a valid URI'
} else if (!isValidUrl(inputs.uri)) {
uriError = 'Invalid URI, please provide a valid URI'
}
if (inputs.royalties) {
validateRoyalties()
}
if (inputs.attributes) {
validateAttributes()
}
const hasErrors = Object.values({ ...optionalInputErrors, typeError, nameError, uriError }).some(
(e) => e !== ''
)
return !hasErrors
}
function validateRoyalties(): void {
let royalties: unknown
try {
royalties = JSON.parse(inputs.royalties)
} catch (err) {
optionalInputErrors.royalties = 'Royalties must be a valid JSON'
return
}
const isKeysValid = Object.keys(royalties).every((key) => !validateBech32Address($networkHrp, key))
if (!isKeysValid) {
optionalInputErrors.royalties = `Invalid address, must be a valid ${$networkHrp} address where royalties will be sent to.`
return
}
const isValuesValid = Object.values(royalties).every((value) => value >= 0 && value <= 1)
if (!isValuesValid) {
optionalInputErrors.royalties =
'Invalid value, it must be a numeric decimal representative of the percentage required ie. 0.05'
return
}
const isSumValid = Object.values(royalties).reduce((acc, val) => acc + val, 0) <= 1
if (!isSumValid) {
optionalInputErrors.royalties = 'Invalid value, the sum of all royalties must be less than or equal to 1'
return
}
}
function validateAttributes(): void {
let attributes: unknown
try {
attributes = JSON.parse(inputs.attributes)
} catch (err) {
optionalInputErrors.attributes = 'Attributes must be a valid JSON'
return
}
if (!Array.isArray(attributes)) {
optionalInputErrors.attributes = 'Attributes must be an array'
return
}
const isArrayOfObjects = attributes.every(
(attribute) => typeof attribute === 'object' && !Array.isArray(attribute) && attribute !== null
)
if (!isArrayOfObjects) {
optionalInputErrors.attributes = 'Attributes must be an array of objects'
return
}
const isKeysValid = attributes.every(
(attribute) =>
Object.keys(attribute).every((key) => key === 'trait_type' || key === 'value') &&
Object.keys(attribute).filter((key) => key === 'trait_type').length === 1 &&
Object.keys(attribute).filter((key) => key === 'value').length === 1
)
if (!isKeysValid) {
optionalInputErrors.attributes = 'Invalid key, attributes must have the keys "trait_type" and "value"'
return
}
const isValuesValid = attributes.every(
(attribute) =>
(typeof attribute.trait_type === 'string' &&
attribute.trait_type.length > 0 &&
typeof attribute.value === 'string' &&
attribute.value.length > 0) ||
typeof attribute.value === 'number'
)
if (!isValuesValid) {
optionalInputErrors.attributes =
'Invalid value, "trait_type" must be a non empty string and "value" must be a non empty string or a number'
return
}
}
function convertInputsToMetadataType(inputs: Inputs): IIrc27Metadata {
return {
standard: standard ?? TokenStandard.IRC27,
version,
...inputs,
royalties: inputs.royalties ? JSON.parse(inputs.royalties) : undefined,
attributes: inputs.attributes ? JSON.parse(inputs.attributes) : undefined,
type: inputs.type as MimeType,
}
}
onMount(async () => {
try {
await _onMount()
} catch (err) {
handleError(err)
}
})
</script>
<div class="space-y-6">
<Text type="h4" fontSize="18" lineHeight="6" fontWeight={FontWeight.semibold}>
{localize('popups.mintNftForm.title')}
</Text>
<popup-inputs class="block space-y-4 max-h-100 scrollable-y overflow-x-hidden flex-1">
<Dropdown
bind:value={inputs.type}
bind:error={typeError}
onSelect={handleSelectNftType}
label={localize('popups.mintNftForm.inputs.type')}
placeholder={localize('popups.mintNftForm.inputs.type')}
items={nftTypeOptions}
fontSize="sm"
lineHeight="140"
fontWeight={FontWeight.medium}
/>
<TextInput
bind:value={inputs.uri}
bind:error={uriError}
label={localize('popups.mintNftForm.inputs.uri')}
placeholder={localize('popups.mintNftForm.inputs.uri')}
/>
<TextInput
bind:value={inputs.name}
bind:error={nameError}
label={localize('popups.mintNftForm.inputs.name')}
placeholder={localize('popups.mintNftForm.inputs.name')}
/>
<optional-inputs class="flex flex-row flex-wrap gap-4">
{#each Object.keys(inputs).filter((key) => !['type', 'uri', 'name'].includes(key)) as key}
<OptionalInput
bind:value={inputs[key]}
bind:error={optionalInputErrors[key]}
label={localize(`popups.mintNftForm.inputs.${key}`)}
description={localize(`tooltips.mintNftForm.${key}`)}
fontSize="14"
/>
{/each}
</optional-inputs>
{#if error}
<Error error={error?.message} />
{/if}
</popup-inputs>
<popup-buttons class="flex flex-row flex-nowrap w-full space-x-4">
<Button outline classes="w-full" onClick={handleCancel}>
{localize('actions.cancel')}
</Button>
<Button classes="w-full" onClick={handleContinue}>
{localize('actions.continue')}
</Button>
</popup-buttons>
</div>