Skip to content

Commit

Permalink
solve conflicts
Browse files Browse the repository at this point in the history
related to #103
  • Loading branch information
Saeed99Madi committed Oct 1, 2022
2 parents 0205f71 + 6f7ca5e commit 217eed7
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 112 deletions.
110 changes: 61 additions & 49 deletions client/src/components/Pages/Cart/Cart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@ import { Link } from 'react-router-dom';
import fetchUrl from '../../../utils/fetch';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { useOutletContext } from 'react-router-dom';
import { useNavigate, useOutletContext } from 'react-router-dom';
function Cart() {
const { loading, setLoading } = useOutletContext();
const [productCarts, setCart] = useState([]);
const [count, setCount] = useState(0);
const { title, setPageName } = useOutletContext();
const { user, setUser, title, setPageName } = useOutletContext();
setPageName('Cart');
const navigate = useNavigate();
const changeValue = (e, quantity) => {
const id = e.target.id;
setCount(quantity);
setLoading(true);
setCount(quantity);
if (quantity === 0) deleteProduct(e);
fetchUrl('PUT', '/cart', {
productId: id,
userId: 1,
userId: user.id,
count: quantity,
}).then((data) => {
setLoading(false);
console.log('product', data);
});
};
setPageName('Cart');

const deleteProduct = (e) => {
setLoading(true);
const id = e.target.id;
setCount(+id + 1);
fetchUrl('DELETE', '/cart', {
productId: id,
userId: 1,
userId: user.id,
}).then((data) => {
console.log('product', data, 444);
setLoading(false);
Expand All @@ -47,18 +50,23 @@ function Cart() {
};

useEffect(() => {
setLoading(true);
fetch('/cart/1', {
headers: {
'Content-Type': 'application/json',
},
})
.then((data) => data.json())
.then((data) => {
console.log(data);
setCart(data);
setLoading(false);
});
console.log('tokennn', user);
if (user.token) {
setLoading(true);
fetch(`/cart/${user.id}`, {
headers: {
'Content-Type': 'application/json',
},
})
.then((data) => data.json())
.then((data) => {
setLoading(false);
console.log(data);
setCart(data);
});
} else {
navigate('/');
}
}, [count]);
return (
<>
Expand All @@ -69,38 +77,42 @@ function Cart() {
<div>Quantity</div>
<div>Total</div>
</div>
{productCarts.map((ele) => (
<div className="item" key={ele.id}>
<div className="product-img">
<i
className="fa-solid fa-xmark"
id={ele.id}
onClick={(e) => {
deleteProduct(e);
}}
></i>
<Link to={`/products/${ele.id}`}>
<img src={ele.image} alt={ele.title} />
</Link>
<p>{ele.title}</p>
</div>
<div className="price">${ele.price}</div>
<div className="quantity">
<i
className="fa-solid fa-minus"
id={ele.id}
onClick={(e) => changeValue(e, +ele.count - 1)}
></i>
<span>{ele.count}</span>
<i
className="fa-solid fa-plus"
id={ele.id}
onClick={(e) => changeValue(e, +ele.count + 1)}
></i>
{productCarts.length === 0 ? (
<h2>No products</h2>
) : (
productCarts.map((ele) => (
<div className="item" key={ele.id}>
<div className="product-img">
<i
className="fa-solid fa-xmark"
id={ele.id}
onClick={(e) => {
deleteProduct(e);
}}
></i>
<Link to={`/products/${ele.id}`}>
<img src={ele.image} alt={ele.title} />
</Link>
<p>{ele.title}</p>
</div>
<div className="price">${ele.price}</div>
<div className="quantity">
<i
className="fa-solid fa-minus"
id={ele.id}
onClick={(e) => changeValue(e, +ele.count - 1)}
></i>
<span>{ele.count}</span>
<i
className="fa-solid fa-plus"
id={ele.id}
onClick={(e) => changeValue(e, +ele.count + 1)}
></i>
</div>
<div>${ele.count * ele.price}</div>
</div>
<div>${ele.count * ele.price}</div>
</div>
))}
))
)}
</section>
<section className="total">
<div>
Expand Down
60 changes: 37 additions & 23 deletions client/src/components/Pages/ProductDetails/ProductDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function ProductDetails() {
const [productDetails, setProductDetails] = useState([]);
const { loading, setLoading } = useOutletContext();
const [quantity, setQuantity] = useState(1);
const { title: titleone, setPageName } = useOutletContext();
const { title: titleone, setPageName, user, setUser } = useOutletContext();

useEffect(() => {
setLoading(true);
Expand All @@ -29,29 +29,43 @@ function ProductDetails() {
const navigate = useNavigate();

const addToCart = (e) => {
setLoading(true);
fetchUrl('POST', '/cart', {
productId: id,
userId: 1,
count: quantity,
})
.then((data) => {
if (data) {
console.log(data);
setLoading(false);
toast.success('Product added successfully!', {
position: 'top-left',
autoClose: 2000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
// navigate(`/cart`);
}
if (user.token) {
setLoading(true);
fetchUrl('POST', '/cart', {
productId: id,
userId: user.id,
count: quantity,
})
.catch(console.log);
.then((data) => {
setLoading(false);
if (data) {
console.log(data);
toast.success('Product added successfully!', {
position: 'top-left',
autoClose: 2000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
// navigate(`/cart`);
}
})
.catch((err) => setLoading(false));
} else {
// navigate('/users/signup');
setLoading(false);
toast.error('You Have To Sign Up!', {
position: 'top-left',
autoClose: 2000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
}
};

console.log(quantity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,46 @@ import { ToastContainer, toast } from 'react-toastify';

function ProductCard(props) {
const { id, title, image, price } = props.productCard;
const user = props.user;
const navigate = useNavigate();

const addToCart = (e) => {
const id = e.target.id;
console.log(e.target.id);

fetchUrl('POST', '/cart', {
productId: id,
userId: 1,
count: 1,
})
.then((data) => {
if (data) {
toast.success('Product added successfully!', {
position: 'top-left',
autoClose: 2000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
// navigate(`/cart`);
}
// if (user.token === false) navigate('/users/signup');
if (user.token) {
fetchUrl('POST', '/cart', {
productId: id,
userId: user.id,
count: 1,
})
.catch(console.log);
.then((data) => {
if (data) {
toast.success('Product added successfully!', {
position: 'top-left',
autoClose: 2000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
// navigate(`/cart`);
}
})
.catch(console.log);
} else {
// navigate('/users/signup');
toast.error('You Have To Sign Up!', {
position: 'top-left',
autoClose: 2000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
}
};

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import ProductCard from './ProductCard';

function ProductList(props) {
const { productList } = props;
console.log('props', props);
console.log('products', props.productList);
const { productList, user } = props;
// console.log('props', props);
// console.log('products', props.productList);

return (
<div className="products">
{productList ? (
productList.map((item) => {
console.log(item);
return <ProductCard productCard={item} key={item.id} />;
// console.log(item);
return <ProductCard productCard={item} user={user} key={item.id} />;
})
) : (
<div>Loading....</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import './ProductContainer.css';
import fetchUrl from '../../../utils/fetch';

function ProductsContainer() {
const { title, setPageName } = useOutletContext();
const { title, setPageName, user, setUser } = useOutletContext();
const [category, setCategory] = useState('');
const [priceMin, setPriceMin] = useState(0);
const [priceMax, setPriceMax] = useState(100);
Expand Down Expand Up @@ -62,7 +62,7 @@ function ProductsContainer() {
priceMax={priceMax}
setPriceMax={setPriceMax}
/>
<ProductList productList={currentPosts} />
<ProductList productList={currentPosts} user={user} />
</div>
<Pagination
postsPerPage={postsPerPage}
Expand Down
3 changes: 3 additions & 0 deletions client/src/components/Pages/signup/signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as yup from 'yup';
import { useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { useOutletContext } from 'react-router-dom';

import './signup.css';
const signSh = yup.object().shape({
Expand Down Expand Up @@ -31,6 +32,8 @@ function Signup() {
const [password, setPassword] = useState('');
const [confirmPassword, setConfPassword] = useState('');
const [avatar, setAvatar] = useState('');
const [title, setPageName, user, setUser] = useOutletContext();
setPageName('Sign Up');
const register = async (e) => {
try {
e.preventDefault();
Expand Down
13 changes: 11 additions & 2 deletions client/src/components/Pages/users/SignIn.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ const loginValidationSchema = yup
.required();

function SignIn() {
const { loading, setLoading } = useOutletContext();
console.log(loading, 'under the hood');
const {
title,
setPageName,
user,
setUser,
isLogged,
setIsLogged,
loading,
setLoading,
} = useOutletContext();
setPageName('Log In');

const navigate = useNavigate();
const [email, setEmail] = useState('');
Expand Down
Loading

0 comments on commit 217eed7

Please sign in to comment.