Skip to content

Commit

Permalink
Translate rejections error messages (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoontek authored Oct 6, 2023
1 parent 359801b commit 500c0d8
Show file tree
Hide file tree
Showing 32 changed files with 291 additions and 511 deletions.
34 changes: 17 additions & 17 deletions clients/banking/src/components/AccountStatementCustom.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Array, Option } from "@swan-io/boxed";
import { Array, Option, Result } from "@swan-io/boxed";
import { Link } from "@swan-io/chicane";
import { BorderedIcon } from "@swan-io/lake/src/components/BorderedIcon";
import { Box } from "@swan-io/lake/src/components/Box";
Expand Down Expand Up @@ -31,12 +31,13 @@ import { useUrqlPaginatedQuery } from "@swan-io/lake/src/hooks/useUrqlQuery";
import { showToast } from "@swan-io/lake/src/state/toasts";
import { isNotNullish } from "@swan-io/lake/src/utils/nullish";
import { GetNode } from "@swan-io/lake/src/utils/types";
import { translateError } from "@swan-io/shared-business/src/utils/i18n";
import dayjs from "dayjs";
import { useMemo, useState } from "react";
import { StyleSheet, View } from "react-native";
import { combineValidators, hasDefinedKeys, useForm } from "react-ux-form";
import { Rifm } from "rifm";
import { P, isMatching, match } from "ts-pattern";
import { P, isMatching } from "ts-pattern";
import {
AccountLanguage,
AccountStatementsPageDocument,
Expand Down Expand Up @@ -303,6 +304,7 @@ const NewStatementForm = ({
if (hasDefinedKeys(values, ["startDate", "closingDate", "format", "language"])) {
const now = dayjs();
const closingDate = dayjs(values.closingDate, locale.dateFormat);

return generateStatement({
input: {
accountId,
Expand All @@ -317,21 +319,19 @@ const NewStatementForm = ({
statementType: values.format,
language: values.language,
},
}).onResolve(result =>
result.match({
Error: () => {
showToast({ variant: "error", title: t("error.generic") });
},
Ok: ({ generateAccountStatement: data }) => {
match(data)
.with({ __typename: "Statement" }, () => {
setOpenNewStatement(false);
reload();
})
.otherwise(() => showToast({ variant: "error", title: t("error.generic") }));
},
}),
);
})
.mapOk(data => data.generateAccountStatement)
.mapOkToResult(({ __typename }) =>
__typename === "Statement" ? Result.Ok(undefined) : Result.Error(__typename),
)
.tapOk(() => {
setOpenNewStatement(false);
reload();
})
.tapError(error => {
showToast({ variant: "error", title: translateError(error) });
})
.toPromise();
}
});
};
Expand Down
15 changes: 6 additions & 9 deletions clients/banking/src/components/CardCancelConfirmationModal.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Result } from "@swan-io/boxed";
import { LakeButton } from "@swan-io/lake/src/components/LakeButton";
import { LakeModal } from "@swan-io/lake/src/components/LakeModal";
import { LakeText } from "@swan-io/lake/src/components/LakeText";
import { Space } from "@swan-io/lake/src/components/Space";
import { colors } from "@swan-io/lake/src/constants/design";
import { useUrqlMutation } from "@swan-io/lake/src/hooks/useUrqlMutation";
import { showToast } from "@swan-io/lake/src/state/toasts";
import { match } from "ts-pattern";
import { filterRejectionsToResult } from "@swan-io/lake/src/utils/urql";
import { translateError } from "@swan-io/shared-business/src/utils/i18n";
import { CancelCardDocument } from "../graphql/partner";
import { t } from "../utils/i18n";

Expand All @@ -28,16 +28,13 @@ export const CardCancelConfirmationModal = ({
const onPressConfirm = () => {
if (cardId != null) {
cancelCard({ cardId })
.mapOkToResult(({ cancelCard }) => {
return match(cancelCard)
.with({ __typename: "CancelCardSuccessPayload" }, () => Result.Ok(undefined))
.otherwise(error => Result.Error(error));
})
.mapOk(data => data.cancelCard)
.mapOkToResult(filterRejectionsToResult)
.tapOk(() => {
onSuccess();
})
.tapError(() => {
showToast({ variant: "error", title: t("error.generic") });
.tapError(error => {
showToast({ variant: "error", title: translateError(error) });
});
}
};
Expand Down
15 changes: 7 additions & 8 deletions clients/banking/src/components/CardItemMobilePayment.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Array, Option, Result } from "@swan-io/boxed";
import { Array, Option } from "@swan-io/boxed";
import { Box } from "@swan-io/lake/src/components/Box";
import { FixedListViewEmpty } from "@swan-io/lake/src/components/FixedListView";
import { LakeButton } from "@swan-io/lake/src/components/LakeButton";
Expand All @@ -12,6 +12,8 @@ import { commonStyles } from "@swan-io/lake/src/constants/commonStyles";
import { colors } from "@swan-io/lake/src/constants/design";
import { useUrqlMutation } from "@swan-io/lake/src/hooks/useUrqlMutation";
import { showToast } from "@swan-io/lake/src/state/toasts";
import { filterRejectionsToResult } from "@swan-io/lake/src/utils/urql";
import { translateError } from "@swan-io/shared-business/src/utils/i18n";
import dayjs from "dayjs";
import { Fragment, useState } from "react";
import { Image, StyleSheet, View } from "react-native";
Expand Down Expand Up @@ -128,17 +130,14 @@ export const CardItemMobilePayment = ({

const onPressCancel = ({ digitalCardId }: { digitalCardId: string }) => {
cancelDigitalCard({ digitalCardId })
.mapOkToResult(({ cancelDigitalCard }) => {
return match(cancelDigitalCard)
.with({ __typename: "CancelDigitalCardSuccessPayload" }, () => Result.Ok(undefined))
.otherwise(error => Result.Error(error));
})
.mapOk(data => data.cancelDigitalCard)
.mapOkToResult(filterRejectionsToResult)
.tapOk(() => {
setCancelConfirmationModalModal(Option.None());
onRefreshRequest();
})
.tapError(() => {
showToast({ variant: "error", title: t("error.generic") });
.tapError(error => {
showToast({ variant: "error", title: translateError(error) });
});
};

Expand Down
131 changes: 42 additions & 89 deletions clients/banking/src/components/CardItemPhysicalDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Option, Result } from "@swan-io/boxed";
import { Box } from "@swan-io/lake/src/components/Box";
import { Fill } from "@swan-io/lake/src/components/Fill";
import { Icon } from "@swan-io/lake/src/components/Icon";
Expand All @@ -20,6 +19,7 @@ import { colors } from "@swan-io/lake/src/constants/design";
import { useUrqlMutation } from "@swan-io/lake/src/hooks/useUrqlMutation";
import { showToast } from "@swan-io/lake/src/state/toasts";
import { nullishOrEmptyToUndefined } from "@swan-io/lake/src/utils/nullish";
import { filterRejectionsToResult } from "@swan-io/lake/src/utils/urql";
import { CountryPicker } from "@swan-io/shared-business/src/components/CountryPicker";
import { GMapAddressSearchInput } from "@swan-io/shared-business/src/components/GMapAddressSearchInput";
import {
Expand All @@ -28,6 +28,7 @@ import {
getCountryName,
isCountryCCA3,
} from "@swan-io/shared-business/src/constants/countries";
import { translateError } from "@swan-io/shared-business/src/utils/i18n";
import { useState } from "react";
import { Image, StyleSheet, View } from "react-native";
import { combineValidators, hasDefinedKeys, useForm } from "react-ux-form";
Expand Down Expand Up @@ -481,39 +482,24 @@ export const CardItemPhysicalDetails = ({
address,
},
})
.mapOkToResult(({ printPhysicalCard }) => {
return match(printPhysicalCard)
.mapOk(data => data.printPhysicalCard)
.mapOkToResult(filterRejectionsToResult)
.mapOk(data => data.physicalCard.statusInfo)
.tapOk(data => {
match(data)
.with(
{
__typename: "PrintPhysicalCardSuccessPayload",
physicalCard: {
statusInfo: {
__typename: "PhysicalCardConsentPendingStatusInfo",
consent: { consentUrl: P.select() },
},
},
{ __typename: "PhysicalCardConsentPendingStatusInfo" },
({ consent: { consentUrl } }) => {
window.location.replace(consentUrl);
},
consentUrl => Result.Ok(Option.Some(consentUrl)),
)
.with(
{
__typename: "PrintPhysicalCardSuccessPayload",
},
() => Result.Ok(Option.None()),
)
.otherwise(value => Result.Error(value));
})
.tapOk(consentUrl => {
consentUrl.match({
Some: consentUrl => window.location.replace(consentUrl),
None: () => {
.otherwise(() => {
setIsOrderModalOpen(false);
onRefreshRequest();
},
});
});
})
.tapError(() => {
showToast({ variant: "error", title: t("error.generic") });
.tapError(error => {
showToast({ variant: "error", title: translateError(error) });
});
};

Expand All @@ -524,37 +510,26 @@ export const CardItemPhysicalDetails = ({
reason,
},
})
.mapOkToResult(({ cancelPhysicalCard }) => {
return match(cancelPhysicalCard)
.with(
{
__typename: "CancelPhysicalCardSuccessPayload",
},
() => Result.Ok(undefined),
)
.otherwise(value => Result.Error(value));
})
.mapOk(data => data.cancelPhysicalCard)
.mapOkToResult(filterRejectionsToResult)
.tapOk(() => {
setIsPermanentlyBlockModalOpen(false);
onRefreshRequest();
})
.tapError(() => {
showToast({ variant: "error", title: t("error.generic") });
.tapError(error => {
showToast({ variant: "error", title: translateError(error) });
});
};

const suspendCard = () => {
suspendPhysicalCard({ cardId })
.mapOkToResult(({ suspendPhysicalCard }) => {
return match(suspendPhysicalCard)
.with({ __typename: "SuspendPhysicalCardSuccessPayload" }, () => Result.Ok(undefined))
.otherwise(error => Result.Error(error));
})
.mapOk(data => data.suspendPhysicalCard)
.mapOkToResult(filterRejectionsToResult)
.tapOk(() => {
onRefreshRequest();
})
.tapError(() => {
showToast({ variant: "error", title: t("error.generic") });
.tapError(error => {
showToast({ variant: "error", title: translateError(error) });
});
};

Expand All @@ -567,18 +542,13 @@ export const CardItemPhysicalDetails = ({
Router.AccountCardsItemPhysicalCard({ cardId, accountMembershipId }),
},
})
.mapOkToResult(({ resumePhysicalCard }) => {
return match(resumePhysicalCard)
.with({ __typename: "ResumePhysicalCardSuccessPayload" }, ({ consent: { consentUrl } }) =>
Result.Ok(consentUrl),
)
.otherwise(error => Result.Error(error));
})
.tapOk(consentUrl => {
.mapOk(data => data.resumePhysicalCard)
.mapOkToResult(filterRejectionsToResult)
.tapOk(({ consent: { consentUrl } }) => {
window.location.replace(consentUrl);
})
.tapError(() => {
showToast({ variant: "error", title: t("error.generic") });
.tapError(error => {
showToast({ variant: "error", title: translateError(error) });
});
};

Expand All @@ -591,19 +561,13 @@ export const CardItemPhysicalDetails = ({
Router.AccountCardsItemPhysicalCard({ cardId, accountMembershipId }),
},
})
.mapOkToResult(({ viewPhysicalCardPin }) => {
return match(viewPhysicalCardPin)
.with(
{ __typename: "ViewPhysicalCardPinSuccessPayload" },
({ consent: { consentUrl } }) => Result.Ok(consentUrl),
)
.otherwise(error => Result.Error(error));
})
.tapOk(consentUrl => {
.mapOk(data => data.viewPhysicalCardPin)
.mapOkToResult(filterRejectionsToResult)
.tapOk(({ consent: { consentUrl } }) => {
window.location.replace(consentUrl);
})
.tapError(() => {
showToast({ variant: "error", title: t("error.generic") });
.tapError(error => {
showToast({ variant: "error", title: translateError(error) });
});
};

Expand All @@ -616,19 +580,13 @@ export const CardItemPhysicalDetails = ({
Router.AccountCardsItemPhysicalCard({ cardId, accountMembershipId }),
},
})
.mapOkToResult(({ activatePhysicalCard }) => {
return match(activatePhysicalCard)
.with(
{ __typename: "ActivatePhysicalCardSuccessPayload" },
({ consent: { consentUrl } }) => Result.Ok(consentUrl),
)
.otherwise(error => Result.Error(error));
})
.tapOk(consentUrl => {
.mapOk(data => data.activatePhysicalCard)
.mapOkToResult(filterRejectionsToResult)
.tapOk(({ consent: { consentUrl } }) => {
window.location.replace(consentUrl);
})
.tapError(() => {
showToast({ variant: "error", title: t("error.generic") });
.tapError(error => {
showToast({ variant: "error", title: translateError(error) });
});
};

Expand All @@ -640,18 +598,13 @@ export const CardItemPhysicalDetails = ({
window.location.origin + Router.AccountCardsItem({ cardId, accountMembershipId }),
},
})
.mapOkToResult(({ viewPhysicalCardNumbers }) => {
return match(viewPhysicalCardNumbers)
.with({ __typename: "ViewPhysicalCardNumbersSuccessPayload" }, value =>
Result.Ok(value.consent.consentUrl),
)
.otherwise(error => Result.Error(error));
})
.tapOk(consentUrl => {
.mapOk(data => data.viewPhysicalCardNumbers)
.mapOkToResult(filterRejectionsToResult)
.tapOk(({ consent: { consentUrl } }) => {
window.location.replace(consentUrl);
})
.tapError(() => {
showToast({ variant: "error", title: t("error.generic") });
.tapError(error => {
showToast({ variant: "error", title: translateError(error) });
});
};

Expand Down
19 changes: 9 additions & 10 deletions clients/banking/src/components/CardItemSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { LakeButton, LakeButtonGroup } from "@swan-io/lake/src/components/LakeBu
import { Space } from "@swan-io/lake/src/components/Space";
import { useUrqlMutation } from "@swan-io/lake/src/hooks/useUrqlMutation";
import { showToast } from "@swan-io/lake/src/state/toasts";
import { isNotNullish } from "@swan-io/lake/src/utils/nullish";
import { filterRejectionsToResult } from "@swan-io/lake/src/utils/urql";
import { translateError } from "@swan-io/shared-business/src/utils/i18n";
import { useRef, useState } from "react";
import { StyleSheet, View } from "react-native";
import { P, match } from "ts-pattern";
Expand Down Expand Up @@ -70,18 +73,14 @@ export const CardItemSettings = ({
nonMainCurrencyTransactions,
},
})
.mapOkToResult(({ updateCard }) => {
return match(updateCard)
.with({ __typename: "UpdateCardSuccessPayload" }, ({ consent }) =>
Result.Ok(consent.consentUrl),
)
.otherwise(value => Result.Error(value));
})
.tapOk(consentUrl => {
.mapOk(data => data.updateCard)
.mapOkToResult(data => (isNotNullish(data) ? Result.Ok(data) : Result.Error(undefined)))
.mapOkToResult(filterRejectionsToResult)
.tapOk(({ consent: { consentUrl } }) => {
window.location.replace(consentUrl);
})
.tapError(() => {
showToast({ variant: "error", title: t("error.generic") });
.tapError(error => {
showToast({ variant: "error", title: translateError(error) });
});
};

Expand Down
Loading

0 comments on commit 500c0d8

Please sign in to comment.