Merge pull request #2406 from ecency/sa/cleanup-community-container

cleanup community container
This commit is contained in:
Feruz M 2022-08-05 15:39:37 +03:00 committed by GitHub
commit 6d6c47d8fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 35 deletions

View File

@ -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';

View File

@ -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]);

View File

@ -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,15 +68,19 @@ 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 (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);
@ -66,7 +90,6 @@ const CommunitiesResultsContainer = ({ children, navigation, searchValue }) => {
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 = '';