From 45162d053721911e74bdeac09d2d1f1eaff44f1b Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Mon, 23 Sep 2024 16:44:13 +0700 Subject: update code, false code --- src/lib/home/api/categoryManagementApi.js | 78 +++++----- src/lib/home/components/CategoryDynamic.jsx | 135 +++++++++++------ src/lib/home/components/CategoryDynamicMobile.jsx | 174 +++++++++++++--------- 3 files changed, 234 insertions(+), 153 deletions(-) (limited to 'src/lib') diff --git a/src/lib/home/api/categoryManagementApi.js b/src/lib/home/api/categoryManagementApi.js index 0aeb2aac..2ff4fdfc 100644 --- a/src/lib/home/api/categoryManagementApi.js +++ b/src/lib/home/api/categoryManagementApi.js @@ -1,40 +1,44 @@ export const fetchCategoryManagementSolr = async () => { - let sort ='sort=sequence_i asc'; - try { - const response = await fetch(`/solr/category_management/query?q=*:*&q.op=OR&indent=true&${sort}&&rows=20`); - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - const data = await response.json(); - const promotions = await map(data.response.docs); - return promotions; - } catch (error) { - console.error("Error fetching promotion data:", error); - return []; + let sort = 'sort=sequence_i asc'; + try { + const response = await fetch( + `/solr/category_management/query?q=*:*&q.op=OR&indent=true&${sort}&&rows=20` + ); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); + const promotions = await map(data.response.docs); + return promotions; + } catch (error) { + console.error('Error fetching promotion data:', error); + return []; + } +}; + +const map = async (promotions) => { + return promotions.map((promotion) => { + let parsedCategories = promotion.categories.map((category) => { + // Parse string JSON utama + let parsedCategory = JSON.parse(category); + + // Parse setiap elemen di child_frontend_id_i jika ada + if (parsedCategory.child_frontend_id_i) { + parsedCategory.child_frontend_id_i = + parsedCategory.child_frontend_id_i.map((child) => JSON.parse(child)); } + + return parsedCategory; + }); + let productMapped = { + id: promotion.id, + name: promotion.name_s, + image: promotion.image_s, + sequence: promotion.sequence_i, + numFound: promotion.numFound_i, + categories: parsedCategories, + category_id: promotion.category_id_i, }; - - const map = async (promotions) => { - return promotions.map((promotion) =>{ - let parsedCategories = promotion.categories.map(category => { - // Parse string JSON utama - let parsedCategory = JSON.parse(category); - - // Parse setiap elemen di child_frontend_id_i jika ada - if (parsedCategory.child_frontend_id_i) { - parsedCategory.child_frontend_id_i = parsedCategory.child_frontend_id_i.map(child => JSON.parse(child)); - } - - return parsedCategory; - }); - let productMapped = { - id: promotion.id, - name: promotion.name_s, - image: promotion.image_s, - sequence: promotion.sequence_i, - numFound: promotion.numFound_i, - categories: parsedCategories - }; - return productMapped; - }) - }; \ No newline at end of file + return productMapped; + }); +}; diff --git a/src/lib/home/components/CategoryDynamic.jsx b/src/lib/home/components/CategoryDynamic.jsx index ca104ada..da2faf3d 100644 --- a/src/lib/home/components/CategoryDynamic.jsx +++ b/src/lib/home/components/CategoryDynamic.jsx @@ -1,7 +1,7 @@ import React, { useEffect, useState, useCallback } from 'react'; -import {fetchCategoryManagementSolr} from '../api/categoryManagementApi' +import { fetchCategoryManagementSolr } from '../api/categoryManagementApi'; import NextImage from 'next/image'; -import Link from "next/link"; +import Link from 'next/link'; import { createSlug } from '@/core/utils/slug'; import odooApi from '@/core/api/odooApi'; import { Skeleton } from '@chakra-ui/react'; @@ -12,35 +12,34 @@ import 'swiper/css/pagination'; import { Navigation, Pagination, Autoplay } from 'swiper'; const CategoryDynamic = () => { - - const [categoryManagement, setCategoryManagement] = useState([]) - const [isLoading, setIsLoading] = useState(false) + const [categoryManagement, setCategoryManagement] = useState([]); + const [isLoading, setIsLoading] = useState(false); const loadBrand = useCallback(async () => { - setIsLoading(true) + setIsLoading(true); const items = await fetchCategoryManagementSolr(); - - setIsLoading(false) - setCategoryManagement(items) - }, []) + + setIsLoading(false); + setCategoryManagement(items); + }, []); useEffect(() => { - loadBrand() - }, [loadBrand]) - + loadBrand(); + }, [loadBrand]); + // const [categoryData, setCategoryData] = useState({}); // const [subCategoryData, setSubCategoryData] = useState({}); - + // useEffect(() => { // const fetchCategoryData = async () => { // if (categoryManagement && categoryManagement.data) { // const updatedCategoryData = {}; // const updatedSubCategoryData = {}; - + // for (const category of categoryManagement.data) { // const countLevel1 = await odooApi('GET', `/api/v1/category/numFound?parent_id=${category.categoryIdI}`); - + // updatedCategoryData[category.categoryIdI] = countLevel1?.numFound; - + // for (const subCategory of countLevel1?.children) { // updatedSubCategoryData[subCategory.id] = subCategory?.numFound; // } @@ -55,34 +54,46 @@ const CategoryDynamic = () => { // }, [categoryManagement.isLoading]); const swiperBanner = { - modules: [Pagination, ], - classNames:'mySwiper', + modules: [Pagination], + classNames: 'mySwiper', slidesPerView: 3, - spaceBetween:10, + spaceBetween: 10, pagination: { dynamicBullets: true, clickable: true, - } + }, }; - + return (
- {categoryManagement && categoryManagement?.map((category) => { + {categoryManagement && + categoryManagement?.map((category) => { + console.log('category', category); // const countLevel1 = categoryData[category.categoryIdI] || 0; return (
-
{category.name}
+
+ {category.name} +
{/*

{countLevel1} Produk tersedia

*/} - Lihat Semua + + Lihat Semua +
- + {/* Swiper for SubCategories */} - + {category.categories.map((subCategory) => { // const countLevel2 = subCategoryData[subCategory.idLevel2] || 0; @@ -92,39 +103,69 @@ const CategoryDynamic = () => {
-
{subCategory?.name}
+
+ {subCategory?.name} +
{/*

{countLevel2} Produk tersedia

*/} - Lihat Semua + + Lihat Semua +
- {subCategory.child_frontend_id_i.map((childCategory) => ( -
- - -
-
{childCategory.name}
-
- -
- ))} + {subCategory.child_frontend_id_i.map( + (childCategory) => ( +
+ + +
+
+ {childCategory.name} +
+
+ +
+ ) + )}
diff --git a/src/lib/home/components/CategoryDynamicMobile.jsx b/src/lib/home/components/CategoryDynamicMobile.jsx index 1061f3e4..af151df2 100644 --- a/src/lib/home/components/CategoryDynamicMobile.jsx +++ b/src/lib/home/components/CategoryDynamicMobile.jsx @@ -1,38 +1,41 @@ import React, { useEffect, useState, useCallback } from 'react'; import NextImage from 'next/image'; -import Link from "next/link"; +import Link from 'next/link'; import { createSlug } from '@/core/utils/slug'; import { Swiper, SwiperSlide } from 'swiper/react'; import 'swiper/css'; -import {fetchCategoryManagementSolr} from '../api/categoryManagementApi' +import { fetchCategoryManagementSolr } from '../api/categoryManagementApi'; const CategoryDynamicMobile = () => { const [selectedCategory, setSelectedCategory] = useState({}); - const [categoryManagement, setCategoryManagement] = useState([]) - const [isLoading, setIsLoading] = useState(false) + const [categoryManagement, setCategoryManagement] = useState([]); + const [isLoading, setIsLoading] = useState(false); const loadBrand = useCallback(async () => { - setIsLoading(true) + setIsLoading(true); const items = await fetchCategoryManagementSolr(); - - setIsLoading(false) - setCategoryManagement(items) - }, []) + + setIsLoading(false); + setCategoryManagement(items); + }, []); useEffect(() => { - loadBrand() - }, [loadBrand]) + loadBrand(); + }, [loadBrand]); useEffect(() => { const loadPromo = async () => { try { if (categoryManagement?.length > 0) { - const initialSelections = categoryManagement.reduce((acc, category) => { - if (category.categories.length > 0) { - acc[category.id] = category.categories[0].id_level_2; - } - return acc; - }, {}); + const initialSelections = categoryManagement.reduce( + (acc, category) => { + if (category.categories.length > 0) { + acc[category.id] = category.categories[0].id_level_2; + } + return acc; + }, + {} + ); setSelectedCategory(initialSelections); } } catch (loadError) { @@ -44,69 +47,102 @@ const CategoryDynamicMobile = () => { }, [categoryManagement]); const handleCategoryLevel2Click = (categoryIdI, idLevel2) => { - setSelectedCategory(prev => ({ + setSelectedCategory((prev) => ({ ...prev, - [categoryIdI]: idLevel2 + [categoryIdI]: idLevel2, })); }; - + return (
- {categoryManagement && categoryManagement?.map((category) => ( -
-
-
{category?.name}
- Lihat Semua -
- - {category.categories.map((index) => ( - -
handleCategoryLevel2Click(category.id, index?.id_level_2)} - className={`border flex justify-start items-center max-w-48 max-h-16 rounded ${selectedCategory[category.id] === index?.id_level_2 ? 'bg-red-50 border-red-500 text-red-500' : 'border-gray-200 text-gray-900'}`} - > -
-
- -
-
{index?.name}
-
-
-
-
-
- ))} -
-
-
+ {categoryManagement && + categoryManagement?.map((category) => ( +
+
+
+ {category?.name} +
+ + Lihat Semua + +
+ {category.categories.map((index) => ( - selectedCategory[category.id] === index?.id_level_2 && index?.child_frontend_id_i.map((x) => ( -
- - -
-
{x?.name}
+ +
+ handleCategoryLevel2Click(category.id, index?.id_level_2) + } + className={`border flex justify-start items-center max-w-48 max-h-16 rounded ${ + selectedCategory[category.id] === index?.id_level_2 + ? 'bg-red-50 border-red-500 text-red-500' + : 'border-gray-200 text-gray-900' + }`} + > +
+
+ +
+
+ {index?.name} +
+
- +
- )) +
))} + +
+
+ {category.categories.map( + (index) => + selectedCategory[category.id] === index?.id_level_2 && + index?.child_frontend_id_i.map((x) => ( +
+ + +
+
+ {x?.name} +
+
+ +
+ )) + )} +
-
- ))} + ))}
); }; -- cgit v1.2.3 From 7d89c0280f1801db49b373a4691215833e6e7f92 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Mon, 23 Sep 2024 16:52:50 +0700 Subject: delete console log --- src/lib/home/components/CategoryDynamic.jsx | 1 - src/lib/product/components/ProductSearch.jsx | 121 +++++++++++++-------------- 2 files changed, 59 insertions(+), 63 deletions(-) (limited to 'src/lib') diff --git a/src/lib/home/components/CategoryDynamic.jsx b/src/lib/home/components/CategoryDynamic.jsx index da2faf3d..79092c9d 100644 --- a/src/lib/home/components/CategoryDynamic.jsx +++ b/src/lib/home/components/CategoryDynamic.jsx @@ -68,7 +68,6 @@ const CategoryDynamic = () => {
{categoryManagement && categoryManagement?.map((category) => { - console.log('category', category); // const countLevel1 = categoryData[category.categoryIdI] || 0; return ( diff --git a/src/lib/product/components/ProductSearch.jsx b/src/lib/product/components/ProductSearch.jsx index 1edc31c9..26114acf 100644 --- a/src/lib/product/components/ProductSearch.jsx +++ b/src/lib/product/components/ProductSearch.jsx @@ -28,7 +28,7 @@ import SideBanner from '~/modules/side-banner'; import FooterBanner from '~/modules/footer-banner'; import CategorySection from './CategorySection'; import LobSectionCategory from './LobSectionCategory'; -import { getIdFromSlug } from '@/core/utils/slug' +import { getIdFromSlug } from '@/core/utils/slug'; import { data } from 'autoprefixer'; const ProductSearch = ({ @@ -45,35 +45,40 @@ const ProductSearch = ({ const [orderBy, setOrderBy] = useState(router.query?.orderBy); const [finalQuery, setFinalQuery] = useState({}); const [queryFinal, setQueryFinal] = useState({}); - const [dataCategoriesProduct, setDataCategoriesProduct] = useState([]) - const [dataCategoriesLob, setDataCategoriesLob] = useState([]) - const categoryId = getIdFromSlug(prefixUrl) - const [data, setData] = useState([]) + const [dataCategoriesProduct, setDataCategoriesProduct] = useState([]); + const [dataCategoriesLob, setDataCategoriesLob] = useState([]); + const categoryId = getIdFromSlug(prefixUrl); + const [data, setData] = useState([]); const [dataLob, setDataLob] = useState([]); if (defaultBrand) query.brand = defaultBrand.toLowerCase(); - const dataIdCategories = [] + const dataIdCategories = []; useEffect(() => { - if(prefixUrl.includes('category')){ + if (prefixUrl.includes('category')) { const loadProduct = async () => { - const getCategoriesId = await odooApi('GET', `/api/v1/category/numFound?parent_id=${categoryId}`); + const getCategoriesId = await odooApi( + 'GET', + `/api/v1/category/numFound?parent_id=${categoryId}` + ); if (getCategoriesId) { setDataCategoriesProduct(getCategoriesId); } }; loadProduct(); - }else if(prefixUrl.includes('lob')){ + } else if (prefixUrl.includes('lob')) { const loadProduct = async () => { - const lobData = await odooApi('GET', `/api/v1/lob_homepage/${categoryId}/category_id`); - + const lobData = await odooApi( + 'GET', + `/api/v1/lob_homepage/${categoryId}/category_id` + ); + if (lobData) { setDataLob(lobData); } }; loadProduct(); - } }, [categoryId]); - + const collectIds = (category) => { const ids = []; function recurse(cat) { @@ -88,45 +93,40 @@ const ProductSearch = ({ return ids; }; useEffect(() => { - if(prefixUrl.includes('category')){ + if (prefixUrl.includes('category')) { const ids = collectIds(dataCategoriesProduct); const newQuery = { fq: `category_id_ids:(${ids.join(' OR ')})`, - page : router.query.page? router.query.page : 1, - brand : router.query.brand? router.query.brand : '', - category : router.query.category? router.query.category : '', - priceFrom : router.query.priceFrom? router.query.priceFrom : '', - priceTo : router.query.priceTo? router.query.priceTo : '', - limit : router.query.limit? router.query.limit : '', - orderBy : router.query.orderBy? router.query.orderBy : '' + page: router.query.page ? router.query.page : 1, + brand: router.query.brand ? router.query.brand : '', + category: router.query.category ? router.query.category : '', + priceFrom: router.query.priceFrom ? router.query.priceFrom : '', + priceTo: router.query.priceTo ? router.query.priceTo : '', + limit: router.query.limit ? router.query.limit : '', + orderBy: router.query.orderBy ? router.query.orderBy : '', }; setFinalQuery(newQuery); - } else if (prefixUrl.includes('lob')){ - + } else if (prefixUrl.includes('lob')) { const fetchCategoryData = async () => { if (dataLob[0]?.categoryIds) { - for (const cate of dataLob[0].categoryIds) { - - dataIdCategories.push(cate.childId) + dataIdCategories.push(cate.childId); } - - + const mergedArray = dataIdCategories.flat(); - + const newQuery = { fq: `category_id_ids:(${mergedArray.join(' OR ')})`, - category : router.query.category? router.query.category : '', - page : router.query.page? router.query.page : 1, - brand : router.query.brand? router.query.brand : '', - priceFrom : router.query.priceFrom? router.query.priceFrom : '', - priceTo : router.query.priceTo? router.query.priceTo : '', - limit : router.query.limit? router.query.limit : '', - orderBy : router.query.orderBy? router.query.orderBy : '' + category: router.query.category ? router.query.category : '', + page: router.query.page ? router.query.page : 1, + brand: router.query.brand ? router.query.brand : '', + priceFrom: router.query.priceFrom ? router.query.priceFrom : '', + priceTo: router.query.priceTo ? router.query.priceTo : '', + limit: router.query.limit ? router.query.limit : '', + orderBy: router.query.orderBy ? router.query.orderBy : '', }; - + setFinalQuery(newQuery); - } }; fetchCategoryData(); @@ -139,7 +139,7 @@ const ProductSearch = ({ } else { setQueryFinal({ ...query, q, limit, orderBy }); } - }, [prefixUrl,dataCategoriesProduct, query, finalQuery]); + }, [prefixUrl, dataCategoriesProduct, query, finalQuery]); const { productSearch } = useProductSearch({ query: queryFinal, @@ -162,7 +162,7 @@ const ProductSearch = ({ const [categoryValues, setCategory] = useState( router.query?.category?.split(',') || router.query?.category?.split(',') ); - + const [priceFrom, setPriceFrom] = useState(router.query?.priceFrom || null); const [priceTo, setPriceTo] = useState(router.query?.priceTo || null); @@ -170,8 +170,8 @@ const ProductSearch = ({ const productStart = productSearch.data?.responseHeader.params.start; const productRows = limit; const productFound = productSearch.data?.response.numFound; - const [dataCategories, setDataCategories] = useState([]) - + const [dataCategories, setDataCategories] = useState([]); + useEffect(() => { if (productFound == 0 && query.q && !spellings) { searchSpellApi({ query: query.q }).then((response) => { @@ -201,7 +201,7 @@ const ProductSearch = ({ }); } }, [productFound, query, spellings]); - let id = [] + let id = []; useEffect(() => { const checkIfBrand = async () => { const brand = await axios( @@ -218,21 +218,21 @@ const ProductSearch = ({ checkIfBrand(); } }, [q]); - + useEffect(() => { - if(prefixUrl.includes('category')){ + if (prefixUrl.includes('category')) { const loadCategories = async () => { - const getCategories = await odooApi('GET', `/api/v1/category/child?parent_id=${categoryId}`) - if(getCategories){ - setDataCategories(getCategories) - } - } - loadCategories() + const getCategories = await odooApi( + 'GET', + `/api/v1/category/child?parent_id=${categoryId}` + ); + if (getCategories) { + setDataCategories(getCategories); + } + }; + loadCategories(); } - }, []) - - - + }, []); const brands = []; for ( @@ -248,7 +248,6 @@ const ProductSearch = ({ brands.push({ brand, qty }); } } - const categories = []; for ( @@ -263,7 +262,6 @@ const ProductSearch = ({ categories.push({ name, qty }); } } - const orderOptions = [ { value: '', label: 'Pilih Filter' }, @@ -382,7 +380,6 @@ const ProductSearch = ({ const isNotReadyStockPage = router.asPath !== '/shop/search?orderBy=stock'; - console.log('is spelling', spellings); return ( <> @@ -443,8 +440,8 @@ const ProductSearch = ({ SpellingComponent )}
- - + + {productFound > 0 && (
@@ -536,8 +533,8 @@ const ProductSearch = ({
- - + + {bannerPromotionHeader && bannerPromotionHeader?.image && (