busqueda functionality amend

This commit is contained in:
María
2025-08-27 14:14:49 +02:00
parent 5577628c56
commit 49855b11fd
4 changed files with 79 additions and 51 deletions

View File

@@ -1,16 +1,25 @@
<template> <template>
<div class="navsearch_container container-fluid"> <div class="navsearch_container container-fluid">
<NuxtLink to="/busqueda"> Todos</NuxtLink> <NuxtLink to="/busqueda"> Todos</NuxtLink>
<NuxtLink :to="{ name: 'busqueda', query: { order: 'newest' } }"> <button @click="searchLastestProducts"> Últimos productos</button>
Últimos productos</NuxtLink
>
<NuxtLink to="/busqueda"> Más buscados</NuxtLink> <NuxtLink to="/busqueda"> Más buscados</NuxtLink>
<NuxtLink to="/c"> Cooperativas</NuxtLink> <NuxtLink to="/c"> Cooperativas</NuxtLink>
<NuxtLink to="/registro"> Regístrate</NuxtLink> <NuxtLink to="/registro"> Regístrate</NuxtLink>
</div> </div>
</template> </template>
<script></script> <script>
export default {
methods: {
searchLastestProducts() {
return navigateTo({
name: 'busqueda',
query: { order: 'newest' }
})
}
}
}
</script>
<style lang="scss" scoped> <style lang="scss" scoped>
.navsearch_container { .navsearch_container {
@@ -22,7 +31,7 @@
display: none; display: none;
} }
a { a, button {
font-size: $m; font-size: $m;
font-weight: $bold; font-weight: $bold;
color: $color-navy; color: $color-navy;
@@ -31,7 +40,11 @@
a:nth-child(1):after, a:nth-child(1):after,
a:nth-child(2):after, a:nth-child(2):after,
a:nth-child(3):after, a:nth-child(3):after,
a:nth-child(4):after { a:nth-child(4):after,
button:nth-child(1):after,
button:nth-child(2):after,
button:nth-child(3):after,
button:nth-child(4):after {
color: $color-navy; color: $color-navy;
content: '\22EE'; content: '\22EE';
margin: 0.5rem; margin: 0.5rem;

View File

@@ -36,20 +36,13 @@
> >
<p <p
v-if="product.description" v-if="product.description"
v-sanitize="[ v-html="sanitizedDescription"
{ ></p>
allowedTags: ['p'],
allowedAttributes: {},
},
product.description,
]"
>
<span v-if="product.shipping_terms">{{ <span v-if="product.shipping_terms">{{
product.shipping_terms product.shipping_terms
}}</span> }}</span>
</p>
</div> </div>
<b-collapse :id="'collapse-' + product.id" accordion="my-accordion"> <b-collapse :id="'collapse-' + product.id" accordion="my-accordion" @show="onOpen" @hide="onClose">
<div class="tags_container"> <div class="tags_container">
<NuxtLink <NuxtLink
:to="tagRoute(n)" :to="tagRoute(n)"
@@ -142,16 +135,16 @@
}}</span> }}</span>
</div> </div>
</div> </div>
<div v-if="expanded && relatedProducts" class="related_products"> <div v-if="expanded && relatedProducts" class="related_products">
<h2>Productos relacionados</h2> <h2>Productos relacionados</h2>
<ProductsRelated :related-products="relatedProducts" /> <ProductsRelated :related-products="relatedProducts" />
</div> </div>
<ProductModal :product="product" @close="closeModal" /> <ProductModal v-if="modal" :product="product" @close-modal="closeModal" />
</div> </div>
</template> </template>
<script> <script>
import DOMPurify from 'dompurify'
import { mapState } from 'pinia' import { mapState } from 'pinia'
import socialShare from '~/utils/socialShare' import socialShare from '~/utils/socialShare'
export default { export default {
@@ -171,30 +164,40 @@ export default {
}, },
computed: { computed: {
...mapState(useAuthStore, ['access']), ...mapState(useAuthStore, ['access']),
sanitizedDescription() {
return DOMPurify.sanitize(this.product.description, {
ALLOWED_TAGS: ['p'],
ALLOWED_ATTR: [],
})
}
}, },
mounted() { mounted() {
this.productUrl = window.location.origin + `/productos/${this.product.id}` this.productUrl = window.location.origin + `/productos/${this.product.id}`
this.getRelatedProducts()
this.$root.$on('bv::collapse::state', async (collapseId, isJustShown) => {
if (collapseId === `collapse-${this.product.id}`) {
this.expanded = isJustShown
if (this.expanded) {
await this.getRelatedProducts()
await this.sendLog('view')
}
}
})
}, },
methods: { methods: {
async onOpen() {
this.expanded = true
await this.getRelatedProducts()
await this.sendLog('view')
},
onClose() {
this.expanded = false
},
tagRoute(tag) { tagRoute(tag) {
return `/busqueda?tags=${tag}` return `/busqueda?tags=${tag}`
}, },
async getRelatedProducts() { async getRelatedProducts() {
try { try {
const { data } = await this.$api.get( const config = useRuntimeConfig()
`/products/${this.product.id}/related/` const data = await $fetch(`/products/${this.product.id}/related/`, {
) baseURL: config.public.baseURL,
method: 'GET',
headers: {
Authorization: '/',
},
})
this.relatedProducts = data this.relatedProducts = data
} catch { } catch {
this.relatedProducts = null this.relatedProducts = null

View File

@@ -42,7 +42,7 @@ export default {
text-align: center; text-align: center;
padding: 20px 20px; padding: 20px 20px;
a { a, button {
font-size: $m; font-size: $m;
font-weight: $bold; font-weight: $bold;
color: $color-navy; color: $color-navy;
@@ -50,7 +50,11 @@ export default {
} }
a:nth-child(1):after, a:nth-child(1):after,
a:nth-child(2):after, a:nth-child(2):after,
a:nth-child(3):after { a:nth-child(3):after,
button:nth-child(1):after,
button:nth-child(2):after,
button:nth-child(3):after,
button:nth-child(4):after {
color: $color-navy; color: $color-navy;
content: '\22EE'; content: '\22EE';
margin: 0.5rem; margin: 0.5rem;

View File

@@ -17,7 +17,7 @@
<div v-if="!loadingProducts" class="col-md-9"> <div v-if="!loadingProducts" class="col-md-9">
<div class="carousel"> <div class="carousel">
<h2 class="title">Últimos productos</h2> <h2 class="title">Últimos productos</h2>
<ItemsRow class="items" :type="`product`" :items="carouselProducts" /> <ItemsRow class="items" :type="`product`" :items="carouselProducts.results" />
</div> </div>
<div v-if="hasFilterTags" class="applied-filters"> <div v-if="hasFilterTags" class="applied-filters">
<h2 class="title">FILTROS APLICADOS</h2> <h2 class="title">FILTROS APLICADOS</h2>
@@ -82,14 +82,14 @@
</div> </div>
</div> </div>
</div> </div>
appliedFilters: {{appliedFilters}} <br> <!-- appliedFilters: {{appliedFilters}} <br>
filters: {{filters}} <br> filters: {{filters}} <br>
prices: {{prices}} <br> prices: {{prices}} <br>
coordinates: {{coordinates}} <br> coordinates: {{coordinates}} <br>
products: {{products}} <br> products: {{products}} <br>
defaultProducts: {{defaultProducts}} <br> defaultProducts: {{defaultProducts}} <br>
<!-- carouselProducts: {{carouselProducts}} <br> --> carouselProducts: {{carouselProducts}} <br>
count: {{count}} count: {{count}} -->
</template> </template>
<script> <script>
@@ -151,29 +151,25 @@ export default {
async beforeCreate() { async beforeCreate() {
const config = useRuntimeConfig() const config = useRuntimeConfig()
const params = import.meta.client ? clientSearch(this.$route.query) : serverSearch(this.$route.query) const params = import.meta.client ? clientSearch(this.$route.query) : serverSearch(this.$route.query)
const data = await $fetch(`/search_products/?`, { const data = await $fetch(`/search_products/?`, {
baseURL: config.public.baseURL, baseURL: config.public.baseURL,
method: 'GET', method: 'GET',
query: { params: params,
q: params.q || '',
limit: 10,
offset: 0
},
headers: { headers: {
Authorization: '/', Authorization: '/',
}, },
}) })
console.log('data', data) //console.log('data', data)
const products = data.products const products = data.products
//console.log('products', products)
let defaultProducts = [] let defaultProducts = []
if (products.length === 0) { if (products.length === 0) {
console.log('no products, fetching default') //console.log('no products, fetching default')
const data = await $fetch(`/search_products/?q=${params.q}`, { const data = await $fetch(`/search_products/?q=${params.q}`, {
baseURL: config.public.baseURL, baseURL: config.public.baseURL,
method: 'GET', method: 'GET',
query: { params: {
order: 'newest', order: 'newest',
limit: 10, limit: 10,
offset: 0 offset: 0
@@ -188,11 +184,15 @@ export default {
const carouselProducts = await $fetch(`/products/`, { const carouselProducts = await $fetch(`/products/`, {
baseURL: config.public.baseURL, baseURL: config.public.baseURL,
method: 'GET', method: 'GET',
params: {
limit: 10,
offset: 0
},
headers: { headers: {
Authorization: '/', Authorization: '/',
}, },
}) })
console.log('carouselProducts', carouselProducts) //console.log('carouselProducts', carouselProducts)
let coordinates let coordinates
if (params.latitude && params.longitude) { if (params.latitude && params.longitude) {
@@ -210,7 +210,7 @@ export default {
} else { } else {
prices = { max: null, min: null } prices = { max: null, min: null }
} }
//TODO: Implement filtering logic
this.appliedFilters = params.q this.appliedFilters = params.q
this.filters = data.filters this.filters = data.filters
this.prices = prices this.prices = prices
@@ -234,10 +234,18 @@ export default {
}, },
async getMoreProducts(offset) { async getMoreProducts(offset) {
const data = await this.$api.get(`/search_products/?limit=10&offset=${offset}`, { const config = useRuntimeConfig()
const data = await $fetch(`/search_products/`, {
baseURL: config.public.baseURL, baseURL: config.public.baseURL,
method: 'GET', method: 'GET',
body: { params: this.appliedFilters }, params: {
...this.appliedFilters,
limit: 10,
offset: offset
},
headers: {
Authorization: '/',
},
}) })
return data.products return data.products
}, },