Skip to content

Commit

Permalink
Added fav count beside HeartSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
TPH777 committed Jul 13, 2024
1 parent 5bc6da0 commit d1af53f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
19 changes: 13 additions & 6 deletions src/components/ConCards.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Badge, Card, Col, Row } from "react-bootstrap";
import { Badge, Card, Col, Container, Row } from "react-bootstrap";
import { timestampToString } from "../functions/Date";
import { HeartSwitch } from "@anatoliygatt/heart-switch";
import { FoodItem } from "../interface/FoodItem";
Expand Down Expand Up @@ -53,11 +53,18 @@ export const ConCards = ({
{food.business}
</Badge>
{/* HeartSwitch to toggle favorite status */}
<HeartSwitch
size="sm"
checked={favList.includes(food.id)}
onChange={() => toggleFavorite(food.id)}
/>
<Container className="mt-2">
<Row className="justify-content-md-end">
<Col xs={2}>
<HeartSwitch
size="sm"
checked={favList.includes(food.id)}
onChange={() => toggleFavorite(food.id)}
/>
</Col>
<Col xs={1}>{food.favoriteCount || 0}</Col>
</Row>
</Container>
</Card.Body>
</Card>
</Col>
Expand Down
1 change: 1 addition & 0 deletions src/interface/FoodItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface FoodItem {
business: string; // Name of the business posted the food item
imageURL: string; // URL to the image of the food item
imagePath: string; // Path to access the image of the food item in firebase storage
favoriteCount: number;
}
4 changes: 3 additions & 1 deletion src/pages/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ export function Dashboard() {
let navigate = useNavigate();
if (!user) {
navigate("/login");
return;
}
if (isConsumer) {
navigate("/");
return;
}

const [isAdding, setIsAdding] = useState<boolean>(false); // State for adding a new food item
Expand Down Expand Up @@ -107,7 +109,7 @@ export function Dashboard() {

{!isLoading && !isAdding && !isEditing && (
<>
<h1 className="mb-4">{user?.displayName}'s Dashboard</h1>
<h1 className="mb-4">{user.displayName}'s Dashboard</h1>
<div className="d-grid gap-2 mb-4">
{/* Button to add new food */}
<button
Expand Down
28 changes: 15 additions & 13 deletions src/pages/Favorites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getFoodList } from "../functions/GetFood";
import { useAuth } from "../context/Auth";
import { getFavFoodList } from "../functions/GetFav";
import { useNavigate } from "react-router-dom";
import { doc, updateDoc } from "firebase/firestore";
import { doc, increment, updateDoc } from "firebase/firestore";
import { db } from "../config/firebase";
import { deleteWarning } from "../functions/Alert";
import { ConCards } from "../components/ConCards";
Expand Down Expand Up @@ -78,19 +78,21 @@ export function FavoritePage() {

// Function to toggle favorite status of a food item
const toggleFavorite = useCallback(async (foodId: string) => {
if (!user) {
navigate("/login");
} else {
const confirm = await deleteWarning();
if (confirm) {
setFoodList((prev) => {
const newFavList = prev.filter((food) => food.id !== foodId);
updateDoc(doc(db, "consumer", user.uid), {
favorites: newFavList.map((f) => f.id),
});
return newFavList;
const confirm = await deleteWarning();
if (confirm) {
setFoodList((prev) => {
// Remove from favorites
const newFavList = prev.filter((food) => food.id !== foodId);
updateDoc(doc(db, "consumer", user.uid), {
favorites: newFavList.map((f) => f.id),
});
}

// Decrement favorite count in Firestore
updateDoc(doc(db, "food", foodId), {
favoriteCount: increment(-1),
});
return newFavList;
});
}
}, []);

Expand Down
28 changes: 24 additions & 4 deletions src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getFoodList } from "../functions/GetFood";
import { useAuth } from "../context/Auth";
import { getFavFoodList } from "../functions/GetFav";
import { useNavigate } from "react-router-dom";
import { doc, updateDoc } from "firebase/firestore";
import { doc, increment, updateDoc } from "firebase/firestore";
import { db } from "../config/firebase";
import { ConCards } from "../components/ConCards";

Expand Down Expand Up @@ -72,10 +72,30 @@ export function Home() {
navigate("/login"); // Redirect to login if not logged in
} else {
setFavList((prev) => {
const newFavList = prev.includes(foodId)
? prev.filter((id) => id !== foodId) // Remove from favorites
: [...prev, foodId]; // Add to favorites
const add = !prev.includes(foodId);
const newFavList = add
? [...prev, foodId] // Add to favorites
: prev.filter((id) => id !== foodId); // Remove from favorites
updateDoc(doc(db, "consumer", user.uid), { favorites: newFavList }); // Update in Firestore

// Update favorite count in Firestore
updateDoc(doc(db, "food", foodId), {
favoriteCount: add ? increment(1) : increment(-1),
});

// Update local state for favorite count
setFoodList((prevFoodList) =>
prevFoodList.map((food) =>
food.id === foodId
? {
...food,
favoriteCount: add
? food.favoriteCount + 1
: food.favoriteCount - 1,
}
: food
)
);
return newFavList;
});
}
Expand Down

0 comments on commit d1af53f

Please sign in to comment.