Skip to content

Commit

Permalink
chore: Merging details part 3 (Uniswap#4637)
Browse files Browse the repository at this point in the history
* Merging details part 3



Co-authored-by: Alex Ball <[email protected]>
  • Loading branch information
aballerr and Alex Ball authored Sep 19, 2022
1 parent 02b617d commit e7d498c
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 41 deletions.
47 changes: 43 additions & 4 deletions src/nft/hooks/useBag.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,61 @@
import { BagItem } from 'nft/types'
import { v4 as uuidv4 } from 'uuid'
import create from 'zustand'
import { devtools } from 'zustand/middleware'

interface BagState {
import { BagItem, BagItemStatus, BagStatus, UpdatedGenieAsset } from '../types'

type BagState = {
itemsInBag: BagItem[]
addAssetToBag: (asset: UpdatedGenieAsset) => void
removeAssetFromBag: (asset: UpdatedGenieAsset) => void
isLocked: boolean
setLocked: (isLocked: boolean) => void
bagExpanded: boolean
toggleBag: () => void
}

export const useBag = create<BagState>()(
devtools(
(set) => ({
(set, get) => ({
bagExpanded: false,
itemsInBag: [],
toggleBag: () =>
set(({ bagExpanded }) => ({
bagExpanded: !bagExpanded,
})),
isLocked: false,
setLocked: (_isLocked) =>
set(() => ({
isLocked: _isLocked,
})),
itemsInBag: [],
addAssetToBag: (asset) =>
set(({ itemsInBag }) => {
if (get().isLocked) return { itemsInBag }
const assetWithId = { asset: { id: uuidv4(), ...asset }, status: BagItemStatus.ADDED_TO_BAG }
if (itemsInBag.length === 0)
return {
itemsInBag: [assetWithId],
bagStatus: BagStatus.ADDING_TO_BAG,
}
else
return {
itemsInBag: [...itemsInBag, assetWithId],
bagStatus: BagStatus.ADDING_TO_BAG,
}
}),
removeAssetFromBag: (asset) => {
set(({ itemsInBag }) => {
if (get().isLocked) return { itemsInBag }
if (itemsInBag.length === 0) return { itemsInBag: [] }
const itemsCopy = [...itemsInBag]
const index = itemsCopy.findIndex((n) =>
asset.id ? n.asset.id === asset.id : n.asset.tokenId === asset.tokenId && n.asset.address === asset.address
)
if (index === -1) return { itemsInBag }
itemsCopy.splice(index, 1)
return { itemsInBag: itemsCopy }
})
},
}),
{ name: 'useBag' }
)
Expand Down
31 changes: 31 additions & 0 deletions src/nft/hooks/useTimeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useEffect, useState } from 'react'

const MINUTE = 1000 * 60
const HOUR = MINUTE * 60
const DAY = 24 * HOUR

const getReturnValues = (countDown: number): [number, number, number, number] => {
// calculate time left
const days = Math.floor(countDown / DAY)
const hours = Math.floor((countDown % DAY) / HOUR)
const minutes = Math.floor((countDown % HOUR) / MINUTE)
const seconds = Math.floor((countDown % MINUTE) / 1000)

return [days, hours, minutes, seconds]
}

export const useTimeout = (targetDate: Date) => {
const countDownDate = new Date(targetDate).getTime()

const [countDown, setCountDown] = useState<number>(countDownDate - new Date().getTime())

useEffect(() => {
const interval = setInterval(() => {
setCountDown(countDownDate - new Date().getTime())
}, 1000)

return () => clearInterval(interval)
}, [countDownDate])

return getReturnValues(countDown)
}
2 changes: 1 addition & 1 deletion src/nft/pages/asset/Asset.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const marketplace = sprinkles({ borderRadius: '4' })

export const tab = style([
subhead,
sprinkles({ color: 'darkGray', border: 'none', padding: '0', background: 'transparent' }),
sprinkles({ color: 'darkGray', border: 'none', padding: '0', background: 'transparent', cursor: 'pointer' }),
{
selectors: {
'&[data-active="true"]': {
Expand Down
Loading

0 comments on commit e7d498c

Please sign in to comment.