Skip to content

Commit

Permalink
ref(swaps): pass step as prop to Preview.vue
Browse files Browse the repository at this point in the history
  • Loading branch information
hassnian committed Nov 14, 2024
1 parent e82f84a commit 9442a8a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
4 changes: 3 additions & 1 deletion components/swap/DestinationProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
/>

<template #preview>
<SwapPreview />
<SwapPreview :step="SwapStep.DESIRED" />
</template>
</SwapLayoutSelection>
</template>

<script setup lang="ts">
import { SwapStep } from '@/components/swap/types'
const { accountId } = useAuth()
const route = useRoute()
Expand Down
2 changes: 1 addition & 1 deletion components/swap/GridListFilters.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div
class="flex gap-4"
class="flex flex-wrap gap-4"
>
<ChainDropdown
position="bottom-auto"
Expand Down
17 changes: 11 additions & 6 deletions components/swap/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
:name="nft.name"
:image="sanitizeIpfsUrl(nft.meta.image)"
image-class="border border-k-shade"
@remove="() => swapStore.removeStepItem(nft.id)"
@remove="swapStore.removeStepItem(nft.id)"
/>

<SwapPreviewItem
Expand Down Expand Up @@ -147,8 +147,12 @@ const stepDetailsMap: Partial<Record<SwapStep, StepDetails>> = {
},
}
const props = defineProps<{
step: SwapStep
}>()
const swapStore = useAtomicSwapsStore()
const { swap, step, stepItems } = storeToRefs(swapStore)
const { swap } = storeToRefs(swapStore)
const { $i18n } = useNuxtApp()
const { accountId } = useAuth()
const { decimals } = useChain()
Expand All @@ -160,19 +164,20 @@ const amount = ref()
const itemsContainer = ref()
const isTargetVisible = useElementVisibility(target)
const surchargeDisabled = computed(() => Boolean(swap.value.surcharge))
const stepDetails = computed(() => stepDetailsMap[step.value] as StepDetails)
const stepItems = computed(() => swapStore.getStepItems(props.step))
const stepDetails = computed(() => stepDetailsMap[props.step] as StepDetails)
const title = computed(() => $i18n.t(stepDetails.value.title))
const surchargeTitle = computed(() => $i18n.t(stepDetails.value.surchargeTitle))
const surchargeDisabled = computed(() => Boolean(swap.value.surcharge))
const stepHasSurcharge = computed(() => swap.value.surcharge?.direction === stepDetails.value.surchargeDirection)
const count = computed(() => stepItems.value.length + (stepHasSurcharge.value ? 1 : 0))
const isOverOneToOneSwap = computed(() => swap.value.offered.length > swap.value.desired.length && step.value === SwapStep.OFFERED)
const isOverOneToOneSwap = computed(() => swap.value.offered.length > swap.value.desired.length && props.step === SwapStep.OFFERED)
const disabled = computed(() => {
if (!accountId.value || isOverOneToOneSwap.value) {
return true
}
if (step.value === SwapStep.DESIRED) {
if (props.step === SwapStep.DESIRED) {
return !swap.value.desired.length
}
Expand Down
4 changes: 3 additions & 1 deletion components/swap/YourProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
/>

<template #preview>
<SwapPreview />
<SwapPreview :step="SwapStep.OFFERED" />
</template>
</SwapLayoutSelection>
</template>

<script setup lang="ts">
import { SwapStep } from '@/components/swap/types'
const { accountId } = useAuth()
const { swap } = storeToRefs(useAtomicSwapsStore())
Expand Down
2 changes: 1 addition & 1 deletion components/swap/banner/title.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="px-4 py-2 bg-neutral-3 text-black text-xl rounded-full">
{{ step }}
</div>
<div class="flex flex-col justify-center">
<div class="flex flex-col justify-center capitalize">
<h1 class="title">
{{ title }}
</h1>
Expand Down
23 changes: 8 additions & 15 deletions stores/atomicSwaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,7 @@ export const useAtomicSwapsStore = defineStore('atomicSwaps', () => {
const step = ref(SwapStep.COUNTERPARTY)

const getItems = computed(() => items.value)
const stepItemsKey = computed(() => {
if (step.value === SwapStep.DESIRED) {
return 'desired'
}

if (step.value === SwapStep.OFFERED) {
return 'offered'
}
})

const stepItems = computed<SwapItem[]>(() => swap.value?.[stepItemsKey.value || ''] || [])
const stepItems = computed(() => getStepItems(step.value))

const createSwap = (counterparty: string) => {
const newAtomicSwap: AtomicSwap = {
Expand All @@ -80,14 +70,17 @@ export const useAtomicSwapsStore = defineStore('atomicSwaps', () => {
return newAtomicSwap
}

const getStepItems = (step: SwapStep) => swap.value[getStepItemsKey(step) || ''] || []

const updateStepItems = (items: SwapItem[]) => {
if (swap.value && stepItemsKey.value) {
swap.value[stepItemsKey.value] = items
const key = getStepItemsKey(step.value)
if (key) {
swap.value[key] = items
}
}

const removeStepItem = (id: string) => {
updateStepItems(stepItems.value.filter(item => item.id !== id))
updateStepItems(getStepItems(step.value).filter(item => item.id !== id))
}

return {
Expand All @@ -99,10 +92,10 @@ export const useAtomicSwapsStore = defineStore('atomicSwaps', () => {
itemsInChain,
getItems,
step,
stepItemsKey,
stepItems,
// actions
getItem,
getStepItems,
swap,
setItem,
updateItem,
Expand Down
10 changes: 10 additions & 0 deletions utils/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ export const getSwapStep = (swap: AtomicSwap): SwapStep => {

return SwapStep.REVIEW
}

export const getStepItemsKey = (step: SwapStep) => {
if (step === SwapStep.DESIRED) {
return 'desired'
}

if (step === SwapStep.OFFERED) {
return 'offered'
}
}

0 comments on commit 9442a8a

Please sign in to comment.