Skip to content

Commit

Permalink
fix(swaps): getSwapStepRouteName util
Browse files Browse the repository at this point in the history
  • Loading branch information
hassnian committed Nov 10, 2024
1 parent 21b362c commit e843fea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
8 changes: 4 additions & 4 deletions components/swap/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ const stepDetailsMap: Partial<Record<SwapStep, StepDetails>> = {
[SwapStep.DESIRED]: {
title: 'swap.yourSwapList',
surchargeTitle: 'swap.requestToken',
nextRouteName: 'prefix-swap-id-offer',
backRouteName: 'prefix-swap',
nextRouteName: getSwapStepRouteName(SwapStep.OFFERED),
backRouteName: getSwapStepRouteName(SwapStep.COUNTERPARTY),
surchargeDirection: 'Receive',
},
[SwapStep.OFFERED]: {
title: 'swap.yourOffer',
surchargeTitle: 'swap.addToken',
nextRouteName: 'prefix-swap-id-review',
backRouteName: 'prefix-swap-id',
nextRouteName: getSwapStepRouteName(SwapStep.REVIEW),
backRouteName: getSwapStepRouteName(SwapStep.DESIRED),
surchargeDirection: 'Send',
},
}
Expand Down
21 changes: 7 additions & 14 deletions middleware/swap.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
const ROUTE_NAME_STEP_MAP = {
'prefix-swap': SwapStep.COUNTERPARTY,
'prefix-swap-id': SwapStep.DESIRED,
'prefix-swap-id-offer': SwapStep.OFFERED,
'prefix-swap-id-review': SwapStep.REVIEW,
}

export default defineNuxtRouteMiddleware((to) => {
const { toast } = useToast()
const atomicSwapsStore = useAtomicSwapsStore()
const { swap, items, step } = storeToRefs(atomicSwapsStore)
const swapStore = useAtomicSwapsStore()
const { swap, items, step } = storeToRefs(swapStore)
const { urlPrefix } = usePrefix()

const id = to.params.id?.toString()
const routeName = to.name?.toString()

if (!id || !routeName) {
return navigateTo({ name: ROUTE_NAME_STEP_MAP[SwapStep.COUNTERPARTY] })
return navigateTo({ name: getSwapStepRouteName(SwapStep.COUNTERPARTY) })
}

step.value = ROUTE_NAME_STEP_MAP[routeName]
step.value = SWAP_ROUTE_NAME_STEP_MAP[routeName]
swap.value = items.value
.filter(item =>
item.counterparty === id
&& item.urlPrefix === urlPrefix.value,
).sort((a, b) => b.createdAt - a.createdAt)[0]

if (to.name === ROUTE_NAME_STEP_MAP[SwapStep.DESIRED] && !swap.value) {
atomicSwapsStore.createSwap(id)
if (to.name === getSwapStepRouteName(SwapStep.DESIRED) && !swap.value) {
swapStore.createSwap(id)
return
}

if (!swap.value) {
toast('First select the NFTs you want to offer')
return navigateTo({ name: ROUTE_NAME_STEP_MAP[SwapStep.DESIRED], params: { id } })
return navigateTo({ name: getSwapStepRouteName(SwapStep.DESIRED), params: { id } })
}
})
11 changes: 11 additions & 0 deletions utils/swaps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const SWAP_ROUTE_NAME_STEP_MAP = {
'prefix-swap': SwapStep.COUNTERPARTY,
'prefix-swap-id': SwapStep.DESIRED,
'prefix-swap-id-offer': SwapStep.OFFERED,
'prefix-swap-id-review': SwapStep.REVIEW,
}

export const getSwapStepRouteName = (step: SwapStep) => {
const index = Object.values(SWAP_ROUTE_NAME_STEP_MAP).findIndex(name => name === step)
return Object.keys(SWAP_ROUTE_NAME_STEP_MAP)[index]
}

0 comments on commit e843fea

Please sign in to comment.