-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcreate-invoice-form.svelte
308 lines (266 loc) · 8.06 KB
/
create-invoice-form.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<svelte:options customElement="create-invoice-form" />
<script lang="ts">
// Types
import { APP_STATUS } from "@requestnetwork/shared-types/enums";
import type { IConfig } from "@requestnetwork/shared-types";
// Utils
import { calculateInvoiceTotals } from "@requestnetwork/shared-utils/invoiceTotals";
import { config as defaultConfig } from "@requestnetwork/shared-utils/config";
import { initializeCurrencyManager } from "@requestnetwork/shared-utils/initCurrencyManager";
// Components
import Button from "@requestnetwork/shared-components/button.svelte";
import Status from "@requestnetwork/shared-components/status.svelte";
import Modal from "@requestnetwork/shared-components/modal.svelte";
import { InvoiceForm, InvoiceView } from "./invoice";
import { getInitialFormData, prepareRequestParams } from "./utils";
import type { RequestNetwork } from "@requestnetwork/request-client.js";
export let config: IConfig;
export let signer: string = "";
export let requestNetwork: RequestNetwork | null | undefined;
export let currencies: any;
let isTimeout = false;
let activeConfig = config ? config : defaultConfig;
let mainColor = activeConfig.colors.main;
let secondaryColor = activeConfig.colors.secondary;
let currencyManager = initializeCurrencyManager(currencies);
const extractUniqueNetworkNames = (): string[] => {
const networkSet = new Set<string>();
currencyManager.knownCurrencies.forEach((currency: any) => {
networkSet.add(currency.network);
});
return Array.from(networkSet);
};
let networks = extractUniqueNetworkNames();
let network = networks[0];
const handleNetworkChange = (network: string) => {
if (network) {
const newCurrencies = currencyManager.knownCurrencies.filter(
(currency: any) => currency.network === network
);
network = network;
defaultCurrencies = newCurrencies;
currency = newCurrencies[0];
}
};
let activeRequest: any = null;
let canSubmit = false;
let appStatus: APP_STATUS[] = [];
let formData = getInitialFormData();
let defaultCurrencies = currencyManager.knownCurrencies.filter(
(currency: any) => currency.network === network
);
let currency = defaultCurrencies[0];
const handleCurrencyChange = (value: string) => {
currency = value;
};
let invoiceTotals = {
amountWithoutTax: 0,
totalTaxAmount: 0,
totalAmount: 0,
};
$: {
formData.creatorId = signer;
invoiceTotals = calculateInvoiceTotals(formData.items);
}
let payeeAddressError = false;
let clientAddressError = false;
$: {
const basicDetailsFilled =
formData.payeeAddress && formData.payerAddress && formData.dueDate;
const hasItems =
formData.items.length > 0 &&
formData.items.every(
(item) => item.description && item.quantity > 0 && item.unitPrice > 0
);
const addressesAreValid = !payeeAddressError && !clientAddressError;
canSubmit =
basicDetailsFilled && hasItems && requestNetwork && addressesAreValid
? true
: false;
}
const addToStatus = (newStatus: APP_STATUS) => {
appStatus = [...appStatus, newStatus];
};
const removeAllStatuses = () => {
appStatus = [];
};
const handleGoToDashboard = (dashboardLink: string) => {
removeAllStatuses();
window.location.href = dashboardLink;
};
const hanldeCreateNewInvoice = () => {
removeAllStatuses();
formData = getInitialFormData();
};
const handleCloseInvoiceModal = () => {
removeAllStatuses();
};
const submitForm = async (e: Event) => {
e.preventDefault();
formData.miscellaneous.builderId = activeConfig?.builderId || "";
formData.miscellaneous.createdWith = window.location.hostname;
const requestCreateParameters = prepareRequestParams({
signer,
formData,
currency,
invoiceTotals,
});
if (requestNetwork) {
try {
addToStatus(APP_STATUS.PERSISTING_TO_IPFS);
const request = await requestNetwork.createRequest({
requestInfo: requestCreateParameters.requestInfo,
paymentNetwork: requestCreateParameters.paymentNetwork,
contentData: requestCreateParameters.contentData,
signer: requestCreateParameters.signer,
});
activeRequest = request;
addToStatus(APP_STATUS.PERSISTING_ON_CHAIN);
await request.waitForConfirmation();
addToStatus(APP_STATUS.REQUEST_CONFIRMED);
} catch (error: any) {
if (error.message.includes("Transactioon confirmation not received")) {
isTimeout = true;
removeAllStatuses();
} else {
addToStatus(APP_STATUS.ERROR_OCCURRED);
console.error("Failed to create request:", error);
}
}
}
};
</script>
<div
class="create-invoice-form-wrapper"
style="--mainColor: {mainColor}; --secondaryColor: {secondaryColor}"
>
<div class="create-invoice-form-content">
<InvoiceForm
bind:formData
config={activeConfig}
bind:defaultCurrencies
bind:payeeAddressError
bind:clientAddressError
{handleCurrencyChange}
{handleNetworkChange}
{networks}
/>
<div class="invoice-view-wrapper">
<InvoiceView
config={activeConfig}
{currency}
bind:formData
bind:canSubmit
{invoiceTotals}
{submitForm}
bind:defaultCurrencies
/>
</div>
</div>
<Modal
config={activeConfig}
title="Creating the invoice"
isOpen={appStatus?.length > 0}
onClose={handleCloseInvoiceModal}
>
<Status config={activeConfig} statuses={appStatus} />
<div class="modal-footer">
<Button
type="button"
onClick={() => handleGoToDashboard(activeConfig.dashboardLink)}
text="Go to dashboard"
disabled={!appStatus.includes(APP_STATUS.REQUEST_CONFIRMED)}
/>
<Button
type="button"
onClick={hanldeCreateNewInvoice}
text="Create a new invoice"
disabled={!appStatus.includes(APP_STATUS.REQUEST_CONFIRMED)}
/>
</div>
</Modal>
<Modal
config={activeConfig}
title="Invoice Creation Taking Longer Than Expected"
isOpen={isTimeout}
onClose={() => (isTimeout = false)}
>
<p>
Creating the invoice is taking longer than expected. You can refresh and
keep waiting or return to the dashboard. Your invoice will be created
eventually.
</p>
<div class="modal-footer">
<Button
type="button"
onClick={async () => {
isTimeout = false;
addToStatus(APP_STATUS.PERSISTING_TO_IPFS);
addToStatus(APP_STATUS.PERSISTING_ON_CHAIN);
await activeRequest.waitForConfirmation();
addToStatus(APP_STATUS.REQUEST_CONFIRMED);
}}
text="Refresh and Keep Waiting"
/>
<Button
type="button"
onClick={() => handleGoToDashboard(activeConfig.dashboardLink)}
text="Return to Dashboard"
/>
</div>
</Modal>
</div>
<style>
@font-face {
font-family: "Montserrat";
src: url("./fonts/Montserrat-VariableFont_wght.ttf") format("truetype");
font-weight: normal;
font-style: normal;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Montserrat", sans-serif;
color-scheme: light;
}
.create-invoice-form-wrapper {
display: flex;
flex-direction: column;
gap: 20px;
box-sizing: border-box;
color: black;
}
.create-invoice-form-content {
display: flex;
gap: 20px;
width: 100%;
}
@media only screen and (max-width: 1024px) {
.create-invoice-form-content {
flex-direction: column;
}
}
.invoice-view-wrapper {
height: fit-content;
display: flex;
flex-direction: column;
gap: 12px;
width: 100%;
}
.modal-footer {
display: flex;
justify-content: space-between;
margin-top: 20px;
}
@media only screen and (max-width: 880px) {
.modal-footer {
gap: 10px;
}
}
:global(.modal-footer button) {
padding: 6px 14px !important;
width: fit-content !important;
height: fit-content !important;
}
</style>