diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-07-23 09:28:21 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-07-23 09:28:21 +0700 |
| commit | bf5c3b1d48da54e0e44689412ad9de9c10cf9edb (patch) | |
| tree | c60e3fb26f1d8e519dfc0f87fa702a995ec14627 /src/lib | |
| parent | a78251dc5cdfdd5438aad3ce7b7e342fceb7275e (diff) | |
<iman> update category management
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/home/api/categoryManagementApi.js | 8 | ||||
| -rw-r--r-- | src/lib/home/components/CategoryDynamic.jsx | 32 | ||||
| -rw-r--r-- | src/lib/home/components/CategoryDynamicMobile.jsx | 41 | ||||
| -rw-r--r-- | src/lib/home/components/CategoryPilihan.jsx | 2 | ||||
| -rw-r--r-- | src/lib/home/hooks/useCategoryManagement.js | 43 | ||||
| -rw-r--r-- | src/lib/product/components/CategorySection.jsx | 10 |
6 files changed, 55 insertions, 81 deletions
diff --git a/src/lib/home/api/categoryManagementApi.js b/src/lib/home/api/categoryManagementApi.js new file mode 100644 index 00000000..b70d60ce --- /dev/null +++ b/src/lib/home/api/categoryManagementApi.js @@ -0,0 +1,8 @@ +import odooApi from '@/core/api/odooApi' + +const categoryManagementApi = async () => { + const dataCategoryManagement = await odooApi('GET', '/api/v1/categories_management') + return dataCategoryManagement +} + +export default categoryManagementApi diff --git a/src/lib/home/components/CategoryDynamic.jsx b/src/lib/home/components/CategoryDynamic.jsx index 2edd5485..cac8a138 100644 --- a/src/lib/home/components/CategoryDynamic.jsx +++ b/src/lib/home/components/CategoryDynamic.jsx @@ -1,36 +1,24 @@ import React, { useEffect, useState } from 'react'; -import {fetchProductManagementSolr} from '../hooks/useCategoryManagement'; +import useCategoryManagement from '../hooks/useCategoryManagement'; import NextImage from 'next/image'; import Link from "next/link" import router from 'next/router'; import { createSlug } from '@/core/utils/slug' -const CategoryDynamic = () => { - const [promoItems, setPromoItems] = useState([]); - useEffect(() => { - const loadPromo = async () => { - - try { - const items = await fetchProductManagementSolr(); - setPromoItems(items); - - } catch (loadError) { - // console.error("Error loading promo items:", loadError) - } - } - loadPromo() - },[]); +const CategoryDynamic = () => { + const { categoryManagement } = useCategoryManagement() + return ( <div> - {promoItems && promoItems.map((category) => ( + {categoryManagement && categoryManagement.data?.map((category) => ( <div key={category.id}> <div className='bagian-judul flex flex-row justify-start items-center gap-3 mb-4 mt-4'> <div className='font-semibold sm:text-h-lg mr-2'>{category.name}</div> <p className='text-gray_r-10 text-sm'>999 rb+ Produk tersedia</p> - <Link href={createSlug('/shop/category/', category?.name, category?.category_id)} className="!text-red-500 font-semibold">Lihat Semua</Link> + <Link href={createSlug('/shop/category/', category?.name, category?.categoryIdI)} className="!text-red-500 font-semibold">Lihat Semua</Link> </div> <div className='grid grid-cols-3 gap-2'> - {category.category_id2.map((index)=> ( + {category.categories.map((index)=> ( <div key={index.id} className='border rounded justify-start items-start'> <div className='p-3'> <div className='flex flex-row border rounded mb-2 justify-start items-center'> @@ -44,13 +32,13 @@ const CategoryDynamic = () => { <div className='bagian-judul flex flex-col justify-center items-start gap-2 ml-2'> <div className='font-semibold text-lg mr-2'>{index.name}</div> <p className='text-gray_r-10 text-sm'>999 rb+ Produk</p> - <Link href={createSlug('/shop/category/', index?.name, index?.id_level_2)} className="!text-red-500 font-semibold">Lihat Semua</Link> + <Link href={createSlug('/shop/category/', index?.name, index?.idLevel2)} className="!text-red-500 font-semibold">Lihat Semua</Link> </div> </div> <div className='grid grid-cols-2 gap-2 overflow-y-auto max-h-[240px]' > - {index.child_frontend_id_i.map((x)=> ( + {index.childFrontendIdI.map((x)=> ( <div key={x.id}> - <Link href={createSlug('/shop/category/', x?.name, x?.id_level_3)} className="flex flex-row gap-2 border rounded group hover:border-red-500"> + <Link href={createSlug('/shop/category/', x?.name, x?.idLevel3)} className="flex flex-row gap-2 border rounded group hover:border-red-500"> <NextImage src={x.image? x.image : "/images/noimage.jpeg"} alt={x.name} diff --git a/src/lib/home/components/CategoryDynamicMobile.jsx b/src/lib/home/components/CategoryDynamicMobile.jsx index 317042b8..c1433a2d 100644 --- a/src/lib/home/components/CategoryDynamicMobile.jsx +++ b/src/lib/home/components/CategoryDynamicMobile.jsx @@ -1,5 +1,5 @@ import React, { useEffect, useState } from 'react'; -import { fetchProductManagementSolr } from '../hooks/useCategoryManagement'; +import useCategoryManagement from '../hooks/useCategoryManagement'; import NextImage from 'next/image'; import Link from "next/link"; import { createSlug } from '@/core/utils/slug'; @@ -7,51 +7,50 @@ import { Swiper, SwiperSlide } from 'swiper/react'; import 'swiper/css'; const CategoryDynamicMobile = () => { - const [promoItems, setPromoItems] = useState([]); + const { categoryManagement } = useCategoryManagement() const [selectedCategory, setSelectedCategory] = useState({}); useEffect(() => { const loadPromo = async () => { try { - const items = await fetchProductManagementSolr(); - setPromoItems(items); - if (items.length > 0) { - const initialSelections = items.reduce((acc, category) => { - if (category.category_id2.length > 0) { - acc[category.id] = category.category_id2[0].id_level_2; + if (categoryManagement.data?.length > 0) { + const initialSelections = categoryManagement.data.reduce((acc, category) => { + if (category.categories.length > 0) { + acc[category.id] = category.categories[0].idLevel2; } return acc; }, {}); setSelectedCategory(initialSelections); } } catch (loadError) { - // console.error("Error loading promo items:", loadError) + // console.error("Error loading promo items:", loadError); } }; + loadPromo(); - }, []); + }, [categoryManagement.data]); - const handleCategoryLevel2Click = (categoryLevel1Id, categoryLevel2Id) => { + const handleCategoryLevel2Click = (categoryIdI, idLevel2) => { setSelectedCategory(prev => ({ ...prev, - [categoryLevel1Id]: categoryLevel2Id + [categoryIdI]: idLevel2 })); }; - + return ( <div className='p-4'> - {promoItems && promoItems.map((category) => ( + {categoryManagement.data && categoryManagement.data.map((category) => ( <div key={category.id}> <div className='bagian-judul flex flex-row justify-between items-center gap-3 mb-4 mt-4'> <div className='font-semibold sm:text-h-sm mr-2'>{category.name}</div> - <Link href={createSlug('/shop/category/', category?.name, category?.category_id)} className="!text-red-500 font-semibold text-sm">Lihat Semua</Link> + <Link href={createSlug('/shop/category/', category?.name, category?.categoryIdI)} className="!text-red-500 font-semibold text-sm">Lihat Semua</Link> </div> <Swiper slidesPerView={2.3} spaceBetween={10}> - {category.category_id2.map((index) => ( + {category.categories.map((index) => ( <SwiperSlide key={index.id}> <div - onClick={() => 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'}`} + onClick={() => handleCategoryLevel2Click(category.id, index?.idLevel2)} + className={`border flex justify-start items-center max-w-48 max-h-16 rounded ${selectedCategory[category.id] === index?.idLevel2 ? 'bg-red-50 border-red-500 text-red-500' : 'border-gray-200 text-gray-900'}`} > <div className='p-1 flex justify-start items-center'> <div className='flex flex-row justify-center items-center'> @@ -74,10 +73,10 @@ const CategoryDynamicMobile = () => { </Swiper> <div className='p-3 mt-2 border'> <div className='grid grid-cols-2 gap-2 overflow-y-auto max-h-[240px]'> - {category.category_id2.map((index) => ( - selectedCategory[category.id] === index?.id_level_2 && index.child_frontend_id_i.map((x) => ( + {category.categories.map((index) => ( + selectedCategory[category.id] === index?.idLevel2 && index.childFrontendIdI.map((x) => ( <div key={x.id}> - <Link href={createSlug('/shop/category/', x?.name, x?.id_level_3)} className="flex flex-row gap-1 border rounded group hover:border-red-500"> + <Link href={createSlug('/shop/category/', x?.name, x?.idLevel3)} className="flex flex-row gap-1 border rounded group hover:border-red-500"> <NextImage src={x.image ? x.image : "/images/noimage.jpeg"} alt={x.name} diff --git a/src/lib/home/components/CategoryPilihan.jsx b/src/lib/home/components/CategoryPilihan.jsx index 409a8918..7b9f0094 100644 --- a/src/lib/home/components/CategoryPilihan.jsx +++ b/src/lib/home/components/CategoryPilihan.jsx @@ -43,7 +43,7 @@ const CategoryPilihan = ({ id, categories }) => { <div className='KartuB absolute h-48 w-60 inset-0 flex items-center justify-center '> <div className="group/edit flex items-center justify-center h-48 w-60 flex-col group-hover/item:visible"> <div className=' h-36'> - <Image className='group-hover:scale-110 transition-transform duration-300' src={category?.image_1920? category?.image_1920 : '/images/noimage.jpeg'} width={120} height={120} alt={category?.name} /> + <Image className='group-hover:scale-110 transition-transform duration-300' src={category?.image1920? category?.image1920 : '/images/noimage.jpeg'} width={120} height={120} alt={category?.name} /> </div> <h2 className="text-gray-700 content-center h-12 border-t-[1px] px-1 w-60 border-gray-200 font-normal text-sm text-center">{category?.name}</h2> </div> diff --git a/src/lib/home/hooks/useCategoryManagement.js b/src/lib/home/hooks/useCategoryManagement.js index db4e79e1..c1dda585 100644 --- a/src/lib/home/hooks/useCategoryManagement.js +++ b/src/lib/home/hooks/useCategoryManagement.js @@ -1,34 +1,13 @@ -export const fetchProductManagementSolr = async () => { - try { - const queryParams = new URLSearchParams({q: 'type_value_s:bundling'}) - const response = await fetch(`/solr/product_category_management/query?q=*:*&q.op=OR&sort=sequence_i asc&indent=true`); - // console.log("response", response) - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - const data = await response.json(); - // console.log("data",data) - const dataManagement = await map(data.response.docs); - // console.log("dataManagement",dataManagement) - return dataManagement; - } catch (error) { - console.error("Error fetching promotion data:", error); - return []; - } -}; +import categoryManagementApi from '../api/categoryManagementApi' +import { useQuery } from 'react-query' + +const useCategoryManagement = () => { + const fetchCategoryManagement = async () => await categoryManagementApi() + const { isLoading, data } = useQuery('categoryManagementApi', fetchCategoryManagement) -const map = async (promotions) => { - const result = []; - for (const promotion of promotions) { - const data = { - id: promotion.id, - category_id: promotion.category_id_i, - name: promotion.name_s, - sequence: promotion.sequence_i, - image: promotion.image_s, - category_id2: JSON.parse(promotion.category_id2_s), - }; - result.push(data); + return { + categoryManagement: { data, isLoading } } - return result; -}; +} + +export default useCategoryManagement
\ No newline at end of file diff --git a/src/lib/product/components/CategorySection.jsx b/src/lib/product/components/CategorySection.jsx index e80429f4..749a56eb 100644 --- a/src/lib/product/components/CategorySection.jsx +++ b/src/lib/product/components/CategorySection.jsx @@ -25,10 +25,10 @@ const CategorySection = ({ categories }) => { <div className="group/item grid grid-cols-5 gap-y-2 gap-x-2 w-full h-full col-span-2 "> {categories.slice(0, 10).map((category) => ( <Link href={createSlug('/shop/category/', category?.name, category?.id)} key={category?.id} passHref> - <div className="group transition-colors duration-300"> - <div className="KartuInti h-18 w-26 max-w-sm lg:max-w-full flex flex-col border-[2px] border-gray-200 group-hover:border-red-400 rounded relative "> - <div className="flex items-center justify-center h-full px-1 flex-row"> - <Image className="" src={category?.image_1920 ? category?.image_1920 : '/images/noimage.jpeg'} width={56} height={48} alt={category?.name} /> + <div className="group transition-colors duration-300 "> + <div className="KartuInti h-12 w-26 max-w-sm lg:max-w-full flex flex-col border-[2px] border-gray-200 group-hover:border-red-400 rounded relative "> + <div className="flex items-center justify-start h-full px-1 flex-row"> + <Image className="" src={category?.image1920? category?.image1920 : '/images/noimage.jpeg'} width={56} height={48} alt={category?.name} /> <h2 className="text-gray-700 group-hover:text-[#E20613] line-clamp-2 content-center h-fit w-60 px-1 font-semibold text-sm text-start">{category?.name}</h2> </div> </div> @@ -54,7 +54,7 @@ const CategorySection = ({ categories }) => { <div className="KartuInti h-18 w-26 max-w-sm lg:max-w-full flex flex-col border-[2px] border-gray-200 group-hover:bg-red-200 group-hover:border-red-400 rounded relative"> <div className="flex items-center justify-center h-full px-1 flex-row"> <Image - src={category?.image_1920 ? category?.image_1920 : '/images/noimage.jpeg'} + src={category?.image1920 ? category?.image1920 : '/images/noimage.jpeg'} width={56} height={48} alt={category?.name} |
