Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
adizafri2000 committed Jun 30, 2024
1 parent 68f71cd commit c1fbecf
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
public record CartItemDetailsResponseDTO(
Integer cartId,
Integer accountId,
Integer produceId,
String produceName,
BigDecimal produceUnitPrice,
String produceSellingUnit,
Integer produceStore,
Integer cartItemQuantity
Integer cartItemQuantity,
Integer storeId,
String storeName
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ public interface CartItemRepository extends JpaRepository<CartItem, Integer>{

// retrieves all cart items in cart by account id, containing the necessary produce details
@Query("select new com.example.springbootbackend.dto.cartitem.CartItemDetailsResponseDTO(" +
"c.id, c.account, p.name, p.unitPrice, p.sellingUnit, p.store, ci.quantity) " +
"c.id, c.account, p.id, p.name, p.unitPrice, p.sellingUnit, p.store, ci.quantity, s.id, s.name) " +
"from CartItem ci " +
"left join Cart c on c.id = ci.cart " +
"left join Produce p on p.id = ci.produce " +
"left join Store s on s.id = p.store " +
"where ci.cart = :cartId")
List<CartItemDetailsResponseDTO> findCartItemDetailsByCartId(Integer cartId);
}
120 changes: 75 additions & 45 deletions web/src/pages/CartPage.jsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,98 @@
import {useUserCheck} from '../hooks/useUserCheck';
import {useConsumerCheck} from "../hooks/useConsumerCheck.jsx";
import cartService from "../services/cart.jsx";
import {useContext, useEffect, useState} from "react";
import { useContext, useEffect, useState } from "react";
import { useNavigate } from 'react-router-dom';
import UserContext from "../contexts/UserContext.jsx";
import CircularProgress from "@mui/material/CircularProgress";
import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, Button, Typography, Box } from '@mui/material';
import cartService from "../services/cart.jsx";
import { useConsumerCheck } from "../hooks/useConsumerCheck.jsx";

const CartPage = () => {
const { user, loading } = useContext(UserContext);
const { user } = useContext(UserContext);
const [cartItems, setCartItems] = useState([]);
const [isLoading, setIsLoading] = useState(false)
const [isLoading, setIsLoading] = useState(false);
const navigate = useNavigate();
useConsumerCheck();

useEffect(() => {
console.log('cart page useeffect')
const initialCartChecking = async () => {
if (!user.cart){
console.log('no active cart found for user, creating new one')
try{
if (!user.cart) {
try {
const data = {
isActive: true,
account: user.id
}
account: user.id,
};
const response = await cartService.createCart(user.accessToken, data);
console.log('created new active cart for user: ', response.data)
updateUserDetails({ ...user, cart: response.data.id });
} catch (error) {
console.log('error creating new active cart: ', error)
console.log("Error creating new active cart:", error);
}
} else{
console.log('user seems to have cart already with id: ', user.cart)
}
}
};

const fetchCartItems = async () => {
await initialCartChecking()
try{
const response = await cartService.getCartItems(user.cart)
console.log('response from cartservice.getCartItems: ', response)
setCartItems(response.data)
} catch(error){
console.log('error fetching cart items: ', error)
await initialCartChecking();
try {
const response = await cartService.getCartItems(user.cart);
setCartItems(response.data);
} catch (error) {
console.log("Error fetching cart items:", error);
} finally {
console.log('completed fetching cart items')
setIsLoading(false);
}
}
setIsLoading(true)
fetchCartItems()
setIsLoading(false)
}, [loading]);
};

setIsLoading(true);
fetchCartItems();
}, [user]);

if (isLoading || loading)
return <CircularProgress />
if (isLoading) return <CircularProgress />;

return (
<>
<h2>Cart Page</h2>
{cartItems.map((item) => (
<div>
<h3>produce id: {item.produce}</h3>
<p>quantity: {item.quantity}</p>
</div>
))}
{cartItems.length === 0 && <p>No items in cart</p>}
</>
)
}
<>
<Typography variant="h4" gutterBottom>
Cart Page
</Typography>
{cartItems.length > 0 ? (
<>
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow>
<TableCell>Produce Name</TableCell>
<TableCell align="right">Unit Price (RM)</TableCell>
<TableCell align="right">Selling Unit</TableCell>
<TableCell align="right">Store ID</TableCell>
<TableCell align="right">Quantity</TableCell>
</TableRow>
</TableHead>
<TableBody>
{cartItems.map((item) => (
<TableRow key={item.produceName}>
<TableCell component="th" scope="row">
{item.produceName}
</TableCell>
<TableCell align="right">{item.produceUnitPrice}</TableCell>
<TableCell align="right">{item.produceSellingUnit}</TableCell>
<TableCell align="right">{item.produceStore}</TableCell>
<TableCell align="right">{item.cartItemQuantity}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
<Box mt={2}>
<Button variant="contained" color="primary" onClick={() => navigate('/payment')}>
Proceed to Payment
</Button>
</Box>
</>
) : (
<Typography variant="body1" color="textSecondary">
Your cart is empty.
</Typography>
)}
</>
);
};

export default CartPage;
export default CartPage;

0 comments on commit c1fbecf

Please sign in to comment.