Skip to content

Commit

Permalink
category added
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir Amiri committed Feb 25, 2023
1 parent aef5010 commit 7302ce4
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const App = () => {
< Routes >
<Route path='/' element={<Navigation />}>
<Route index element={<Home />} />
<Route path='shop' element={<Shop />} />
<Route path='shop/*' element={<Shop />} />
<Route path='auth' element={<Authentication />} />
<Route path='checkout' element={<Checkout />} />
</Route>
Expand Down
22 changes: 22 additions & 0 deletions src/components/category-preview/category-preview.component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Link } from 'react-router-dom';
import ProductCard from '../product-card/product-card.component';
import './category-preview.styles.scss';

const CategoryPreview = ({ title, products }) => {
return (
<div className='category-preview-container'>
<h2>
<Link className='title' to={title}>{title.toUpperCase()}</Link>
</h2>
<div className='preview'>
{
products
.filter((_, idx) => idx < 4)
.map((product) => <ProductCard key={product.id} product={product} />)
}
</div>
</div>
)
}

export default CategoryPreview;
18 changes: 18 additions & 0 deletions src/components/category-preview/category-preview.styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.category-preview-container {
display: flex;
flex-direction: column;
margin-bottom: 30px;

.title {
font-size: 28px;
margin-bottom: 25px;
cursor: pointer;
}

.preview {
display: grid;
grid-template-columns: repeat(4, 1fr);
column-gap: 20px;
}
}

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import './category-item.styles.scss'
import './directory-item.styles.scss'

const CategoryItem = ({category}) => {
const DirectoryItem = ({category}) => {
const {imageUrl, title} = category;
return (
<div className="category-container">
<div className="directory-item-container">
<div className="background-image"
style={{
backgroundImage:`url(${imageUrl})`
}}/>
<div className="category-body-container">
<div className="directory-item-body">
<h2>{title}</h2>
<p>Shop Now</p>

Expand All @@ -17,4 +17,4 @@ const CategoryItem = ({category}) => {
)
}

export default CategoryItem
export default DirectoryItem
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.category-container {
.directory-item-container {
min-width: 30%;
height: 240px;
flex: 1 1 auto;
Expand All @@ -17,7 +17,7 @@
transition: transform 6s cubic-bezier(0.25, 0.45, 0.45, 0.95);
}

& .category-body-container {
& .directory-item-body {
opacity: 0.9;
}
}
Expand All @@ -41,7 +41,7 @@
background-position: center;
}

.category-body-container {
.directory-item-body {
height: 90px;
padding: 0 25px;
display: flex;
Expand Down
4 changes: 2 additions & 2 deletions src/components/directory/directory.component.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import CategoryItem from '../category-item/category-item.component'
import DirectoryItem from '../directory-item/directory-item.component';
import './directory.styles.scss'
const Directory = ({categories}) => {
return (
<div className="directory-container">
{categories.map((category) => (
<CategoryItem key={category.id} category={category}/>
<DirectoryItem key={category.id} category={category}/>
))}
</div>
)
Expand Down
26 changes: 26 additions & 0 deletions src/contexts/categories.context.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect } from "react";
import { createContext, useState } from "react";
import { GetCategoriesAndDocuments } from "../utils/firebase/firebase.utils";


export const CategoriesContext = createContext({
categoriesMap: [],
});

export const CategoriesProvider = ({ children }) => {
const [categoriesMap, setCategoriesMap] = useState({});
useEffect(() => {
const getCategoriesMap = async () => {
const categoriesMap = await GetCategoriesAndDocuments();
setCategoriesMap(categoriesMap)
}

getCategoriesMap();
}, [])
const value = { categoriesMap };
return (
<CategoriesContext.Provider value={value}>
{children}
</CategoriesContext.Provider>
)
}
17 changes: 0 additions & 17 deletions src/contexts/products.context.jsx

This file was deleted.

6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { BrowserRouter } from 'react-router-dom';
import './index.css';
import App from './App';
import { UserProvider } from './contexts/user.context';
import { ProductsProvider } from './contexts/products.context';
import { CategoriesProvider } from './contexts/categories.context';
import { CartProvider } from './contexts/cart.context';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<BrowserRouter>
<UserProvider>
<ProductsProvider>
<CategoriesProvider>
<CartProvider>
<App />
</CartProvider>
</ProductsProvider>
</CategoriesProvider>
</UserProvider>
</BrowserRouter>
</React.StrictMode>
Expand Down
22 changes: 22 additions & 0 deletions src/routes/categories-preview/categories-preview.component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useContext, Fragment } from "react";
import { CategoriesContext } from "../../contexts/categories.context";
import CategoryPreview from "../../components/category-preview/category-preview.component";

const CategoriesPreview = () => {
const { categoriesMap } = useContext(CategoriesContext);

return (
<Fragment>
{
Object.keys(categoriesMap).map(title => {
const products = categoriesMap[title];
return <CategoryPreview key={title} title={title} products={products} />
})
}

</Fragment>
)

}

export default CategoriesPreview;
31 changes: 31 additions & 0 deletions src/routes/category/category-component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Fragment, useContext, useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import { CategoriesContext } from '../../contexts/categories.context';
import ProductCard from '../../components/product-card/product-card.component';
import './category.styles.scss';

const Category = () => {
const { category } = useParams()
const { categoriesMap } = useContext(CategoriesContext)
const [products, setProducts] = useState(categoriesMap[category])

useEffect(() => {
setProducts(categoriesMap[category])
}, [categoriesMap, category])

return (
<Fragment>
<h2 className='category-title'>
{category}
</h2>
<div className='category-container'>
{products &&
products.map((product) => <ProductCard key={product.id} product={product} />)
}
</div>
</Fragment>

)
}

export default Category;
13 changes: 13 additions & 0 deletions src/routes/category/category.styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.category-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
column-gap: 20px;
row-gap: 50px;

}

.category-title {
font-size: 38px;
margin-bottom: 25px;
text-align: center;
}
17 changes: 7 additions & 10 deletions src/routes/shop/shop.component.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { useContext } from "react";
import { ProductContext } from "../../contexts/products.context";
import ProductCard from "../../components/product-card/product-card.component";
import { Routes, Route } from 'react-router-dom';
import CategoriesPreview from '../categories-preview/categories-preview.component';
import Category from '../category/category-component';
import './shop.styles.scss';

const Shop = () => {
const { products } = useContext(ProductContext);

return (
<div className="products-container">
{products.map((product) => (
<ProductCard key={product.id} product={product} />
))}
</div>
<Routes>
<Route index element={<CategoriesPreview />} />
<Route path=':category' element={<Category />} />
</Routes>
)

}
Expand Down
Loading

0 comments on commit 7302ce4

Please sign in to comment.