Skip to content

Need to save selectedProduct #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/components/ProductView/ProductView.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,63 @@
import React from 'react';
import React, { useState, useEffect } from "react";
import ProductListItem from "../ProductListItem";
import ProductDetails from "../ProductDetails";
import './ProductView.css'
import "./ProductView.css";

function ProductView({ products }) {
const sidePanel = localStorage.getItem("sideOpen") === "false" ? false : true;
const [sideOpen, setSideOpen] = useState(sidePanel);
const [selectedProduct, setSelectedProduct] = useState();

// TODO: Replace with state variable
const sideOpen = true;
// Open side panel when product is selected
useEffect(() => {
console.log(`selectedProduct CHANGED to: `, selectedProduct);

if (selectedProduct) setSideOpen(true);
}, [selectedProduct]);

// Deselect product when side panel is closed
useEffect(() => {
console.log(`sideOpen CHANGED to: `, sideOpen);
if (!sideOpen) setSelectedProduct();
}, [sideOpen]);

// Restore Side Panel on Refresh
useEffect(() => {
localStorage.setItem("sideOpen", sideOpen);
if (!sideOpen) setSelectedProduct();
}, [sideOpen]);

console.log("Return statement in ProductView.js has been triggered");
return (
<div className="product-view">
<div className="product-main-area">
<h1>Products</h1>
<div className="product-list">
{products.map(item =>
{products.map((item) => (
<ProductListItem
key={item.id}
product={item}
onClick={() => console.log('SELECT PRODUCT', item)}
isSelected={
!selectedProduct ? false : item.id === selectedProduct.id
}
onClick={() => setSelectedProduct(item)}
/>
)}
))}
</div>
</div>
<div className="product-side-panel">
<div className="product-side-panel-toggle-wrapper">
<div className="product-side-panel-toggle"
onClick={() => console.log('TOGGLE SIDE PANEL')}>
{sideOpen ? '>' : '<'}
<div
className="product-side-panel-toggle"
onClick={() => setSideOpen(!sideOpen)}
>
{sideOpen ? ">" : "<"}
</div>
</div>
<ProductDetails visible={sideOpen} />
<ProductDetails visible={sideOpen} product={selectedProduct} />
</div>
</div>
);
}

export default ProductView;
export default ProductView;