From 1c94b8d4237a56028d0c9ac90bcae8ded43fff7f Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Thu, 30 Jun 2022 11:38:42 +0500 Subject: [PATCH 01/12] add status message in communities reducer --- src/redux/constants/communitiesConstants.ts | 6 +++++ src/redux/reducers/communitiesReducer.js | 25 +++++++++++++++++++ .../container/communitiesContainer.js | 18 +++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/redux/constants/communitiesConstants.ts diff --git a/src/redux/constants/communitiesConstants.ts b/src/redux/constants/communitiesConstants.ts new file mode 100644 index 000000000..9b9652c06 --- /dev/null +++ b/src/redux/constants/communitiesConstants.ts @@ -0,0 +1,6 @@ +export const statusMessage = { + PENDING : 'PENDING', + SUCCESS : 'SUCCESS', + FAIL : 'FAIL', +}; + \ No newline at end of file diff --git a/src/redux/reducers/communitiesReducer.js b/src/redux/reducers/communitiesReducer.js index 7106d18a0..8ce03ff55 100644 --- a/src/redux/reducers/communitiesReducer.js +++ b/src/redux/reducers/communitiesReducer.js @@ -12,6 +12,7 @@ import { LEAVE_COMMUNITY_SUCCESS, LEAVE_COMMUNITY_FAIL, } from '../constants/constants'; +import { statusMessage } from '../constants/communitiesConstants'; const initialState = { communities: { @@ -121,6 +122,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: true, error: false, + status: statusMessage.PENDING, }, }, }; @@ -133,6 +135,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: true, error: false, + status: statusMessage.PENDING, }, }, }; @@ -145,6 +148,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: true, error: false, + status: statusMessage.PENDING, }, }, }; @@ -157,6 +161,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: true, error: false, + status: statusMessage.PENDING, }, }, }; @@ -174,6 +179,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: false, error: false, + status: statusMessage.SUCCESS, }, }, }; @@ -186,6 +192,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: false, error: false, + status: statusMessage.SUCCESS, }, }, }; @@ -198,6 +205,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: false, error: false, + status: statusMessage.SUCCESS, }, }, }; @@ -210,6 +218,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: false, error: false, + status: statusMessage.SUCCESS, }, }, }; @@ -227,6 +236,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: false, error: true, + status: statusMessage.FAIL, }, }, }; @@ -239,6 +249,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: false, error: true, + status: statusMessage.FAIL, }, }, }; @@ -251,6 +262,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: false, error: true, + status: statusMessage.FAIL, }, }, }; @@ -263,6 +275,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: false, error: true, + status: statusMessage.FAIL, }, }, }; @@ -280,6 +293,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: true, error: false, + status: statusMessage.PENDING, }, }, }; @@ -292,6 +306,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: true, error: false, + status: statusMessage.PENDING, }, }, }; @@ -304,6 +319,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: true, error: false, + status: statusMessage.PENDING, }, }, }; @@ -316,6 +332,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: true, error: false, + status: statusMessage.PENDING, }, }, }; @@ -333,6 +350,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: false, error: false, + status: statusMessage.SUCCESS, }, }, }; @@ -345,6 +363,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: false, error: false, + status: statusMessage.SUCCESS, }, }, }; @@ -357,6 +376,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: false, error: false, + status: statusMessage.SUCCESS, }, }, }; @@ -369,6 +389,7 @@ export default function (state = initialState, action) { isSubscribed: false, loading: false, error: false, + status: statusMessage.SUCCESS, }, }, }; @@ -386,6 +407,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: false, error: true, + status: statusMessage.FAIL, }, }, }; @@ -398,6 +420,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: false, error: true, + status: statusMessage.FAIL, }, }, }; @@ -410,6 +433,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: false, error: true, + status: statusMessage.FAIL, }, }, }; @@ -422,6 +446,7 @@ export default function (state = initialState, action) { isSubscribed: true, loading: false, error: true, + status: statusMessage.FAIL, }, }, }; diff --git a/src/screens/communities/container/communitiesContainer.js b/src/screens/communities/container/communitiesContainer.js index 5eb28c925..f74121021 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(true); + const [selectedCommunityItem, setSelectedCommunityItem] = useState(null); const currentAccount = useSelector((state) => state.account.currentAccount); const pinCode = useSelector((state) => state.application.pin); @@ -31,6 +32,22 @@ const CommunitiesContainer = ({ children, navigation }) => { _getSubscriptions(); }, []); + useEffect(() => { + if (subscribingCommunitiesInJoinedTab && selectedCommunityItem) { + const { status } = subscribingCommunitiesInJoinedTab[selectedCommunityItem.communityId]; + // hande cache here + console.log('subscribingCommunitiesInJoinedTab status : ', status); + } + }, [subscribingCommunitiesInJoinedTab]); + + useEffect(() => { + if (subscribingCommunitiesInDiscoverTab && selectedCommunityItem) { + const { status } = subscribingCommunitiesInDiscoverTab[selectedCommunityItem.communityId]; + // hande cache here + console.log('subscribingCommunitiesInDiscoverTab status : ', status); + } + }, [subscribingCommunitiesInDiscoverTab]); + useEffect(() => { const discoversData = [...discovers]; @@ -119,6 +136,7 @@ const CommunitiesContainer = ({ children, navigation }) => { }; const _handleSubscribeButtonPress = (data, screen) => { + setSelectedCommunityItem(data); //set selected item to handle its cache let subscribeAction; let successToastText = ''; let failToastText = ''; From d77b27d79894bc84d9f652a9679ecdd3242cd570 Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Thu, 30 Jun 2022 16:07:09 +0500 Subject: [PATCH 02/12] added subscribed communities cache actions/reducer in cache and added item into cache upon subscribe/leave --- .../view/CommunitiesListItem.js | 5 ++- .../view/subscribedCommunitiesListView.js | 1 + src/redux/actions/cacheActions.ts | 29 +++++++++++++++- src/redux/constants/constants.js | 2 ++ src/redux/reducers/cacheReducer.ts | 33 ++++++++++++++++++- src/redux/store/store.ts | 4 ++- .../container/communitiesContainer.js | 21 +++++++++--- 7 files changed, 87 insertions(+), 8 deletions(-) diff --git a/src/components/communitiesList/view/communitiesListItem/view/CommunitiesListItem.js b/src/components/communitiesList/view/communitiesListItem/view/CommunitiesListItem.js index 7669ff6a8..da346ac5f 100644 --- a/src/components/communitiesList/view/communitiesListItem/view/CommunitiesListItem.js +++ b/src/components/communitiesList/view/communitiesListItem/view/CommunitiesListItem.js @@ -28,7 +28,10 @@ const CommunitiesListItem = ({ const intl = useIntl(); const _handleSubscribeButtonPress = () => { - handleSubscribeButtonPress({ isSubscribed: isSubscribed, communityId: name }, screen); + handleSubscribeButtonPress( + { isSubscribed: isSubscribed, communityId: name, communityTitle: title }, + screen, + ); }; return ( diff --git a/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js b/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js index f61910924..faaa35af8 100644 --- a/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js +++ b/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js @@ -81,6 +81,7 @@ const SubscribedCommunitiesListView = ({ { isSubscribed: item[4], communityId: item[0], + communityTitle: item[1], }, 'communitiesScreenJoinedTab', ) diff --git a/src/redux/actions/cacheActions.ts b/src/redux/actions/cacheActions.ts index 3d16366cb..93252e835 100644 --- a/src/redux/actions/cacheActions.ts +++ b/src/redux/actions/cacheActions.ts @@ -8,8 +8,10 @@ import { DELETE_COMMENT_CACHE_ENTRY, UPDATE_DRAFT_CACHE, DELETE_DRAFT_CACHE_ENTRY, + UPDATE_SUBSCRIBED_COMMUNITY_CACHE, + DELETE_SUBSCRIBED_COMMUNITY_CACHE, } from '../constants/constants'; -import { Comment, Draft, Vote } from '../reducers/cacheReducer'; +import { Comment, Draft, SubscribedCommunity, Vote } from '../reducers/cacheReducer'; @@ -87,6 +89,31 @@ export const deleteDraftCacheEntry = (id: string) => ({ type: DELETE_DRAFT_CACHE_ENTRY }) +export const updateSubscribedCommunitiesCache = (data: any) => { + const path = data.communityId; + const created = new Date(); + const communityTitle = data.communityTitle ? data.communityTitle : ''; + const userRole = ''; + const userLabel = ''; + const subscribedCommunity:SubscribedCommunity = { + data : [data.communityId, communityTitle, userRole, userLabel, !data.isSubscribed], + expiresAt : created.getTime() + 6000000, + }; + + return ({ + payload: { + path, + subscribedCommunity + }, + type: UPDATE_SUBSCRIBED_COMMUNITY_CACHE + }) +} + +export const deleteSubscribedCommunityCacheEntry = (path: string) => ({ + payload: path, + type: DELETE_SUBSCRIBED_COMMUNITY_CACHE +}) + export const purgeExpiredCache = () => ({ type: PURGE_EXPIRED_CACHE }) diff --git a/src/redux/constants/constants.js b/src/redux/constants/constants.js index 2c3c0b1b9..946e08cb3 100644 --- a/src/redux/constants/constants.js +++ b/src/redux/constants/constants.js @@ -114,6 +114,8 @@ export const DELETE_COMMENT_CACHE_ENTRY = 'DELETE_COMMENT_CACHE_ENTRY'; export const UPDATE_DRAFT_CACHE = 'UPDATE_DRAFT_CACHE'; export const DELETE_DRAFT_CACHE_ENTRY = 'DELETE_DRAFT_CACHE_ENTRY'; export const DEFAULT_USER_DRAFT_ID = 'DEFAULT_USER_DRAFT_ID_'; +export const UPDATE_SUBSCRIBED_COMMUNITY_CACHE = 'UPDATE_SUBSCRIBED_COMMUNITY_CACHE'; +export const DELETE_SUBSCRIBED_COMMUNITY_CACHE = 'DELETE_SUBSCRIBED_COMMUNITY_CACHE'; // TOOLTIPS export const REGISTER_TOOLTIP = 'REGISTER_TOOLTIP'; diff --git a/src/redux/reducers/cacheReducer.ts b/src/redux/reducers/cacheReducer.ts index 6e5ff76c6..bd609082a 100644 --- a/src/redux/reducers/cacheReducer.ts +++ b/src/redux/reducers/cacheReducer.ts @@ -1,4 +1,4 @@ -import { PURGE_EXPIRED_CACHE, UPDATE_VOTE_CACHE, UPDATE_COMMENT_CACHE, DELETE_COMMENT_CACHE_ENTRY, DELETE_DRAFT_CACHE_ENTRY, UPDATE_DRAFT_CACHE, } from "../constants/constants"; +import { PURGE_EXPIRED_CACHE, UPDATE_VOTE_CACHE, UPDATE_COMMENT_CACHE, DELETE_COMMENT_CACHE_ENTRY, DELETE_DRAFT_CACHE_ENTRY, UPDATE_DRAFT_CACHE, UPDATE_SUBSCRIBED_COMMUNITY_CACHE, DELETE_SUBSCRIBED_COMMUNITY_CACHE, } from "../constants/constants"; export interface Vote { amount:number; @@ -36,10 +36,15 @@ export interface Draft { expiresAt?:number; } +export interface SubscribedCommunity { + data: Array, + expiresAt?:number; +} interface State { votes:Map comments:Map //TODO: handle comment array per post, if parent is same drafts: Map + subscribedCommunities: Map lastUpdate:{ postPath:string, updatedAt:number, @@ -51,6 +56,7 @@ const initialState:State = { votes:new Map(), comments:new Map(), drafts: new Map(), + subscribedCommunities: new Map(), lastUpdate:null, }; @@ -120,6 +126,23 @@ const initialState:State = { } return { ...state } + case UPDATE_SUBSCRIBED_COMMUNITY_CACHE: + if(!state.subscribedCommunities){ + state.subscribedCommunities = new Map(); + } + const subscribedCommunities = new Map(state.subscribedCommunities); + subscribedCommunities.set(payload.path, payload.subscribedCommunity); + return { + ...state, //spread operator in requried here, otherwise persist do not register change + subscribedCommunities: subscribedCommunities + }; + + case DELETE_SUBSCRIBED_COMMUNITY_CACHE: + if (state.subscribedCommunities && state.subscribedCommunities.has(payload)) { + state.subscribedCommunities.delete(payload); + } + return { ...state } + case PURGE_EXPIRED_CACHE: const currentTime = new Date().getTime(); @@ -146,6 +169,14 @@ const initialState:State = { } }) } + + if(state.subscribedCommunities && state.subscribedCommunities.size){ + Array.from(state.subscribedCommunities).forEach((entry)=>{ + if(entry[1].expiresAt < currentTime){ + state.subscribedCommunities.delete(entry[0]); + } + }) + } return { ...state diff --git a/src/redux/store/store.ts b/src/redux/store/store.ts index 1eee95a19..c3f84a861 100644 --- a/src/redux/store/store.ts +++ b/src/redux/store/store.ts @@ -13,12 +13,14 @@ const transformCacheVoteMap = createTransform( votes : Array.from(inboundState.votes), comments : Array.from(inboundState.comments), drafts : Array.from(inboundState.drafts), + subscribedCommunities: Array.from(inboundState.subscribedCommunities) }), (outboundState) => ({ ...outboundState, votes:new Map(outboundState.votes), comments:new Map(outboundState.comments), - drafts: new Map(outboundState.drafts) + drafts: new Map(outboundState.drafts), + subscribedCommunities: new Map(outboundState.subscribedCommunities) }), {whitelist:['cache']} ); diff --git a/src/screens/communities/container/communitiesContainer.js b/src/screens/communities/container/communitiesContainer.js index f74121021..81c4a36ac 100644 --- a/src/screens/communities/container/communitiesContainer.js +++ b/src/screens/communities/container/communitiesContainer.js @@ -9,6 +9,8 @@ import ROUTES from '../../../constants/routeNames'; import { getCommunities, getSubscriptions } from '../../../providers/hive/dhive'; import { subscribeCommunity, leaveCommunity } from '../../../redux/actions/communitiesAction'; +import { statusMessage } from '../../../redux/constants/communitiesConstants'; +import { updateSubscribedCommunitiesCache } from '../../../redux/actions/cacheActions'; const CommunitiesContainer = ({ children, navigation }) => { const dispatch = useDispatch(); @@ -27,27 +29,37 @@ const CommunitiesContainer = ({ children, navigation }) => { const subscribingCommunitiesInJoinedTab = useSelector( (state) => state.communities.subscribingCommunitiesInCommunitiesScreenJoinedTab, ); + const subscribedCommunitiesCache = useSelector((state) => state.cache.subscribedCommunities); useEffect(() => { _getSubscriptions(); }, []); + // handle cache in joined/membership tab useEffect(() => { if (subscribingCommunitiesInJoinedTab && selectedCommunityItem) { const { status } = subscribingCommunitiesInJoinedTab[selectedCommunityItem.communityId]; - // hande cache here - console.log('subscribingCommunitiesInJoinedTab status : ', status); + if (status === statusMessage.SUCCESS) { + dispatch(updateSubscribedCommunitiesCache(selectedCommunityItem)); + } } }, [subscribingCommunitiesInJoinedTab]); + // handle cache in discover tab useEffect(() => { if (subscribingCommunitiesInDiscoverTab && selectedCommunityItem) { const { status } = subscribingCommunitiesInDiscoverTab[selectedCommunityItem.communityId]; - // hande cache here - console.log('subscribingCommunitiesInDiscoverTab status : ', status); + if (status === statusMessage.SUCCESS) { + dispatch(updateSubscribedCommunitiesCache(selectedCommunityItem)); + } } }, [subscribingCommunitiesInDiscoverTab]); + // side effect for subscribed communities cache update + useEffect(() => { + console.log('subscribedCommunitiesCache updated : ', subscribedCommunitiesCache); + }, [subscribedCommunitiesCache]); + useEffect(() => { const discoversData = [...discovers]; @@ -166,6 +178,7 @@ const CommunitiesContainer = ({ children, navigation }) => { ); }; + console.log('subscriptions : ', subscriptions); return ( children && children({ From e416b7ce4990ef47968df03f52d0eca10209cac8 Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Thu, 30 Jun 2022 22:28:44 +0500 Subject: [PATCH 03/12] cache implemented in membership/joined tab with merge and delete functionality --- .../view/subscribedCommunitiesListView.js | 4 +- src/redux/actions/cacheActions.ts | 7 +- src/redux/actions/communitiesAction.js | 5 +- .../container/communitiesContainer.js | 92 ++++++++++++++----- src/utils/communitiesUtils.ts | 20 ++++ 5 files changed, 99 insertions(+), 29 deletions(-) create mode 100644 src/utils/communitiesUtils.ts diff --git a/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js b/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js index faaa35af8..cf4446f9c 100644 --- a/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js +++ b/src/components/subscribedCommunitiesList/view/subscribedCommunitiesListView.js @@ -79,9 +79,11 @@ const SubscribedCommunitiesListView = ({ onPress={() => handleSubscribeButtonPress( { - isSubscribed: item[4], communityId: item[0], communityTitle: item[1], + userRole: item[2], + userLabel: item[3], + isSubscribed: item[4], }, 'communitiesScreenJoinedTab', ) diff --git a/src/redux/actions/cacheActions.ts b/src/redux/actions/cacheActions.ts index 93252e835..19c01d32b 100644 --- a/src/redux/actions/cacheActions.ts +++ b/src/redux/actions/cacheActions.ts @@ -93,11 +93,12 @@ export const updateSubscribedCommunitiesCache = (data: any) => { const path = data.communityId; const created = new Date(); const communityTitle = data.communityTitle ? data.communityTitle : ''; - const userRole = ''; - const userLabel = ''; + const userRole = data.userRole ? data.userRole : ''; + const userLabel = data.userLabel ? data.userLabel : ''; + const subscribedCommunity:SubscribedCommunity = { data : [data.communityId, communityTitle, userRole, userLabel, !data.isSubscribed], - expiresAt : created.getTime() + 6000000, + expiresAt : created.getTime() + 86400000, }; return ({ diff --git a/src/redux/actions/communitiesAction.js b/src/redux/actions/communitiesAction.js index 3ae39cfc2..c84a74cad 100644 --- a/src/redux/actions/communitiesAction.js +++ b/src/redux/actions/communitiesAction.js @@ -45,7 +45,10 @@ export const fetchSubscribedCommunities = (username) => { return (dispatch) => { dispatch({ type: FETCH_SUBSCRIBED_COMMUNITIES }); getSubscriptions(username) - .then((res) => dispatch(fetchSubscribedCommunitiesSuccess(res))) + .then((res) => { + res.forEach((item) => item.push(true)); + dispatch(fetchSubscribedCommunitiesSuccess(res)); + }) .catch((err) => dispatch(fetchSubscribedCommunitiesFail(err))); }; }; diff --git a/src/screens/communities/container/communitiesContainer.js b/src/screens/communities/container/communitiesContainer.js index 81c4a36ac..b6247c65f 100644 --- a/src/screens/communities/container/communitiesContainer.js +++ b/src/screens/communities/container/communitiesContainer.js @@ -1,7 +1,7 @@ import { useState, useEffect } from 'react'; import { withNavigation } from 'react-navigation'; import { useSelector, useDispatch } from 'react-redux'; -import { shuffle } from 'lodash'; +import { shuffle, isEmpty } from 'lodash'; import { useIntl } from 'react-intl'; import ROUTES from '../../../constants/routeNames'; @@ -10,7 +10,11 @@ import { getCommunities, getSubscriptions } from '../../../providers/hive/dhive' import { subscribeCommunity, leaveCommunity } from '../../../redux/actions/communitiesAction'; import { statusMessage } from '../../../redux/constants/communitiesConstants'; -import { updateSubscribedCommunitiesCache } from '../../../redux/actions/cacheActions'; +import { + deleteSubscribedCommunityCacheEntry, + updateSubscribedCommunitiesCache, +} from '../../../redux/actions/cacheActions'; +import { mergeSubCommunitiesCacheInSubList } from '../../../utils/communitiesUtils'; const CommunitiesContainer = ({ children, navigation }) => { const dispatch = useDispatch(); @@ -23,6 +27,7 @@ const CommunitiesContainer = ({ children, navigation }) => { const currentAccount = useSelector((state) => state.account.currentAccount); const pinCode = useSelector((state) => state.application.pin); + const subscribedCommunities = useSelector((state) => state.communities.subscribedCommunities); const subscribingCommunitiesInDiscoverTab = useSelector( (state) => state.communities.subscribingCommunitiesInCommunitiesScreenDiscoverTab, ); @@ -57,7 +62,18 @@ const CommunitiesContainer = ({ children, navigation }) => { // side effect for subscribed communities cache update useEffect(() => { - console.log('subscribedCommunitiesCache updated : ', subscribedCommunitiesCache); + if ( + subscribedCommunitiesCache && + subscribedCommunitiesCache.size && + subscriptions && + subscriptions.length > 0 + ) { + const updatedSubsList = mergeSubCommunitiesCacheInSubList( + subscriptions, + subscribedCommunitiesCache, + ); + setSubscriptions(updatedSubsList.slice()); + } }, [subscribedCommunitiesCache]); useEffect(() => { @@ -87,36 +103,54 @@ const CommunitiesContainer = ({ children, navigation }) => { }, [subscribingCommunitiesInDiscoverTab]); useEffect(() => { - const subscribedsData = [...subscriptions]; - - Object.keys(subscribingCommunitiesInJoinedTab).map((communityId) => { - if (!subscribingCommunitiesInJoinedTab[communityId].loading) { - if (!subscribingCommunitiesInJoinedTab[communityId].error) { - if (subscribingCommunitiesInJoinedTab[communityId].isSubscribed) { - subscribedsData.forEach((item) => { - if (item[0] === communityId) { - item[4] = true; - } - }); - } else { - subscribedsData.forEach((item) => { - if (item[0] === communityId) { - item[4] = false; - } - }); + if (!isEmpty(subscribingCommunitiesInJoinedTab)) { + const subscribedsData = mergeSubCommunitiesCacheInSubList( + subscribedCommunities.data, + subscribedCommunitiesCache, + ); + Object.keys(subscribingCommunitiesInJoinedTab).map((communityId) => { + if (!subscribingCommunitiesInJoinedTab[communityId].loading) { + if (!subscribingCommunitiesInJoinedTab[communityId].error) { + if (subscribingCommunitiesInJoinedTab[communityId].isSubscribed) { + subscribedsData.forEach((item) => { + if (item[0] === communityId) { + item[4] = true; + } + }); + } else { + subscribedsData.forEach((item) => { + if (item[0] === communityId) { + item[4] = false; + } + }); + } } } - } - }); + }); - setSubscriptions(subscribedsData); + setSubscriptions(subscribedsData); + } }, [subscribingCommunitiesInJoinedTab]); const _getSubscriptions = () => { setIsSubscriptionsLoading(true); + if ( + subscribedCommunities && + subscribedCommunities.data && + subscribedCommunities.data.length > 0 + ) { + const updatedSubsList = mergeSubCommunitiesCacheInSubList( + subscribedCommunities.data, + subscribedCommunitiesCache, + ); + setSubscriptions(updatedSubsList.slice()); + console.log('just before isSubscriptionsLoading : ', updatedSubsList.slice()); + setIsSubscriptionsLoading(false); + } getSubscriptions(currentAccount.username) .then((subs) => { subs.forEach((item) => item.push(true)); + _invalidateSubscribedCommunityCache(subs); // invalidate subscribed communities cache item when latest data is available getCommunities('', 50, null, 'rank').then((communities) => { communities.forEach((community) => Object.assign(community, { @@ -126,7 +160,7 @@ const CommunitiesContainer = ({ children, navigation }) => { }), ); - setSubscriptions(subs); + setSubscriptions(mergeSubCommunitiesCacheInSubList(subs, subscribedCommunitiesCache)); //merge cache with fetched data setDiscovers(shuffle(communities)); setIsSubscriptionsLoading(false); }); @@ -137,6 +171,14 @@ const CommunitiesContainer = ({ children, navigation }) => { }); }; + const _invalidateSubscribedCommunityCache = (fetchedList) => { + fetchedList.map((listItem) => { + let itemExists = subscribedCommunitiesCache.get(listItem[0]); + if (itemExists) { + dispatch(deleteSubscribedCommunityCacheEntry(listItem[0])); + } + }); + }; // Component Functions const _handleOnPress = (name) => { navigation.navigate({ @@ -178,6 +220,8 @@ const CommunitiesContainer = ({ children, navigation }) => { ); }; + console.log('isSubscriptionsLoading : ', isSubscriptionsLoading); + console.log('subscribedCommunities : ', subscribedCommunities); console.log('subscriptions : ', subscriptions); return ( children && diff --git a/src/utils/communitiesUtils.ts b/src/utils/communitiesUtils.ts new file mode 100644 index 000000000..91438b4fc --- /dev/null +++ b/src/utils/communitiesUtils.ts @@ -0,0 +1,20 @@ +import { SubscribedCommunity } from '../redux/reducers/cacheReducer'; + +export const mergeSubCommunitiesCacheInSubList = ( + subList: any[], + cacheMap: Map, +) => { + if (!cacheMap || !cacheMap.size) { + return subList.sort((a, b) => a[1].localeCompare(b[1])); + } + const cacheList = Array.from(cacheMap, ([path, item]) => item.data); + cacheList.map((cacheListItem) => { + let index = subList.findIndex((subListItem) => subListItem[0] === cacheListItem[0]); + if (index !== -1) { + subList[index] = [...cacheListItem]; + } else { + subList.push(cacheListItem); + } + }); + return subList.sort((a, b) => a[1].localeCompare(b[1])); +}; From 7820a52fa7db3a5643ddf776fe23e18a20716d25 Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Thu, 30 Jun 2022 22:38:01 +0500 Subject: [PATCH 04/12] added CommunitiesPlaceHolder while loading discovers data --- .../communitiesList/view/communitiesList.js | 36 ++++++++++--------- .../container/communitiesContainer.js | 7 +++- .../communities/view/communitiesScreen.js | 2 ++ 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/components/communitiesList/view/communitiesList.js b/src/components/communitiesList/view/communitiesList.js index 155dab441..44a0671a7 100644 --- a/src/components/communitiesList/view/communitiesList.js +++ b/src/components/communitiesList/view/communitiesList.js @@ -16,6 +16,7 @@ const CommunitiesList = ({ isLoggedIn, noResult, screen, + isDiscoversLoading, }) => { const _renderItem = ({ item, index }) => { return ( @@ -45,28 +46,29 @@ const CommunitiesList = ({ const _renderEmptyContent = () => { return ( - <> - - - - - - - - + isDiscoversLoading && ( + <> + + + + + + + + + ) ); }; return ( - {!noResult && ( - index.toString()} - renderItem={_renderItem} - ListEmptyComponent={_renderEmptyContent} - /> - )} + index.toString()} + renderItem={true && _renderItem} + ListEmptyComponent={_renderEmptyContent} + ListFooterComponent={isDiscoversLoading && } + /> ); }; diff --git a/src/screens/communities/container/communitiesContainer.js b/src/screens/communities/container/communitiesContainer.js index b6247c65f..ba0e2adad 100644 --- a/src/screens/communities/container/communitiesContainer.js +++ b/src/screens/communities/container/communitiesContainer.js @@ -23,6 +23,7 @@ const CommunitiesContainer = ({ children, navigation }) => { const [discovers, setDiscovers] = useState([]); const [subscriptions, setSubscriptions] = useState([]); const [isSubscriptionsLoading, setIsSubscriptionsLoading] = useState(true); + const [isDiscoversLoading, setIsDiscoversLoading] = useState(true); const [selectedCommunityItem, setSelectedCommunityItem] = useState(null); const currentAccount = useSelector((state) => state.account.currentAccount); @@ -134,6 +135,7 @@ const CommunitiesContainer = ({ children, navigation }) => { const _getSubscriptions = () => { setIsSubscriptionsLoading(true); + setIsDiscoversLoading(true); if ( subscribedCommunities && subscribedCommunities.data && @@ -161,13 +163,15 @@ const CommunitiesContainer = ({ children, navigation }) => { ); setSubscriptions(mergeSubCommunitiesCacheInSubList(subs, subscribedCommunitiesCache)); //merge cache with fetched data - setDiscovers(shuffle(communities)); + setDiscovers(communities); setIsSubscriptionsLoading(false); + setIsDiscoversLoading(false); }); }) .catch((err) => { console.warn('Failed to get subscriptions', err); setIsSubscriptionsLoading(false); + setIsDiscoversLoading(false); }); }; @@ -231,6 +235,7 @@ const CommunitiesContainer = ({ children, navigation }) => { subscribingCommunitiesInDiscoverTab, subscribingCommunitiesInJoinedTab, isSubscriptionsLoading, + isDiscoversLoading, handleOnPress: _handleOnPress, handleSubscribeButtonPress: _handleSubscribeButtonPress, handleGetSubscriptions: _getSubscriptions, diff --git a/src/screens/communities/view/communitiesScreen.js b/src/screens/communities/view/communitiesScreen.js index f9262bc87..0078991fa 100644 --- a/src/screens/communities/view/communitiesScreen.js +++ b/src/screens/communities/view/communitiesScreen.js @@ -47,6 +47,7 @@ const CommunitiesScreen = () => { subscribingCommunitiesInJoinedTab, handleGetSubscriptions, isSubscriptionsLoading, + isDiscoversLoading, }) => { return ( @@ -87,6 +88,7 @@ const CommunitiesScreen = () => { isLoggedIn={true} noResult={discovers.length === 0} screen="communitiesScreenDiscoverTab" + isDiscoversLoading={isDiscoversLoading} /> From 87dc6853cbf30e42d416a33e2b628421d8888b2f Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Thu, 30 Jun 2022 22:50:29 +0500 Subject: [PATCH 05/12] sort subscribed communities when fetching --- src/redux/actions/communitiesAction.js | 2 +- src/utils/communitiesUtils.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/redux/actions/communitiesAction.js b/src/redux/actions/communitiesAction.js index c84a74cad..cc06174f8 100644 --- a/src/redux/actions/communitiesAction.js +++ b/src/redux/actions/communitiesAction.js @@ -46,7 +46,7 @@ export const fetchSubscribedCommunities = (username) => { dispatch({ type: FETCH_SUBSCRIBED_COMMUNITIES }); getSubscriptions(username) .then((res) => { - res.forEach((item) => item.push(true)); + res.forEach((item) => item.push(true)).sort((a, b) => a[1].localeCompare(b[1])); //add true value for subscribe status and sort alphabatically dispatch(fetchSubscribedCommunitiesSuccess(res)); }) .catch((err) => dispatch(fetchSubscribedCommunitiesFail(err))); diff --git a/src/utils/communitiesUtils.ts b/src/utils/communitiesUtils.ts index 91438b4fc..77c3a1841 100644 --- a/src/utils/communitiesUtils.ts +++ b/src/utils/communitiesUtils.ts @@ -1,5 +1,11 @@ import { SubscribedCommunity } from '../redux/reducers/cacheReducer'; +/** + * Accepts Array of subscription items arrays as 1st argument, community cache map as second argument. + * Returns single array with union of both lists, sorted alphabatically + * Example subList = [['id', 'title', 'role', 'label', 'true/false'],['id', 'title', 'role', 'label', 'true/false']] + * + **/ export const mergeSubCommunitiesCacheInSubList = ( subList: any[], cacheMap: Map, From 6fca89dd4020b0351905997cc9f28e1edb196f72 Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Thu, 30 Jun 2022 23:29:41 +0500 Subject: [PATCH 06/12] sort communities while fetching --- src/redux/actions/communitiesAction.js | 3 ++- src/redux/reducers/communitiesReducer.js | 4 ++++ .../communities/container/communitiesContainer.js | 9 ++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/redux/actions/communitiesAction.js b/src/redux/actions/communitiesAction.js index cc06174f8..d54a77680 100644 --- a/src/redux/actions/communitiesAction.js +++ b/src/redux/actions/communitiesAction.js @@ -46,7 +46,8 @@ export const fetchSubscribedCommunities = (username) => { dispatch({ type: FETCH_SUBSCRIBED_COMMUNITIES }); getSubscriptions(username) .then((res) => { - res.forEach((item) => item.push(true)).sort((a, b) => a[1].localeCompare(b[1])); //add true value for subscribe status and sort alphabatically + res.forEach((item) => item.push(true)); //add true value for subscribe status + res.sort((a, b) => a[1].localeCompare(b[1])); dispatch(fetchSubscribedCommunitiesSuccess(res)); }) .catch((err) => dispatch(fetchSubscribedCommunitiesFail(err))); diff --git a/src/redux/reducers/communitiesReducer.js b/src/redux/reducers/communitiesReducer.js index 8ce03ff55..9e49956bf 100644 --- a/src/redux/reducers/communitiesReducer.js +++ b/src/redux/reducers/communitiesReducer.js @@ -24,6 +24,7 @@ const initialState = { data: [], loading: false, error: false, + status: statusMessage.PENDING, }, subscribingCommunitiesInFeedScreen: { //['name']: { @@ -91,6 +92,7 @@ export default function (state = initialState, action) { data: [], loading: true, error: false, + status: statusMessage.PENDING, }, }; case FETCH_SUBSCRIBED_COMMUNITIES_SUCCESS: @@ -100,6 +102,7 @@ export default function (state = initialState, action) { data: action.payload || [], loading: false, error: false, + status: statusMessage.SUCCESS, }, }; case FETCH_SUBSCRIBED_COMMUNITIES_FAIL: @@ -109,6 +112,7 @@ export default function (state = initialState, action) { data: [], loading: false, error: action.payload, + status: statusMessage.FAIL, }, }; case SUBSCRIBE_COMMUNITY: diff --git a/src/screens/communities/container/communitiesContainer.js b/src/screens/communities/container/communitiesContainer.js index ba0e2adad..af4fe0068 100644 --- a/src/screens/communities/container/communitiesContainer.js +++ b/src/screens/communities/container/communitiesContainer.js @@ -8,7 +8,11 @@ import ROUTES from '../../../constants/routeNames'; import { getCommunities, getSubscriptions } from '../../../providers/hive/dhive'; -import { subscribeCommunity, leaveCommunity } from '../../../redux/actions/communitiesAction'; +import { + subscribeCommunity, + leaveCommunity, + fetchSubscribedCommunitiesSuccess, +} from '../../../redux/actions/communitiesAction'; import { statusMessage } from '../../../redux/constants/communitiesConstants'; import { deleteSubscribedCommunityCacheEntry, @@ -166,6 +170,9 @@ const CommunitiesContainer = ({ children, navigation }) => { setDiscovers(communities); setIsSubscriptionsLoading(false); setIsDiscoversLoading(false); + dispatch( + fetchSubscribedCommunitiesSuccess(subs.sort((a, b) => a[1].localeCompare(b[1]))), + ); //register subscribed data in communities store }); }) .catch((err) => { From fb30ef818bbc2cdca348958abe20223e4bdc45d9 Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Thu, 30 Jun 2022 23:31:56 +0500 Subject: [PATCH 07/12] commented log statements --- src/screens/communities/container/communitiesContainer.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/screens/communities/container/communitiesContainer.js b/src/screens/communities/container/communitiesContainer.js index af4fe0068..017abc46a 100644 --- a/src/screens/communities/container/communitiesContainer.js +++ b/src/screens/communities/container/communitiesContainer.js @@ -150,7 +150,6 @@ const CommunitiesContainer = ({ children, navigation }) => { subscribedCommunitiesCache, ); setSubscriptions(updatedSubsList.slice()); - console.log('just before isSubscriptionsLoading : ', updatedSubsList.slice()); setIsSubscriptionsLoading(false); } getSubscriptions(currentAccount.username) @@ -231,9 +230,9 @@ const CommunitiesContainer = ({ children, navigation }) => { ); }; - console.log('isSubscriptionsLoading : ', isSubscriptionsLoading); - console.log('subscribedCommunities : ', subscribedCommunities); - console.log('subscriptions : ', subscriptions); + // console.log('isSubscriptionsLoading : ', isSubscriptionsLoading); + // console.log('subscribedCommunities : ', subscribedCommunities); + // console.log('subscriptions : ', subscriptions); return ( children && children({ From 6365165238a869dd0fc6c219f6bed08f60ae563d Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Sun, 3 Jul 2022 09:52:02 +0500 Subject: [PATCH 08/12] clear subscribed communities cache while switching accounts --- .../container/accountsBottomSheetContainer.tsx | 2 ++ src/redux/actions/cacheActions.ts | 6 ++++++ src/redux/constants/constants.js | 1 + src/redux/reducers/cacheReducer.ts | 7 ++++++- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/components/accountsBottomSheet/container/accountsBottomSheetContainer.tsx b/src/components/accountsBottomSheet/container/accountsBottomSheetContainer.tsx index bc54414ca..96056e0ee 100644 --- a/src/components/accountsBottomSheet/container/accountsBottomSheetContainer.tsx +++ b/src/components/accountsBottomSheet/container/accountsBottomSheetContainer.tsx @@ -27,6 +27,7 @@ import { getUnreadNotificationCount } from '../../../providers/ecency/ecency'; import { decryptKey } from '../../../utils/crypto'; import { getPointsSummary} from '../../../providers/ecency/ePoint'; import { fetchSubscribedCommunities } from '../../../redux/actions/communitiesAction'; +import { clearSubscribedCommunitiesCache } from '../../../redux/actions/cacheActions'; const AccountsBottomSheetContainer = ({ navigation }) => { const intl = useIntl(); @@ -110,6 +111,7 @@ const AccountsBottomSheetContainer = ({ navigation }) => { _currentAccount.mutes = await getMutes(_currentAccount.username); dispatch(updateCurrentAccount(_currentAccount)); + dispatch(clearSubscribedCommunitiesCache()); dispatch(fetchSubscribedCommunities(_currentAccount.username)) } diff --git a/src/redux/actions/cacheActions.ts b/src/redux/actions/cacheActions.ts index 19c01d32b..21377dbf7 100644 --- a/src/redux/actions/cacheActions.ts +++ b/src/redux/actions/cacheActions.ts @@ -10,6 +10,7 @@ import { DELETE_DRAFT_CACHE_ENTRY, UPDATE_SUBSCRIBED_COMMUNITY_CACHE, DELETE_SUBSCRIBED_COMMUNITY_CACHE, + CLEAR_SUBSCRIBED_COMMUNITIES_CACHE, } from '../constants/constants'; import { Comment, Draft, SubscribedCommunity, Vote } from '../reducers/cacheReducer'; @@ -115,6 +116,11 @@ export const deleteSubscribedCommunityCacheEntry = (path: string) => ({ type: DELETE_SUBSCRIBED_COMMUNITY_CACHE }) +export const clearSubscribedCommunitiesCache = () => ({ + type: CLEAR_SUBSCRIBED_COMMUNITIES_CACHE +}) + + export const purgeExpiredCache = () => ({ type: PURGE_EXPIRED_CACHE }) diff --git a/src/redux/constants/constants.js b/src/redux/constants/constants.js index 946e08cb3..50571574a 100644 --- a/src/redux/constants/constants.js +++ b/src/redux/constants/constants.js @@ -116,6 +116,7 @@ export const DELETE_DRAFT_CACHE_ENTRY = 'DELETE_DRAFT_CACHE_ENTRY'; export const DEFAULT_USER_DRAFT_ID = 'DEFAULT_USER_DRAFT_ID_'; export const UPDATE_SUBSCRIBED_COMMUNITY_CACHE = 'UPDATE_SUBSCRIBED_COMMUNITY_CACHE'; export const DELETE_SUBSCRIBED_COMMUNITY_CACHE = 'DELETE_SUBSCRIBED_COMMUNITY_CACHE'; +export const CLEAR_SUBSCRIBED_COMMUNITIES_CACHE = 'CLEAR_SUBSCRIBED_COMMUNITIES_CACHE'; // TOOLTIPS export const REGISTER_TOOLTIP = 'REGISTER_TOOLTIP'; diff --git a/src/redux/reducers/cacheReducer.ts b/src/redux/reducers/cacheReducer.ts index bd609082a..c96c75fe2 100644 --- a/src/redux/reducers/cacheReducer.ts +++ b/src/redux/reducers/cacheReducer.ts @@ -1,4 +1,4 @@ -import { PURGE_EXPIRED_CACHE, UPDATE_VOTE_CACHE, UPDATE_COMMENT_CACHE, DELETE_COMMENT_CACHE_ENTRY, DELETE_DRAFT_CACHE_ENTRY, UPDATE_DRAFT_CACHE, UPDATE_SUBSCRIBED_COMMUNITY_CACHE, DELETE_SUBSCRIBED_COMMUNITY_CACHE, } from "../constants/constants"; +import { PURGE_EXPIRED_CACHE, UPDATE_VOTE_CACHE, UPDATE_COMMENT_CACHE, DELETE_COMMENT_CACHE_ENTRY, DELETE_DRAFT_CACHE_ENTRY, UPDATE_DRAFT_CACHE, UPDATE_SUBSCRIBED_COMMUNITY_CACHE, DELETE_SUBSCRIBED_COMMUNITY_CACHE, CLEAR_SUBSCRIBED_COMMUNITIES_CACHE, } from "../constants/constants"; export interface Vote { amount:number; @@ -142,6 +142,11 @@ const initialState:State = { state.subscribedCommunities.delete(payload); } return { ...state } + + case CLEAR_SUBSCRIBED_COMMUNITIES_CACHE: + state.subscribedCommunities = new Map(); + + return {...state} case PURGE_EXPIRED_CACHE: const currentTime = new Date().getTime(); From 309065ee4be815326d7cc66666839d545735677c Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Sun, 3 Jul 2022 10:04:46 +0500 Subject: [PATCH 09/12] add communitiesPlaceholder in communities results screen --- .../communities/container/communitiesResultsContainer.js | 7 +++++-- .../screen/tabs/communities/view/communitiesResults.js | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/screens/searchResult/screen/tabs/communities/container/communitiesResultsContainer.js b/src/screens/searchResult/screen/tabs/communities/container/communitiesResultsContainer.js index 36e96f35c..e187f7080 100644 --- a/src/screens/searchResult/screen/tabs/communities/container/communitiesResultsContainer.js +++ b/src/screens/searchResult/screen/tabs/communities/container/communitiesResultsContainer.js @@ -33,7 +33,7 @@ const CommunitiesResultsContainer = ({ children, navigation, searchValue }) => { const [data, setData] = useState([]); const [noResult, setNoResult] = useState(false); - + const [isDiscoversLoading, setIsDiscoversLoading] = useState(false); const pinCode = useSelector((state) => state.application.pin); const currentAccount = useSelector((state) => state.account.currentAccount); const isLoggedIn = useSelector((state) => state.application.isLoggedIn); @@ -44,7 +44,7 @@ const CommunitiesResultsContainer = ({ children, navigation, searchValue }) => { useEffect(() => { setData([]); setNoResult(false); - + setIsDiscoversLoading(true); getCommunities('', searchValue ? 100 : 20, searchValue || null, 'rank') .then((communities) => { if (currentAccount && currentAccount.username) { @@ -78,10 +78,12 @@ const CommunitiesResultsContainer = ({ children, navigation, searchValue }) => { setNoResult(true); } } + setIsDiscoversLoading(false); }) .catch((err) => { setNoResult(true); setData([]); + setIsDiscoversLoading(false); }); }, [searchValue]); @@ -160,6 +162,7 @@ const CommunitiesResultsContainer = ({ children, navigation, searchValue }) => { handleSubscribeButtonPress: _handleSubscribeButtonPress, isLoggedIn, noResult, + isDiscoversLoading, }) ); }; diff --git a/src/screens/searchResult/screen/tabs/communities/view/communitiesResults.js b/src/screens/searchResult/screen/tabs/communities/view/communitiesResults.js index c1af842cd..9ffec3c57 100644 --- a/src/screens/searchResult/screen/tabs/communities/view/communitiesResults.js +++ b/src/screens/searchResult/screen/tabs/communities/view/communitiesResults.js @@ -18,6 +18,7 @@ const CommunitiesResultsScreen = ({ navigation, searchValue }) => { handleSubscribeButtonPress, isLoggedIn, noResult, + isDiscoversLoading, }) => noResult ? ( @@ -30,6 +31,7 @@ const CommunitiesResultsScreen = ({ navigation, searchValue }) => { isLoggedIn={isLoggedIn} noResult={noResult} screen="searchResultsScreen" + isDiscoversLoading={isDiscoversLoading} /> ) } From e29239e837c5ded742557884a7e7bb7015d12330 Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Sun, 3 Jul 2022 10:38:02 +0500 Subject: [PATCH 10/12] added subscribed communities cache in community modal container --- .../selectCommunityModalContainer.js | 22 +++++++- .../view/selectCommunityModalView.js | 50 +++++++++---------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/components/editorElements/selectCommunityModal/container/selectCommunityModalContainer.js b/src/components/editorElements/selectCommunityModal/container/selectCommunityModalContainer.js index 83803fefd..28f71cc56 100644 --- a/src/components/editorElements/selectCommunityModal/container/selectCommunityModalContainer.js +++ b/src/components/editorElements/selectCommunityModal/container/selectCommunityModalContainer.js @@ -14,15 +14,18 @@ import { fetchSubscribedCommunities, fetchSubscribedCommunitiesSuccess, } from '../../../../redux/actions/communitiesAction'; +import { mergeSubCommunitiesCacheInSubList } from '../../../../utils/communitiesUtils'; const SelectCommunityModalContainer = ({ onPressCommunity, currentAccount, onCloseModal }) => { const dispatch = useDispatch(); const [searchedCommunities, setSearchedCommunities] = useState([]); const [showSearchedCommunities, setShowSearchedCommunities] = useState(false); + const [subscriptions, setSubscriptions] = useState(null); const topCommunities = useSelector((state) => state.communities.communities); const subscribedCommunities = useSelector((state) => state.communities.subscribedCommunities); + const subscribedCommunitiesCache = useSelector((state) => state.cache.subscribedCommunities); useEffect(() => { callTopCommunities(); @@ -31,7 +34,22 @@ const SelectCommunityModalContainer = ({ onPressCommunity, currentAccount, onClo const callTopCommunities = () => dispatch(fetchCommunities('', 15, '', 'rank')); - const callSubscribedCommunities = () => dispatch(fetchSubscribedCommunities(currentAccount.name)); + const callSubscribedCommunities = () => { + if ( + subscribedCommunities && + subscribedCommunities.data && + subscribedCommunities.data.length > 0 + ) { + const updatedSubsList = mergeSubCommunitiesCacheInSubList( + subscribedCommunities.data, + subscribedCommunitiesCache, + ); + if (updatedSubsList && updatedSubsList.length > 0) { + setSubscriptions(updatedSubsList.filter((item) => item[4] === true)); + } + } + dispatch(fetchSubscribedCommunities(currentAccount.name)); + }; const handleChangeSearch = (text) => { if (text.length >= 3) { @@ -53,7 +71,7 @@ const SelectCommunityModalContainer = ({ onPressCommunity, currentAccount, onClo onPressCommunity(null)} /> - {!subscribedCommunities.loading && - !subscribedCommunities.error && - subscribedCommunities.data?.length > 0 && ( - - - {intl.formatMessage({ id: 'editor.my_communities' }).toUpperCase()} - - } - showsVerticalScrollIndicator={false} - data={subscribedCommunities.data} - keyExtractor={(item, index) => index.toString()} - renderItem={({ item, index, separators }) => { - const community = { name: item[0], title: item[1] }; - return ( - - ); - }} - /> - - )} + {subscribedCommunities && ( + + + {intl.formatMessage({ id: 'editor.my_communities' }).toUpperCase()} + + } + showsVerticalScrollIndicator={false} + data={subscribedCommunities} + keyExtractor={(item, index) => index.toString()} + renderItem={({ item, index, separators }) => { + const community = { name: item[0], title: item[1] }; + return ( + + ); + }} + /> + + )} {!topCommunities.loading && !topCommunities.error && topCommunities.data?.length > 0 && ( From 66a7d4f2dff110b9405b1232b785d6bde736575b Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Sun, 3 Jul 2022 13:55:44 +0500 Subject: [PATCH 11/12] update discovers data when changed in membership tab --- .../container/communitiesContainer.js | 14 ++++++++----- src/utils/communitiesUtils.ts | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/screens/communities/container/communitiesContainer.js b/src/screens/communities/container/communitiesContainer.js index 017abc46a..7c67bdd23 100644 --- a/src/screens/communities/container/communitiesContainer.js +++ b/src/screens/communities/container/communitiesContainer.js @@ -18,7 +18,10 @@ import { deleteSubscribedCommunityCacheEntry, updateSubscribedCommunitiesCache, } from '../../../redux/actions/cacheActions'; -import { mergeSubCommunitiesCacheInSubList } from '../../../utils/communitiesUtils'; +import { + mergeSubCommunitiesCacheInDiscoverList, + mergeSubCommunitiesCacheInSubList, +} from '../../../utils/communitiesUtils'; const CommunitiesContainer = ({ children, navigation }) => { const dispatch = useDispatch(); @@ -77,13 +80,17 @@ const CommunitiesContainer = ({ children, navigation }) => { subscriptions, subscribedCommunitiesCache, ); + const updatedDiscoversList = mergeSubCommunitiesCacheInDiscoverList( + discovers, + subscribedCommunitiesCache, + ); setSubscriptions(updatedSubsList.slice()); + setDiscovers(updatedDiscoversList); } }, [subscribedCommunitiesCache]); useEffect(() => { const discoversData = [...discovers]; - Object.keys(subscribingCommunitiesInDiscoverTab).map((communityId) => { if (!subscribingCommunitiesInDiscoverTab[communityId].loading) { if (!subscribingCommunitiesInDiscoverTab[communityId].error) { @@ -230,9 +237,6 @@ const CommunitiesContainer = ({ children, navigation }) => { ); }; - // console.log('isSubscriptionsLoading : ', isSubscriptionsLoading); - // console.log('subscribedCommunities : ', subscribedCommunities); - // console.log('subscriptions : ', subscriptions); return ( children && children({ diff --git a/src/utils/communitiesUtils.ts b/src/utils/communitiesUtils.ts index 77c3a1841..f4f23137e 100644 --- a/src/utils/communitiesUtils.ts +++ b/src/utils/communitiesUtils.ts @@ -24,3 +24,24 @@ export const mergeSubCommunitiesCacheInSubList = ( }); return subList.sort((a, b) => a[1].localeCompare(b[1])); }; + +/** + * Accepts Array of discover items arrays as 1st argument, community cache map as second argument. + * Returns discovers list with updated isSubscribed status + * + **/ +export const mergeSubCommunitiesCacheInDiscoverList = ( + discoverList: any[], + cacheMap: Map, +) => { + if (!cacheMap || !cacheMap.size) { + return discoverList; + } + discoverList.forEach((discoverListItem) => { + let itemExist = cacheMap.get(discoverListItem.name); + if (itemExist) { + discoverListItem.isSubscribed = itemExist.data[4]; + } + }); + return discoverList; +}; From f842469ed553e670baee3a9c0b815097cb2a54ce Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Tue, 5 Jul 2022 22:16:07 +0500 Subject: [PATCH 12/12] added cache in community page while joining/leaving --- .../community/container/communityContainer.js | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/src/screens/community/container/communityContainer.js b/src/screens/community/container/communityContainer.js index 50e33d97d..98cc9bacd 100644 --- a/src/screens/community/container/communityContainer.js +++ b/src/screens/community/container/communityContainer.js @@ -1,7 +1,7 @@ import { useState, useEffect } from 'react'; import { withNavigation } from 'react-navigation'; import get from 'lodash/get'; -import { connect, useDispatch } from 'react-redux'; +import { connect, useDispatch, useSelector } from 'react-redux'; import { useIntl } from 'react-intl'; import { getCommunity, getSubscriptions } from '../../../providers/hive/dhive'; @@ -9,14 +9,32 @@ import { getCommunity, getSubscriptions } from '../../../providers/hive/dhive'; import { subscribeCommunity, leaveCommunity } from '../../../redux/actions/communitiesAction'; import ROUTES from '../../../constants/routeNames'; +import { updateSubscribedCommunitiesCache } from '../../../redux/actions/cacheActions'; +import { statusMessage } from '../../../redux/constants/communitiesConstants'; const CommunityContainer = ({ children, navigation, currentAccount, pinCode, isLoggedIn }) => { const [data, setData] = useState(null); const [isSubscribed, setIsSubscribed] = useState(false); + const [selectedCommunityItem, setSelectedCommunityItem] = useState(null); + const tag = get(navigation, 'state.params.tag'); const dispatch = useDispatch(); const intl = useIntl(); + const subscribingCommunitiesInDiscoverTab = useSelector( + (state) => state.communities.subscribingCommunitiesInCommunitiesScreenDiscoverTab, + ); + const subscribedCommunitiesCache = useSelector((state) => state.cache.subscribedCommunities); + + useEffect(() => { + if (subscribingCommunitiesInDiscoverTab && selectedCommunityItem) { + const { status } = subscribingCommunitiesInDiscoverTab[selectedCommunityItem.communityId]; + if (status === statusMessage.SUCCESS) { + dispatch(updateSubscribedCommunitiesCache(selectedCommunityItem)); + } + } + }, [subscribingCommunitiesInDiscoverTab]); + useEffect(() => { getCommunity(tag) .then((res) => { @@ -29,17 +47,26 @@ const CommunityContainer = ({ children, navigation, currentAccount, pinCode, isL useEffect(() => { if (data) { - //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); - }); + if ( + subscribedCommunitiesCache && + subscribedCommunitiesCache.size && + subscribedCommunitiesCache.get(data.name) + ) { + 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); + }); + } } }, [data]); @@ -48,6 +75,7 @@ const CommunityContainer = ({ children, navigation, currentAccount, pinCode, isL isSubscribed: isSubscribed, communityId: data.name, }; + setSelectedCommunityItem(_data); //set selected item to handle its cache const screen = 'communitiesScreenDiscoverTab'; let subscribeAction; let successToastText = '';