-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add functionality to update the quote inputs from outside the w…
…idget
- Loading branch information
1 parent
8aaee05
commit d9722fb
Showing
5 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useUpdateQuoteInput } from './useUpdateQuoteInput'; |
50 changes: 50 additions & 0 deletions
50
widget/embedded/src/hooks/useUpdateQuoteInput/useUpdateQuoteInput.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters