diff --git a/screens/HomeScreen.js b/screens/HomeScreen.js index 9824b1b..56dd916 100644 --- a/screens/HomeScreen.js +++ b/screens/HomeScreen.js @@ -118,7 +118,7 @@ export default function HomeScreen({ navigation }) { Hi, - {userData !== undefined ? userData.lastName : "Anonymous"} + {userData !== undefined ? userData.firstName : "Anonymous"} @@ -146,7 +146,13 @@ export default function HomeScreen({ navigation }) { ) : null} {/* Icons */} - + - + + {renderStatsIcon(updatedUserData, userData)} My Stats @@ -230,7 +236,6 @@ export default function HomeScreen({ navigation }) { Tutorial - ); diff --git a/screens/borrow-screens/BorrowApi.js b/screens/borrow-screens/BorrowApi.js index 1d0a4d1..85470ff 100644 --- a/screens/borrow-screens/BorrowApi.js +++ b/screens/borrow-screens/BorrowApi.js @@ -72,7 +72,7 @@ export function getBorrowedNum(uid, setBorrowedCup, setBorrowedContainer) { function addCupDates(uid, cups, setError) { const currDate = moment().format("DD/MM/YYYY"); const dueDate = moment().add(7, "d").format("DD/MM/YYYY"); // Due date is 7 days away from borrow date - const currTime = moment().format("hh:mm a"); + const currTime = moment().format("hh:mm:ss a"); const userRef = firebase.firestore().collection("users").doc(uid); userRef .get() @@ -96,13 +96,13 @@ function addCupDates(uid, cups, setError) { userRef.set( { cupDate: [ + ...retrievedData, { time: currTime, numCups: cups, date: currDate, dueDate: dueDate, }, - ...retrievedData, ], }, { merge: true } @@ -121,7 +121,7 @@ function addCupDates(uid, cups, setError) { function addContainerDates(uid, containers, setError) { const currDate = moment().format("DD/MM/YYYY"); const dueDate = moment().add(7, "d").format("DD/MM/YYYY"); // Due date is 7 days away from borrow date - const currTime = moment().format("hh:mm a"); + const currTime = moment().format("hh:mm:ss a"); const userRef = firebase.firestore().collection("users").doc(uid); userRef .get() @@ -145,13 +145,13 @@ function addContainerDates(uid, containers, setError) { userRef.set( { containerDate: [ + ...retrievedData, { time: currTime, numContainers: containers, date: currDate, dueDate: dueDate, }, - ...retrievedData, ], }, { merge: true } diff --git a/screens/return-screens/ReturnApi.js b/screens/return-screens/ReturnApi.js index 4adedd2..d9a4bad 100644 --- a/screens/return-screens/ReturnApi.js +++ b/screens/return-screens/ReturnApi.js @@ -72,48 +72,154 @@ export function awardWelcomeGift() { } } -// Get and manipulate return date data +// Get and manipulate return date data for cups +// Update dates, cupReturned, numCup fields function handleCupReturn(uid, numCups, setError) { - return null; - // const userRef = firebase.firestore().collection("users").doc(uid); - // userRef - // .get() - // .then((document) => { - // if (document.exists) { - // const borrowData = document.data().cupDate; - // console.log(`Overall borrow data in cup return loop ${borrowData}`); - // // If there are still returned cups to subtract from borrow data + const userRef = firebase.firestore().collection("users").doc(uid); + userRef + .get() + .then((document) => { + if (document.exists) { + const borrowData = document.data().cupDate; + + // Update total numCup field + userRef.update({ + numCup: firebase.firestore.FieldValue.increment(-numCups), + }); + + // Update cupReturned field + userRef.update({ + cupReturned: firebase.firestore.FieldValue.increment(numCups), + }); + + // console.log("Overall borrow data in cup return loop:"); + // console.log(borrowData); + // If there are still returned cups to subtract from borrow data + while (numCups > 0) { + const currData = borrowData.shift(); + // console.log("Current data in cup return loop:"); + // console.log(currData); + if (currData.numCups <= numCups) { + // If there are equal/more cups returned than in currData, remove entire entry + console.log("Removing entire current entry"); + userRef.update({ + cupDate: firebase.firestore.FieldValue.arrayRemove({ + date: currData.date, + time: currData.time, + dueDate: currData.dueDate, + numCups: currData.numCups, + }), + }); + numCups -= currData.numCups; + } else { + // If there are less cups returned than in currData, just update numCups field + borrowData.shift(); // Removing the first entry in the overall borrow data + // console.log("Remaining data:"); + // console.log(borrowData); + console.log("Updating numCups field in current entry"); + userRef.set( + { + cupDate: [ + { + date: currData.date, + time: currData.time, + dueDate: currData.dueDate, + numCups: currData.numCups - numCups, + }, + ...borrowData, + ], + }, + { merge: true } + ); + numCups = 0; + } + } + } else { + console.log("No such document"); + } + }) + .catch((error) => { + console.log("Error handling cup return data: ", error); + // setError(true); + }); +} + +// Get and manipulate return date data for containers +// Update dates, containerReturned, numContainer fields +function handleContainerReturn(uid, numContainers, setError) { + const userRef = firebase.firestore().collection("users").doc(uid); + userRef + .get() + .then((document) => { + if (document.exists) { + const borrowData = document.data().containerDate; + + // Update total numContainer field + userRef.update({ + numContainer: firebase.firestore.FieldValue.increment(-numContainers), + }); - // while (numCups > 0) { - // const currData = borrowData[0]; - // console.log(`Current data in cup return loop ${currData}`); - // if (currData.numCups <= numCups) { - // // If there are equal/more cups returned than in currData, remove entire entry + // Update containerReturned field + userRef.update({ + containerReturned: + firebase.firestore.FieldValue.increment(numContainers), + }); - // } else { - // // If there are less cups returned than in currData, just update numCups field - // userRef.set({ - // cupDate: [{ - // time: currData.time, - // numCups: currData.numCups - numCups, - // date: currData.date, - // }, ...] - // }); - // } - // } - // } else { - // console.log("No such document"); - // } - // }) - // .catch((error) => { - // console.log("Error handling cup return data: ", error); - // // setError(true); - // }); + // console.log("Overall borrow data in container return loop:"); + // console.log(borrowData); + // If there are still returned cups to subtract from borrow data + while (numContainers > 0) { + // console.log("Overall borrow data in container return loop:"); + // console.log(borrowData); + const currData = borrowData.shift(); + // console.log("Current data in container return loop:"); + // console.log(currData); + if (currData.numContainers <= numContainers) { + // If there are equal/more containers returned than in currData, remove entire entry + console.log("Removing entire current entry"); + userRef.update({ + containerDate: firebase.firestore.FieldValue.arrayRemove({ + date: currData.date, + time: currData.time, + dueDate: currData.dueDate, + numContainers: currData.numContainers, + }), + }); + numContainers -= currData.numContainers; + } else { + // If there are less containers returned than in currData, just update numContainers field + borrowData.shift(); // Removing the first entry in the overall borrow data + // console.log("Remaining data:"); + // console.log(borrowData); + // console.log("Updating numCups field in current entry"); + userRef.set( + { + containerDate: [ + { + date: currData.date, + time: currData.time, + dueDate: currData.dueDate, + numContainers: currData.numContainers - numContainers, + }, + ...borrowData, + ], + }, + { merge: true } + ); + numContainers = 0; + } + } + } else { + console.log("No such document"); + } + }) + .catch((error) => { + console.log("Error handling container return data: ", error); + // setError(true); + }); } export function updateReturnData(uid, numCups, numContainers, setError) { - // Reduce numContainer - // Reduce numCup - // Get rid of dates that have already been returned - return null; + handleCupReturn(uid, numCups, setError); + handleContainerReturn(uid, numContainers, setError); } diff --git a/screens/return-screens/ReturnQRScreen.js b/screens/return-screens/ReturnQRScreen.js index b78040d..9cc8cbe 100644 --- a/screens/return-screens/ReturnQRScreen.js +++ b/screens/return-screens/ReturnQRScreen.js @@ -76,6 +76,18 @@ export default function ReturnQRScreen({ navigation }) { > Feature coming soon! Stay tuned {";)"} + {/* TEST VALUES */} + {/* + navigation.navigate("Return Success Screen", { + numCups: 2, + numContainers: 2, + location: "E4", + }) + } + > + Success Screen + */} // diff --git a/screens/return-screens/ReturnStack.js b/screens/return-screens/ReturnStack.js index 49dacac..93bb638 100644 --- a/screens/return-screens/ReturnStack.js +++ b/screens/return-screens/ReturnStack.js @@ -9,7 +9,7 @@ const Stack = createStackNavigator(); export default function ReturnStack() { return ( - + ); diff --git a/screens/return-screens/ReturnStatusScreen.js b/screens/return-screens/ReturnStatusScreen.js index 4961937..296797f 100644 --- a/screens/return-screens/ReturnStatusScreen.js +++ b/screens/return-screens/ReturnStatusScreen.js @@ -13,6 +13,8 @@ import FooterText from "../../components/FooterText"; import { Icon } from "react-native-elements"; import { UserContext } from "../../assets/UserContext"; import { getBorrowedNum } from "./ReturnApi"; +import { useBackHandler } from "@react-native-community/hooks"; +import { backActionHandler } from "../BasicApi"; export default function ReturnStatusScreen({ navigation }) { const userData = useContext(UserContext); @@ -23,6 +25,9 @@ export default function ReturnStatusScreen({ navigation }) { const [borrowedCup, setBorrowedCup] = useState(0); const [borrowedContainer, setBorrowedContainer] = useState(0); + // Prevent back button action on Android + useBackHandler(backActionHandler); + useEffect(() => { getBorrowedNum(uid, setBorrowedCup, setBorrowedContainer); console.log("Setting up current user's borrowed items number"); diff --git a/screens/return-screens/ReturnSuccessfulScreen.js b/screens/return-screens/ReturnSuccessfulScreen.js index d482e26..b74e117 100644 --- a/screens/return-screens/ReturnSuccessfulScreen.js +++ b/screens/return-screens/ReturnSuccessfulScreen.js @@ -6,6 +6,8 @@ import { globalStyles } from "../../assets/globalStyles"; import FooterText from "../../components/FooterText"; import { UserContext } from "../../assets/UserContext"; import { updateCoins, updateReturnData } from "./ReturnApi"; +import { useBackHandler } from "@react-native-community/hooks"; +import { backActionHandler } from "../BasicApi"; export default function ReturnSuccessfulScreen({ route }) { const userData = useContext(UserContext); @@ -16,11 +18,14 @@ export default function ReturnSuccessfulScreen({ route }) { // Can change the value of coins earned accordingly const coinsEarned = numCups + numContainers; - // useEffect(() => { - // updateCoins(uid, coinsEarned); - // awardWelcomeGift(); // Only decreases welcomeThreshold when eligible - // updateReturnData(uid, numCups, numContainers, setError); - // }, []); + // Prevent back button action on Android + useBackHandler(backActionHandler); + + useEffect(() => { + updateCoins(uid, coinsEarned); + // awardWelcomeGift(); // Only decreases welcomeThreshold when eligible + updateReturnData(uid, numCups, numContainers, setError); + }, []); if (hasError) { navigation.navigate("Return Unsuccess Screen"); diff --git a/screens/return-screens/ReturnUnsuccessfulScreen.js b/screens/return-screens/ReturnUnsuccessfulScreen.js index eb18cc2..faa67ba 100644 --- a/screens/return-screens/ReturnUnsuccessfulScreen.js +++ b/screens/return-screens/ReturnUnsuccessfulScreen.js @@ -3,8 +3,13 @@ import { StyleSheet, Text, ScrollView, Image, Dimensions } from "react-native"; import UnsuccessBox from "../../components/UnsuccessBox"; import colors from "../../assets/colors"; import { globalStyles } from "../../assets/globalStyles"; +import { useBackHandler } from "@react-native-community/hooks"; +import { backActionHandler } from "../BasicApi"; export default function ReturnUnsuccessfulScreen() { + // Prevent back button action on Android + useBackHandler(backActionHandler); + return (