Skip to content

Commit

Permalink
Merge pull request #233 from anqi20/branch-release
Browse files Browse the repository at this point in the history
Branch release
  • Loading branch information
tsh22 authored Dec 23, 2021
2 parents f53d67b + 87ef4cf commit 25a7968
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 56 deletions.
15 changes: 10 additions & 5 deletions screens/HomeScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default function HomeScreen({ navigation }) {
<View style={styles.welcomeContainer}>
<Text style={styles.text}>Hi,</Text>
<Text style={[styles.boldText, { fontSize: 36 }]}>
{userData !== undefined ? userData.lastName : "Anonymous"}
{userData !== undefined ? userData.firstName : "Anonymous"}
</Text>
</View>
<View style={styles.coinsContainer}>
Expand Down Expand Up @@ -146,7 +146,13 @@ export default function HomeScreen({ navigation }) {
) : null}

{/* Icons */}
<View style={{ flexDirection: "row", justifyContent: "center", marginBottom: 32 }}>
<View
style={{
flexDirection: "row",
justifyContent: "center",
marginBottom: 32,
}}
>
<View style={{ alignItems: "center" }}>
<TouchableOpacity
style={[styles.topIconBox2]}
Expand Down Expand Up @@ -188,8 +194,8 @@ export default function HomeScreen({ navigation }) {
{renderCarouselView(updatedUserData, userData)}

{/* Quick navigation icons */}
<View style={styles.navigationIcons} >
<View>
<View style={styles.navigationIcons}>
<View>
{renderStatsIcon(updatedUserData, userData)}
<Text>My Stats</Text>
</View>
Expand Down Expand Up @@ -230,7 +236,6 @@ export default function HomeScreen({ navigation }) {
<Text style={styles.text}>Tutorial</Text>
</View>
</View>

</ScrollView>
</View>
);
Expand Down
8 changes: 4 additions & 4 deletions screens/borrow-screens/BorrowApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -96,13 +96,13 @@ function addCupDates(uid, cups, setError) {
userRef.set(
{
cupDate: [
...retrievedData,
{
time: currTime,
numCups: cups,
date: currDate,
dueDate: dueDate,
},
...retrievedData,
],
},
{ merge: true }
Expand All @@ -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()
Expand All @@ -145,13 +145,13 @@ function addContainerDates(uid, containers, setError) {
userRef.set(
{
containerDate: [
...retrievedData,
{
time: currTime,
numContainers: containers,
date: currDate,
dueDate: dueDate,
},
...retrievedData,
],
},
{ merge: true }
Expand Down
182 changes: 144 additions & 38 deletions screens/return-screens/ReturnApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 12 additions & 0 deletions screens/return-screens/ReturnQRScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ export default function ReturnQRScreen({ navigation }) {
>
<Text>Feature coming soon! Stay tuned {";)"}</Text>
</View>
{/* TEST VALUES */}
{/* <TouchableOpacity
onPress={() =>
navigation.navigate("Return Success Screen", {
numCups: 2,
numContainers: 2,
location: "E4",
})
}
>
<Text>Success Screen</Text>
</TouchableOpacity> */}
</View>
</View>
// <View style={{ marginTop: Constants.statusBarHeight }}>
Expand Down
8 changes: 4 additions & 4 deletions screens/return-screens/ReturnStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Stack = createStackNavigator();

export default function ReturnStack() {
return (
<Stack.Navigator>
<Stack.Navigator options={{ gestureEnabled: false }}>
<Stack.Screen
name="Return QR Screen"
component={ReturnQRScreen}
Expand All @@ -18,17 +18,17 @@ export default function ReturnStack() {
<Stack.Screen
name="Return Status Screen"
component={ReturnStatusScreen}
options={{ headerShown: false }}
options={{ headerShown: false, gestureEnabled: false }}
></Stack.Screen>
<Stack.Screen
name="Return Success Screen"
component={ReturnSuccessfulScreen}
options={{ headerShown: false }}
options={{ headerShown: false, gestureEnabled: false }}
></Stack.Screen>
<Stack.Screen
name="Return Unsuccess Screen"
component={ReturnUnsuccessfulScreen}
options={{ headerShown: false }}
options={{ headerShown: false, gestureEnabled: false }}
></Stack.Screen>
</Stack.Navigator>
);
Expand Down
5 changes: 5 additions & 0 deletions screens/return-screens/ReturnStatusScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand Down
15 changes: 10 additions & 5 deletions screens/return-screens/ReturnSuccessfulScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand Down
5 changes: 5 additions & 0 deletions screens/return-screens/ReturnUnsuccessfulScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<ScrollView style={styles.container} showsVerticalScrollIndicator={false}>
<Image
Expand Down

0 comments on commit 25a7968

Please sign in to comment.