This commit is contained in:
parent
c5603e6789
commit
cd32863494
@ -1,14 +1,10 @@
|
|||||||
import { useQuery } from '@tanstack/vue-query'
|
import { useQuery } from '@tanstack/vue-query'
|
||||||
import { unref, watch } from 'vue'
|
import { unref } from 'vue'
|
||||||
import { getProductsDetail } from '~/api/endpoints'
|
import { getProductsDetail } from '~/api/endpoints'
|
||||||
|
|
||||||
export const useGetProductsDetail = (productId: MaybeRef<number>) => {
|
export const useGetProductsDetail = (productId: MaybeRef<number>) => {
|
||||||
const q = useQuery({
|
return useQuery({
|
||||||
queryKey: ['get-products-detail', unref(productId)],
|
queryKey: ['get-products-detail', productId],
|
||||||
queryFn: () => getProductsDetail(unref(productId)),
|
queryFn: () => getProductsDetail(unref(productId)),
|
||||||
})
|
})
|
||||||
|
|
||||||
watch(() => productId, () => q.refetch())
|
|
||||||
|
|
||||||
return q
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,7 +29,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.container {
|
.container {
|
||||||
max-width: 1000px;
|
max-width: 100%;
|
||||||
margin: 48px auto 100px;
|
margin: 48px auto 100px;
|
||||||
|
|
||||||
@media (max-width: 1280px) {
|
@media (max-width: 1280px) {
|
||||||
|
|||||||
@ -1,8 +1,38 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div class="card">
|
||||||
<pre>
|
<div class="card__images">
|
||||||
{{ productsData?.images?.filter(img => img?.src?.includes(colorVariants.blackCottonPolyester)) }}
|
<div
|
||||||
</pre>
|
v-for="image in currentVariantImages"
|
||||||
|
:key="image?.id"
|
||||||
|
>
|
||||||
|
<img width="100%" :src="image?.src" :alt="image?.src">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card__description">
|
||||||
|
<h1>{{ productsData?.name }}</h1>
|
||||||
|
|
||||||
|
Цена
|
||||||
|
{{ currentVariant?.price }}
|
||||||
|
|
||||||
|
Коротокое описание
|
||||||
|
{{ productsData?.short_description }}
|
||||||
|
|
||||||
|
<div class="card__variation">
|
||||||
|
<div
|
||||||
|
v-for="variation in productsData?.variations"
|
||||||
|
:key="variation?.id"
|
||||||
|
@click="() => currentVariantId = variation?.id"
|
||||||
|
>
|
||||||
|
{{ variation?.id }}
|
||||||
|
<img width="200px" :src="variation?.image[0]?.src" :alt="variation?.image[0]?.src">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
{{ currentVariant }}
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@ -11,10 +41,53 @@ import { useGetProductsDetail } from '~/api/queries'
|
|||||||
|
|
||||||
const { data: productsData } = useGetProductsDetail(79)
|
const { data: productsData } = useGetProductsDetail(79)
|
||||||
|
|
||||||
const colorVariants = {
|
function getAttribute(attributes, name: string) {
|
||||||
beigeCottonPolyester: 'beige_cotton-polyester_',
|
return attributes?.find(attribute => attribute?.name === name)?.option
|
||||||
blackCotton: 'black_cotton_',
|
|
||||||
greyCottonPolyester: 'grey_cotton-polyester_',
|
|
||||||
blackCottonPolyester: 'black_cotton-polyester_',
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultColor = computed(() => getAttribute(productsData?.value?.default_attributes, 'color'))
|
||||||
|
const defaultMaterial = computed(() => getAttribute(productsData?.value?.default_attributes, 'material'))
|
||||||
|
|
||||||
|
const defaultVariant = computed(() => {
|
||||||
|
return productsData?.value?.variations?.find(variation => getAttribute(variation?.attributes, 'color') === defaultColor?.value)
|
||||||
|
&& productsData?.value?.variations?.find(variation => getAttribute(variation?.attributes, 'material') === defaultMaterial?.value)
|
||||||
|
})
|
||||||
|
|
||||||
|
const currentVariantId = ref(defaultVariant?.value?.id)
|
||||||
|
|
||||||
|
const currentVariant = computed(() => productsData?.value?.variations?.find(variation => variation?.id === currentVariantId?.value))
|
||||||
|
|
||||||
|
const currentColor = computed(() => getAttribute(currentVariant?.value?.attributes, 'color'))
|
||||||
|
const currentMaterial = computed(() => getAttribute(currentVariant?.value?.attributes, 'material'))
|
||||||
|
|
||||||
|
const currentVariantImages
|
||||||
|
= computed(() => productsData?.value?.images
|
||||||
|
?.filter(img => img?.src?.includes(`${currentColor.value}_${currentMaterial.value}_`)))
|
||||||
|
|
||||||
|
watch(() => defaultVariant.value, newValue => currentVariantId.value = newValue?.id)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss">
|
||||||
|
.card {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
|
||||||
|
&__images {
|
||||||
|
margin-left: 10%;
|
||||||
|
margin-right: 10%;
|
||||||
|
width: 40%;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
&__description {
|
||||||
|
width: 40%;
|
||||||
|
}
|
||||||
|
|
||||||
|
&__variation {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user