From abdd12f114ddad8ca38734a065861bae2a9b48df Mon Sep 17 00:00:00 2001 From: Eric Corson Date: Fri, 23 Jun 2023 11:28:51 +0900 Subject: [PATCH] fix: calculate toast props from toast type 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. --- apps/namada-interface/src/slices/transfers.ts | 115 ++++++++++-------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/apps/namada-interface/src/slices/transfers.ts b/apps/namada-interface/src/slices/transfers.ts index 75851a928c..d74da5f201 100644 --- a/apps/namada-interface/src/slices/transfers.ts +++ b/apps/namada-interface/src/slices/transfers.ts @@ -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, @@ -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.TransferStarted ? TransferStartedToastProps : + T extends Toasts.TransferCompleted ? TransferCompletedToastProps : + never; -export const getToast = ( +export const getToast = ( 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) => 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) => CreateToastPayload; }; const TRANSFERS_ACTIONS_BASE = "transfers"; @@ -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: { @@ -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)() + // ) + //); } ); @@ -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. @@ -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)() + // ) + //); } );