Skip to content

Commit

Permalink
add blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
imhson committed Mar 25, 2024
1 parent b5281f3 commit 77197f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
34 changes: 14 additions & 20 deletions app/(private)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ const initList = {
}
export default function Home() {
const config = getConfig()
const [blackList, setBlackList] = useState<any[]>([])
const { assets, lastAssetsUpdate, fetchAssets } = useContext(Context)
const [filteredAssets, setFilteredAssets] = useState<Token[]>([])
const { assets, lastAssetsUpdate, fetchAssets, setBlackListId } = useContext(Context)
const { address, chain, getSigningCosmWasmClient } = useChain(config.COSMOSKIT_CHAINKEY)
const { isOpen, onOpen, onOpenChange, onClose } = useDisclosure()
const [tempBlackList, setTempBlackList] = useState<any[]>([])
Expand Down Expand Up @@ -99,7 +97,7 @@ export default function Home() {
}
}
}
const gemList = filteredAssets.reduce(
const gemList = assets.reduce(
(total, current) => {
total[current.type]++
return total
Expand All @@ -115,12 +113,7 @@ export default function Home() {
}
}
setGems(Map(gemList))
}, [mainGem, materialGems, filteredAssets?.length])

useEffect(() => {
const fa = assets.filter((a) => !blackList.includes(a.token_id))
setFilteredAssets([...fa])
}, [assets.length, blackList.length])
}, [mainGem, materialGems, assets?.length])

const addGemHandler = (type: string) => {
if (loading) return
Expand All @@ -141,7 +134,7 @@ export default function Home() {
try {
setLoading(true)
const bl = []
const main = filteredAssets.find((asset) => asset.type == mainGem)
const main = assets.find((asset) => asset.type == mainGem)
const msgs = [
{
contract: main?.cw721_contract.smart_contract.address,
Expand All @@ -158,7 +151,7 @@ export default function Home() {
const material: any[] = []
for (let i = 0; i < 5; i++) {
if (materialGems[i] != undefined) {
const asset = filteredAssets.find(
const asset = assets.find(
(a) =>
a.type == materialGems[i] &&
!material.find((g) => g.tokenId == a.token_id) &&
Expand All @@ -182,7 +175,7 @@ export default function Home() {
}
let shield
if (useShield) {
shield = filteredAssets.find((asset) => asset.type == 'shield')
shield = assets.find((asset) => asset.type == 'shield')
msgs.push({
contract: shield?.cw721_contract.smart_contract.address,
msg: {
Expand Down Expand Up @@ -262,7 +255,10 @@ export default function Home() {
setTimeout(() => setRequestId(undefined), 500)
}}
revealSuccessCallBack={(result: string) => {
setBlackList(result == 'success' ? [...blackList, ...tempBlackList] : [...blackList, ...tempBlackList.slice(1)])
setBlackListId(((prev: string[]) => {
const next = [...prev, ...(result == 'success' ? tempBlackList : tempBlackList.slice(1))]
return next as string[]
}) as any)
setMainGem(undefined)
setMaterialGems([undefined, undefined, undefined, undefined, undefined])
setUseShield(false)
Expand Down Expand Up @@ -419,12 +415,10 @@ export default function Home() {
</div>
<div
className={`mx-auto w-fit flex items-center mt-10 md:mt-20 gap-3 cursor-pointer ${
filteredAssets.filter((asset) => asset.type == 'shield').length
? ''
: 'opacity-50 pointer-events-none'
assets.filter((asset) => asset.type == 'shield').length ? '' : 'opacity-50 pointer-events-none'
}`}
onClick={() =>
filteredAssets.filter((asset) => asset.type == 'shield').length && !loading
assets.filter((asset) => asset.type == 'shield').length && !loading
? setUseShield(!useShield)
: undefined
}>
Expand All @@ -446,7 +440,7 @@ export default function Home() {
<div className='relative'>
<Image src={TopBar2} alt='' className='w-[352px] relative z-10' />
<div className='relative bg-[#E6D8B9] rounded-b-[4px] p-4 -top-2 w-[338px] mx-auto text-[#292929]'>
{filteredAssets.length ? (
{assets.length ? (
<div className='min-h-[622px]'>
<div className={`text-[#6D3A0A] font-bold ${Bangkok.className} text-2xl`}>Your Gems</div>
<div className='mt-2 text-sm'>Select gems here to forge</div>
Expand Down Expand Up @@ -590,7 +584,7 @@ export default function Home() {
<div>
<Image src={ShieldItem} alt='' />
</div>
<div className=''>{filteredAssets.filter((asset) => asset.type == 'shield').length}</div>
<div className=''>{assets.filter((asset) => asset.type == 'shield').length}</div>
</div>
</div>
</div>
Expand Down
24 changes: 9 additions & 15 deletions app/(private)/wish/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ const initList = {
export default function Page() {
const config = getConfig()
const { address, chain, getSigningCosmWasmClient } = useChain(config.COSMOSKIT_CHAINKEY)
const { assets, lastAssetsUpdate, fetchAssets, account } = useContext(Context)
const [filteredAssets, setFilteredAssets] = useState<Token[]>([])
const [blackList, setBlackList] = useState<any[]>([])
const { assets, lastAssetsUpdate, fetchAssets, account, setBlackListId } = useContext(Context)

const [selectedColorKey, setSelectedColorKey] = useState(new Set(['all_colors']))
const selectedColorValue = useMemo(
Expand All @@ -86,12 +84,7 @@ export default function Page() {
const submissionDisclosure = useDisclosure()

useEffect(() => {
const fa = assets.filter((a) => !blackList.includes(a.token_id))
setFilteredAssets([...fa])
}, [assets.length, blackList.length])

useEffect(() => {
const gemList = filteredAssets.reduce(
const gemList = assets.reduce(
(total, current) => {
total[current.type]++
return total
Expand All @@ -104,7 +97,7 @@ export default function Page() {
}
}
setGems(Map(gemList))
}, [selectedGems.filter((g) => !g).length, filteredAssets?.length, blackList.length])
}, [selectedGems.filter((g) => !g).length, assets?.length])

useEffect(() => {
if (jackpotData?.jackpots?.[0]?.slot) {
Expand All @@ -130,9 +123,7 @@ export default function Page() {
const tokens: any[] = []
const msgs = []
for (let i = 0; i < jackpotData?.jackpots?.[0]?.slot; i++) {
const asset = filteredAssets.find(
(a) => a.type == selectedGems[i] && !tokens.find((g) => g.token_id == a.token_id)
)
const asset = assets.find((a) => a.type == selectedGems[i] && !tokens.find((g) => g.token_id == a.token_id))
tokens.push({
token_id: asset?.token_id as string,
contract_address: asset?.cw721_contract.smart_contract.address as string,
Expand Down Expand Up @@ -161,7 +152,10 @@ export default function Page() {
if (res.data) {
setTimeout(() => {
setSubmittedGems(selectedGems)
setBlackList([...blackList, ...tokens.map((c) => c.token_id)])
setBlackListId(((prev: string[]) => {
const next = [...prev, ...tokens.map((c) => c.token_id)]
return next as string[]
}) as any)
setSelectedGems(new Array(jackpotData?.jackpots?.[0]?.slot).fill(undefined))
onOpen()
setLoading(false)
Expand Down Expand Up @@ -339,7 +333,7 @@ export default function Page() {
<div className='relative'>
<Image src={TopBar2} alt='' className='w-[352px] relative z-10' />
<div className='relative bg-[#E6D8B9] rounded-b-[4px] p-4 -top-2 w-[338px] mx-auto text-[#292929]'>
{filteredAssets.filter((asset) => asset.type[1] <= (jackpotData?.jackpots?.[0]?.max_star || 7)).length ? (
{assets.filter((asset) => asset.type[1] <= (jackpotData?.jackpots?.[0]?.max_star || 7)).length ? (
<>
<div className='flex justify-between gap-1 items-center'>
<div className={`text-[#6D3A0A] font-bold ${Bangkok.className} text-2xl`}>Your Gems</div>
Expand Down
9 changes: 9 additions & 0 deletions provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const Context = createContext<{
assets: Token[]
fetchAssets: () => void
setAccount: (account: Account) => void
setBlackListId: (list: string[]) => void
horoscopeClient: ApolloClient<NormalizedCacheObject> | undefined
disconnect: () => void
submitCode: (value: string) => Promise<void>
Expand All @@ -46,6 +47,7 @@ export const Context = createContext<{
fetchAssets: () => {},
horoscopeClient: undefined,
setAccount: () => {},
setBlackListId: () => {},
disconnect: () => {},
submitCode: async () => {},
})
Expand Down Expand Up @@ -120,6 +122,7 @@ function ContextProvider({ children }: { children: ReactNode }) {
const [client, setClient] = useState<ApolloClient<NormalizedCacheObject>>()
const [horoscopeClient, setHoroscopeClient] = useState<ApolloClient<NormalizedCacheObject>>()
const [assets, setAssets] = useState<Token[]>([])
const [blackListId, setBlackListId] = useState<string[]>([])
const [isInit, setIsInit] = useState(true)
const [lastAssetsUpdate, setLastAssetsUpdate] = useState<number>()
const searchParams = useSearchParams()
Expand Down Expand Up @@ -229,6 +232,11 @@ function ContextProvider({ children }: { children: ReactNode }) {
}
}, [account?.wallet_address, horoscopeClient])

useEffect(() => {
const newList = assets.filter((a) => !blackListId.includes(a.token_id))
setAssets([...newList])
}, [blackListId.length, assets.length])

const fetchAssets = async () => {
const chainKey = getConfig().HOROSCOPE_CHAINKEY
let list: Token[] = []
Expand Down Expand Up @@ -402,6 +410,7 @@ function ContextProvider({ children }: { children: ReactNode }) {
disconnect,
submitCode,
horoscopeClient,
setBlackListId,
assets,
fetchAssets,
lastAssetsUpdate,
Expand Down

0 comments on commit 77197f9

Please sign in to comment.