Skip to content

Commit

Permalink
Merge pull request #70 from ThatNinjaGuy/develop
Browse files Browse the repository at this point in the history
Integrate data on home screen #45
  • Loading branch information
ThatNinjaGuy authored Sep 16, 2024
2 parents 757acbb + 8ec367d commit 171a11f
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 72 deletions.
1 change: 0 additions & 1 deletion components/Authentication/ApproveSignUpRequestsScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ const ApproveSignUpRequestsScreen = () => {
};

const handleApprove = async (id) => {
console.log("id: ", id);
const request = requests.find((item) => item.id === id);
try {
setLoading(true);
Expand Down
8 changes: 4 additions & 4 deletions components/InventoryItem/InventoryItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { View, Image } from "react-native";
import styles from "./styles";
import ThemedButton from "../common/ThemedButton";
import { ThemedText } from "../common/ThemedText";
import { INDIAN_RUPPEE_SYMBOL, DEFAULT_IMAGE } from "@/constants/common";

const InventoryItem = ({ item, onEdit }) => (
<ThemedButton style={styles.container} onPress={onEdit} type="secondary">
<Image
source={{
uri: item.image
? item.image
: "https://dummyimage.com/650x450/cc00cc/fff",
uri: item.image ? item.image : DEFAULT_IMAGE,
}}
style={styles.image}
/>
<View style={styles.infoContainer}>
<ThemedText style={styles.name}>{item.name}</ThemedText>
<ThemedText style={styles.price}>
{item.price + "/ " + item.unit}
{INDIAN_RUPPEE_SYMBOL}
{item.price + "/ " + item.unit}
</ThemedText>
<ThemedText style={styles.cuisine}>
{item.quantity + " " + item.unit}
Expand Down
3 changes: 2 additions & 1 deletion components/MenuItems/MenuItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ThemedView } from "@/components/common/ThemedView";
import { ThemedText } from "@/components/common/ThemedText";
import useResponsiveLayout from "@/hooks/useResponsiveLayout";
import ThemedButton from "@/components/common/ThemedButton";
import { DEFAULT_IMAGE } from "@/constants/common";

const MenuItems = ({ items, onEdit }) => {
const [refreshing, setRefreshing] = useState(false);
Expand All @@ -35,7 +36,7 @@ const MenuItems = ({ items, onEdit }) => {
<View style={styles.imageContainer}>
<Image
source={{
uri: image ? image : "https://dummyimage.com/650x450/cc00cc/fff",
uri: image ? image : DEFAULT_IMAGE,
}}
style={[styles.image, { width: layoutParams.itemWidth }]}
resizeMode="cover"
Expand Down
9 changes: 7 additions & 2 deletions components/OrderDetails/OrderDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ThemedView } from "../common/ThemedView";
import { ThemedText } from "../common/ThemedText";
import ThemedButton from "../common/ThemedButton";
import { generateUUID } from "@/utils/uuidGenerator";
import { INDIAN_RUPPEE_SYMBOL } from "@/constants/common";

const OrderDetails = ({
style,
Expand Down Expand Up @@ -35,7 +36,10 @@ const OrderDetails = ({
<ThemedView style={styles.orderItem}>
<ThemedView style={styles.itemDetailsContainer}>
<ThemedText style={styles.itemName}>{item.name}</ThemedText>
<ThemedText style={styles.itemPrice}>@ ₹ {item.price}</ThemedText>
<ThemedText style={styles.itemPrice}>
@ {INDIAN_RUPPEE_SYMBOL}
{item.price}
</ThemedText>
</ThemedView>
<ThemedView style={styles.quantityContainer}>
<ThemedView style={styles.quantityManipulator}>
Expand All @@ -54,7 +58,8 @@ const OrderDetails = ({
</ThemedButton>
</ThemedView>
<ThemedText style={styles.totalItemPrice}>
{(item.price * item.quantity).toFixed(2)}
{INDIAN_RUPPEE_SYMBOL}
{(item.price * item.quantity).toFixed(2)}
</ThemedText>
</ThemedView>
</ThemedView>
Expand Down
40 changes: 0 additions & 40 deletions components/Overview.js

This file was deleted.

176 changes: 176 additions & 0 deletions components/RestaurantOverview/Overview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import React, { useEffect, useState } from "react";
import { StyleSheet } from "react-native";
import { ThemedText } from "@/components/common/ThemedText";
import { ThemedView } from "@/components/common/ThemedView";
import { collection, query, onSnapshot } from "firebase/firestore";
import { db } from "@/firebase/firebaseConfig";
import { completedOrderPath } from "@/firebase/queries/completedOrder";
import { tablesPath } from "@/firebase/queries/tables";
import { INDIAN_RUPPEE_SYMBOL } from "@/constants/common";

const Overview = () => {
const [overviewItems, setOverviewItems] = useState([
{ title: "Active Tables", message: 0 },
{ title: "Active Orders", message: 0 },
{ title: "Closed Orders", message: 0 },
{ title: "Today's Revenue", message: 0 },
{ title: "Last 7 days rev", message: 0 },
{ title: "Last day rev", message: 0 },
]);

useEffect(() => {
const getCompletedOrderSummary = async () => {
try {
const completedOrdersRef = collection(db, completedOrderPath);
const q = query(completedOrdersRef);

const unsubscribe = onSnapshot(q, (querySnapshot) => {
let orderCountToday = 0;
let revenueToday = 0;
let revenueLastDay = 0;
let revenueLastWeek = 0;

// Set current day to midnight
const now = new Date();
const midnightToday = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate()
);

const lastDayStart = new Date(midnightToday);
lastDayStart.setDate(midnightToday.getDate() - 1);

const lastWeekStart = new Date(midnightToday);
lastWeekStart.setDate(midnightToday.getDate() - 7);

querySnapshot.docs.forEach((doc) => {
const data = doc.data();
const orderDate = data.bookingTime.toDate();

// Calculate order count and revenue for today
if (orderDate >= midnightToday) {
orderCountToday += data.totalOrders;
revenueToday += data.orderValue;
}

// Calculate revenue for the last day
if (orderDate >= lastDayStart && orderDate < midnightToday) {
revenueLastDay += data.orderValue;
}

// Calculate revenue for the last week
if (orderDate >= lastWeekStart && orderDate < midnightToday) {
revenueLastWeek += data.orderValue;
}
});

/// Update overview items using functional update
setOverviewItems((prevItems) =>
prevItems.map((item) => {
if (item.title === "Today's Revenue") {
return {
...item,
message: INDIAN_RUPPEE_SYMBOL + revenueToday,
};
} else if (item.title === "Closed Orders") {
return { ...item, message: orderCountToday };
} else if (item.title === "Last day rev") {
return {
...item,
message: INDIAN_RUPPEE_SYMBOL + revenueLastDay,
};
} else if (item.title === "Last 7 days rev") {
return {
...item,
message: INDIAN_RUPPEE_SYMBOL + revenueLastWeek,
};
}
return item;
})
);
});

// Clean up the listener on component unmount
return () => unsubscribe();
} catch (error) {
console.error("Error fetching completed order summary:", error);
}
};

const getActiveOrdersSummary = async () => {
try {
const tablesRef = collection(db, tablesPath);
const q = query(tablesRef);
let activeOrders = 0;
let itemsInKitchen = 0;

// Set up real-time listener
const unsubscribe = onSnapshot(q, (querySnapshot) => {
querySnapshot.docs.forEach((doc) => {
const data = doc.data();
if (data.status === "Occupied" && data.orders) {
activeOrders++;
data.orders.map((order) => {
if (order.status === "ACTIVE") itemsInKitchen++;
});
}
});

// Update overview items using functional update
setOverviewItems((prevItems) =>
prevItems.map((item) => {
if (item.title === "Active Tables") {
return { ...item, message: activeOrders };
} else if (item.title === "Active Orders") {
return { ...item, message: itemsInKitchen };
}
return item;
})
);
});
// Clean up the listener on component unmount
return () => unsubscribe();
} catch (error) {
console.error("Error fetching tables:", error);
}
};

getActiveOrdersSummary();
getCompletedOrderSummary();
}, []);

return (
<ThemedView style={styles.overviewContainer}>
{overviewItems.map((item) => (
<ThemedView key={item.title} style={styles.overviewItem}>
<ThemedText style={styles.itemTitle}>{item.title}</ThemedText>
<ThemedText style={styles.itemMessage}>{item.message}</ThemedText>
</ThemedView>
))}
</ThemedView>
);
};

const styles = StyleSheet.create({
overviewContainer: {
flexDirection: "row",
flexWrap: "wrap",
justifyContent: "space-around",
marginTop: 16,
},
overviewItem: {
width: "45%",
marginBottom: 16,
alignItems: "center",
},
itemTitle: {
fontWeight: "bold",
marginBottom: 4,
},
itemMessage: {
textAlign: "center",
},
});

export default Overview;
5 changes: 2 additions & 3 deletions components/StaffItem/StaffItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { View, Image } from "react-native";
import styles from "./styles";
import ThemedButton from "../common/ThemedButton";
import { ThemedText } from "../common/ThemedText";
import { DEFAULT_IMAGE } from "@/constants/common";

const StaffItem = ({ item, onEdit }) => (
<ThemedButton style={styles.container} onPress={onEdit} type="secondary">
<Image
source={{
uri: item.image
? item.image
: "https://dummyimage.com/650x450/cc00cc/fff",
uri: item.image ? item.image : DEFAULT_IMAGE,
}}
style={styles.image}
/>
Expand Down
14 changes: 11 additions & 3 deletions components/TableManagement/OrderDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ThemedView } from "@/components/common/ThemedView";
import React from "react";
import { StyleSheet, FlatList } from "react-native";
import { aggregateOrders, calculateOrderValue } from "@/utils/orderManagement";
import { INDIAN_RUPPEE_SYMBOL } from "@/constants/common";

const OrderDetails = ({ rawOrders }) => {
const orders = aggregateOrders(rawOrders);
Expand All @@ -12,9 +13,13 @@ const OrderDetails = ({ rawOrders }) => {
<ThemedView style={styles.orderItem}>
<ThemedText style={styles.itemName}>{item.name}</ThemedText>
<ThemedText style={styles.itemQuantity}>x{item.quantity}</ThemedText>
<ThemedText style={styles.itemPrice}>{item.price.toFixed(2)}</ThemedText>
<ThemedText style={styles.itemPrice}>
{item.itemValue.toFixed(2)}
{INDIAN_RUPPEE_SYMBOL}
{item.price.toFixed(2)}
</ThemedText>
<ThemedText style={styles.itemPrice}>
{INDIAN_RUPPEE_SYMBOL}
{item.itemValue.toFixed(2)}
</ThemedText>
</ThemedView>
);
Expand All @@ -29,7 +34,10 @@ const OrderDetails = ({ rawOrders }) => {
/>
<ThemedView style={styles.totalContainer}>
<ThemedText style={styles.totalText}>Total:</ThemedText>
<ThemedText style={styles.totalAmount}>{orderValue}</ThemedText>
<ThemedText style={styles.totalAmount}>
{INDIAN_RUPPEE_SYMBOL}
{orderValue}
</ThemedText>
</ThemedView>
</ThemedView>
);
Expand Down
4 changes: 3 additions & 1 deletion components/Tables/TableList.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getDarkBgColorWithTableStatus,
} from "@/utils/colorPicker";
import useResponsiveLayout from "@/hooks/useResponsiveLayout";
import { INDIAN_RUPPEE_SYMBOL } from "@/constants/common";

const TableList = ({ tables, onTablePress, onOrderDetailsPress }) => {
const { layoutParams, key } = useResponsiveLayout({
Expand Down Expand Up @@ -90,7 +91,8 @@ const TableList = ({ tables, onTablePress, onOrderDetailsPress }) => {
</ThemedText>
<ThemedText style={styles.tableDetails}>{tableWaitTime}</ThemedText>
<ThemedText style={styles.tableDetails}>
Bill: ₹ {orderValue || 0}
Bill: {INDIAN_RUPPEE_SYMBOL}
{orderValue || 0}
</ThemedText>
</View>
</View>
Expand Down
3 changes: 2 additions & 1 deletion components/UserProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from "react";
import { Image, StyleSheet } from "react-native";
import { ThemedText } from "./common/ThemedText";
import { ThemedView } from "./common/ThemedView";
import { DEFAULT_IMAGE } from "@/constants/common";

// Define the colors for different item types
const typeColors = {
Expand All @@ -18,7 +19,7 @@ const UserProfile = ({ name, role, imageUrl, plan }) => {
<ThemedView style={[styles.container, { backgroundColor: typeColor }]}>
<Image
source={{
uri: imageUrl || "https://dummyimage.com/650x450/cc00cc/fff",
uri: imageUrl || DEFAULT_IMAGE,
}}
style={styles.image}
/>
Expand Down
3 changes: 3 additions & 0 deletions constants/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const INDIAN_RUPPEE_SYMBOL = "₹";

export const DEFAULT_IMAGE = "https://dummyimage.com/650x450/cc00cc/fff";
2 changes: 1 addition & 1 deletion firebase/queries/completedOrder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { collection, writeBatch, doc } from "firebase/firestore";
import { db } from "@/firebase/firebaseConfig";

const completedOrderPath = "hotel-details/completed-orders/orders/";
export const completedOrderPath = "hotel-details/completed-orders/orders/";

export const addCompletedOrder = async (order) => {
const batch = writeBatch(db);
Expand Down
Loading

0 comments on commit 171a11f

Please sign in to comment.