Skip to content

Commit

Permalink
fix: calculate toast props from toast type
Browse files Browse the repository at this point in the history
This fixes a problem where TypeScript was not throwing an error when
getToast was called with the wrong arguments. The code calling
getToast has been commented out for now.
  • Loading branch information
emccorson committed Jul 19, 2023
1 parent cfa2f5e commit abdd12f
Showing 1 changed file with 62 additions and 53 deletions.
115 changes: 62 additions & 53 deletions apps/namada-interface/src/slices/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import BigNumber from "bignumber.js";

import { Account, Tokens, TokenType, Signer } from "@anoma/types";
import { assertNever } from "@anoma/utils";
import { getIntegration } from "@anoma/hooks";

import {
actions as notificationsActions,
//actions as notificationsActions,
CreateToastPayload,
ToastId,
ToastTimeout,
Expand All @@ -20,39 +21,43 @@ export enum Toasts {

type TransferCompletedToastProps = { msgId: string; success: boolean };
type TransferStartedToastProps = { msgId: string };
type GetToastProps =
| TransferCompletedToastProps
| TransferStartedToastProps
| void;
type GetToastProps<T extends Toasts> =
T extends Toasts.TransferStarted ? TransferStartedToastProps :
T extends Toasts.TransferCompleted ? TransferCompletedToastProps :
never;

export const getToast = (
export const getToast = <T extends Toasts>(
toastId: ToastId,
toast: Toasts
): ((props: GetToastProps) => CreateToastPayload) => {
const toasts = {
[Toasts.TransferStarted]: (payload: TransferCompletedToastProps) => ({
id: toastId,
data: {
title: "Transfer in progress!",
message: payload.msgId,
type: "pending-info" as ToastType,
timeout: ToastTimeout.None(),
},
}),
[Toasts.TransferCompleted]: ({
success,
msgId,
}: TransferCompletedToastProps) => ({
id: toastId,
data: {
title: success ? "Transfer successful!" : "Transfer failed.",
message: msgId,
type: success ? "success" : "error",
},
}),
};
toast: T
): ((props: GetToastProps<T>) => CreateToastPayload) => {
const toastFunction =
toast === Toasts.TransferStarted ?
(payload: TransferCompletedToastProps) => ({
id: toastId,
data: {
title: "Transfer in progress!",
message: payload.msgId,
type: "pending-info" as ToastType,
timeout: ToastTimeout.None(),
},
}) :

toast === Toasts.TransferCompleted ?
({
success,
msgId,
}: TransferCompletedToastProps) => ({
id: toastId,
data: {
title: success ? "Transfer successful!" : "Transfer failed.",
message: msgId,
type: success ? "success" : "error",
},
}) :

assertNever(toast);

return toasts[toast] as (props: GetToastProps) => CreateToastPayload;
return toastFunction as (props: GetToastProps<T>) => CreateToastPayload;
};

const TRANSFERS_ACTIONS_BASE = "transfers";
Expand Down Expand Up @@ -177,15 +182,16 @@ export const submitIbcTransferTransaction = createAsyncThunk<
{ state: RootState }
>(
`${TRANSFERS_ACTIONS_BASE}/${TransfersThunkActions.SubmitIbcTransferTransaction}`,
async (txIbcTransferArgs, { getState, dispatch, requestId }) => {
async (txIbcTransferArgs, { getState, /* dispatch, requestId */ }) => {
const { chainId } = getState().settings;
const integration = getIntegration(chainId);

dispatch(
notificationsActions.createToast(
getToast(`${requestId}-pending`, Toasts.TransferStarted)()
)
);
// TODO: Add toast props
//dispatch(
// notificationsActions.createToast(
// getToast(`${requestId}-pending`, Toasts.TransferStarted)()
// )
//);

await integration.submitBridgeTransfer({
ibcProps: {
Expand All @@ -204,11 +210,12 @@ export const submitIbcTransferTransaction = createAsyncThunk<
},
});

dispatch(
notificationsActions.createToast(
getToast(`${requestId}-fullfilled`, Toasts.TransferCompleted)()
)
);
// TODO: Add toast props
//dispatch(
// notificationsActions.createToast(
// getToast(`${requestId}-fullfilled`, Toasts.TransferCompleted)()
// )
//);
}
);

Expand All @@ -218,15 +225,16 @@ export const submitBridgeTransferTransaction = createAsyncThunk<
{ state: RootState }
>(
`${TRANSFERS_ACTIONS_BASE}/${TransfersThunkActions.SubmitBridgeTransferTransaction}`,
async (txBridgeTransferArgs, { getState, dispatch, requestId }) => {
async (txBridgeTransferArgs, { getState, /* dispatch, requestId */ }) => {
const { chainId } = getState().settings;
const integration = getIntegration(chainId);

dispatch(
notificationsActions.createToast(
getToast(`${requestId}-pending`, Toasts.TransferStarted)()
)
);
// TODO: Add toast props
//dispatch(
// notificationsActions.createToast(
// getToast(`${requestId}-pending`, Toasts.TransferStarted)()
// )
//);

await integration.submitBridgeTransfer({
// TODO: tx (below) is *not* required for Keplr, but are required for this type.
Expand All @@ -245,11 +253,12 @@ export const submitBridgeTransferTransaction = createAsyncThunk<
},
});

dispatch(
notificationsActions.createToast(
getToast(`${requestId}-fullfilled`, Toasts.TransferCompleted)()
)
);
// TODO: Add toast props
//dispatch(
// notificationsActions.createToast(
// getToast(`${requestId}-fullfilled`, Toasts.TransferCompleted)()
// )
//);
}
);

Expand Down

0 comments on commit abdd12f

Please sign in to comment.