NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

"use client"

import { useEffect, useState } from "react"
import React from 'react'
import QuotationModal from "@/app/components/modals/page";
import { useTranslation } from "react-i18next";
import { apiUrl } from "@/app/constant/constant";
import Link from "next/link";

const TableData = () => {

const { t } = useTranslation();
const [categoryWiseData, setCategoryWiseData] = useState([]);

const [modalIsOpen, setModalIsOpen] = useState(false);
const [isChecked, setIsChecked] = useState(false);
const [sendData, setSendData] = useState([]);
const [product, setProduct] = useState([]);
const [subCategoryProduct, setsubCategoryProduct] = useState([]);
console.log("subCategoryProduct:-", subCategoryProduct);
useEffect(() => {
const fetchData = async () => {
try {
const sessionVariable =
typeof window !== "undefined"
? JSON.parse(localStorage.getItem("selectedCatIds"))
: null;
const purchasedId = localStorage.getItem("purchasedCatId");

const newSessionVar = purchasedId
? sessionVariable.concat(parseInt(purchasedId))
: sessionVariable;

if (sessionVariable) {
const response = await fetch(`${apiUrl}api/product/categorywise`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
category_ids: newSessionVar,
lang: null,
}),
});

const responseData = await response.json();

if (responseData.success === "true") {
const allProductArrays = extractProductArrays(responseData.sub_categories.data);
console.log("allProductArrays:-", allProductArrays);

setsubCategoryProduct(allProductArrays);
setCategoryWiseData(responseData);
}
}
} catch (error) {
console.error("Error fetching data:", error);
}
};
fetchData();
}, []);


const extractProductArrays = (categories) => {
let allProductArrays = [];

categories.forEach(category => {
if (category.product && category.product.length > 0) {
allProductArrays.push(category.product);
}

if (category.subcategories && category.subcategories.length > 0) {
const subcategoryProductArrays = extractProductArrays(category.subcategories);
allProductArrays = [...allProductArrays, ...subcategoryProductArrays];
}
});

return allProductArrays;
};

const removeElement = (id) => {
const updatedData = product.filter((item) => item.id !== id);
setProduct(updatedData);

localStorage.setItem("selectData", JSON.stringify(updatedData));
};


const sendRequestQuotation = async () => {
const userConfirmed = window.confirm(
"Are you sure you want to send the quotation request?"
);
if (userConfirmed) {
setModalIsOpen(true);
}
};

const closeModal = () => {
setModalIsOpen(false);
};

const handleOnChange = (e, id) => {
setIsChecked((prevStates) => ({
...prevStates,
[id]: e.target.checked,
}));
setSendData((prevData) => {
if (e.target.checked) {
return [...prevData, id];
} else {
return prevData.filter((itemId) => itemId !== id);
}
});
};


return (
<>
<div className="container">
<div className="row">
<div className="col-md-12">
<div className="comparemodal-body">
<div className="table-wrapper table-responsive ratio_asos">
<table className="table table-compare">
<thead>
<tr>
<th>{t("Request Quote")}</th>
<th>{t("IMAGE")}</th>
<th>{t("NAME")}</th>
<th>{t("PRICE")}</th>
<th>{t("HANDLING")}</th>
<th>{t("COMPATIBILITY")}</th>
<th>{t("CONNECTIVITY")}</th>
<th>{t("Suppliers")}</th>
<th>{t("SELF-PROGRAMMING")}</th>
<th>{t("DIRECT BUY FROM MANUFACTURER")}</th>
</tr>
</thead>
<tbody>
{/* {categoryWiseData?.sub_categories?.data.map(
(cateItem) =>
cateItem.product.map((product,index) => ( */}
{subCategoryProduct.map((productArray, arrayIndex) => (
productArray.map((product, index) => (
<tr
key={index}
className="table-product-details"
>
<td>
<a>
<input
type="checkbox"
id={index}
name={index}
value="requestQuote"
checked={isChecked[product.id] || false}
onChange={(e) =>
handleOnChange(e, product.id)
}
/>
</a>
</td>
<td>
<div className="product-box">
<div className="product-image">
<button
className="btn-remove"
onClick={() =>
removeElement(product.id)
}
>
<i className="jki jki-times-solid"></i>
</button>

<div className="img-style">
<img
src={`${apiUrl}storage/product/${product.pro_image}`}
className="img-fluid bg-img img-80"
alt=""
/>
</div>
</div>
</div>
</td>
<td>
<div className="product-title">
<a href="#">
<h6 className="truncate">
{product.pro_name}
</h6>
</a>
</div>
<p className="content-color truncate-2"></p>
</td>
<td>
<span className="font-light">
{product.price}
</span>
</td>
<td>
<p>{product.auth_guard}</p>
</td>
<td>{product.compatibility_secondary}</td>
<td>{product.connectivity_primary}</td>
<td>{product.supplier}</td>
<td>{product.self_programming}</td>
<td>
<Link
href={product.link ? product.link : "NA"}
target="_blank"
className={product.link ? "" : "disabled"}
>
<button className="btn btn-success text-nowrap">
{t("Direct Buy")}
</button>
</Link>
</td>
</tr>

))
))}


<tr>
<td
style={{ borderLeft: "none !important" }}
colSpan="9"
className="border-0"
>
<div className="d-flex mt-2">
<button
className="btn btn-success btn-full bg-success"
type="submit"
disabled={Object.values(isChecked).every(
(state) => !state
)}
onClick={sendRequestQuotation}
>
{t("RequestQuotation")}
</button>
</div>
</td>
</tr>

</tbody>
</table>
</div>
</div>
</div>
</div>
</div>

<QuotationModal
showModal={modalIsOpen}
closeModal={closeModal}
sendData={sendData}
modalContent="Modal Content Goes Here"
/>

</>
)
}

export default TableData
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.