Skip to content

Commit

Permalink
feat(ui): avoid token picker closing when a token list is picked (#420)
Browse files Browse the repository at this point in the history
* feat(ui): avoid token picker closing when a token list is picked

* chore: add changeset

---------

Co-authored-by: luzzifoss <fedeluzzi00@gmail.com>
  • Loading branch information
luzzif and luzzifoss authored Oct 9, 2023
1 parent 0637ab9 commit 8b69377
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-mangos-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@carrot-kpi/ui": minor
---

Avoid token picker closing when a token list is selected
12 changes: 11 additions & 1 deletion packages/ui/src/components/erc20-token-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ export function ERC20TokenPicker({
[onSelectedTokenChange],
);

const handleSelectedListChange = useCallback(
(list: TokenListWithBalance) => {
if (onSelectedListChange) {
setCurrentView("search");
onSelectedListChange(list);
}
},
[onSelectedListChange],
);

const handleManageListsClick = useCallback(() => {
setCurrentView("manage-lists");
}, []);
Expand Down Expand Up @@ -131,7 +141,7 @@ export function ERC20TokenPicker({
<ManageLists
onDismiss={onDismiss}
loading={loading}
onSelectedListChange={onSelectedListChange}
onSelectedListChange={handleSelectedListChange}
selectedList={selectedList}
lists={lists}
chainId={chainId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,10 @@ export const ManageLists = ({
const index = (event.target as HTMLLIElement).dataset.index;
if (index !== undefined) {
const parsedIndex = parseInt(index);
if (parsedIndex >= 0) {
onSelectedListChange(lists[parsedIndex]);
if (!!onDismiss) onDismiss();
}
if (parsedIndex >= 0) onSelectedListChange(lists[parsedIndex]);
}
},
[lists, onDismiss, onSelectedListChange],
[lists, onSelectedListChange],
);

return (
Expand Down
24 changes: 19 additions & 5 deletions packages/ui/stories/erc20-token-picker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ const Component = (props: ERC20TokenPickerProps) => {

const [open, setOpen] = useState(false);
const [value, setValue] = useState<TokenInfoWithBalance | null>(null);
const [lists, setLists] = useState<TokenListWithBalance[]>([]);
const [list, setList] = useState<TokenListWithBalance | null>(null);

const lists = useMemo(() => (list ? [list] : []), [list]);

const {
data: rawBalances,
isLoading: loadingBalances,
Expand Down Expand Up @@ -113,15 +112,29 @@ const Component = (props: ERC20TokenPickerProps) => {
useEffect(() => {
let cancelled = false;
const fetchData = async () => {
const response = await fetch(
let response = await fetch(
`${getServiceURL(Service.STATIC_CDN, false)}/token-list.json`,
);
if (!response.ok) {
console.warn("could not fetch carrot token list");
return;
}
if (!cancelled)
setList((await response.json()) as TokenListWithBalance);
const carrotList = (await response.json()) as TokenListWithBalance;

response = await fetch(
"https://tokens.coingecko.com/uniswap/all.json",
);
if (!response.ok) {
console.warn("could not fetch coingecko token list");
return;
}
const coingeckoList =
(await response.json()) as TokenListWithBalance;

if (!cancelled) {
setLists([carrotList, coingeckoList]);
setList(carrotList);
}
};
void fetchData();
return () => {
Expand Down Expand Up @@ -176,6 +189,7 @@ const Component = (props: ERC20TokenPickerProps) => {
onDismiss={handleDismiss}
lists={lists}
selectedList={selectedListWithBalances}
onSelectedListChange={setList}
chainId={CHAIN_ID}
messages={{
search: {
Expand Down

0 comments on commit 8b69377

Please sign in to comment.