Skip to content

Commit fcd9d3b

Browse files
author
Sanjeev Yadav
committed
feat: Create Spinner component
1 parent 416ed31 commit fcd9d3b

File tree

7 files changed

+62
-38
lines changed

7 files changed

+62
-38
lines changed

src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Navigator from './navigation';
55
import { ApolloClient, ApolloProvider } from '@apollo/client';
66
import { getApolloClient } from './apollo/client';
77
import { theme } from './theme';
8-
import { ActivityIndicator } from 'react-native';
8+
import { Spinner } from './components';
99

1010
const App = (): React.ReactElement => {
1111
const [client, setClient] = useState<ApolloClient<any>>();
@@ -29,7 +29,7 @@ const App = (): React.ReactElement => {
2929
}
3030

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

3535
export default App;

src/components/GenericTemplate/GenericTemplate.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import React, { useContext, ReactNode } from 'react';
2-
import {
3-
View,
4-
StyleSheet,
5-
ScrollView,
6-
ActivityIndicator,
7-
ViewStyle,
8-
} from 'react-native';
9-
import { ThemeContext } from 'react-native-elements';
10-
import { SPACING } from '../../constants';
1+
import React, { ReactNode } from 'react';
2+
import { View, StyleSheet, ScrollView, ViewStyle } from 'react-native';
113
import { Text } from 'react-native-elements';
4+
import { SPACING } from '../../constants';
5+
import Spinner from '../Spinner/Spinner';
126

137
interface Props {
148
/**
@@ -45,13 +39,12 @@ const GenericTemplate = ({
4539
errorMessage,
4640
style = {},
4741
}: Props): React.ReactElement => {
48-
const { theme } = useContext(ThemeContext);
4942
const ViewGroup = scrollable ? ScrollView : View;
5043

5144
const renderLoader = () =>
5245
loading && (
5346
<View style={styles.center}>
54-
<ActivityIndicator size="large" color={theme.colors?.primary} />
47+
<Spinner />
5548
</View>
5649
);
5750

src/components/Spinner/Spinner.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { useContext } from 'react';
2+
import { ActivityIndicator, ViewStyle } from 'react-native';
3+
import { ThemeContext } from 'react-native-elements';
4+
5+
interface Props {
6+
/**
7+
* size of the spinner, can be
8+
* 1. 'large'
9+
* 2. 'small'
10+
*/
11+
size?: 'large' | 'small';
12+
/**
13+
* custom color for the spinner
14+
*/
15+
color?: string;
16+
/**
17+
* style containing padding & margin
18+
*/
19+
style?: ViewStyle;
20+
}
21+
22+
const Spinner = ({
23+
size = 'large',
24+
color,
25+
style = {},
26+
}: Props): React.ReactElement => {
27+
const { theme } = useContext(ThemeContext);
28+
return (
29+
<ActivityIndicator
30+
style={style}
31+
size={size}
32+
color={color ?? theme.colors?.primary}
33+
/>
34+
);
35+
};
36+
37+
export default Spinner;

src/components/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import GenericTemplate from './GenericTemplate/GenericTemplate';
77
import MediaGallery from './MediaGallery/MediaGallery';
88
import ProductListItem from './ProductListItem/ProductListItem';
9+
import Spinner from './Spinner/Spinner';
910

1011
export {
1112
CategoryListItem,
@@ -14,4 +15,5 @@ export {
1415
GenericTemplate,
1516
MediaGallery,
1617
ProductListItem,
18+
Spinner,
1719
};

src/screens/HomeScreen/FeaturedProductList.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
22
import { useNavigation } from '@react-navigation/native';
3-
import { ActivityIndicator, View, FlatList, StyleSheet } from 'react-native';
3+
import { View, FlatList, StyleSheet } from 'react-native';
44
import { Text } from 'react-native-elements';
55
import { ProductInListType } from '../../apollo/queries/productsFragment';
6-
import { ProductListItem } from '../../components';
6+
import { ProductListItem, Spinner } from '../../components';
77
import { DIMENS, SPACING } from '../../constants';
88
import { useCategoryProducts } from '../../logic';
99
import { Routes } from '../../navigation';
@@ -55,11 +55,10 @@ const FeaturedProductList = ({
5555
);
5656
}
5757

58-
// TODO: Remove hard-coded values
5958
if (networkStatus === NetworkStatus.loading) {
6059
return (
6160
<View style={styles.loadingBox}>
62-
<ActivityIndicator color="black" size="large" />
61+
<Spinner />
6362
</View>
6463
);
6564
}

src/screens/ProductListScreen/ProductListScreen.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import React from 'react';
2-
import {
3-
View,
4-
RefreshControl,
5-
StyleSheet,
6-
ActivityIndicator,
7-
FlatList,
8-
} from 'react-native';
2+
import { View, RefreshControl, StyleSheet, FlatList } from 'react-native';
93
import { RouteProp } from '@react-navigation/native';
104
import { StackNavigationProp } from '@react-navigation/stack';
115
import { useCategoryProducts } from '../../logic';
126
import { Routes, AppStackParamList } from '../../navigation';
137
import { ProductInListType } from '../../apollo/queries/productsFragment';
148
import { SPACING } from '../../constants';
15-
import { GenericTemplate, ProductListItem } from '../../components';
9+
import { GenericTemplate, ProductListItem, Spinner } from '../../components';
1610
import { NetworkStatus } from '@apollo/client';
1711

1812
interface Props {
@@ -60,11 +54,10 @@ const ProductListScreen = ({
6054
);
6155
};
6256

63-
// TODO: remove hard-coded values
6457
const renderFooterComponent = () =>
6558
(networkStatus === NetworkStatus.fetchMore && (
6659
<View style={styles.footerContainer}>
67-
<ActivityIndicator color="black" size="large" />
60+
<Spinner />
6861
</View>
6962
)) || <></>;
7063

src/screens/SearchScreen/SearchScreen.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React from 'react';
2-
import { StyleSheet, FlatList, View, ActivityIndicator } from 'react-native';
3-
import { Text, SearchBar } from 'react-native-elements';
1+
import React, { useContext, useMemo } from 'react';
2+
import { StyleSheet, FlatList, View } from 'react-native';
3+
import { Text, SearchBar, ThemeContext } from 'react-native-elements';
44
import { StackNavigationProp } from '@react-navigation/stack';
5-
import { GenericTemplate, ProductListItem } from '../../components';
5+
import { GenericTemplate, ProductListItem, Spinner } from '../../components';
66
import { useSearch } from '../../logic';
77
import { AppStackParamList, Routes } from '../../navigation';
88
import { ProductInListType } from '../../apollo/queries/productsFragment';
@@ -26,6 +26,10 @@ const SearchScreen = ({ navigation }: Props): React.ReactElement => {
2626
loadMore,
2727
data: { products: { items: products = [] } = {} } = {},
2828
} = useSearch();
29+
const { theme } = useContext(ThemeContext);
30+
const loadingProps = useMemo(() => ({ color: theme.colors?.primary }), [
31+
theme,
32+
]);
2933

3034
const handleBackPress = () => navigation.pop();
3135

@@ -66,11 +70,10 @@ const SearchScreen = ({ navigation }: Props): React.ReactElement => {
6670
</View>
6771
)) || <></>;
6872

69-
// TODO: Remove hard-coded values
7073
const renderFooterComponent = () =>
7174
(networkStatus === NetworkStatus.fetchMore && (
7275
<View style={styles.footerContainer}>
73-
<ActivityIndicator color="black" size="large" />
76+
<Spinner />
7477
</View>
7578
)) || <></>;
7679

@@ -85,7 +88,7 @@ const SearchScreen = ({ navigation }: Props): React.ReactElement => {
8588
name: 'arrow-back',
8689
onPress: handleBackPress,
8790
}}
88-
loadingProps={styles.searchBarLoading}
91+
loadingProps={loadingProps}
8992
containerStyle={styles.searchBarContainer}
9093
inputContainerStyle={styles.searchBarInputContainer}
9194
/>
@@ -130,9 +133,6 @@ const styles = StyleSheet.create({
130133
borderRadius: 0,
131134
backgroundColor: 'white',
132135
},
133-
searchBarLoading: {
134-
color: 'black',
135-
},
136136
footerContainer: {
137137
alignItems: 'center',
138138
marginVertical: SPACING.small,

0 commit comments

Comments
 (0)