Fix all chains logic
This commit is contained in:
parent
76b74d2c96
commit
278ff37129
@ -1,5 +1,11 @@
|
|||||||
import { ChainItemType } from "../../types";
|
import { ChainItemType } from "../../types";
|
||||||
import { Dispatch, FunctionComponent, SetStateAction } from "react";
|
import {
|
||||||
|
Dispatch,
|
||||||
|
FunctionComponent,
|
||||||
|
SetStateAction,
|
||||||
|
useEffect,
|
||||||
|
useState,
|
||||||
|
} from "react";
|
||||||
|
|
||||||
import { ChainImage } from "./chain-image";
|
import { ChainImage } from "./chain-image";
|
||||||
import { Flex1 } from "../../styles/flex-1";
|
import { Flex1 } from "../../styles/flex-1";
|
||||||
@ -12,41 +18,54 @@ import {
|
|||||||
import color from "../../styles/color";
|
import color from "../../styles/color";
|
||||||
import styled from "styled-components";
|
import styled from "styled-components";
|
||||||
import { Checkbox } from "../checkbox";
|
import { Checkbox } from "../checkbox";
|
||||||
|
import AllChainsIcon from "../../public/images/svg/all-chains-icon.svg";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
allChecked: boolean;
|
chainList: ChainItemType[];
|
||||||
setAllChecked: Dispatch<SetStateAction<boolean>>;
|
checkedItems: Set<unknown>;
|
||||||
chainItem: ChainItemType;
|
setCheckedItems: Dispatch<SetStateAction<Set<unknown>>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AllChainsItem: FunctionComponent<Props> = (props) => {
|
export const AllChainsItem: FunctionComponent<Props> = (props) => {
|
||||||
const { allChecked, setAllChecked, chainItem } = props;
|
const { chainList, checkedItems, setCheckedItems } = props;
|
||||||
|
|
||||||
|
const [checked, setChecked] = useState(false);
|
||||||
|
|
||||||
const checkHandler = () => {
|
const checkHandler = () => {
|
||||||
setAllChecked(!allChecked);
|
if (checked) {
|
||||||
|
setCheckedItems(new Set());
|
||||||
|
} else if (chainList.length !== checkedItems.size) {
|
||||||
|
setCheckedItems(new Set(chainList));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (chainList.length === checkedItems.size && checkedItems.size !== 0) {
|
||||||
|
setChecked(true);
|
||||||
|
} else {
|
||||||
|
setChecked(false);
|
||||||
|
}
|
||||||
|
}, [checkedItems]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AllChainsContainer>
|
<AllChainsContainer>
|
||||||
<ChainItemContainer
|
<ChainItemContainer
|
||||||
key={chainItem.prefix}
|
key="all chains"
|
||||||
isLoading={false}
|
isLoading={false}
|
||||||
checked={allChecked}
|
checked={checked}
|
||||||
onClick={checkHandler}
|
onClick={checkHandler}
|
||||||
>
|
>
|
||||||
<ChainImage
|
<ChainImage src={AllChainsIcon} fill={true} alt={`all chain images`} />
|
||||||
src={chainItem.chainImageUrl}
|
|
||||||
fill={true}
|
|
||||||
alt={`${chainItem.prefix} chain image`}
|
|
||||||
/>
|
|
||||||
<ChainInfoContainer>
|
<ChainInfoContainer>
|
||||||
<ChainName>{`.${chainItem.prefix}`}</ChainName>
|
<ChainName>{`.all chains(${chainList.length})`}</ChainName>
|
||||||
<WalletAddress>{chainItem.address}</WalletAddress>
|
<WalletAddress>
|
||||||
|
{chainList.map((chain) => chain.chainName).join(", ")}
|
||||||
|
</WalletAddress>
|
||||||
</ChainInfoContainer>
|
</ChainInfoContainer>
|
||||||
|
|
||||||
<Flex1 />
|
<Flex1 />
|
||||||
|
|
||||||
<Checkbox checked={allChecked} />
|
<Checkbox checked={checked} />
|
||||||
</ChainItemContainer>
|
</ChainItemContainer>
|
||||||
</AllChainsContainer>
|
</AllChainsContainer>
|
||||||
);
|
);
|
||||||
|
@ -5,8 +5,6 @@ import styled from "styled-components";
|
|||||||
import { ChainItem } from "./chain-item";
|
import { ChainItem } from "./chain-item";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
allChecked: boolean;
|
|
||||||
setAllChecked: Dispatch<SetStateAction<boolean>>;
|
|
||||||
chainList: ChainItemType[];
|
chainList: ChainItemType[];
|
||||||
disabledChainList: ChainItemType[];
|
disabledChainList: ChainItemType[];
|
||||||
checkedItems: Set<unknown>;
|
checkedItems: Set<unknown>;
|
||||||
@ -14,14 +12,7 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const ChainList: FunctionComponent<Props> = (props) => {
|
export const ChainList: FunctionComponent<Props> = (props) => {
|
||||||
const {
|
const { chainList, disabledChainList, checkedItems, setCheckedItems } = props;
|
||||||
allChecked,
|
|
||||||
setAllChecked,
|
|
||||||
chainList,
|
|
||||||
disabledChainList,
|
|
||||||
checkedItems,
|
|
||||||
setCheckedItems,
|
|
||||||
} = props;
|
|
||||||
|
|
||||||
const checkedItemHandler = (chainItem: ChainItemType, isChecked: boolean) => {
|
const checkedItemHandler = (chainItem: ChainItemType, isChecked: boolean) => {
|
||||||
const tempSet = new Set(checkedItems);
|
const tempSet = new Set(checkedItems);
|
||||||
@ -35,22 +26,6 @@ export const ChainList: FunctionComponent<Props> = (props) => {
|
|||||||
setCheckedItems(tempSet);
|
setCheckedItems(tempSet);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (allChecked) {
|
|
||||||
setCheckedItems(new Set(chainList));
|
|
||||||
} else if (chainList.length === checkedItems.size) {
|
|
||||||
setCheckedItems(new Set());
|
|
||||||
}
|
|
||||||
}, [allChecked]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (chainList.length === checkedItems.size && checkedItems.size !== 0) {
|
|
||||||
setAllChecked(true);
|
|
||||||
} else {
|
|
||||||
setAllChecked(false);
|
|
||||||
}
|
|
||||||
}, [checkedItems]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ChainContainer color={color.grey["900"]}>
|
<ChainContainer color={color.grey["900"]}>
|
||||||
{chainList.map((chainItem) => (
|
{chainList.map((chainItem) => (
|
||||||
|
@ -30,7 +30,6 @@ import {
|
|||||||
} from "../../wallets";
|
} from "../../wallets";
|
||||||
import { ChainIdHelper } from "@keplr-wallet/cosmos";
|
import { ChainIdHelper } from "@keplr-wallet/cosmos";
|
||||||
|
|
||||||
import AllChainsIcon from "../../public/images/svg/all-chains-icon.svg";
|
|
||||||
import { AllChainsItem } from "../../components/chain-list/all-chains-item";
|
import { AllChainsItem } from "../../components/chain-list/all-chains-item";
|
||||||
import { SearchInput } from "../../components/search-input";
|
import { SearchInput } from "../../components/search-input";
|
||||||
import {
|
import {
|
||||||
@ -71,9 +70,6 @@ export default function VerificationPage() {
|
|||||||
const [registeredChainList, setRegisteredChainList] = useState<
|
const [registeredChainList, setRegisteredChainList] = useState<
|
||||||
RegisteredAddresses[]
|
RegisteredAddresses[]
|
||||||
>([]);
|
>([]);
|
||||||
|
|
||||||
const [allChains, setAllChains] = useState<ChainItemType>();
|
|
||||||
const [allChecked, setAllChecked] = useState(false);
|
|
||||||
const [checkedItems, setCheckedItems] = useState(new Set());
|
const [checkedItems, setCheckedItems] = useState(new Set());
|
||||||
|
|
||||||
const [searchValue, setSearchValue] = useState("");
|
const [searchValue, setSearchValue] = useState("");
|
||||||
@ -88,21 +84,11 @@ export default function VerificationPage() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (wallet) {
|
if (wallet) {
|
||||||
window.addEventListener("keplr_keystorechange", async () => {
|
window.addEventListener("keplr_keystorechange", async () => {
|
||||||
init();
|
await init();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [wallet]);
|
}, [wallet]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setAllChains({
|
|
||||||
chainId: "all chains",
|
|
||||||
chainName: "all chains",
|
|
||||||
prefix: `all chains(${chainList.length})`,
|
|
||||||
address: chainList.map((chain) => chain.chainName).join(", "),
|
|
||||||
chainImageUrl: AllChainsIcon,
|
|
||||||
});
|
|
||||||
}, [chainList]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const disabledChainList = chainList.filter((chain) => {
|
const disabledChainList = chainList.filter((chain) => {
|
||||||
for (const registeredChain of registeredChainList) {
|
for (const registeredChain of registeredChainList) {
|
||||||
@ -121,18 +107,16 @@ export default function VerificationPage() {
|
|||||||
(chain) => !disabledChainList.includes(chain),
|
(chain) => !disabledChainList.includes(chain),
|
||||||
);
|
);
|
||||||
|
|
||||||
setAllChains({
|
|
||||||
chainId: "all chains",
|
|
||||||
chainName: "all chains",
|
|
||||||
prefix: `all chains(${filteredChainList.length})`,
|
|
||||||
address: filteredChainList.map((chain) => chain.chainName).join(", "),
|
|
||||||
chainImageUrl: AllChainsIcon,
|
|
||||||
});
|
|
||||||
|
|
||||||
setChainList(filteredChainList);
|
setChainList(filteredChainList);
|
||||||
setDisabledChainList(disabledChainList);
|
setDisabledChainList(disabledChainList);
|
||||||
|
|
||||||
|
setCheckedItems(new Set(filteredChainList));
|
||||||
}, [registeredChainList]);
|
}, [registeredChainList]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setCheckedItems(new Set(chainList));
|
||||||
|
}, [chainList]);
|
||||||
|
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
if (window.location.search) {
|
if (window.location.search) {
|
||||||
try {
|
try {
|
||||||
@ -381,17 +365,15 @@ export default function VerificationPage() {
|
|||||||
/>
|
/>
|
||||||
</ChainListTitleContainer>
|
</ChainListTitleContainer>
|
||||||
|
|
||||||
{allChains && !searchValue ? (
|
{!searchValue ? (
|
||||||
<AllChainsItem
|
<AllChainsItem
|
||||||
allChecked={allChecked}
|
chainList={chainList}
|
||||||
setAllChecked={setAllChecked}
|
checkedItems={checkedItems}
|
||||||
chainItem={allChains}
|
setCheckedItems={setCheckedItems}
|
||||||
/>
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
<ChainList
|
<ChainList
|
||||||
allChecked={allChecked}
|
|
||||||
setAllChecked={setAllChecked}
|
|
||||||
chainList={chainList.filter(
|
chainList={chainList.filter(
|
||||||
(chain) =>
|
(chain) =>
|
||||||
chain.chainId.includes(searchValue) ||
|
chain.chainId.includes(searchValue) ||
|
||||||
|
Loading…
Reference in New Issue
Block a user