fixed chechout and product add
This commit is contained in:
parent
af0608ed43
commit
37f7cc23b7
8 changed files with 18 additions and 29 deletions
|
|
@ -35,7 +35,7 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
// Get cart items with product details
|
// Get cart items with product details
|
||||||
const cartItemsResult = await query(
|
const cartItemsResult = await query(
|
||||||
`SELECT ci.id, ci.quantity, ci.added_at,
|
`SELECT ci.id, ci.quantity, ci.added_at,
|
||||||
p.id AS product_id, p.name, p.description, p.price, p.image_url,
|
p.id AS product_id, p.name, p.description, p.price,
|
||||||
p.category_id, pc.name AS category_name
|
p.category_id, pc.name AS category_name
|
||||||
FROM cart_items ci
|
FROM cart_items ci
|
||||||
JOIN products p ON ci.product_id = p.id
|
JOIN products p ON ci.product_id = p.id
|
||||||
|
|
@ -69,7 +69,7 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
if (req.user.id !== userId) {
|
if (req.user.id !== userId) {
|
||||||
return res.status(403).json({
|
return res.status(403).json({
|
||||||
error: true,
|
error: true,
|
||||||
message: 'You can only modify your own cart'
|
message: 'You can only modify your own cart' + req.user.id + " "+ userId
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Check if product exists
|
// Check if product exists
|
||||||
|
|
@ -125,7 +125,7 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
// Get updated cart
|
// Get updated cart
|
||||||
const updatedCartItems = await query(
|
const updatedCartItems = await query(
|
||||||
`SELECT ci.id, ci.quantity, ci.added_at,
|
`SELECT ci.id, ci.quantity, ci.added_at,
|
||||||
p.id AS product_id, p.name, p.description, p.price, p.image_url,
|
p.id AS product_id, p.name, p.description, p.price,
|
||||||
p.category_id, pc.name AS category_name
|
p.category_id, pc.name AS category_name
|
||||||
FROM cart_items ci
|
FROM cart_items ci
|
||||||
JOIN products p ON ci.product_id = p.id
|
JOIN products p ON ci.product_id = p.id
|
||||||
|
|
@ -158,7 +158,7 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
if (req.user.id !== userId) {
|
if (req.user.id !== userId) {
|
||||||
return res.status(403).json({
|
return res.status(403).json({
|
||||||
error: true,
|
error: true,
|
||||||
message: 'You can only modify your own cart'
|
message: 'You can only modify your own cart' + req.user.id + " "+ userId
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Get cart
|
// Get cart
|
||||||
|
|
@ -193,7 +193,7 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
// Get updated cart
|
// Get updated cart
|
||||||
const updatedCartItems = await query(
|
const updatedCartItems = await query(
|
||||||
`SELECT ci.id, ci.quantity, ci.added_at,
|
`SELECT ci.id, ci.quantity, ci.added_at,
|
||||||
p.id AS product_id, p.name, p.description, p.price, p.image_url,
|
p.id AS product_id, p.name, p.description, p.price,
|
||||||
p.category_id, pc.name AS category_name
|
p.category_id, pc.name AS category_name
|
||||||
FROM cart_items ci
|
FROM cart_items ci
|
||||||
JOIN products p ON ci.product_id = p.id
|
JOIN products p ON ci.product_id = p.id
|
||||||
|
|
@ -226,7 +226,7 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
if (req.user.id !== userId) {
|
if (req.user.id !== userId) {
|
||||||
return res.status(403).json({
|
return res.status(403).json({
|
||||||
error: true,
|
error: true,
|
||||||
message: 'You can only modify your own cart'
|
message: 'You can only modify your own cart' + req.user.id + " "+ userId
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Get cart
|
// Get cart
|
||||||
|
|
|
||||||
|
|
@ -41,15 +41,6 @@ const Footer = () => {
|
||||||
<Link component={RouterLink} to="/products" color="inherit" display="block">
|
<Link component={RouterLink} to="/products" color="inherit" display="block">
|
||||||
Shop All
|
Shop All
|
||||||
</Link>
|
</Link>
|
||||||
<Link component={RouterLink} to="/products?category=Rock" color="inherit" display="block">
|
|
||||||
Rocks
|
|
||||||
</Link>
|
|
||||||
<Link component={RouterLink} to="/products?category=Bone" color="inherit" display="block">
|
|
||||||
Bones
|
|
||||||
</Link>
|
|
||||||
<Link component={RouterLink} to="/products?category=Stick" color="inherit" display="block">
|
|
||||||
Sticks
|
|
||||||
</Link>
|
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
<Grid item xs={12} sm={4}>
|
<Grid item xs={12} sm={4}>
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,6 @@ const AdminLayout = () => {
|
||||||
const mainListItems = [
|
const mainListItems = [
|
||||||
{ text: 'Dashboard', icon: <DashboardIcon />, path: '/admin' },
|
{ text: 'Dashboard', icon: <DashboardIcon />, path: '/admin' },
|
||||||
{ text: 'Products', icon: <CategoryIcon />, path: '/admin/products' },
|
{ text: 'Products', icon: <CategoryIcon />, path: '/admin/products' },
|
||||||
{ text: 'Add Product', icon: <AddCircleIcon />, path: '/admin/products/new' },
|
|
||||||
{ text: 'Categories', icon: <ClassIcon />, path: '/admin/categories' },
|
{ text: 'Categories', icon: <ClassIcon />, path: '/admin/categories' },
|
||||||
{ text: 'Orders', icon: <ShoppingCartIcon />, path: '/admin/orders' },
|
{ text: 'Orders', icon: <ShoppingCartIcon />, path: '/admin/orders' },
|
||||||
{ text: 'Customers', icon: <PeopleIcon />, path: '/admin/customers' },
|
{ text: 'Customers', icon: <PeopleIcon />, path: '/admin/customers' },
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ import {
|
||||||
Snackbar,
|
Snackbar,
|
||||||
Autocomplete
|
Autocomplete
|
||||||
} from '@mui/material';
|
} from '@mui/material';
|
||||||
import { useNavigate, useParams } from 'react-router-dom';
|
import { useNavigate, useParams, useLocation } from 'react-router-dom';
|
||||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||||
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
|
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
|
||||||
import SaveIcon from '@mui/icons-material/Save';
|
import SaveIcon from '@mui/icons-material/Save';
|
||||||
|
|
@ -28,12 +28,12 @@ import apiClient from '@services/api';
|
||||||
import { useAuth } from '@hooks/reduxHooks';
|
import { useAuth } from '@hooks/reduxHooks';
|
||||||
|
|
||||||
const ProductEditPage = () => {
|
const ProductEditPage = () => {
|
||||||
const { id } = useParams();
|
const { pathname } = useLocation();
|
||||||
|
const id = pathname.split('/').pop();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const { apiKey } = useAuth();
|
const { apiKey } = useAuth();
|
||||||
const isNewProduct = id === 'new';
|
const isNewProduct = id === 'new';
|
||||||
|
|
||||||
// Form state
|
// Form state
|
||||||
const [formData, setFormData] = useState({
|
const [formData, setFormData] = useState({
|
||||||
name: '',
|
name: '',
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ import ShoppingCartIcon from '@mui/icons-material/ShoppingCart';
|
||||||
import NavigateNextIcon from '@mui/icons-material/NavigateNext';
|
import NavigateNextIcon from '@mui/icons-material/NavigateNext';
|
||||||
import { Link as RouterLink, useNavigate } from 'react-router-dom';
|
import { Link as RouterLink, useNavigate } from 'react-router-dom';
|
||||||
import { useAuth } from '@hooks/reduxHooks';
|
import { useAuth } from '@hooks/reduxHooks';
|
||||||
import { useGetCart, useUpdateCartItem, useClearCart } from '.@hooks/apiHooks';
|
import { useGetCart, useUpdateCartItem, useClearCart } from '@hooks/apiHooks';
|
||||||
import imageUtils from '@utils/imageUtils';
|
import imageUtils from '@utils/imageUtils';
|
||||||
|
|
||||||
const CartPage = () => {
|
const CartPage = () => {
|
||||||
|
|
@ -30,7 +30,7 @@ const CartPage = () => {
|
||||||
const { user } = useAuth();
|
const { user } = useAuth();
|
||||||
|
|
||||||
// Get cart data
|
// Get cart data
|
||||||
const { data: cart, isLoading, error } = useGetCart(user?.id);
|
const { data: cart, isLoading, error } = useGetCart(user);
|
||||||
|
|
||||||
// Cart mutations
|
// Cart mutations
|
||||||
const updateCartItem = useUpdateCartItem();
|
const updateCartItem = useUpdateCartItem();
|
||||||
|
|
@ -39,7 +39,7 @@ const CartPage = () => {
|
||||||
// Handle quantity change
|
// Handle quantity change
|
||||||
const handleUpdateQuantity = (productId, newQuantity) => {
|
const handleUpdateQuantity = (productId, newQuantity) => {
|
||||||
updateCartItem.mutate({
|
updateCartItem.mutate({
|
||||||
userId: user.id,
|
userId: user,
|
||||||
productId,
|
productId,
|
||||||
quantity: newQuantity
|
quantity: newQuantity
|
||||||
});
|
});
|
||||||
|
|
@ -48,7 +48,7 @@ const CartPage = () => {
|
||||||
// Handle remove item
|
// Handle remove item
|
||||||
const handleRemoveItem = (productId) => {
|
const handleRemoveItem = (productId) => {
|
||||||
updateCartItem.mutate({
|
updateCartItem.mutate({
|
||||||
userId: user.id,
|
userId: user,
|
||||||
productId,
|
productId,
|
||||||
quantity: 0
|
quantity: 0
|
||||||
});
|
});
|
||||||
|
|
@ -56,7 +56,7 @@ const CartPage = () => {
|
||||||
|
|
||||||
// Handle clear cart
|
// Handle clear cart
|
||||||
const handleClearCart = () => {
|
const handleClearCart = () => {
|
||||||
clearCart.mutate(user.id);
|
clearCart.mutate(user);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle proceed to checkout
|
// Handle proceed to checkout
|
||||||
|
|
|
||||||
|
|
@ -120,7 +120,7 @@ ${formData.country}
|
||||||
${formData.email}`;
|
${formData.email}`;
|
||||||
|
|
||||||
checkout.mutate({
|
checkout.mutate({
|
||||||
userId: user.id,
|
userId: user,
|
||||||
shippingAddress
|
shippingAddress
|
||||||
}, {
|
}, {
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ const ProductDetailPage = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
addToCart.mutate({
|
addToCart.mutate({
|
||||||
userId: user.id,
|
userId: user,
|
||||||
productId: id,
|
productId: id,
|
||||||
quantity
|
quantity
|
||||||
});
|
});
|
||||||
|
|
@ -180,7 +180,6 @@ const ProductDetailPage = () => {
|
||||||
<Grid item xs={12} md={6}>
|
<Grid item xs={12} md={6}>
|
||||||
<Box sx={{ mb: 2 }}>
|
<Box sx={{ mb: 2 }}>
|
||||||
<Card>
|
<Card>
|
||||||
{console.log(product)}
|
|
||||||
<CardMedia
|
<CardMedia
|
||||||
component="img"
|
component="img"
|
||||||
image={
|
image={
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,7 @@ const ProductsPage = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
addToCart.mutate({
|
addToCart.mutate({
|
||||||
userId: user.id,
|
userId: user,
|
||||||
productId,
|
productId,
|
||||||
quantity: 1
|
quantity: 1
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue