diff --git a/src/components/editorElements/selectCommunityModal/container/selectCommunityModalContainer.js b/src/components/editorElements/selectCommunityModal/container/selectCommunityModalContainer.js index 28f71cc56..4370c2822 100644 --- a/src/components/editorElements/selectCommunityModal/container/selectCommunityModalContainer.js +++ b/src/components/editorElements/selectCommunityModal/container/selectCommunityModalContainer.js @@ -3,16 +3,14 @@ import { useSelector, useDispatch } from 'react-redux'; import { debounce } from 'lodash'; // HIVE -import { getCommunities, getSubscriptions } from '../../../../providers/hive/dhive'; +import { getCommunities } from '../../../../providers/hive/dhive'; import SelectCommunityModalView from '../view/selectCommunityModalView'; // Actions import { fetchCommunities, - fetchCommunitiesSuccess, fetchSubscribedCommunities, - fetchSubscribedCommunitiesSuccess, } from '../../../../redux/actions/communitiesAction'; import { mergeSubCommunitiesCacheInSubList } from '../../../../utils/communitiesUtils'; diff --git a/src/screens/community/container/communityContainer.js b/src/screens/community/container/communityContainer.js index 98cc9bacd..05a8495f4 100644 --- a/src/screens/community/container/communityContainer.js +++ b/src/screens/community/container/communityContainer.js @@ -4,7 +4,7 @@ import get from 'lodash/get'; import { connect, useDispatch, useSelector } from 'react-redux'; import { useIntl } from 'react-intl'; -import { getCommunity, getSubscriptions } from '../../../providers/hive/dhive'; +import { getCommunity } from '../../../providers/hive/dhive'; import { subscribeCommunity, leaveCommunity } from '../../../redux/actions/communitiesAction'; @@ -25,6 +25,7 @@ const CommunityContainer = ({ children, navigation, currentAccount, pinCode, isL (state) => state.communities.subscribingCommunitiesInCommunitiesScreenDiscoverTab, ); const subscribedCommunitiesCache = useSelector((state) => state.cache.subscribedCommunities); + const subscribedCommunities = useSelector((state) => state.communities.subscribedCommunities); useEffect(() => { if (subscribingCommunitiesInDiscoverTab && selectedCommunityItem) { @@ -55,17 +56,9 @@ const CommunityContainer = ({ children, navigation, currentAccount, pinCode, isL const itemExistInCache = subscribedCommunitiesCache.get(data.name); setIsSubscribed(itemExistInCache.data[4]); //if item exist in cache, get isSubscribed value from cache } else { - //check and set user role - getSubscriptions(currentAccount.username) - .then((result) => { - if (result) { - const _isSubscribed = result.some((item) => item[0] === data.name); - setIsSubscribed(_isSubscribed); - } - }) - .catch((e) => { - console.log(e); - }); + // check in subscribed communities list if selected community exists + const itemExist = subscribedCommunities.data.find((item) => item[0] === data.name); + setIsSubscribed(itemExist ? true : false); } } }, [data]); diff --git a/src/screens/searchResult/screen/tabs/communities/container/communitiesResultsContainer.js b/src/screens/searchResult/screen/tabs/communities/container/communitiesResultsContainer.js index e187f7080..12b601f68 100644 --- a/src/screens/searchResult/screen/tabs/communities/container/communitiesResultsContainer.js +++ b/src/screens/searchResult/screen/tabs/communities/container/communitiesResultsContainer.js @@ -6,12 +6,14 @@ import { shuffle } from 'lodash'; import ROUTES from '../../../../../../constants/routeNames'; -import { getCommunities, getSubscriptions } from '../../../../../../providers/hive/dhive'; +import { getCommunities } from '../../../../../../providers/hive/dhive'; import { subscribeCommunity, leaveCommunity, } from '../../../../../../redux/actions/communitiesAction'; +import { updateSubscribedCommunitiesCache } from '../../../../../../redux/actions/cacheActions'; +import { statusMessage } from '../../../../../../redux/constants/communitiesConstants'; // const DEFAULT_COMMUNITIES = [ // 'hive-125125', @@ -37,9 +39,27 @@ const CommunitiesResultsContainer = ({ children, navigation, searchValue }) => { const pinCode = useSelector((state) => state.application.pin); const currentAccount = useSelector((state) => state.account.currentAccount); const isLoggedIn = useSelector((state) => state.application.isLoggedIn); + const [selectedCommunityItem, setSelectedCommunityItem] = useState(null); const subscribingCommunities = useSelector( (state) => state.communities.subscribingCommunitiesInSearchResultsScreen, ); + const subscribingCommunitiesInSearchResultsScreen = useSelector( + (state) => state.communities.subscribingCommunitiesInSearchResultsScreen, + ); + const subscribedCommunities = useSelector((state) => state.communities.subscribedCommunities); + const subscribedCommunitiesCache = useSelector((state) => state.cache.subscribedCommunities); + + // handle cache when searchResultsScreen data updates in communities reducer + useEffect(() => { + if (subscribingCommunitiesInSearchResultsScreen && selectedCommunityItem) { + const { status } = subscribingCommunitiesInSearchResultsScreen[ + selectedCommunityItem.communityId + ]; + if (status === statusMessage.SUCCESS) { + dispatch(updateSubscribedCommunitiesCache(selectedCommunityItem)); + } + } + }, [subscribingCommunitiesInSearchResultsScreen]); useEffect(() => { setData([]); @@ -48,25 +68,28 @@ const CommunitiesResultsContainer = ({ children, navigation, searchValue }) => { getCommunities('', searchValue ? 100 : 20, searchValue || null, 'rank') .then((communities) => { if (currentAccount && currentAccount.username) { - getSubscriptions(currentAccount.username).then((subs) => { - if (subs) { - communities.forEach((community) => - Object.assign(community, { - isSubscribed: subs.some( - (subscribedCommunity) => subscribedCommunity[0] === community.name, - ), - }), - ); - } - if (searchValue) { - setData(communities); - } else { - setData(shuffle(communities)); - } - if (communities.length === 0) { - setNoResult(true); - } - }); + if (subscribedCommunities.data && subscribedCommunities.data.length) { + communities.forEach((community) => { + // first check in cache and then in subscription list + const itemExistInCache = subscribedCommunitiesCache.get(community.name); + const _isSubscribed = itemExistInCache + ? itemExistInCache.data[4] + : subscribedCommunities.data.findIndex((item) => item[0] === community.name) === -1 + ? false + : true; + return Object.assign(community, { + isSubscribed: _isSubscribed, + }); + }); + } + if (searchValue) { + setData(communities); + } else { + setData(shuffle(communities)); + } + if (communities.length === 0) { + setNoResult(true); + } } else { if (searchValue) { setData(communities); @@ -124,6 +147,7 @@ const CommunitiesResultsContainer = ({ children, navigation, searchValue }) => { }; const _handleSubscribeButtonPress = (_data, screen) => { + setSelectedCommunityItem(_data); //set selected item to handle its cache let subscribeAction; let successToastText = ''; let failToastText = '';