Skip to content

Commit

Permalink
quote validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ncomont committed Nov 23, 2023
1 parent c78c8bf commit 7a5c65c
Showing 1 changed file with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AsyncData, Result } from "@swan-io/boxed";
import { Box } from "@swan-io/lake/src/components/Box";
import { Fill } from "@swan-io/lake/src/components/Fill";
import { LakeAlert } from "@swan-io/lake/src/components/LakeAlert";
import { LakeButton, LakeButtonGroup } from "@swan-io/lake/src/components/LakeButton";
import { LakeHeading } from "@swan-io/lake/src/components/LakeHeading";
import { LakeLabel } from "@swan-io/lake/src/components/LakeLabel";
Expand All @@ -22,6 +23,7 @@ import {
GetInternationalCreditTransferQuoteQuery,
} from "../graphql/partner";
import { Currency, currencies, formatCurrency, formatNestedMessage, t } from "../utils/i18n";
import { isCombinedError } from "../utils/urql";
import { ErrorView } from "./ErrorView";

const styles = StyleSheet.create({
Expand Down Expand Up @@ -109,6 +111,27 @@ export const TransferInternationalWizardAmount = ({
);
}, [listenFields]);

const errors = match(quote)
.with(AsyncData.P.Done(Result.P.Error(P.select())), error => {
if (isCombinedError(error)) {
return match(error)
.with(
{
graphQLErrors: P.array({
extensions: {
code: "QuoteValidationError",
errors: P.array({ message: P.select(P.string) }),
},
}),
},
([messages]) => messages ?? [],
)
.otherwise(() => []);
}
return [];
})
.otherwise(() => []);

return (
<View>
<Tile>
Expand Down Expand Up @@ -152,7 +175,7 @@ export const TransferInternationalWizardAmount = ({
error={error}
valid={valid}
onChangeText={nextValue => {
onChange({ currency, value: nextValue });
onChange({ currency, value: nextValue.replace(/,/g, ".") });
}}
onBlur={onBlur}
units={currencies.toSorted() as unknown as string[]}
Expand All @@ -169,18 +192,23 @@ export const TransferInternationalWizardAmount = ({

<Space height={24} />

{match(quote)
.with(AsyncData.P.NotAsked, () => null)
.with(AsyncData.P.Loading, () => <QuoteDetailsPlaceholder />)
.with(
AsyncData.P.Done(
Result.P.Ok({ internationalCreditTransferQuote: P.select(P.not(P.nullish)) }),
),
quote => <QuoteDetails quote={quote} />,
)
.otherwise(() => (
<ErrorView />
))}
{errors.length
? errors.map((message, i) => (
<View key={`validation-alert-${i}`}>
<LakeAlert variant="error" title={message} />
<Space height={12} />
</View>
))
: match(quote)
.with(AsyncData.P.NotAsked, () => null)
.with(AsyncData.P.Loading, () => <QuoteDetailsPlaceholder />)
.with(
AsyncData.P.Done(
Result.P.Ok({ internationalCreditTransferQuote: P.select(P.not(P.nullish)) }),
),
quote => <QuoteDetails quote={quote} />,
)
.otherwise(() => <ErrorView />)}
</Tile>

<Space height={32} />
Expand All @@ -195,6 +223,7 @@ export const TransferInternationalWizardAmount = ({
<LakeButton
color="current"
onPress={() =>
errors?.length === 0 &&
submitForm(values => {
if (hasDefinedKeys(values, ["amount"])) {
onSave({
Expand Down

0 comments on commit 7a5c65c

Please sign in to comment.