From 423eaad521b302e99e672f50c15619954dbdb10e Mon Sep 17 00:00:00 2001 From: Nouman Tahir Date: Tue, 27 Apr 2021 00:53:18 +0500 Subject: [PATCH] handing empty view and pull to refresh in subscribed community tab --- .../view/subscribedCommunitiesListStyles.js | 5 + .../view/subscribedCommunitiesListView.js | 111 ++++++++++-------- .../container/communitiesContainer.js | 44 ++++--- .../communities/view/communitiesScreen.js | 4 + 4 files changed, 102 insertions(+), 62 deletions(-) diff --git a/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListStyles.js b/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListStyles.js index 7b3d00622..b1f42ffb0 100644 --- a/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListStyles.js +++ b/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListStyles.js @@ -48,4 +48,9 @@ export default EStyleSheet.create({ tabbarItem: { flex: 1, }, + noContentText: { + textAlign: 'center', + marginVertical: 32, + color: '$primaryBlack', + }, }); diff --git a/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js b/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js index 43b7aef8d..378f8597f 100644 --- a/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js +++ b/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js @@ -1,5 +1,12 @@ import React from 'react'; -import { View, FlatList, TouchableOpacity, Text, ActivityIndicator } from 'react-native'; +import { + View, + FlatList, + TouchableOpacity, + ActivityIndicator, + Text, + RefreshControl, +} from 'react-native'; import { useIntl } from 'react-intl'; import { Tag, UserAvatar } from '../../index'; @@ -8,73 +15,83 @@ import { ListPlaceHolder } from '../../basicUIElements'; import DEFAULT_IMAGE from '../../../assets/no_image.png'; import styles from './subscribedCommunitiesListStyles'; +import globalStyles from '../../../globalStyles'; const SubscribedCommunitiesListView = ({ data, subscribingCommunities, handleOnPress, handleSubscribeButtonPress, + handleGetSubscriptions, + isLoading, }) => { const intl = useIntl(); const _renderEmptyContent = () => { - return ( + return isLoading ? ( <> + ) : ( + + {intl.formatMessage({ id: 'empty_screen.nothing_here' })} + ); }; + const _renderListItem = ({ item, index }) => ( + + + handleOnPress(item[0])}> + + + handleOnPress(item[0])}> + {item[1]} + + + + {subscribingCommunities.hasOwnProperty(item[0]) && + subscribingCommunities[item[0]].loading ? ( + + + + ) : ( + + handleSubscribeButtonPress( + { + isSubscribed: item[4], + communityId: item[0], + }, + 'communitiesScreenJoinedTab', + ) + } + /> + )} + + + ); + return ( index.toString()} - renderItem={({ item, index }) => ( - - - handleOnPress(item[0])}> - - - handleOnPress(item[0])}> - {item[1]} - - - - {subscribingCommunities.hasOwnProperty(item[0]) && - subscribingCommunities[item[0]].loading ? ( - - - - ) : ( - - handleSubscribeButtonPress( - { - isSubscribed: item[4], - communityId: item[0], - }, - 'communitiesScreenJoinedTab', - ) - } - /> - )} - - - )} + renderItem={_renderListItem} ListEmptyComponent={_renderEmptyContent} + refreshControl={} /> ); }; diff --git a/src/screens/communities/container/communitiesContainer.js b/src/screens/communities/container/communitiesContainer.js index 7de2b79e7..7378029f8 100644 --- a/src/screens/communities/container/communitiesContainer.js +++ b/src/screens/communities/container/communitiesContainer.js @@ -17,6 +17,7 @@ const CommunitiesContainer = ({ children, navigation }) => { const [discovers, setDiscovers] = useState([]); const [subscriptions, setSubscriptions] = useState([]); + const [isSubscriptionsLoading, setIsSubscriptionsLoading] = useState(false); const currentAccount = useSelector((state) => state.account.currentAccount); const pinCode = useSelector((state) => state.application.pin); @@ -28,21 +29,7 @@ const CommunitiesContainer = ({ children, navigation }) => { ); useEffect(() => { - getSubscriptions(currentAccount.username).then((subs) => { - subs.forEach((item) => item.push(true)); - getCommunities('', 50, null, 'rank').then((communities) => { - communities.forEach((community) => - Object.assign(community, { - isSubscribed: subs.some( - (subscribedCommunity) => subscribedCommunity[0] === community.name, - ), - }), - ); - - setSubscriptions(subs); - setDiscovers(shuffle(communities)); - }); - }); + _getSubscriptions(); }, []); useEffect(() => { @@ -97,6 +84,31 @@ const CommunitiesContainer = ({ children, navigation }) => { setSubscriptions(subscribedsData); }, [subscribingCommunitiesInJoinedTab]); + const _getSubscriptions = () => { + setIsSubscriptionsLoading(true); + getSubscriptions(currentAccount.username) + .then((subs) => { + setIsSubscriptionsLoading(false); + subs.forEach((item) => item.push(true)); + getCommunities('', 50, null, 'rank').then((communities) => { + communities.forEach((community) => + Object.assign(community, { + isSubscribed: subs.some( + (subscribedCommunity) => subscribedCommunity[0] === community.name, + ), + }), + ); + + setSubscriptions(subs); + setDiscovers(shuffle(communities)); + }); + }) + .catch((err) => { + console.warn('Failed to get subscriptions', err); + setIsSubscriptionsLoading(false); + }); + }; + // Component Functions const _handleOnPress = (name) => { navigation.navigate({ @@ -146,6 +158,8 @@ const CommunitiesContainer = ({ children, navigation }) => { subscribingCommunitiesInJoinedTab, handleOnPress: _handleOnPress, handleSubscribeButtonPress: _handleSubscribeButtonPress, + handleGetSubscriptions: _getSubscriptions, + isSubscriptionsLoading, }) ); }; diff --git a/src/screens/communities/view/communitiesScreen.js b/src/screens/communities/view/communitiesScreen.js index 9c2896265..e2526247a 100644 --- a/src/screens/communities/view/communitiesScreen.js +++ b/src/screens/communities/view/communitiesScreen.js @@ -55,6 +55,8 @@ const CommunitiesScreen = ({ navigation, searchValue }) => { handleSubscribeButtonPress, subscribingCommunitiesInDiscoverTab, subscribingCommunitiesInJoinedTab, + handleGetSubscriptions, + isSubscriptionsLoading, }) => { return ( @@ -77,6 +79,8 @@ const CommunitiesScreen = ({ navigation, searchValue }) => { subscribingCommunities={subscribingCommunitiesInJoinedTab} handleSubscribeButtonPress={handleSubscribeButtonPress} handleOnPress={handleOnPress} + handleGetSubscriptions={handleGetSubscriptions} + isLoading={isSubscriptionsLoading} />