Skip to content

Commit ba3c8ef

Browse files
committed
fix: update reactivity for invoiceCurrenyc and filtering
1 parent b816f11 commit ba3c8ef

File tree

2 files changed

+68
-49
lines changed

2 files changed

+68
-49
lines changed

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,53 +58,55 @@
5858
5959
const handleNetworkChange = (newNetwork: string) => {
6060
if (newNetwork) {
61-
const newCurrencies = currencyManager.knownCurrencies.filter(
62-
(currency: CurrencyTypes.CurrencyDefinition) =>
63-
currency.type === Types.RequestLogic.CURRENCY.ISO4217 ||
64-
currency.network === newNetwork
65-
);
66-
6761
network = newNetwork;
68-
defaultCurrencies = newCurrencies;
62+
63+
invoiceCurrency = undefined;
6964
currency = undefined;
65+
66+
defaultCurrencies = currencyManager.knownCurrencies.filter(
67+
(curr: CurrencyTypes.CurrencyDefinition) =>
68+
curr.type === Types.RequestLogic.CURRENCY.ISO4217 ||
69+
curr.network === newNetwork
70+
);
7071
}
7172
};
7273
7374
let activeRequest: any = null;
7475
let canSubmit = false;
7576
let appStatus: APP_STATUS[] = [];
7677
let formData = getInitialFormData();
77-
let defaultCurrencies = currencyManager.knownCurrencies.filter(
78-
(currency: CurrencyTypes.CurrencyDefinition) =>
79-
currency.type === Types.RequestLogic.CURRENCY.ISO4217 || network
80-
? currency.network === network
81-
: true
82-
);
78+
let defaultCurrencies = currencyManager.knownCurrencies;
8379
8480
const handleInvoiceCurrencyChange = (
8581
value: CurrencyTypes.CurrencyDefinition
8682
) => {
87-
invoiceCurrency = value;
88-
network = undefined;
89-
currency = undefined;
83+
if (value !== invoiceCurrency) {
84+
invoiceCurrency = value;
85+
currency = undefined;
9086
91-
if (
92-
invoiceCurrency &&
93-
invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217
94-
) {
95-
networks = getCurrencySupportedNetworksForConversion(
96-
invoiceCurrency.hash,
97-
currencyManager
98-
);
99-
} else {
100-
networks = extractUniqueNetworkNames();
87+
if (value.type !== Types.RequestLogic.CURRENCY.ISO4217) {
88+
network = value.network;
89+
}
10190
}
10291
};
10392
10493
const handleCurrencyChange = (value: CurrencyTypes.CurrencyDefinition) => {
10594
currency = value;
10695
};
10796
97+
$: {
98+
if (invoiceCurrency) {
99+
if (invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217) {
100+
networks = getCurrencySupportedNetworksForConversion(
101+
invoiceCurrency.hash,
102+
currencyManager
103+
);
104+
} else {
105+
networks = extractUniqueNetworkNames();
106+
}
107+
}
108+
}
109+
108110
let invoiceTotals = {
109111
amountWithoutTax: 0,
110112
totalTaxAmount: 0,

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

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
export let formData: CustomFormData;
2828
export let handleInvoiceCurrencyChange: (value: string) => void;
2929
export let handleCurrencyChange: (value: string) => void;
30-
3130
export let handleNetworkChange: (chainId: string) => void;
3231
export let networks;
3332
export let defaultCurrencies: any = [];
@@ -117,21 +116,6 @@
117116
}
118117
};
119118
120-
const filterSettlementCurrencies = (
121-
currency: CurrencyTypes.CurrencyDefinition
122-
) => {
123-
return invoiceCurrency
124-
? invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217
125-
? currency.type !== Types.RequestLogic.CURRENCY.ISO4217 &&
126-
currencyManager?.getConversionPath(
127-
invoiceCurrency,
128-
currency,
129-
currency.network
130-
)?.length > 0
131-
: invoiceCurrency.hash === currency.hash
132-
: false;
133-
};
134-
135119
const addInvoiceItem = () => {
136120
const newItem = {
137121
name: "",
@@ -418,10 +402,19 @@
418402
? `${invoiceCurrency.symbol} ${invoiceCurrency?.network ? `(${invoiceCurrency?.network})` : ""}`
419403
: undefined}
420404
placeholder="Invoice currency (labeling)"
421-
options={defaultCurrencies.map((currency) => ({
422-
value: currency,
423-
label: `${currency.symbol} ${currency?.network ? `(${currency?.network})` : ""}`,
424-
}))}
405+
options={defaultCurrencies
406+
?.filter((curr) => {
407+
if (!curr) return false;
408+
409+
return (
410+
curr.type === Types.RequestLogic.CURRENCY.ISO4217 ||
411+
(curr.network && curr.network === network)
412+
);
413+
})
414+
.map((currency) => ({
415+
value: currency,
416+
label: `${currency?.symbol ?? "Unknown"} ${currency?.network ? `(${currency.network})` : ""}`,
417+
})) ?? []}
425418
onchange={handleInvoiceCurrencyChange}
426419
/>
427420
<Dropdown
@@ -431,11 +424,35 @@
431424
? `${currency.symbol ?? "Unknown"} (${currency?.network ?? "Unknown"})`
432425
: undefined}
433426
options={defaultCurrencies
434-
.filter((currency) => filterSettlementCurrencies(currency))
427+
?.filter((curr) => {
428+
if (!curr || !invoiceCurrency) return false;
429+
430+
if (
431+
invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217
432+
) {
433+
return (
434+
(curr.type === Types.RequestLogic.CURRENCY.ERC20 ||
435+
curr.type === Types.RequestLogic.CURRENCY.ISO4217) &&
436+
curr.network === network
437+
);
438+
} else if (
439+
invoiceCurrency.type === Types.RequestLogic.CURRENCY.ERC20
440+
) {
441+
return (
442+
curr.type === Types.RequestLogic.CURRENCY.ERC20 &&
443+
curr.network === invoiceCurrency.network
444+
);
445+
} else {
446+
return (
447+
curr.type === Types.RequestLogic.CURRENCY.ERC20 &&
448+
curr.network === invoiceCurrency.network
449+
);
450+
}
451+
})
435452
.map((currency) => ({
436453
value: currency,
437-
label: `${currency.symbol ?? "Unknown"} (${currency?.network ?? "Unknown"})`,
438-
}))}
454+
label: `${currency?.symbol ?? "Unknown"} ${currency?.network ? `(${currency.network})` : ""}`,
455+
})) ?? []}
439456
onchange={handleCurrencyChange}
440457
/>
441458
</div>

0 commit comments

Comments
 (0)