Skip to content

Commit

Permalink
feat: add functionality to update the quote inputs from outside the w…
Browse files Browse the repository at this point in the history
…idget
  • Loading branch information
Ikari-Shinji-re authored and nikaaru committed Jul 28, 2024
1 parent 8aaee05 commit d9722fb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
5 changes: 5 additions & 0 deletions widget/embedded/src/containers/WidgetInfo/WidgetInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useManager } from '@rango-dev/queue-manager-react';
import React, { createContext, useContext } from 'react';

import { useLanguage } from '../../hooks/useLanguage';
import { useUpdateQuoteInput } from '../../hooks/useUpdateQuoteInput/useUpdateQuoteInput';
import { useAppStore } from '../../store/AppStore';
import { useNotificationStore } from '../../store/notification';
import { useQuoteStore } from '../../store/quote';
Expand Down Expand Up @@ -35,6 +36,7 @@ export function WidgetInfo(props: React.PropsWithChildren) {
const resetLanguage = useLanguage().resetLanguage;
const notifications = useNotificationStore().getNotifications();
const clearNotifications = useNotificationStore().clearNotifications;
const updateQuoteInput = useUpdateQuoteInput();

// eslint-disable-next-line react/jsx-no-constructed-context-values
const value: WidgetInfoContextInterface = {
Expand All @@ -59,6 +61,9 @@ export function WidgetInfo(props: React.PropsWithChildren) {
list: notifications,
clearAll: clearNotifications,
},
quote: {
updateQuoteInput,
},
};

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { WidgetHistory } from './WidgetInfo.helpers';
import type { FetchStatus, FindToken } from '../../store/slices/data';
import type { ConnectedWallet } from '../../store/wallets';
import type { Wallet } from '../../types';
import type { UpdateQuoteInput, Wallet } from '../../types';
import type { Notification } from '../../types/notification';
import type { BlockchainMeta, SwapperMeta, Token } from 'rango-sdk';

Expand All @@ -27,4 +27,7 @@ export interface WidgetInfoContextInterface {
list: Notification[];
clearAll: () => void;
};
quote: {
updateQuoteInput: UpdateQuoteInput;
};
}
1 change: 1 addition & 0 deletions widget/embedded/src/hooks/useUpdateQuoteInput/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useUpdateQuoteInput } from './useUpdateQuoteInput';
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { UpdateQuoteInput } from '../../types';

import { useAppStore } from '../../store/AppStore';
import { useQuoteStore } from '../../store/quote';

// This hook provides a function to update quote inputs using fewer and simpler parameters.
export function useUpdateQuoteInput() {
const { findToken } = useAppStore();
const blockchains = useAppStore().blockchains();
const tokens = useAppStore().tokens();
const {
setFromBlockchain,
setFromToken,
setToBlockchain,
setToToken,
setInputAmount,
} = useQuoteStore();

const updateQuoteInput: UpdateQuoteInput = (params) => {
const { fromBlockchain, fromToken, toBlockchain, toToken, requestAmount } =
params;
const meta = { blockchains, tokens };

if (fromBlockchain !== undefined) {
const blockchainMeta =
blockchains.find((blockchain) => blockchain.name === fromBlockchain) ??
null;
setFromBlockchain(blockchainMeta);
}
if (fromToken !== undefined) {
const token = fromToken ? findToken(fromToken) ?? null : null;
setFromToken({ meta, token });
}
if (toBlockchain !== undefined) {
const blockchainMeta =
blockchains.find((blockchain) => blockchain.name === toBlockchain) ??
null;
setToBlockchain(blockchainMeta);
}
if (toToken !== undefined) {
const token = toToken ? findToken(toToken) ?? null : null;
setToToken({ meta, token });
}
if (requestAmount !== undefined) {
setInputAmount(requestAmount);
}
};

return updateQuoteInput;
}
9 changes: 9 additions & 0 deletions widget/embedded/src/types/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { PriceImpactWarningLevel } from '@rango-dev/ui';
import type BigNumber from 'bignumber.js';
import type {
Asset,
BlockchainValidationStatus,
RouteTag,
RoutingResultType,
Expand Down Expand Up @@ -131,3 +132,11 @@ export type QuoteErrorResponse = {
message: string;
options: QuoteError;
};

export type UpdateQuoteInput = (params: {
fromBlockchain?: string | null;
toBlockchain?: string | null;
fromToken?: Asset | null;
toToken?: Asset | null;
requestAmount?: string;
}) => void;

0 comments on commit d9722fb

Please sign in to comment.