From e843fea7602c44161c7677ee80517cc6b0b2a270 Mon Sep 17 00:00:00 2001 From: hassnian <44554284+hassnian@users.noreply.github.com> Date: Sun, 10 Nov 2024 11:49:04 +0500 Subject: [PATCH] fix(swaps): `getSwapStepRouteName` util --- components/swap/Preview.vue | 8 ++++---- middleware/swap.ts | 21 +++++++-------------- utils/swaps.ts | 11 +++++++++++ 3 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 utils/swaps.ts diff --git a/components/swap/Preview.vue b/components/swap/Preview.vue index 43efd41d4b..cc33be2b50 100644 --- a/components/swap/Preview.vue +++ b/components/swap/Preview.vue @@ -156,15 +156,15 @@ const stepDetailsMap: Partial> = { [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', }, } diff --git a/middleware/swap.ts b/middleware/swap.ts index 2d24eee462..e3633499bd 100644 --- a/middleware/swap.ts +++ b/middleware/swap.ts @@ -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 } }) } }) diff --git a/utils/swaps.ts b/utils/swaps.ts new file mode 100644 index 0000000000..e34560dcdf --- /dev/null +++ b/utils/swaps.ts @@ -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] +}