Skip to content

Commit

Permalink
made changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon Si committed Mar 31, 2020
1 parent 22c285e commit 362176d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 32 deletions.
5 changes: 1 addition & 4 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ export default function App() {
if (pushToken) return;

const {status} = await Permissions.askAsync(Permissions.NOTIFICATIONS);
if (status !== 'granted') {
alert('No notification permissions!');
return;
}
if (status !== 'granted') return;
let token = await Notifications.getExpoPushTokenAsync();
await SecureStore.setItemAsync('pushToken', token);
})();
Expand Down
13 changes: 13 additions & 0 deletions hooks/useDidUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {useRef, useEffect} from 'react';

export default function useDidUpdate(callback, deps) {
const hasMount = useRef(false);

useEffect(() => {
if (hasMount.current) {
callback()
} else {
hasMount.current = true;
}
}, deps)
}
75 changes: 48 additions & 27 deletions screens/GroceryItemSearchScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,42 @@ import {SearchBar} from 'react-native-elements';
import GroceryItemListing from '../components/GroceryItemListing';
import StackWrapper from "../navigation/StackWrapper";
import CurrentStoreContext from "../contexts/CurrentStore";
import useDidUpdate from "../hooks/useDidUpdate";

function GroceryItemSearchScreen({navigation, ...props}) {
const [loading, setLoading] = useState(true);
const [skipValue, setSkipValue] = useState(0);
const [searchValue, setSearchValue] = useState('');
const [items, updateItems] = useState([]);
const [endReached, setEndReached] = useState(false);
const {currentStore} = useContext(CurrentStoreContext);

async function fetchData() {
const response = await fetch(`https://grocerserver.herokuapp.com/items?storeId=${currentStore.id}&filter=${searchValue}&skip=${skipValue}&first=16`);
const result = await response.json();
setEndReached(result.length < 16);
updateItems([...items, ...result]);
async function fetchData(search, skip) {
const response = await fetch(`https://grocerserver.herokuapp.com/items?storeId=${currentStore.id}&filter=${search}&skip=${skip}&first=16`);
return await response.json();
}

useDidUpdate(() => {
(async () => {
if (skipValue === 0) return;
const result = await fetchData(searchValue, skipValue);
setEndReached(result.length < 16);
updateItems([...items, ...result]);
})();
}, [skipValue]);

useDidUpdate(() => {
setLoading(false);
}, [items]);

useEffect(() => {
(async () => {
await fetchData();
setLoading(true);
const result = await fetchData(searchValue, 0);
setSkipValue(0);
updateItems(result);
})();
}, [searchValue, skipValue]);
}, [currentStore.id, searchValue]);

function search(text) {
const searchVal = text;
Expand All @@ -45,26 +61,28 @@ function GroceryItemSearchScreen({navigation, ...props}) {
platform="ios"
containerStyle={{backgroundColor: 'white'}}
/>
<FlatList
data={items}
horizontal={false}
numColumns={2}
renderItem={({item}) => <GroceryItemListing
_id={item._id}
name={item.name}
rating={item.rating}
price={item.price}
imageUrl={item.imageUrl}
/>}
keyExtractor={(item) => item._id.toString()}
contentContainerStyle={styles.listContainer}
onEndReached={() => {
setSkipValue(skipValue + 16);
}}
onEndReachedThreshold={0.1}
initialNumToRender={16}
ListFooterComponent={endReached ? null : <ActivityIndicator size="large"/>}
/>
{loading ? <ActivityIndicator size="large"/> :
<FlatList
data={items}
horizontal={false}
numColumns={2}
renderItem={({item}) => <GroceryItemListing
_id={item._id}
name={item.name}
rating={item.rating}
price={item.price}
imageUrl={item.imageUrl}
/>}
keyExtractor={(item) => item._id.toString()}
contentContainerStyle={styles.listContainer}
onEndReached={() => {
setSkipValue(skipValue + 16);
}}
ListFooterComponent={endReached ? null :
<ActivityIndicator size="large" style={styles.loading}/>
}
/>
}
</SafeAreaView>
);
}
Expand All @@ -73,6 +91,9 @@ const styles = StyleSheet.create({
container: {
flex: 1
},
loading: {
margin: 20
}
});

export default StackWrapper(GroceryItemSearchScreen);
Expand Down
2 changes: 1 addition & 1 deletion screens/YourGroceryListScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function YourGroceryListScreen() {
},
body: JSON.stringify({
author: user?.firstName + ' ' + user?.lastName,
storeId: store.id,
storeId: currentStore.id,
createdAt: Date.now(),
qrCode: cuid(),
items: groceryList,
Expand Down

0 comments on commit 362176d

Please sign in to comment.