Skip to content

Commit

Permalink
made changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon Si committed Apr 8, 2020
1 parent 5c2a2ab commit 471d41e
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 62 deletions.
10 changes: 1 addition & 9 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const Drawer = createDrawerNavigator();
function DrawerContent(props) {
const {user, setUser} = useContext(UserContext);


return (
<DrawerContentScrollView {...props}>
<View style={{flex: 1, padding: 15}}>
Expand Down Expand Up @@ -89,15 +90,6 @@ export default function App() {
})();
}, []);

useEffect(() => {
(async() => {
const id = await SecureStore.getItemAsync('userId');
if (!id) return;
const response = await fetch(`https://grocerserver.herokuapp.com/users/${id}`);
const result = await response.json();
})();
}, []);

React.useEffect(() => {
isMountedRef.current = true;
return () => {isMountedRef.current = false};
Expand Down
90 changes: 60 additions & 30 deletions screens/GroceryListChecklistScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,63 @@ import {View, Text, FlatList, Image, StyleSheet, Alert} from 'react-native';
import SvgQRCode from 'react-native-qrcode-svg';
import {Button, CheckBox, ListItem, Overlay} from "react-native-elements";
import StackWrapperScreenOptions from "../constants/StackWrapperScreenOptions";
import {Notifications} from "expo";

export default function GroceryListChecklistScreen({navigation, route}) {
const groceryList = route.params;
const [groceryListItems, setGroceryListItems] = useState([]);
const [checkedItems, setCheckedItems] = useState({});
const [isQRCodeVisible, setQRCodeVisibility] = useState(false);

function handleNotification(notification) {
console.log(notification);
alert(`Order completed. You received ${groceryList.items.length} Grocer Point(s).`);
setQRCodeVisibility(false);
navigation.dangerouslyGetParent()?.dangerouslyGetParent()?.dangerouslyGetParent()?.setOptions(StackWrapperScreenOptions);
navigation.reset({
index: 0,
routes: [
{name: 'GroceryListSearch'}
]
});
}

useEffect(() => {
let orderedItems = [];
for (let [key, value] of Object.entries(groceryList.items)) {
let obj = value;
obj._id = key;
orderedItems.push(obj);
const listener = Notifications.addListener(handleNotification);
return () => {
console.log("Removed listener.");
listener.remove();
}
setGroceryListItems(orderedItems);
}, []);

return (
<View style={{flex: 1}}>
<FlatList
data={groceryListItems}
renderItem={({item}) => (
<View>
<ListItem
leftAvatar={{source: {uri: item.imageUrl}}}
rightElement={<CheckBox
checked={checkedItems[item._id]}
onPress={() => {
const newCheckedItems = {...checkedItems};
if (checkedItems[item._id])
delete newCheckedItems[item._id];
else
newCheckedItems[item._id] = true;
setCheckedItems(newCheckedItems);
}}
/>}
title={`${item.name} (${item.count?.toString()})`}
bottomDivider={true}
/>
</View>
)}
data={groceryList.items}
renderItem={({item: groceryItem}) => {
const item = groceryItem.item;
return (
<View>
<ListItem
leftAvatar={{source: {uri: item.imageUrl}}}
rightElement={
<CheckBox
checked={checkedItems[item._id]}
onPress={() => {
const newCheckedItems = {...checkedItems};
if (checkedItems[item._id])
delete newCheckedItems[item._id];
else
newCheckedItems[item._id] = true;
setCheckedItems(newCheckedItems);
}}
/>
}
title={`${item.name} (${groceryItem.count?.toString()})`}
bottomDivider={true}
/>
</View>
);
}}
keyExtractor={item => item._id}
/>
<View
Expand Down Expand Up @@ -86,9 +102,23 @@ export default function GroceryListChecklistScreen({navigation, route}) {
<Button
containerStyle={[styles.buttonContainer, {marginLeft: 5}]}
title="Complete"
disabled={Object.keys(checkedItems).length !== groceryListItems.length}
onPress={() => {
setQRCodeVisibility(true);
if (Object.keys(checkedItems).length !== groceryList.items.length) {
Alert.alert(
"Are you sure?",
"It looks like you haven't checked all the items off the" +
" grocery list. If certain items are out of stock, make sure" +
" to let the workers know!",
[
{text: "Finish", onPress: () => {
setQRCodeVisibility(true);
}},
{text: "Nevermind"}
]
);
} else {
setQRCodeVisibility(true);
}
}}
>
Complete
Expand Down
4 changes: 3 additions & 1 deletion screens/GroceryListSearchScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import GroceryListChecklistScreen from "./GroceryListChecklistScreen";
import {createStackNavigator} from "@react-navigation/stack";
import dateFormat from 'dateformat';
import UserContext from "../contexts/User";
import CurrentStoreContext from "../../client/contexts/CurrentStore";

function GroceryListSearch({navigation}) {
const [groceryLists, updateGroceryLists] = useState([]);
const [refreshing, setRefreshing] = useState(false);
const {user} = useContext(UserContext);
const {currentStore} = useContext(CurrentStoreContext);

useEffect(() => {
(async() => {
Expand All @@ -20,7 +22,7 @@ function GroceryListSearch({navigation}) {
}, []);

async function updateData() {
const listsUrl = 'https://grocerserver.herokuapp.com/lists';
const listsUrl = `https://grocerserver.herokuapp.com/lists?storeId=${currentStore.id}`;
setRefreshing(true);
const response = await fetch(listsUrl);
const result = await response.json();
Expand Down
8 changes: 6 additions & 2 deletions screens/GroceryListSummaryScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ export default function GroceryListSummaryScreen({navigation, route}) {

function handleNotification(notification) {
console.log("notification: ", notification);
if (notification.data.isActivation && notification.data.userId === user._id)
console.log(user._id);
if (notification.data?.isActivation && notification.data?.userId === user._id)
navigation.navigate('GroceryListChecklist', groceryList);
}

useEffect(() => {
const listener = Notifications.addListener(handleNotification);
return () => listener.remove();
return () => {
console.log("Removed listener.");
listener.remove();
}
}, []);

useEffect(() => {
Expand Down
4 changes: 4 additions & 0 deletions screens/ScannerScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ function QRCodeScanner() {
const completeUrl = 'https://grocerserver.herokuapp.com/lists/complete';
const response = await fetch(completeUrl, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({qrCode: data}),
});
const result = await response.json();
Expand Down
30 changes: 10 additions & 20 deletions screens/StoreSelectionScreen.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import React from "react";
import React, {useState, useEffect} from "react";
import {View, Dimensions, StyleSheet} from 'react-native';
import MapView from 'react-native-maps';
import StoreMarker from "../components/StoreMarker";

const storeLocations = [
{
name: "Walmart Toronto Downsview",
coords: {
latitude: 43.7577199,
longitude: -79.491159,
},
id: 0
},
{
name: "Metro Warden Ave",
coords: {
latitude: 43.797456,
longitude: -79.3200992,
},
id: 1
},
];

export default function StoreSelectionScreen() {
const [storeLocations, setStoreLocations] = useState([]);
useEffect(() => {
(async() => {
const response = await fetch('https://grocerserver.herokuapp.com/stores');
const result = await response.json();
setStoreLocations(result);
})();
}, []);

return (
<View>
<MapView
Expand Down
2 changes: 2 additions & 0 deletions screens/YourGroceryListScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ function YourGroceryListScreen({navigation}) {
}),
});
setLoading(false);
const result = await response.json();
if (response.status === 400) {
alert("Something went wrong while trying to process your order");
console.log(result);
return;
}
setGroceryList({});
Expand Down

0 comments on commit 471d41e

Please sign in to comment.