Skip to content

Commit 416ed31

Browse files
author
Sanjeev Yadav
committed
refactor: Use networkStatus to show different loading states
1 parent 71e8e4d commit 416ed31

File tree

6 files changed

+51
-33
lines changed

6 files changed

+51
-33
lines changed

src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const App = (): React.ReactElement => {
2929
}
3030

3131
// TODO: SplashScreen logic
32-
return <ActivityIndicator size="large" />;
32+
return <ActivityIndicator color="black" size="large" />;
3333
};
3434

3535
export default App;

src/logic/products/useCategoryProducts.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from 'react';
2-
import { useQuery, ApolloError } from '@apollo/client';
2+
import { useQuery, ApolloError, NetworkStatus } from '@apollo/client';
33
import {
44
GET_CATGEORY_PRODUCTS,
55
GetCategoryProductsVars,
@@ -13,17 +13,16 @@ interface Props {
1313

1414
interface Result {
1515
data: CategoryProductsDataType | undefined;
16-
loading: boolean;
16+
networkStatus: NetworkStatus;
1717
error: ApolloError | undefined;
18-
currentPage: number;
1918
refresh(): void;
2019
loadMore(): void;
2120
}
2221

2322
export const useCategoryProducts = ({ categoryId: id }: Props): Result => {
2423
const [currentPage, setCurrentPage] = useState<number>(1);
2524

26-
const { refetch, data, loading, error, fetchMore } = useQuery<
25+
const { refetch, loading, data, error, fetchMore, networkStatus } = useQuery<
2726
CategoryProductsDataType,
2827
GetCategoryProductsVars
2928
>(GET_CATGEORY_PRODUCTS, {
@@ -32,6 +31,7 @@ export const useCategoryProducts = ({ categoryId: id }: Props): Result => {
3231
pageSize: LIMITS.categoryProductsPageSize,
3332
currentPage: 1,
3433
},
34+
notifyOnNetworkStatusChange: true,
3535
});
3636

3737
useEffect(() => {
@@ -65,9 +65,8 @@ export const useCategoryProducts = ({ categoryId: id }: Props): Result => {
6565

6666
return {
6767
data,
68-
loading,
68+
networkStatus,
6969
error,
70-
currentPage,
7170
refresh,
7271
loadMore,
7372
};

src/logic/products/useSearch.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState, useEffect } from 'react';
2-
import { useLazyQuery } from '@apollo/client';
2+
import { NetworkStatus, useLazyQuery } from '@apollo/client';
33
import {
44
GetSearchProductsVars,
55
GET_SEARCH_PRODUCTS,
@@ -9,7 +9,7 @@ import { LIMITS } from '../../constants';
99

1010
interface Result {
1111
data: SearchProductsDataType | undefined;
12-
loading: boolean;
12+
networkStatus: NetworkStatus;
1313
called: boolean;
1414
searchText: string;
1515
handleChange(arg1: string): void;
@@ -22,9 +22,12 @@ export const useSearch = (): Result => {
2222
const [currentPage, setCurrentPage] = useState<number>(1);
2323
const [
2424
getSearchProducts,
25-
{ called, loading, error, fetchMore, data },
25+
{ called, loading, error, networkStatus, fetchMore, data },
2626
] = useLazyQuery<SearchProductsDataType, GetSearchProductsVars>(
2727
GET_SEARCH_PRODUCTS,
28+
{
29+
notifyOnNetworkStatusChange: true,
30+
},
2831
);
2932

3033
useEffect(() => {
@@ -74,7 +77,7 @@ export const useSearch = (): Result => {
7477

7578
return {
7679
data,
77-
loading,
80+
networkStatus,
7881
called,
7982
searchText,
8083
loadMore,

src/screens/HomeScreen/FeaturedProductList.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { ActivityIndicator, View, FlatList, StyleSheet } from 'react-native';
44
import { Text } from 'react-native-elements';
55
import { ProductInListType } from '../../apollo/queries/productsFragment';
66
import { ProductListItem } from '../../components';
7-
import { SPACING } from '../../constants';
7+
import { DIMENS, SPACING } from '../../constants';
88
import { useCategoryProducts } from '../../logic';
99
import { Routes } from '../../navigation';
10+
import { NetworkStatus } from '@apollo/client';
1011

1112
type Props = {
1213
name?: string;
@@ -17,7 +18,7 @@ const FeaturedProductList = ({
1718
name,
1819
categoryId,
1920
}: Props): React.ReactElement => {
20-
const { data, loading, error } = useCategoryProducts({ categoryId });
21+
const { data, networkStatus, error } = useCategoryProducts({ categoryId });
2122
const navigation = useNavigation();
2223

2324
const onProductItemClicked = (index: number) => {
@@ -54,8 +55,13 @@ const FeaturedProductList = ({
5455
);
5556
}
5657

57-
if (loading) {
58-
return <ActivityIndicator size="large" />;
58+
// TODO: Remove hard-coded values
59+
if (networkStatus === NetworkStatus.loading) {
60+
return (
61+
<View style={styles.loadingBox}>
62+
<ActivityIndicator color="black" size="large" />
63+
</View>
64+
);
5965
}
6066

6167
return (
@@ -87,6 +93,14 @@ const styles = StyleSheet.create({
8793
paddingVertical: SPACING.small,
8894
fontSize: 16,
8995
},
96+
loadingBox: {
97+
alignContent: 'center',
98+
justifyContent: 'center',
99+
width: '100%',
100+
backgroundColor: '#fff',
101+
marginBottom: SPACING.large,
102+
height: (DIMENS.common.WINDOW_WIDTH / 3) * 2, // This is linked to ProductListItem height
103+
},
90104
});
91105

92106
export default FeaturedProductList;

src/screens/ProductListScreen/ProductListScreen.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Routes, AppStackParamList } from '../../navigation';
1313
import { ProductInListType } from '../../apollo/queries/productsFragment';
1414
import { SPACING } from '../../constants';
1515
import { GenericTemplate, ProductListItem } from '../../components';
16+
import { NetworkStatus } from '@apollo/client';
1617

1718
interface Props {
1819
navigation: StackNavigationProp<
@@ -28,16 +29,11 @@ const ProductListScreen = ({
2829
params: { categoryId },
2930
},
3031
}: Props): React.ReactElement => {
31-
const {
32-
data,
33-
loading,
34-
currentPage,
35-
error,
36-
refresh,
37-
loadMore,
38-
} = useCategoryProducts({
39-
categoryId,
40-
});
32+
const { data, networkStatus, error, refresh, loadMore } = useCategoryProducts(
33+
{
34+
categoryId,
35+
},
36+
);
4137

4238
const onProductItemClicked = (index: number) => {
4339
if (data?.products?.items) {
@@ -64,10 +60,11 @@ const ProductListScreen = ({
6460
);
6561
};
6662

63+
// TODO: remove hard-coded values
6764
const renderFooterComponent = () =>
68-
(loading && data?.products?.items?.length !== 0 && (
65+
(networkStatus === NetworkStatus.fetchMore && (
6966
<View style={styles.footerContainer}>
70-
<ActivityIndicator size="large" />
67+
<ActivityIndicator color="black" size="large" />
7168
</View>
7269
)) || <></>;
7370

@@ -80,7 +77,10 @@ const ProductListScreen = ({
8077
keyExtractor={item => `productListItem${item.sku}`}
8178
refreshControl={
8279
<RefreshControl
83-
refreshing={loading && currentPage === 1}
80+
refreshing={
81+
networkStatus === NetworkStatus.refetch ||
82+
networkStatus === NetworkStatus.loading
83+
}
8484
onRefresh={refresh}
8585
/>
8686
}

src/screens/SearchScreen/SearchScreen.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { AppStackParamList, Routes } from '../../navigation';
88
import { ProductInListType } from '../../apollo/queries/productsFragment';
99
import { translate } from '../../i18n';
1010
import { LIMITS, SPACING } from '../../constants';
11+
import { NetworkStatus } from '@apollo/client';
1112

1213
type Props = {
1314
navigation: StackNavigationProp<
@@ -20,7 +21,7 @@ const SearchScreen = ({ navigation }: Props): React.ReactElement => {
2021
const {
2122
searchText,
2223
handleChange,
23-
loading,
24+
networkStatus,
2425
called,
2526
loadMore,
2627
data: { products: { items: products = [] } = {} } = {},
@@ -57,18 +58,19 @@ const SearchScreen = ({ navigation }: Props): React.ReactElement => {
5758
(searchText.length >= LIMITS.searchTextMinLength &&
5859
products.length === 0 &&
5960
called &&
60-
!loading && (
61+
networkStatus !== NetworkStatus.loading && (
6162
<View style={styles.center}>
6263
<Text>
6364
{translate('searchScreen.noProductsFound', { searchText })}
6465
</Text>
6566
</View>
6667
)) || <></>;
6768

69+
// TODO: Remove hard-coded values
6870
const renderFooterComponent = () =>
69-
(loading && products.length !== 0 && (
71+
(networkStatus === NetworkStatus.fetchMore && (
7072
<View style={styles.footerContainer}>
71-
<ActivityIndicator size="large" />
73+
<ActivityIndicator color="black" size="large" />
7274
</View>
7375
)) || <></>;
7476

@@ -78,7 +80,7 @@ const SearchScreen = ({ navigation }: Props): React.ReactElement => {
7880
placeholder={translate('searchScreen.searchBarHint')}
7981
onChangeText={handleChange}
8082
value={searchText}
81-
showLoading={loading}
83+
showLoading={networkStatus === NetworkStatus.loading}
8284
searchIcon={{
8385
name: 'arrow-back',
8486
onPress: handleBackPress,

0 commit comments

Comments
 (0)