jgudo / ecommerce-react

eCommerce web application using React, Redux, Redux-Saga, Firebase and SASS.
https://salinaka-ecommerce.web.app
Apache License 2.0
1.14k stars 629 forks source link

Keywords #27

Closed rajeev-ops closed 3 years ago

rajeev-ops commented 3 years ago

Why can't we search with keywords

jgudo commented 3 years ago

I will work on that later. For now, searching works only for product name.

jgudo commented 3 years ago

@rajeev-ops The previous search functionality works only when you have fetched all the products and uses a selector for filtering searched item which is not ideal. The current search works by fetching all the products in firestore collection by their name.

rajeev-ops commented 3 years ago

Ok sir no problem . thanks

rajeev-ops commented 3 years ago

Sir because if someone types the second word of name in search box then the current search functionality doesn't give any result for eg. If someone wants to search kibal batal (product) he has to type kibal or kibal batal . But if he want types only batal or any other second word of any product having two or more words in product name it doesn't work .

jgudo commented 3 years ago

@rajeev-ops Yes I'm going to work on that.

rajeev-ops commented 3 years ago

Ok sir

jgudo commented 3 years ago

Please refer to commit 7e33dc9 Searching for keyword is enabled. However, searching for a product's name substring is not doable at the moment. I found a workaround for this using a third-party service using Algolia to enable full-text searching. However, we have to use Firebase Functions in this case which is not free. I'll get back on this improvement soon.

rajeev-ops commented 3 years ago

Ok thank-you sir

rajeev-ops commented 3 years ago

Searching for keywords doesn't work

jgudo commented 3 years ago

@rajeev-ops Which keywords are you referring to? I've enabled searching for keywords stored on every document. For example, if you searched for "wala" it will display the product "Quake Overload" because "wala" is stored in Quake Overload's? keywords array : keywords: ['wala', 'gago']

rajeev-ops commented 3 years ago

No sir why would I lie to you . I referred to commit https://github.com/jgudo/ecommerce-react/commit/7e33dc91b8dcc817ade857b741c626b14f753967 and made changes accordingly. But the keywords search is really not working . You can also try by cloning your own repo.

jgudo commented 3 years ago

@rajeev-ops Do you have the keywords array field in your documents? What does the console say?

rajeev-ops commented 3 years ago

I have the the array but when i search any of the keywords for eg. Jacket or blue or white it says failed to fetch products try using other keyword

On Thu, 31 Dec, 2020, 4:38 AM Julius Guevarra, notifications@github.com wrote:

@rajeev-ops https://github.com/rajeev-ops Do you have the keywords array field in your documents? What does the console say?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jgudo/ecommerce-react/issues/27#issuecomment-752785018, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARWNWKMA24JYNITMBD7OML3SXOXHPANCNFSM4VH5PBLQ .

jgudo commented 3 years ago

@rajeev-ops Are the items in keywords array capitalized like Jacket or jacket?

rajeev-ops commented 3 years ago

No they are not

On Thu, 31 Dec, 2020, 10:11 AM Julius Guevarra, notifications@github.com wrote:

@rajeev-ops https://github.com/rajeev-ops Are the items in keywords array capitalized like Jacket or jacket?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jgudo/ecommerce-react/issues/27#issuecomment-752841208, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARWNWKODZ2XLECEZR7NCLV3SXP6HPANCNFSM4VH5PBLQ .

jgudo commented 3 years ago

Could try to run the searchProducts in isolation to see if it really gives result? Run this on the bottom of your firebase.js file or anywhere you like then check for the console log

(async function () {
    try {  
               const searchKey =  'THE ITEM'; // Change this, Try your products keyword
        const prod = await firebase.searchProducts(searchKey);

        console.log(prod)

    } catch (e) {
        console.log(e);
    }
})()
rajeev-ops commented 3 years ago

Ok, I will check.

On Thu, 31 Dec, 2020, 10:36 AM Julius Guevarra, notifications@github.com wrote:

Could try to run the searchProducts in isolation to see if it really gives result? Run this on the bottom of your firebase.js file or anywhere you like then check for the console log

(async function () { try { const searchKey = 'THE ITEM'; // Change this, Try your products keyword const prod = await firebase.searchProducts(searchKey);

  console.log(prod)

} catch (e) { console.log(e); } })()

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jgudo/ecommerce-react/issues/27#issuecomment-752845370, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARWNWKMZLIBERRXWLBIWCETSXQBFDANCNFSM4VH5PBLQ .

rajeev-ops commented 3 years ago

Sir I tried that too but it still shows failed to fetch

On Thu, 31 Dec, 2020, 10:36 AM Julius Guevarra, notifications@github.com wrote:

Could try to run the searchProducts in isolation to see if it really gives result? Run this on the bottom of your firebase.js file or anywhere you like then check for the console log

(async function () { try { const searchKey = 'THE ITEM'; // Change this, Try your products keyword const prod = await firebase.searchProducts(searchKey);

  console.log(prod)

} catch (e) { console.log(e); } })()

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jgudo/ecommerce-react/issues/27#issuecomment-752845370, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARWNWKMZLIBERRXWLBIWCETSXQBFDANCNFSM4VH5PBLQ .

jgudo commented 3 years ago

@rajeev-ops Please attach yoursearchProducts method here.

rajeev-ops commented 3 years ago

searchProducts = (searchKey) => { let didTimeout = false;

    return new Promise(async (resolve, reject) => {
        let productsRef = this.db.collection('products');

        const timeout = setTimeout(() => {
            didTimeout = true;
            reject('Request timeout, please try again');
        }, 15000);

        try {
            // const totalQueryRef = productsRef
            //  .where('name_lower', '>=', searchKey)
            //  .where('name_lower', '<=', searchKey + '\uf8ff');
            const searchedNameRef = productsRef
                .orderBy('name_lower')
                .where('name_lower', '>=', searchKey)
                .where('name_lower', '<=', searchKey + '\uf8ff')
                .limit(12);
            const searchedKeywordsRef = productsRef
                .orderBy('dateAdded', 'desc')
                .where('keywords', 'array-contains-any', searchKey.split

(' ')) .limit(12)

            // const totalResult = await totalQueryRef.get();
            const nameSnaps = await searchedNameRef.get();
            const keywordsSnaps = await searchedKeywordsRef.get();
            // const total = totalResult.docs.length;

            clearTimeout(timeout);
            if (!didTimeout) {
                const searchedNameProducts = [];
                const searchedKeywordsProducts = [];
                let lastKey = null;

                if (!nameSnaps.empty) {
                    nameSnaps.forEach(doc => searchedNameProducts.push

({ id: doc.id, ...doc.data() })); lastKey = nameSnaps.docs[nameSnaps.docs.length - 1]; }

                if (!keywordsSnaps.empty) {
                    keywordsSnaps.forEach(doc =>

searchedKeywordsProducts.push({ id: doc.id, ...doc.data() })); }

                // MERGE PRODUCTS
                const mergedProducts = [...searchedNameProducts, ...

searchedKeywordsProducts]; const hash = {};

                mergedProducts.forEach(product => {
                    hash[product.id] = product;
                });

                resolve({ products: Object.values(hash), lastKey });
            }
        } catch (e) {
            if (didTimeout) return;
            console.log(

'Failed to fetch products: An error occured while trying to fetch products or there may be no product ' , e); reject(':( Failed to fetch products.'); }

    });
}

Show quoted text

On Thu, 31 Dec, 2020, 5:07 PM Julius Guevarra, notifications@github.com wrote:

@rajeev-ops https://github.com/rajeev-ops Please attach your searchProducts method here.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jgudo/ecommerce-react/issues/27#issuecomment-752932957, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARWNWKPUKOQ724CB7PVAE43SXRO53ANCNFSM4VH5PBLQ .

jgudo commented 3 years ago

@rajeev-ops It's working fine. What's the complete error log? What's after Failed to fetch products:? It should be something like:

Failed to fetch products: An error occured while trying to fetch products or there may be no product  ReferenceError: searchedNameRe is not defined
rajeev-ops commented 3 years ago

Bt i have products with that keywords

On Thu, 31 Dec, 2020, 6:05 PM Julius Guevarra, notifications@github.com wrote:

@rajeev-ops https://github.com/rajeev-ops It's working fine. What's the complete error log? What's after Failed to fetch products: ? It should be something like:

Failed to fetch products: An error occured while trying to fetch products or there may be no product ReferenceError: searchedNameRe is not defined

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jgudo/ecommerce-react/issues/27#issuecomment-752947910, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARWNWKM6EYKISBPSRJEDJCTSXRV2VANCNFSM4VH5PBLQ .

jgudo commented 3 years ago

@rajeev-ops I mean, did you run the previous test for searchProducts ? It should only return an array or should throw an error. You will see the bug in the console. I'm not talking about the error message in UI.

jgudo commented 3 years ago

@rajeev-ops It will throw an error maybe firebase-related error. That's why we need to make sure.

jgudo commented 3 years ago
(async function () {
    try {  
               const searchKey =  'THE ITEM'; // Change this, Try your products keyword
        const prod = await firebase.searchProducts(searchKey);

        console.log(prod)

    } catch (e) {
        console.log(e);
    }
})()

What's the value of prod in console?