Skip to content

Commit 49644e7

Browse files
fix: currencies (#273)
1 parent c742b8c commit 49644e7

File tree

3 files changed

+65
-70
lines changed

3 files changed

+65
-70
lines changed

packages/create-invoice-form/src/lib/create-invoice-form.svelte

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
let secondaryColor = activeConfig.colors.secondary;
4444
let currencyManager = initializeCurrencyManager(currencies);
4545
46+
let invoiceCurrencyDropdown: { clear: () => void; };
47+
let networkDropdown: { clear: () => void; };
48+
let currencyDropdown: { clear: () => void; };
49+
4650
const extractUniqueNetworkNames = (): string[] => {
4751
const networkSet = new Set<string>();
4852
@@ -65,16 +69,33 @@
6569
6670
const handleNetworkChange = (newNetwork: string) => {
6771
if (newNetwork) {
72+
currencyDropdown.clear();
6873
network = newNetwork;
6974
70-
invoiceCurrency = undefined;
7175
currency = undefined;
7276
73-
defaultCurrencies = currencyManager.knownCurrencies.filter(
74-
(curr: CurrencyTypes.CurrencyDefinition) =>
75-
curr.type === Types.RequestLogic.CURRENCY.ISO4217 ||
76-
curr.network === newNetwork
77-
);
77+
filteredSettlementCurrencies = currencyManager.knownCurrencies.filter((currency: CurrencyTypes.CurrencyDefinition) => {
78+
if (!invoiceCurrency) {
79+
return false;
80+
}
81+
82+
// For ISO4217 currencies (like EUR)
83+
if (invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217) {
84+
const hasValidPath =
85+
currencyManager?.getConversionPath(
86+
invoiceCurrency,
87+
currency,
88+
currency?.network
89+
)?.length > 0;
90+
91+
return (
92+
currency.type !== Types.RequestLogic.CURRENCY.ISO4217 && hasValidPath
93+
);
94+
}
95+
96+
// For other currency types (like ERC20)
97+
return invoiceCurrency.hash === currency?.hash;
98+
});;
7899
}
79100
};
80101
@@ -83,36 +104,35 @@
83104
let appStatus: APP_STATUS[] = [];
84105
let formData = getInitialFormData();
85106
let defaultCurrencies = currencyManager.knownCurrencies;
107+
let filteredSettlementCurrencies: CurrencyTypes.CurrencyDefinition[] = [];
86108
87109
const handleInvoiceCurrencyChange = (
88110
value: CurrencyTypes.CurrencyDefinition
89111
) => {
90112
if (value !== invoiceCurrency) {
113+
networkDropdown.clear();
114+
currencyDropdown.clear();
115+
91116
invoiceCurrency = value;
92117
currency = undefined;
118+
filteredSettlementCurrencies = [];
119+
network = undefined;
120+
networks = [];
93121
94-
if (value.type !== Types.RequestLogic.CURRENCY.ISO4217) {
95-
network = value.network;
96-
}
97-
}
98-
};
99-
100-
const handleCurrencyChange = (value: CurrencyTypes.CurrencyDefinition) => {
101-
currency = value;
102-
};
103-
104-
$: {
105-
if (invoiceCurrency) {
106122
if (invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217) {
107-
networks = getCurrencySupportedNetworksForConversion(
123+
networks = (getCurrencySupportedNetworksForConversion(
108124
invoiceCurrency.hash,
109125
currencyManager
110-
);
126+
) ?? []) as string[];
111127
} else {
112128
networks = extractUniqueNetworkNames();
113129
}
114130
}
115-
}
131+
};
132+
133+
const handleCurrencyChange = (value: CurrencyTypes.CurrencyDefinition) => {
134+
currency = value;
135+
};
116136
117137
let invoiceTotals = {
118138
amountWithoutTax: 0,
@@ -272,17 +292,17 @@
272292
<div class="create-invoice-form-content">
273293
<InvoiceForm
274294
bind:formData
275-
bind:currency
276295
config={activeConfig}
277296
bind:defaultCurrencies
278-
bind:network
297+
bind:filteredSettlementCurrencies
279298
{handleInvoiceCurrencyChange}
280299
{handleCurrencyChange}
281300
{handleNetworkChange}
282-
{networks}
283-
{currencyManager}
284-
{invoiceCurrency}
301+
bind:networks
285302
{cipherProvider}
303+
bind:invoiceCurrencyDropdown
304+
bind:networkDropdown
305+
bind:currencyDropdown
286306
/>
287307
<div class="invoice-view-wrapper">
288308
<InvoiceView

packages/create-invoice-form/src/lib/invoice/form.svelte

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<script lang="ts">
22
// Components
33
import Button from "@requestnetwork/shared-components/button.svelte";
4-
import Dropdown from "@requestnetwork/shared-components/dropdown.svelte";
54
import Input from "@requestnetwork/shared-components/input.svelte";
65
import Labels from "@requestnetwork/shared-components/labels.svelte";
76
import Accordion from "@requestnetwork/shared-components/accordion.svelte";
@@ -19,7 +18,6 @@
1918
import { calculateItemTotal } from "@requestnetwork/shared-utils/invoiceTotals";
2019
import { checkAddress } from "@requestnetwork/shared-utils/checkEthAddress";
2120
import { inputDateFormat } from "@requestnetwork/shared-utils/formatDate";
22-
import { Types } from "@requestnetwork/request-client.js";
2321
import { CurrencyTypes, CipherProviderTypes } from "@requestnetwork/types";
2422
import isEmail from "validator/es/lib/isEmail";
2523
@@ -31,15 +29,13 @@
3129
export let handleNetworkChange: (chainId: string) => void;
3230
export let networks;
3331
export let defaultCurrencies: any = [];
34-
export let currencyManager: any;
35-
export let invoiceCurrency: CurrencyTypes.CurrencyDefinition | undefined;
36-
export let currency:
37-
| CurrencyTypes.ERC20Currency
38-
| CurrencyTypes.NativeCurrency
39-
| undefined;
40-
export let network: any;
32+
export let filteredSettlementCurrencies: CurrencyTypes.CurrencyDefinition[] = [];
4133
export let cipherProvider: CipherProviderTypes.ICipherProvider | undefined;
4234
35+
export let invoiceCurrencyDropdown;
36+
export let networkDropdown;
37+
export let currencyDropdown;
38+
4339
let validationErrors = {
4440
payeeAddress: false,
4541
clientAddress: false,
@@ -53,7 +49,6 @@
5349
};
5450
5551
let showPayeeAddressInput = false;
56-
let filteredSettlementCurrencies: CurrencyTypes.CurrencyDefinition[] = [];
5752
5853
const validateEmail = (email: string, type: "sellerInfo" | "buyerInfo") => {
5954
validationErrors[`${type}`].email = !isEmail(email);
@@ -160,32 +155,6 @@
160155
formData.payeeAddress = formData.creatorId;
161156
}
162157
163-
$: {
164-
// Filter settlement currencies whenever network, invoiceCurrency, or currencyManager changes
165-
filteredSettlementCurrencies = defaultCurrencies.filter((currency) => {
166-
if (!invoiceCurrency) {
167-
return false;
168-
}
169-
170-
// For ISO4217 currencies (like EUR)
171-
if (invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217) {
172-
const hasValidPath =
173-
currencyManager?.getConversionPath(
174-
invoiceCurrency,
175-
currency,
176-
currency.network
177-
)?.length > 0;
178-
179-
return (
180-
currency.type !== Types.RequestLogic.CURRENCY.ISO4217 && hasValidPath
181-
);
182-
}
183-
184-
// For other currency types (like ERC20)
185-
return invoiceCurrency.hash === currency.hash;
186-
});
187-
}
188-
189158
$: if (!formData.issuedOn) {
190159
formData.issuedOn = inputDateFormat(new Date());
191160
}
@@ -433,29 +402,26 @@
433402
</div>
434403
</Accordion>
435404
</div>
436-
437405
<div class="searchable-dropdown-container">
438406
<SearchableDropdown
407+
bind:this={invoiceCurrencyDropdown}
439408
getValue={(currency) => currency.value.symbol}
440409
getDisplayValue={(currency) =>
441410
`${currency.value.symbol} ${currency.value.network ? `(${currency.value.network})` : ""}`}
442411
placeholder="Invoice currency"
443412
items={defaultCurrencies
444-
?.filter((curr) => {
445-
if (!curr) return false;
446-
return (
447-
curr.type === Types.RequestLogic.CURRENCY.ISO4217 ||
448-
(curr.network && curr.network === network)
449-
);
450-
})
413+
?.filter((curr) => curr)
451414
.map((currency) => ({
452415
value: currency,
453416
label: `${currency?.symbol ?? "Unknown"} ${currency?.network ? `(${currency.network})` : ""}`,
454417
type: "invoiceCurrency",
455418
})) ?? []}
456419
onSelect={handleInvoiceCurrencyChange}
457420
/>
421+
</div>
422+
<div class="searchable-dropdown-container">
458423
<SearchableDropdown
424+
bind:this={networkDropdown}
459425
items={networks
460426
.filter((networkItem) => networkItem)
461427
.map((networkItem) => {
@@ -471,6 +437,7 @@
471437
onSelect={handleNetworkChange}
472438
/>
473439
<SearchableDropdown
440+
bind:this={currencyDropdown}
474441
items={filteredSettlementCurrencies.map((currency) => ({
475442
value: currency,
476443
type: "settlementCurrency",
@@ -481,6 +448,8 @@
481448
`${currency.value.symbol} (${currency.value.network})`}
482449
onSelect={handleCurrencyChange}
483450
/>
451+
</div>
452+
<div>
484453
{#if cipherProvider}
485454
<Input
486455
type="checkbox"

shared/components/searchable-dropdown.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
}
7575
7676
$: if (!isOpen) selectedIndex = -1;
77+
78+
export function clear() {
79+
searchTerm = "";
80+
isOpen = false;
81+
selectedIndex = -1;
82+
}
7783
</script>
7884

7985
{#if isOpen && !disabled}

0 commit comments

Comments
 (0)