vendure-ecommerce / storefront-qwik-starter

An e-commerce storefront starter built with Qwik and Vendure
https://qwik-storefront.vendure.io
227 stars 86 forks source link

looking for documentation to list products by category #127

Closed benjamin-corailfactory closed 1 year ago

benjamin-corailfactory commented 1 year ago

Hi, do you have any documentation for creating a component that queries products by category? Thanks, Best regards

gioboa commented 1 year ago

Hi @benjamin-corailfactory, with GraphQL we load products by category and then we loop through them. Here is the code In the search variable we stored the GraphQL response

michaelbromley commented 1 year ago

@benjamin-corailfactory I recently have been updating our docs, you might find this guide helpful: https://beta-docs.vendure.io/guides/storefront/listing-products/#listing-products-in-a-collection

benjamin-corailfactory commented 1 year ago

Hi @gioboa, Sorry, I've just started react ... And I try your storefront-qwik-starter.

I tried to refactor as component (<CollectionItems collection={collection} />) But I have problem with : searchSignal.value ... Do you have any idea ?

import { component$, useStore } from '@builder.io/qwik';
import { Collection } from '~/generated/graphql';
import ProductCard from '~/components/products/ProductCard';
import { SearchResponse } from '~/generated/graphql';
import {
    searchQueryWithCollectionSlug,
} from '~/providers/shop/products/products';

interface IProps {
    collection: Collection;
}

export async function useSearchLoader(slug: any) {
        return await searchQueryWithCollectionSlug(slug);
}

export default component$(({ collection }: IProps) => 
{

    const searchSignal = useSearchLoader(collection.slug);

    const state = useStore<{
        search: SearchResponse;
    }>({
        search: searchSignal.value as SearchResponse,
    });

    return (

                <div class="sm:col-span-5 lg:col-span-4">
                    <div class="grid grid-cols-1 gap-y-10 gap-x-6 sm:grid-cols-2 lg:grid-cols-4 xl:gap-x-8">
                        {state.search.items.map((item) => (
                            <ProductCard
                                key={item.productId}
                                productAsset={item.productAsset}
                                productName={item.productName}
                                slug={item.slug}
                                priceWithTax={item.priceWithTax}
                                currencyCode={item.currencyCode}
                            ></ProductCard>
                        ))}
                    </div>
                </div>

    );
});

Thanks, and ... sorry (I'm sure it's a silly mistake)

gioboa commented 1 year ago

Have you got a public repo with this project?

benjamin-corailfactory commented 1 year ago

@gioboa No, sorry... I'm not at that stage yet. But the structure is exactly the same. No big changes. Just this file in the components. Thanks