Skip to content

Commit

Permalink
search
Browse files Browse the repository at this point in the history
  • Loading branch information
adizafri2000 committed Jul 1, 2024
1 parent 7c3e4f0 commit 3e28e67
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface ProduceRepository extends JpaRepository<Produce, Integer> {
List<Produce> findByStore(Integer storeId);

// to get all produce by name
List<Produce> findByNameContaining(String name);
List<Produce> findByNameContainingIgnoreCase(String name);

List<Produce> findTop5ByOrderByRatingScoreDesc();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface StoreRepository extends JpaRepository<Store, Integer> {
Optional<Account> findAccountByFarmer(@Param("farmer") Integer farmer);

// to get all stores by name
List<Store> findByNameContaining(String name);
List<Store> findByNameContainingIgnoreCase(String name);

List<Store> findTop5ByOrderByRatingScoreDesc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public List<OrderDetailsResponseDTO> getOrdersByAccount(Integer accountId) {
List<OrderDetailsResponseDTO> results = new ArrayList<>();
accountOrders.forEach(order -> {
PaymentResponseDTO payment = paymentService.getPaymentByOrderId(order.id());
List<CartItemDetailsResponseDTO> orderItems = cartItemService.getCartItemDetailsByCartId(order.account());
List<CartItemDetailsResponseDTO> orderItems = cartItemService.getCartItemDetailsByCartId(order.cart()).stream()
.filter(item -> item.storeId().equals(order.store()))
.collect(Collectors.toList());
results.add(new OrderDetailsResponseDTO(order, payment, orderItems));
});
return results;
Expand Down
23 changes: 21 additions & 2 deletions web/src/pages/OrderHistoryPage.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import {useUserCheck} from '../hooks/useUserCheck';
import React, {useContext, useEffect, useState} from 'react';
import orderService from '../services/order';
import {useConsumerCheck} from "../hooks/useConsumerCheck.jsx";
import UserContext from "../contexts/UserContext.jsx";

const OrderHistoryPage = () => {
useUserCheck();
const {user, loading} = useContext(UserContext);
const [orders, setOrders] = useState([]);
useConsumerCheck();

useEffect(() => {
const fetchOrders = async () => {
try {
const response = await orderService.getByAccount(user.id);
setOrders(response.data);
} catch (error) {
console.error('OrderHistoryPage fetchOrders error: ', error);
}
};
if (!loading) {
fetchOrders();
}
}, [user, loading]);

return <h2>Order History Page</h2>
}
Expand Down
18 changes: 10 additions & 8 deletions web/src/pages/PaymentPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,24 @@ const PaymentPage = () => {
return total + (item.produceUnitPrice * item.cartItemQuantity);
}
return total;
}, 0),
}, 0).toFixed(2),
paymentTimestamp: new Date().toISOString(),
method: paymentMethod
};


const paymentResponse = await paymentService.create(user.accessToken, paymentData);

// Update cart to set isActive to false
const cartUpdateData = {
isActive: false,
account: user.id
};

await cartService.update(user.accessToken, user.cart, cartUpdateData);
}


}
// Update cart to set isActive to false
const cartUpdateData = {
isActive: false,
account: user.id
};
await cartService.updateCart(user.accessToken, user.cart, cartUpdateData);
toast.success('Payment confirmed!');
navigate('/'); // Navigate to confirmation page

Expand Down
86 changes: 74 additions & 12 deletions web/src/pages/SearchResultsPage.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,83 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { useLocation } from 'react-router-dom';
import searchService from '../services/search';
import CircularProgress from "@mui/material/CircularProgress";
import StorePreviewCard from "../components/StorePreviewCard";
import ProduceCard from "../components/ProduceCard.jsx";
import { Divider, Grid, Typography, Box } from "@mui/material";

const useQuery = () => {
return new URLSearchParams(useLocation().search);
return new URLSearchParams(useLocation().search);
};

const SearchResultsPage = () => {
const query = useQuery();
const searchQuery = query.get('query');

return (
<div>
<h2>Search Results Page</h2>
{searchQuery && <p>Search Query: {searchQuery}</p>}
{/* Fetch and display search results based on the searchQuery */}
</div>
);
const [isLoading, setIsLoading] = useState(false);
const [produceList, setProduceList] = useState([]);
const [storeList, setStoreList] = useState([]);
const query = useQuery();
const searchQuery = query.get('query');

useEffect(() => {
const fetchData = async (searchQuery) => {
try {
setIsLoading(true);
const response = await searchService.search(searchQuery);
setProduceList(response.data.produce);
setStoreList(response.data.store);
} catch (error) {
console.log('Error fetching search results:', error);
} finally {
setIsLoading(false);
}
}
if (searchQuery) {
fetchData(searchQuery);
}
}, [searchQuery]);

if (isLoading) {
return <CircularProgress />;
}

return (
<div>
<h2>Search Results Page</h2>
{searchQuery && <Typography variant="subtitle1">Searching for: {searchQuery}</Typography>}
<Divider />

<Box mt={4}>
<Typography variant="h4" gutterBottom>Produce Results</Typography>
{produceList.length === 0 ? (
<Typography variant="body1">No produce found</Typography>
) : (
<Grid container spacing={3}>
{produceList.map(produce => (
<Grid item xs={12} sm={6} md={4} lg={3} key={produce.id}>
<ProduceCard produce={produce} />
</Grid>
))}
</Grid>
)}
</Box>

<Divider style={{ margin: '20px 0' }} />

<Box mt={4}>
<Typography variant="h4" gutterBottom>Store Results</Typography>
{storeList.length === 0 ? (
<Typography variant="body1">No stores found</Typography>
) : (
<Grid container spacing={3}>
{storeList.map(store => (
<Grid item xs={12} sm={6} md={4} lg={3} key={store.id}>
<StorePreviewCard store={store} />
</Grid>
))}
</Grid>
)}
</Box>
</div>
);
};

export default SearchResultsPage;
14 changes: 14 additions & 0 deletions web/src/services/search.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import api from './api.jsx';
const baseURL = `/search`;


const search = async (searchQuery) => {
try {
return await api.get(`${baseURL}?query=${searchQuery}`);
} catch (error) {
console.log('Error fetching search results:', error);
throw error.response.data;
}
}

export default {search}

0 comments on commit 3e28e67

Please sign in to comment.