diff --git a/package-lock.json b/package-lock.json index 7a4ab34..2b37d5d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "axios-app", "version": "0.1.0", "dependencies": { "@emotion/react": "^11.7.0", diff --git a/src/App.js b/src/App.js index 7a13708..9425b30 100644 --- a/src/App.js +++ b/src/App.js @@ -1,23 +1,37 @@ -import logo from './logo.svg'; import './App.css'; -import Dashboard from './pages/Dashboard' -import Categories from './pages/Categories' -import Products from './pages/Products' import Login from './pages/Login' -import {Navigate, Route, Routes} from 'react-router-dom' -import {useEffect,useState} from 'react' +import { Route, Routes} from 'react-router-dom' +import {useState} from 'react' import Drawer from './components/Drawer' - function App(props) { + let [cartItems, setCartItems] = useState(JSON.parse(localStorage.getItem('data'))); + const addToLocalStorage = ((addData)=>{ + let obj ={ + id : addData.id, + title : addData.title, + price : addData.price, + image : addData.image, + } + cartItems.push(obj) + localStorage.setItem("data", JSON.stringify(cartItems)) + setCartItems(JSON.parse(localStorage.getItem('data'))) + }) + + + const removeFromLocalStorage = ((index)=>{ + cartItems.splice(index, 1) + localStorage.setItem('data', JSON.stringify(cartItems)) + setCartItems(JSON.parse(localStorage.getItem('data'))) +}) return }/> }/> - }/> + }/> + }/> }/> - } diff --git a/src/components/Drawer.jsx b/src/components/Drawer.jsx index 9e615f1..2039970 100644 --- a/src/components/Drawer.jsx +++ b/src/components/Drawer.jsx @@ -16,12 +16,11 @@ import ChevronRightIcon from '@mui/icons-material/ChevronRight'; import ListItem from '@mui/material/ListItem'; import ListItemIcon from '@mui/material/ListItemIcon'; import ListItemText from '@mui/material/ListItemText'; -import InboxIcon from '@mui/icons-material/MoveToInbox'; -import MailIcon from '@mui/icons-material/Mail'; -import { Routes, Link, Route, useLocation, useNavigate } from 'react-router-dom' +import { Link, useLocation, useNavigate } from 'react-router-dom' import Dashboard from '../pages/Dashboard' import Categories from '../pages/Categories' import Products from '../pages/Products' +import Cart from '../pages/Cart' import LogoutIcon from '@mui/icons-material/Logout'; import GridViewIcon from '@mui/icons-material/GridView'; import CategoryIcon from '@mui/icons-material/Category'; @@ -31,8 +30,9 @@ import Avatar from './Avatar' import MenuItem from '@mui/material/MenuItem'; import Menu from '@mui/material/Menu'; import Tooltip from '@mui/material/Tooltip'; -import {navigate} from 'react-router-dom' import {TOKEN_KEY} from '../utils/Constants' +import ShoppingCartIcon from '@mui/icons-material/ShoppingCart'; +import { Stack } from '@mui/material'; const drawerWidth = 240; @@ -104,6 +104,10 @@ function PersistentDrawerLeft(props) { navigate('/login') handleCloseUserMenu() } + const goToCart = () => { + navigate('/cart') + + } useEffect(() => { }, []) @@ -113,11 +117,13 @@ function PersistentDrawerLeft(props) { case '/login': return case '/products': - return + return case '/dashboard': return case '/categories': return + case '/cart': + return } } const handleOpenUserMenu = (event) => { @@ -150,12 +156,19 @@ function PersistentDrawerLeft(props) { + + + + + + + - + - + diff --git a/src/components/InputField.jsx b/src/components/InputField.jsx new file mode 100644 index 0000000..2ca2eb3 --- /dev/null +++ b/src/components/InputField.jsx @@ -0,0 +1,27 @@ +import * as React from 'react'; +import Box from '@mui/material/Box'; +import TextField from '@mui/material/TextField'; + +export default function FormPropsTextFields({ph, ty, val, onCh}) { + return ( + +
+ +
+
+ ); +} diff --git a/src/components/ProductCard.jsx b/src/components/ProductCard.jsx new file mode 100644 index 0000000..8438ab5 --- /dev/null +++ b/src/components/ProductCard.jsx @@ -0,0 +1,42 @@ +import'./page.css' +import Progress from './Progress' +import InputField from './InputField' +import {useState} from "react"; + +function ProductCard ({passedData, onAdd}){ + const [query, setQuery] = useState("") + if (!passedData.length) return + return ( +
+ setQuery(event.target.value)} /> +
+ { + passedData.filter(product => { + if (query === '') { + return passedData; + } else if (product.title.toLowerCase().includes(query.toLowerCase()) || product.category.toLowerCase().includes(query.toLowerCase())) { + return product; + } + }).map((product, index)=>( +
+
+ +
+

{product.category}

+

+ {product.title} +

+

+ $ {product.price} +

+ +
+ +)) +} +
+
+ ) +} + +export default ProductCard; diff --git a/src/components/Progress.jsx b/src/components/Progress.jsx new file mode 100644 index 0000000..c8cc4b5 --- /dev/null +++ b/src/components/Progress.jsx @@ -0,0 +1,12 @@ +import * as React from 'react'; +import Box from '@mui/material/Box'; +import LinearProgress from '@mui/material/LinearProgress'; + +export default function LinearIndeterminate() { + return ( + +

Loading Products

+ +
+ ); +} diff --git a/src/components/page.css b/src/components/page.css new file mode 100644 index 0000000..a0f4589 --- /dev/null +++ b/src/components/page.css @@ -0,0 +1,106 @@ +body{ + background: rgb(255, 255, 255); +} + +.btn{ + padding-inline: 90px; + padding-block: 10px; + border:none; + border-radius:5px; + background: green; + -webkit-border-radius:5px; + -moz-border-radius:5px; + -ms-border-radius:5px; + -o-border-radius:5px; +} + + +.pro{ + display:grid; + grid-template-columns:repeat(5, 1fr) +} +.pro-cart{ + display:grid; + grid-template-columns:repeat(3, 1fr); +} +.pr { + display:flex; + margin-top: 30px; + height: 320px; + width: 250px; + position: relative; + background: rgb(243, 241, 241); + border-radius:10px; + -webkit-border-radius:10px; + -moz-border-radius:10px; + -ms-border-radius:10px; + -o-border-radius:10px; +} +.img-div{ + width:100%; + margin-top: 25px; + text-align:center +} +img { + + height: 50%; + width: 50%; +} +.pro-nam{ + position:absolute; + bottom: 38px; + left: 10px; + font-size: 16px; + +} +.pro-desc{ + position:absolute; + bottom: 13px; + left: 10px; + font-size: 14px; + color: rgb(121, 121, 121); + +} +.price{ + position:absolute; + bottom: 2px; + right: 10px; + background: white; + color: orange; + padding:2px; + border-radius: 10px; + -webkit-border-radius: 10px; + -moz-border-radius: 10px; + -ms-border-radius: 10px; + -o-border-radius: 10px; +} + +.cart-btn{ + position:absolute; + bottom: -20px; + background: #000; + color: white; + width: inherit; + padding: 10px; + border-radius: 15px; + -webkit-border-radius: 15px; + -moz-border-radius: 15px; + -ms-border-radius: 15px; + -o-border-radius: 15px; + border-top-left-radius: 0; + border-top-right-radius: 0; + border:0; +} + +.form{ + display:flex; + justify-content: space-between; +} + +.right-form{ +display:flex; +} +.right-form >div{ + margin-inline:10px; + +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index bb29a24..6714bc6 100644 --- a/src/index.js +++ b/src/index.js @@ -4,8 +4,7 @@ import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; import {BrowserRouter} from 'react-router-dom' -import Drawer from './components/Drawer' -import { createTheme, ThemeProvider, styled } from '@mui/material/styles'; +import { createTheme, ThemeProvider } from '@mui/material/styles'; const theme = createTheme({ diff --git a/src/pages/Cart.jsx b/src/pages/Cart.jsx new file mode 100644 index 0000000..1a166b0 --- /dev/null +++ b/src/pages/Cart.jsx @@ -0,0 +1,27 @@ + +function Cart({onRemove, cartItems}) { + return ( +
+
+ { + cartItems.map((product, index)=>( +
+
+ +
+

+ {product.title} +

+

+ $ {product.price} +

+ +
+ )) + } +
+
+ ) +} + +export default Cart; diff --git a/src/pages/Dashboard.jsx b/src/pages/Dashboard.jsx index 3c13707..fdef1b1 100644 --- a/src/pages/Dashboard.jsx +++ b/src/pages/Dashboard.jsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect} from 'react'; import {Navigate} from 'react-router-dom' function Dashboard(props) { const [isLogged, setIsLogged] = React.useState(true) diff --git a/src/pages/Products.jsx b/src/pages/Products.jsx index e3fcba4..f1135b3 100644 --- a/src/pages/Products.jsx +++ b/src/pages/Products.jsx @@ -1,10 +1,33 @@ -import React, { useEffect } from 'react'; +import axios from 'axios'; +import React, { useEffect, useState } from 'react'; +import { Navigate } from 'react-router-dom'; +import ProductCard from '../components/ProductCard'; +function Products({onAdd}) { + const [isLogged, setIsLogged] = useState(true) + const [data, setData] = useState([]); + useEffect(()=>{ + axios.get('https://fakestoreapi.com/products').then((response) => { + let redata = response.data + setData(redata); + }); -function Products(props) { - + let token; + try { + token = JSON.parse(localStorage.getItem('token')) + if(!token) + setIsLogged(false) + } + catch (error) + { + setIsLogged(false) + } + },[]) + + if(!isLogged){ + return } return (
- Products +
); }