@selfcommunity/react-core 0.4.9-alpha.2 → 0.4.9-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/constants/Cache.d.ts +3 -0
- package/lib/cjs/constants/Cache.js +6 -2
- package/lib/cjs/constants/Routes.js +1 -1
- package/lib/cjs/hooks/useSCFetchGroups.d.ts +22 -0
- package/lib/cjs/hooks/useSCFetchGroups.js +83 -0
- package/lib/cjs/hooks/useSCSubscribedGroupsManager.d.ts +2 -0
- package/lib/cjs/hooks/useSCSubscribedGroupsManager.js +23 -1
- package/lib/cjs/index.d.ts +2 -1
- package/lib/cjs/index.js +3 -1
- package/lib/cjs/types/context.d.ts +4 -0
- package/lib/esm/constants/Cache.d.ts +3 -0
- package/lib/esm/constants/Cache.js +3 -0
- package/lib/esm/constants/Routes.js +1 -1
- package/lib/esm/hooks/useSCFetchGroups.d.ts +22 -0
- package/lib/esm/hooks/useSCFetchGroups.js +81 -0
- package/lib/esm/hooks/useSCSubscribedGroupsManager.d.ts +2 -0
- package/lib/esm/hooks/useSCSubscribedGroupsManager.js +23 -1
- package/lib/esm/index.d.ts +2 -1
- package/lib/esm/index.js +2 -1
- package/lib/esm/types/context.d.ts +4 -0
- package/lib/umd/react-core.js +1 -1
- package/package.json +5 -5
|
@@ -20,6 +20,9 @@ export declare const getCategoryObjectCacheKey: (id: any) => string;
|
|
|
20
20
|
/** GROUP OBJECT **/
|
|
21
21
|
export declare const GROUP_OBJECT_CACHE_PREFIX_KEY = "_grp_";
|
|
22
22
|
export declare const getGroupObjectCacheKey: (id: any) => string;
|
|
23
|
+
/** GROUPS OBJECT **/
|
|
24
|
+
export declare const GROUPS_OBJECT_CACHE_PREFIX_KEY = "_grps_";
|
|
25
|
+
export declare const getGroupsObjectCacheKey: () => string;
|
|
23
26
|
/** INCUBATOR OBJECT **/
|
|
24
27
|
export declare const INCUBATOR_OBJECT_CACHE_PREFIX_KEY = "_inc_";
|
|
25
28
|
export declare const getIncubatorObjectCacheKey: (id: any) => string;
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* Cache prefixes
|
|
4
4
|
*/
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
exports.getWidgetStateCacheKey = exports.GROUPS_LIST_TOOLS_STATE_CACHE_PREFIX_KEY = exports.GROUP_REQUESTS_TOOLS_STATE_CACHE_PREFIX_KEY = exports.GROUP_MEMBERS_TOOLS_STATE_CACHE_PREFIX_KEY = void 0;
|
|
6
|
+
exports.INCUBATOR_LIST_TOOLS_STATE_CACHE_PREFIX_KEY = exports.PEOPLE_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = exports.TRENDING_PEOPLE_TOOLS_STATE_CACHE_PREFIX_KEY = exports.RELATED_FEED_TOOLS_STATE_CACHE_PREFIX_KEY = exports.TRENDING_FEED_TOOLS_STATE_CACHE_PREFIX_KEY = exports.USER_CONNECTIONS_REQUESTS_SENT_TOOLS_STATE_CACHE_PREFIX_KEY = exports.USER_CONNECTIONS_REQUESTS_TOOLS_STATE_CACHE_PREFIX_KEY = exports.USER_CONNECTIONS_TOOLS_STATE_CACHE_PREFIX_KEY = exports.USER_FOLLOWED_TOOLS_STATE_CACHE_PREFIX_KEY = exports.USER_FOLLOWERS_TOOLS_STATE_CACHE_PREFIX_KEY = exports.CATEGORIES_POPULAR_TOOLS_STATE_CACHE_PREFIX_KEY = exports.CATEGORIES_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = exports.CATEGORIES_FOLLOWED_TOOLS_STATE_CACHE_PREFIX_KEY = exports.CATEGORIES_LIST_TOOLS_STATE_CACHE_PREFIX_KEY = exports.getPmSnippetObjectCacheKey = exports.PM_SNIPPET_OBJECT_CACHE_PREFIX_KEY = exports.getPmSnippetsObjectCacheKey = exports.PM_SNIPPETS_OBJECT_CACHE_PREFIX_KEY = exports.getAdvObjectCacheKey = exports.ADV_OBJECT_CACHE_PREFIX_KEY = exports.getFeedSPCacheKey = exports.FEED_CACHE_SP_KEY = exports.getVirtualizedScrollStateCacheKey = exports.VIRTUALIZED_SCROLL_STATE_CACHE_PREFIX_KEY = exports.getStateFeedCacheKey = exports.FEED_STATE_CACHE_PREFIX_KEY = exports.getFeedCacheKey = exports.FEED_CACHE_PREFIX_KEY = exports.getBroadcastMessagesObjectCacheKey = exports.BROADCAST_MESSAGES_OBJECT_CACHE_PREFIX_KEY = exports.getContributorsCachePrefixKeys = exports.getContributorsCacheKey = exports.CONTRIBUTORS_CACHE_PREFIX_KEY = exports.getIncubatorObjectCacheKey = exports.INCUBATOR_OBJECT_CACHE_PREFIX_KEY = exports.getGroupsObjectCacheKey = exports.GROUPS_OBJECT_CACHE_PREFIX_KEY = exports.getGroupObjectCacheKey = exports.GROUP_OBJECT_CACHE_PREFIX_KEY = exports.getCategoryObjectCacheKey = exports.CATEGORY_OBJECT_CACHE_PREFIX_KEY = exports.getCategoriesObjectCacheKey = exports.CATEGORIES_OBJECT_CACHE_PREFIX_KEY = exports.getCommentObjectsCachePrefixKeys = exports.getCommentObjectsCacheKey = exports.COMMENT_OBJECTS_CACHE_PREFIX_KEY = exports.getCommentObjectCacheKey = exports.COMMENT_OBJECT_CACHE_PREFIX_KEY = exports.getFeedObjectCacheKey = exports.FEED_OBJECT_CACHE_PREFIX_KEY = void 0;
|
|
7
|
+
exports.getWidgetStateCacheKey = exports.GROUPS_LIST_TOOLS_STATE_CACHE_PREFIX_KEY = exports.GROUP_REQUESTS_TOOLS_STATE_CACHE_PREFIX_KEY = exports.GROUP_MEMBERS_TOOLS_STATE_CACHE_PREFIX_KEY = exports.POLL_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = exports.INCUBATOR_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = void 0;
|
|
8
8
|
/** FEED OBJECT **/
|
|
9
9
|
exports.FEED_OBJECT_CACHE_PREFIX_KEY = '_fo_';
|
|
10
10
|
const getFeedObjectCacheKey = (id, type) => `${exports.FEED_OBJECT_CACHE_PREFIX_KEY}${type}_${id}`;
|
|
@@ -31,6 +31,10 @@ exports.getCategoryObjectCacheKey = getCategoryObjectCacheKey;
|
|
|
31
31
|
exports.GROUP_OBJECT_CACHE_PREFIX_KEY = '_grp_';
|
|
32
32
|
const getGroupObjectCacheKey = (id) => `${exports.GROUP_OBJECT_CACHE_PREFIX_KEY}${id}`;
|
|
33
33
|
exports.getGroupObjectCacheKey = getGroupObjectCacheKey;
|
|
34
|
+
/** GROUPS OBJECT **/
|
|
35
|
+
exports.GROUPS_OBJECT_CACHE_PREFIX_KEY = '_grps_';
|
|
36
|
+
const getGroupsObjectCacheKey = () => `${exports.GROUPS_OBJECT_CACHE_PREFIX_KEY}`;
|
|
37
|
+
exports.getGroupsObjectCacheKey = getGroupsObjectCacheKey;
|
|
34
38
|
/** INCUBATOR OBJECT **/
|
|
35
39
|
exports.INCUBATOR_OBJECT_CACHE_PREFIX_KEY = '_inc_';
|
|
36
40
|
const getIncubatorObjectCacheKey = (id) => `${exports.INCUBATOR_OBJECT_CACHE_PREFIX_KEY}${id}`;
|
|
@@ -69,6 +69,6 @@ exports.defaultRoutes = {
|
|
|
69
69
|
[exports.RECOVER_ROUTE_NAME]: '/recover/',
|
|
70
70
|
[exports.CUSTOM_PAGES_ROUTE_NAME]: '/:id/:slug/',
|
|
71
71
|
[exports.LEGAL_PAGES_ROUTE_NAME]: '/legal/:policy/',
|
|
72
|
-
[exports.GROUP_ROUTE_NAME]: '/group/:id/',
|
|
72
|
+
[exports.GROUP_ROUTE_NAME]: '/group/:id/:slug/',
|
|
73
73
|
[exports.GROUPS_ROUTE_NAME]: '/groups/',
|
|
74
74
|
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { SCGroupType } from '@selfcommunity/types';
|
|
2
|
+
import { CacheStrategies } from '@selfcommunity/utils';
|
|
3
|
+
/**
|
|
4
|
+
:::info
|
|
5
|
+
This custom hook is used to fetch groups.
|
|
6
|
+
@param object.cacheStrategy
|
|
7
|
+
|
|
8
|
+
:::tip Context can be consumed in this way:
|
|
9
|
+
|
|
10
|
+
```jsx
|
|
11
|
+
const {groups, isLoading} = useSCFetchGroups();
|
|
12
|
+
```
|
|
13
|
+
:::
|
|
14
|
+
* @param props
|
|
15
|
+
*/
|
|
16
|
+
declare const useSCFetchGroups: (props?: {
|
|
17
|
+
cacheStrategy?: CacheStrategies;
|
|
18
|
+
}) => {
|
|
19
|
+
groups: SCGroupType[];
|
|
20
|
+
isLoading: boolean;
|
|
21
|
+
};
|
|
22
|
+
export default useSCFetchGroups;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const Errors_1 = require("../constants/Errors");
|
|
6
|
+
const api_services_1 = require("@selfcommunity/api-services");
|
|
7
|
+
const utils_1 = require("@selfcommunity/utils");
|
|
8
|
+
const Cache_1 = require("../constants/Cache");
|
|
9
|
+
const init = { groups: [], isLoading: true };
|
|
10
|
+
// HYDRATE the cache
|
|
11
|
+
const hydrate = (ids) => {
|
|
12
|
+
if (!ids) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const groups = ids.map((id) => {
|
|
16
|
+
const __groupCacheKey = (0, Cache_1.getGroupObjectCacheKey)(id);
|
|
17
|
+
return utils_1.LRUCache.get(__groupCacheKey);
|
|
18
|
+
});
|
|
19
|
+
if (groups.filter((c) => !c).length > 0) {
|
|
20
|
+
// REVALIDATE CACHE
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
return groups;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
:::info
|
|
27
|
+
This custom hook is used to fetch groups.
|
|
28
|
+
@param object.cacheStrategy
|
|
29
|
+
|
|
30
|
+
:::tip Context can be consumed in this way:
|
|
31
|
+
|
|
32
|
+
```jsx
|
|
33
|
+
const {groups, isLoading} = useSCFetchGroups();
|
|
34
|
+
```
|
|
35
|
+
:::
|
|
36
|
+
* @param props
|
|
37
|
+
*/
|
|
38
|
+
const useSCFetchGroups = (props) => {
|
|
39
|
+
// PROPS
|
|
40
|
+
const { cacheStrategy = utils_1.CacheStrategies.CACHE_FIRST } = props || {};
|
|
41
|
+
// CACHE
|
|
42
|
+
const __groupsCacheKey = (0, Cache_1.getGroupsObjectCacheKey)();
|
|
43
|
+
// STATE
|
|
44
|
+
const groups = cacheStrategy !== utils_1.CacheStrategies.NETWORK_ONLY ? hydrate(utils_1.LRUCache.get(__groupsCacheKey, null)) : null;
|
|
45
|
+
const [data, setData] = (0, react_1.useState)(groups !== null ? { groups, isLoading: false } : init);
|
|
46
|
+
/**
|
|
47
|
+
* Fetch groups
|
|
48
|
+
*/
|
|
49
|
+
const fetchGroups = (next = api_services_1.Endpoints.GetAllGroups.url()) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
const response = yield api_services_1.http.request({
|
|
51
|
+
url: next,
|
|
52
|
+
method: api_services_1.Endpoints.GetAllGroups.method,
|
|
53
|
+
});
|
|
54
|
+
const data = response.data;
|
|
55
|
+
if (data.next) {
|
|
56
|
+
return data.results.concat(yield fetchGroups(data.next));
|
|
57
|
+
}
|
|
58
|
+
return data.results;
|
|
59
|
+
});
|
|
60
|
+
/**
|
|
61
|
+
* Get groups
|
|
62
|
+
*/
|
|
63
|
+
(0, react_1.useEffect)(() => {
|
|
64
|
+
if (cacheStrategy === utils_1.CacheStrategies.CACHE_FIRST && groups) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
fetchGroups()
|
|
68
|
+
.then((data) => {
|
|
69
|
+
setData({ groups: data, isLoading: false });
|
|
70
|
+
utils_1.LRUCache.set(__groupsCacheKey, data.map((group) => {
|
|
71
|
+
const __groupCacheKey = (0, Cache_1.getGroupObjectCacheKey)(group.id);
|
|
72
|
+
utils_1.LRUCache.set(__groupCacheKey, group);
|
|
73
|
+
return group.id;
|
|
74
|
+
}));
|
|
75
|
+
})
|
|
76
|
+
.catch((error) => {
|
|
77
|
+
console.log(error);
|
|
78
|
+
utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, 'Unable to retrieve groups');
|
|
79
|
+
});
|
|
80
|
+
}, []);
|
|
81
|
+
return data;
|
|
82
|
+
};
|
|
83
|
+
exports.default = useSCFetchGroups;
|
|
@@ -20,6 +20,7 @@ export default function useSCSubscribedGroupsManager(user?: SCUserType): {
|
|
|
20
20
|
id: number;
|
|
21
21
|
}) => boolean;
|
|
22
22
|
subscribe?: undefined;
|
|
23
|
+
unsubscribe?: undefined;
|
|
23
24
|
subscriptionStatus?: undefined;
|
|
24
25
|
refresh?: undefined;
|
|
25
26
|
emptyCache?: undefined;
|
|
@@ -30,6 +31,7 @@ export default function useSCSubscribedGroupsManager(user?: SCUserType): {
|
|
|
30
31
|
id: number;
|
|
31
32
|
}) => boolean;
|
|
32
33
|
subscribe: (group: SCGroupType) => Promise<any>;
|
|
34
|
+
unsubscribe: (group: SCGroupType) => Promise<any>;
|
|
33
35
|
subscriptionStatus: (group: SCGroupType) => string;
|
|
34
36
|
refresh: () => void;
|
|
35
37
|
emptyCache: () => void;
|
|
@@ -75,6 +75,28 @@ function useSCSubscribedGroupsManager(user) {
|
|
|
75
75
|
return Promise.resolve(res.data);
|
|
76
76
|
});
|
|
77
77
|
}, [data, loading, cache]);
|
|
78
|
+
/**
|
|
79
|
+
* Memoized subscribe Group
|
|
80
|
+
* Toggle action
|
|
81
|
+
*/
|
|
82
|
+
const unsubscribe = (0, react_1.useMemo)(() => (group) => {
|
|
83
|
+
setLoading(group.id);
|
|
84
|
+
return api_services_1.http
|
|
85
|
+
.request({
|
|
86
|
+
url: api_services_1.Endpoints.UnsubscribeFromGroup.url({ id: group.id }),
|
|
87
|
+
method: api_services_1.Endpoints.UnsubscribeFromGroup.method,
|
|
88
|
+
})
|
|
89
|
+
.then((res) => {
|
|
90
|
+
if (res.status >= 300) {
|
|
91
|
+
return Promise.reject(res);
|
|
92
|
+
}
|
|
93
|
+
updateCache([group.id]);
|
|
94
|
+
const isSubscribed = data.includes(group.id);
|
|
95
|
+
setData((prev) => (isSubscribed ? prev.filter((id) => id !== group.id) : [...[group.id], ...prev]));
|
|
96
|
+
setUnLoading(group.id);
|
|
97
|
+
return Promise.resolve(res.data);
|
|
98
|
+
});
|
|
99
|
+
}, [data, loading, cache]);
|
|
78
100
|
/**
|
|
79
101
|
* Check the authenticated user subscription status to the group
|
|
80
102
|
* Update the groups cached
|
|
@@ -171,6 +193,6 @@ function useSCSubscribedGroupsManager(user) {
|
|
|
171
193
|
if (!user) {
|
|
172
194
|
return { groups: data, loading, isLoading };
|
|
173
195
|
}
|
|
174
|
-
return { groups: data, loading, isLoading, subscribe, subscriptionStatus, refresh, emptyCache };
|
|
196
|
+
return { groups: data, loading, isLoading, subscribe, unsubscribe, subscriptionStatus, refresh, emptyCache };
|
|
175
197
|
}
|
|
176
198
|
exports.default = useSCSubscribedGroupsManager;
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -61,6 +61,7 @@ import useSCFetchBroadcastMessages from './hooks/useSCFetchBroadcastMessages';
|
|
|
61
61
|
import useSCFetchUserBlockedBy from './hooks/useSCFetchUserBlockedBy';
|
|
62
62
|
import useSCUserIsBlocked from './hooks/useSCUserIsBlocked';
|
|
63
63
|
import useSCFetchGroup from './hooks/useSCFetchGroup';
|
|
64
|
+
import useSCFetchGroups from './hooks/useSCFetchGroups';
|
|
64
65
|
/**
|
|
65
66
|
* Routing component
|
|
66
67
|
*/
|
|
@@ -80,4 +81,4 @@ import * as Locale from './constants/Locale';
|
|
|
80
81
|
/**
|
|
81
82
|
* List all exports
|
|
82
83
|
*/
|
|
83
|
-
export { SCUserContextType, SCFollowedCategoriesManagerType, SCContextProviderType, SCContextType, SCSettingsType, SCSessionType, SCSettingsManagerType, SCFollowedManagerType, SCFollowersManagerType, SCConnectionsManagerType, SCSubscribedIncubatorsManagerType, SCLocaleType, SCNotificationContextType, SCPreferencesContextType, SCThemeContextType, SCRoutingContextType, SCLocaleContextType, SCAlertMessagesContextType, SCThemeAvatarVariableType, SCThemeCategoryIconVariableType, SCThemeCategoryVariableType, SCThemeVariablesType, SCThemeType, SCSubscribedGroupsManagerType, SCContext, SCUserContext, SCThemeContext, SCRoutingContext, SCLocaleContext, SCPreferencesContext, useSCContext, SCContextProvider, SCUserProvider, useSCUser, useSCPreferences, SCThemeProvider, useSCTheme, withSCTheme, SCRoutingProvider, useSCRouting, SCLocaleProvider, useSCLocale, withSCLocale, SCPreferencesProvider, SCPreferences, SCFeatures, SCNotification, SCNotificationProvider, SCNotificationContext, useSCNotification, SCAlertMessagesProvider, SCAlertMessagesContext, useSCAlertMessages, Link, SCRoutes, SCCache, UserUtils, Locale, useSCFetchUser, useSCFetchUserProviders, useSCFetchVote, useSCFetchFeedObject, useSCFetchCommentObject, useSCFetchCommentObjects, useSCFetchCustomAdv, useSCFetchTag, useSCFetchAddressingTagList, useSCFetchCategory, useSCFetchCategories, useSCFetchIncubator, useSCMediaClick, useSCFetchContributors, useSCFetchFeed, useIsComponentMountedRef, usePreviousValue, useIsomorphicLayoutEffect, useEffectOnce, useNoInitialEffect, useSCFetchPrivateMessageSnippets, useSCFetchBroadcastMessages, useSCFetchUserBlockedBy, useSCUserIsBlocked, useSCFetchGroup, };
|
|
84
|
+
export { SCUserContextType, SCFollowedCategoriesManagerType, SCContextProviderType, SCContextType, SCSettingsType, SCSessionType, SCSettingsManagerType, SCFollowedManagerType, SCFollowersManagerType, SCConnectionsManagerType, SCSubscribedIncubatorsManagerType, SCLocaleType, SCNotificationContextType, SCPreferencesContextType, SCThemeContextType, SCRoutingContextType, SCLocaleContextType, SCAlertMessagesContextType, SCThemeAvatarVariableType, SCThemeCategoryIconVariableType, SCThemeCategoryVariableType, SCThemeVariablesType, SCThemeType, SCSubscribedGroupsManagerType, SCContext, SCUserContext, SCThemeContext, SCRoutingContext, SCLocaleContext, SCPreferencesContext, useSCContext, SCContextProvider, SCUserProvider, useSCUser, useSCPreferences, SCThemeProvider, useSCTheme, withSCTheme, SCRoutingProvider, useSCRouting, SCLocaleProvider, useSCLocale, withSCLocale, SCPreferencesProvider, SCPreferences, SCFeatures, SCNotification, SCNotificationProvider, SCNotificationContext, useSCNotification, SCAlertMessagesProvider, SCAlertMessagesContext, useSCAlertMessages, Link, SCRoutes, SCCache, UserUtils, Locale, useSCFetchUser, useSCFetchUserProviders, useSCFetchVote, useSCFetchFeedObject, useSCFetchCommentObject, useSCFetchCommentObjects, useSCFetchCustomAdv, useSCFetchTag, useSCFetchAddressingTagList, useSCFetchCategory, useSCFetchCategories, useSCFetchIncubator, useSCMediaClick, useSCFetchContributors, useSCFetchFeed, useIsComponentMountedRef, usePreviousValue, useIsomorphicLayoutEffect, useEffectOnce, useNoInitialEffect, useSCFetchPrivateMessageSnippets, useSCFetchBroadcastMessages, useSCFetchUserBlockedBy, useSCUserIsBlocked, useSCFetchGroup, useSCFetchGroups, };
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useIsComponentMountedRef = exports.useSCFetchFeed = exports.useSCFetchContributors = exports.useSCMediaClick = exports.useSCFetchIncubator = exports.useSCFetchCategories = exports.useSCFetchCategory = exports.useSCFetchAddressingTagList = exports.useSCFetchTag = exports.useSCFetchCustomAdv = exports.useSCFetchCommentObjects = exports.useSCFetchCommentObject = exports.useSCFetchFeedObject = exports.useSCFetchVote = exports.useSCFetchUserProviders = exports.useSCFetchUser = exports.Locale = exports.UserUtils = exports.SCCache = exports.SCRoutes = exports.Link = exports.useSCAlertMessages = exports.SCAlertMessagesContext = exports.SCAlertMessagesProvider = exports.useSCNotification = exports.SCNotificationContext = exports.SCNotificationProvider = exports.SCNotification = exports.SCFeatures = exports.SCPreferences = exports.SCPreferencesProvider = exports.withSCLocale = exports.useSCLocale = exports.SCLocaleProvider = exports.useSCRouting = exports.SCRoutingProvider = exports.withSCTheme = exports.useSCTheme = exports.SCThemeProvider = exports.useSCPreferences = exports.useSCUser = exports.SCUserProvider = exports.SCContextProvider = exports.useSCContext = exports.SCPreferencesContext = exports.SCLocaleContext = exports.SCRoutingContext = exports.SCThemeContext = exports.SCUserContext = exports.SCContext = void 0;
|
|
4
|
-
exports.useSCFetchGroup = exports.useSCUserIsBlocked = exports.useSCFetchUserBlockedBy = exports.useSCFetchBroadcastMessages = exports.useSCFetchPrivateMessageSnippets = exports.useNoInitialEffect = exports.useEffectOnce = exports.useIsomorphicLayoutEffect = exports.usePreviousValue = void 0;
|
|
4
|
+
exports.useSCFetchGroups = exports.useSCFetchGroup = exports.useSCUserIsBlocked = exports.useSCFetchUserBlockedBy = exports.useSCFetchBroadcastMessages = exports.useSCFetchPrivateMessageSnippets = exports.useNoInitialEffect = exports.useEffectOnce = exports.useIsomorphicLayoutEffect = exports.usePreviousValue = void 0;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
/**
|
|
7
7
|
* ContextProvider component
|
|
@@ -112,6 +112,8 @@ const useSCUserIsBlocked_1 = tslib_1.__importDefault(require("./hooks/useSCUserI
|
|
|
112
112
|
exports.useSCUserIsBlocked = useSCUserIsBlocked_1.default;
|
|
113
113
|
const useSCFetchGroup_1 = tslib_1.__importDefault(require("./hooks/useSCFetchGroup"));
|
|
114
114
|
exports.useSCFetchGroup = useSCFetchGroup_1.default;
|
|
115
|
+
const useSCFetchGroups_1 = tslib_1.__importDefault(require("./hooks/useSCFetchGroups"));
|
|
116
|
+
exports.useSCFetchGroups = useSCFetchGroups_1.default;
|
|
115
117
|
/**
|
|
116
118
|
* Routing component
|
|
117
119
|
*/
|
|
@@ -266,6 +266,10 @@ export interface SCSubscribedGroupsManagerType {
|
|
|
266
266
|
* Handle user subscription to a group
|
|
267
267
|
*/
|
|
268
268
|
subscribe?: (group: SCGroupType) => Promise<any>;
|
|
269
|
+
/**
|
|
270
|
+
* Handle user unsubscription from a group
|
|
271
|
+
*/
|
|
272
|
+
unsubscribe?: (group: SCGroupType) => Promise<any>;
|
|
269
273
|
/**
|
|
270
274
|
* Handles a user subscription status to a group, caching data
|
|
271
275
|
*/
|
|
@@ -20,6 +20,9 @@ export declare const getCategoryObjectCacheKey: (id: any) => string;
|
|
|
20
20
|
/** GROUP OBJECT **/
|
|
21
21
|
export declare const GROUP_OBJECT_CACHE_PREFIX_KEY = "_grp_";
|
|
22
22
|
export declare const getGroupObjectCacheKey: (id: any) => string;
|
|
23
|
+
/** GROUPS OBJECT **/
|
|
24
|
+
export declare const GROUPS_OBJECT_CACHE_PREFIX_KEY = "_grps_";
|
|
25
|
+
export declare const getGroupsObjectCacheKey: () => string;
|
|
23
26
|
/** INCUBATOR OBJECT **/
|
|
24
27
|
export declare const INCUBATOR_OBJECT_CACHE_PREFIX_KEY = "_inc_";
|
|
25
28
|
export declare const getIncubatorObjectCacheKey: (id: any) => string;
|
|
@@ -20,6 +20,9 @@ export const getCategoryObjectCacheKey = (id) => `${CATEGORY_OBJECT_CACHE_PREFIX
|
|
|
20
20
|
/** GROUP OBJECT **/
|
|
21
21
|
export const GROUP_OBJECT_CACHE_PREFIX_KEY = '_grp_';
|
|
22
22
|
export const getGroupObjectCacheKey = (id) => `${GROUP_OBJECT_CACHE_PREFIX_KEY}${id}`;
|
|
23
|
+
/** GROUPS OBJECT **/
|
|
24
|
+
export const GROUPS_OBJECT_CACHE_PREFIX_KEY = '_grps_';
|
|
25
|
+
export const getGroupsObjectCacheKey = () => `${GROUPS_OBJECT_CACHE_PREFIX_KEY}`;
|
|
23
26
|
/** INCUBATOR OBJECT **/
|
|
24
27
|
export const INCUBATOR_OBJECT_CACHE_PREFIX_KEY = '_inc_';
|
|
25
28
|
export const getIncubatorObjectCacheKey = (id) => `${INCUBATOR_OBJECT_CACHE_PREFIX_KEY}${id}`;
|
|
@@ -66,6 +66,6 @@ export const defaultRoutes = {
|
|
|
66
66
|
[RECOVER_ROUTE_NAME]: '/recover/',
|
|
67
67
|
[CUSTOM_PAGES_ROUTE_NAME]: '/:id/:slug/',
|
|
68
68
|
[LEGAL_PAGES_ROUTE_NAME]: '/legal/:policy/',
|
|
69
|
-
[GROUP_ROUTE_NAME]: '/group/:id/',
|
|
69
|
+
[GROUP_ROUTE_NAME]: '/group/:id/:slug/',
|
|
70
70
|
[GROUPS_ROUTE_NAME]: '/groups/',
|
|
71
71
|
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { SCGroupType } from '@selfcommunity/types';
|
|
2
|
+
import { CacheStrategies } from '@selfcommunity/utils';
|
|
3
|
+
/**
|
|
4
|
+
:::info
|
|
5
|
+
This custom hook is used to fetch groups.
|
|
6
|
+
@param object.cacheStrategy
|
|
7
|
+
|
|
8
|
+
:::tip Context can be consumed in this way:
|
|
9
|
+
|
|
10
|
+
```jsx
|
|
11
|
+
const {groups, isLoading} = useSCFetchGroups();
|
|
12
|
+
```
|
|
13
|
+
:::
|
|
14
|
+
* @param props
|
|
15
|
+
*/
|
|
16
|
+
declare const useSCFetchGroups: (props?: {
|
|
17
|
+
cacheStrategy?: CacheStrategies;
|
|
18
|
+
}) => {
|
|
19
|
+
groups: SCGroupType[];
|
|
20
|
+
isLoading: boolean;
|
|
21
|
+
};
|
|
22
|
+
export default useSCFetchGroups;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import { SCOPE_SC_CORE } from '../constants/Errors';
|
|
4
|
+
import { Endpoints, http } from '@selfcommunity/api-services';
|
|
5
|
+
import { CacheStrategies, Logger, LRUCache } from '@selfcommunity/utils';
|
|
6
|
+
import { getGroupObjectCacheKey, getGroupsObjectCacheKey } from '../constants/Cache';
|
|
7
|
+
const init = { groups: [], isLoading: true };
|
|
8
|
+
// HYDRATE the cache
|
|
9
|
+
const hydrate = (ids) => {
|
|
10
|
+
if (!ids) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
const groups = ids.map((id) => {
|
|
14
|
+
const __groupCacheKey = getGroupObjectCacheKey(id);
|
|
15
|
+
return LRUCache.get(__groupCacheKey);
|
|
16
|
+
});
|
|
17
|
+
if (groups.filter((c) => !c).length > 0) {
|
|
18
|
+
// REVALIDATE CACHE
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return groups;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
:::info
|
|
25
|
+
This custom hook is used to fetch groups.
|
|
26
|
+
@param object.cacheStrategy
|
|
27
|
+
|
|
28
|
+
:::tip Context can be consumed in this way:
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
const {groups, isLoading} = useSCFetchGroups();
|
|
32
|
+
```
|
|
33
|
+
:::
|
|
34
|
+
* @param props
|
|
35
|
+
*/
|
|
36
|
+
const useSCFetchGroups = (props) => {
|
|
37
|
+
// PROPS
|
|
38
|
+
const { cacheStrategy = CacheStrategies.CACHE_FIRST } = props || {};
|
|
39
|
+
// CACHE
|
|
40
|
+
const __groupsCacheKey = getGroupsObjectCacheKey();
|
|
41
|
+
// STATE
|
|
42
|
+
const groups = cacheStrategy !== CacheStrategies.NETWORK_ONLY ? hydrate(LRUCache.get(__groupsCacheKey, null)) : null;
|
|
43
|
+
const [data, setData] = useState(groups !== null ? { groups, isLoading: false } : init);
|
|
44
|
+
/**
|
|
45
|
+
* Fetch groups
|
|
46
|
+
*/
|
|
47
|
+
const fetchGroups = (next = Endpoints.GetAllGroups.url()) => __awaiter(void 0, void 0, void 0, function* () {
|
|
48
|
+
const response = yield http.request({
|
|
49
|
+
url: next,
|
|
50
|
+
method: Endpoints.GetAllGroups.method,
|
|
51
|
+
});
|
|
52
|
+
const data = response.data;
|
|
53
|
+
if (data.next) {
|
|
54
|
+
return data.results.concat(yield fetchGroups(data.next));
|
|
55
|
+
}
|
|
56
|
+
return data.results;
|
|
57
|
+
});
|
|
58
|
+
/**
|
|
59
|
+
* Get groups
|
|
60
|
+
*/
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (cacheStrategy === CacheStrategies.CACHE_FIRST && groups) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
fetchGroups()
|
|
66
|
+
.then((data) => {
|
|
67
|
+
setData({ groups: data, isLoading: false });
|
|
68
|
+
LRUCache.set(__groupsCacheKey, data.map((group) => {
|
|
69
|
+
const __groupCacheKey = getGroupObjectCacheKey(group.id);
|
|
70
|
+
LRUCache.set(__groupCacheKey, group);
|
|
71
|
+
return group.id;
|
|
72
|
+
}));
|
|
73
|
+
})
|
|
74
|
+
.catch((error) => {
|
|
75
|
+
console.log(error);
|
|
76
|
+
Logger.error(SCOPE_SC_CORE, 'Unable to retrieve groups');
|
|
77
|
+
});
|
|
78
|
+
}, []);
|
|
79
|
+
return data;
|
|
80
|
+
};
|
|
81
|
+
export default useSCFetchGroups;
|
|
@@ -20,6 +20,7 @@ export default function useSCSubscribedGroupsManager(user?: SCUserType): {
|
|
|
20
20
|
id: number;
|
|
21
21
|
}) => boolean;
|
|
22
22
|
subscribe?: undefined;
|
|
23
|
+
unsubscribe?: undefined;
|
|
23
24
|
subscriptionStatus?: undefined;
|
|
24
25
|
refresh?: undefined;
|
|
25
26
|
emptyCache?: undefined;
|
|
@@ -30,6 +31,7 @@ export default function useSCSubscribedGroupsManager(user?: SCUserType): {
|
|
|
30
31
|
id: number;
|
|
31
32
|
}) => boolean;
|
|
32
33
|
subscribe: (group: SCGroupType) => Promise<any>;
|
|
34
|
+
unsubscribe: (group: SCGroupType) => Promise<any>;
|
|
33
35
|
subscriptionStatus: (group: SCGroupType) => string;
|
|
34
36
|
refresh: () => void;
|
|
35
37
|
emptyCache: () => void;
|
|
@@ -72,6 +72,28 @@ export default function useSCSubscribedGroupsManager(user) {
|
|
|
72
72
|
return Promise.resolve(res.data);
|
|
73
73
|
});
|
|
74
74
|
}, [data, loading, cache]);
|
|
75
|
+
/**
|
|
76
|
+
* Memoized subscribe Group
|
|
77
|
+
* Toggle action
|
|
78
|
+
*/
|
|
79
|
+
const unsubscribe = useMemo(() => (group) => {
|
|
80
|
+
setLoading(group.id);
|
|
81
|
+
return http
|
|
82
|
+
.request({
|
|
83
|
+
url: Endpoints.UnsubscribeFromGroup.url({ id: group.id }),
|
|
84
|
+
method: Endpoints.UnsubscribeFromGroup.method,
|
|
85
|
+
})
|
|
86
|
+
.then((res) => {
|
|
87
|
+
if (res.status >= 300) {
|
|
88
|
+
return Promise.reject(res);
|
|
89
|
+
}
|
|
90
|
+
updateCache([group.id]);
|
|
91
|
+
const isSubscribed = data.includes(group.id);
|
|
92
|
+
setData((prev) => (isSubscribed ? prev.filter((id) => id !== group.id) : [...[group.id], ...prev]));
|
|
93
|
+
setUnLoading(group.id);
|
|
94
|
+
return Promise.resolve(res.data);
|
|
95
|
+
});
|
|
96
|
+
}, [data, loading, cache]);
|
|
75
97
|
/**
|
|
76
98
|
* Check the authenticated user subscription status to the group
|
|
77
99
|
* Update the groups cached
|
|
@@ -168,5 +190,5 @@ export default function useSCSubscribedGroupsManager(user) {
|
|
|
168
190
|
if (!user) {
|
|
169
191
|
return { groups: data, loading, isLoading };
|
|
170
192
|
}
|
|
171
|
-
return { groups: data, loading, isLoading, subscribe, subscriptionStatus, refresh, emptyCache };
|
|
193
|
+
return { groups: data, loading, isLoading, subscribe, unsubscribe, subscriptionStatus, refresh, emptyCache };
|
|
172
194
|
}
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -61,6 +61,7 @@ import useSCFetchBroadcastMessages from './hooks/useSCFetchBroadcastMessages';
|
|
|
61
61
|
import useSCFetchUserBlockedBy from './hooks/useSCFetchUserBlockedBy';
|
|
62
62
|
import useSCUserIsBlocked from './hooks/useSCUserIsBlocked';
|
|
63
63
|
import useSCFetchGroup from './hooks/useSCFetchGroup';
|
|
64
|
+
import useSCFetchGroups from './hooks/useSCFetchGroups';
|
|
64
65
|
/**
|
|
65
66
|
* Routing component
|
|
66
67
|
*/
|
|
@@ -80,4 +81,4 @@ import * as Locale from './constants/Locale';
|
|
|
80
81
|
/**
|
|
81
82
|
* List all exports
|
|
82
83
|
*/
|
|
83
|
-
export { SCUserContextType, SCFollowedCategoriesManagerType, SCContextProviderType, SCContextType, SCSettingsType, SCSessionType, SCSettingsManagerType, SCFollowedManagerType, SCFollowersManagerType, SCConnectionsManagerType, SCSubscribedIncubatorsManagerType, SCLocaleType, SCNotificationContextType, SCPreferencesContextType, SCThemeContextType, SCRoutingContextType, SCLocaleContextType, SCAlertMessagesContextType, SCThemeAvatarVariableType, SCThemeCategoryIconVariableType, SCThemeCategoryVariableType, SCThemeVariablesType, SCThemeType, SCSubscribedGroupsManagerType, SCContext, SCUserContext, SCThemeContext, SCRoutingContext, SCLocaleContext, SCPreferencesContext, useSCContext, SCContextProvider, SCUserProvider, useSCUser, useSCPreferences, SCThemeProvider, useSCTheme, withSCTheme, SCRoutingProvider, useSCRouting, SCLocaleProvider, useSCLocale, withSCLocale, SCPreferencesProvider, SCPreferences, SCFeatures, SCNotification, SCNotificationProvider, SCNotificationContext, useSCNotification, SCAlertMessagesProvider, SCAlertMessagesContext, useSCAlertMessages, Link, SCRoutes, SCCache, UserUtils, Locale, useSCFetchUser, useSCFetchUserProviders, useSCFetchVote, useSCFetchFeedObject, useSCFetchCommentObject, useSCFetchCommentObjects, useSCFetchCustomAdv, useSCFetchTag, useSCFetchAddressingTagList, useSCFetchCategory, useSCFetchCategories, useSCFetchIncubator, useSCMediaClick, useSCFetchContributors, useSCFetchFeed, useIsComponentMountedRef, usePreviousValue, useIsomorphicLayoutEffect, useEffectOnce, useNoInitialEffect, useSCFetchPrivateMessageSnippets, useSCFetchBroadcastMessages, useSCFetchUserBlockedBy, useSCUserIsBlocked, useSCFetchGroup, };
|
|
84
|
+
export { SCUserContextType, SCFollowedCategoriesManagerType, SCContextProviderType, SCContextType, SCSettingsType, SCSessionType, SCSettingsManagerType, SCFollowedManagerType, SCFollowersManagerType, SCConnectionsManagerType, SCSubscribedIncubatorsManagerType, SCLocaleType, SCNotificationContextType, SCPreferencesContextType, SCThemeContextType, SCRoutingContextType, SCLocaleContextType, SCAlertMessagesContextType, SCThemeAvatarVariableType, SCThemeCategoryIconVariableType, SCThemeCategoryVariableType, SCThemeVariablesType, SCThemeType, SCSubscribedGroupsManagerType, SCContext, SCUserContext, SCThemeContext, SCRoutingContext, SCLocaleContext, SCPreferencesContext, useSCContext, SCContextProvider, SCUserProvider, useSCUser, useSCPreferences, SCThemeProvider, useSCTheme, withSCTheme, SCRoutingProvider, useSCRouting, SCLocaleProvider, useSCLocale, withSCLocale, SCPreferencesProvider, SCPreferences, SCFeatures, SCNotification, SCNotificationProvider, SCNotificationContext, useSCNotification, SCAlertMessagesProvider, SCAlertMessagesContext, useSCAlertMessages, Link, SCRoutes, SCCache, UserUtils, Locale, useSCFetchUser, useSCFetchUserProviders, useSCFetchVote, useSCFetchFeedObject, useSCFetchCommentObject, useSCFetchCommentObjects, useSCFetchCustomAdv, useSCFetchTag, useSCFetchAddressingTagList, useSCFetchCategory, useSCFetchCategories, useSCFetchIncubator, useSCMediaClick, useSCFetchContributors, useSCFetchFeed, useIsComponentMountedRef, usePreviousValue, useIsomorphicLayoutEffect, useEffectOnce, useNoInitialEffect, useSCFetchPrivateMessageSnippets, useSCFetchBroadcastMessages, useSCFetchUserBlockedBy, useSCUserIsBlocked, useSCFetchGroup, useSCFetchGroups, };
|
package/lib/esm/index.js
CHANGED
|
@@ -57,6 +57,7 @@ import useSCFetchBroadcastMessages from './hooks/useSCFetchBroadcastMessages';
|
|
|
57
57
|
import useSCFetchUserBlockedBy from './hooks/useSCFetchUserBlockedBy';
|
|
58
58
|
import useSCUserIsBlocked from './hooks/useSCUserIsBlocked';
|
|
59
59
|
import useSCFetchGroup from './hooks/useSCFetchGroup';
|
|
60
|
+
import useSCFetchGroups from './hooks/useSCFetchGroups';
|
|
60
61
|
/**
|
|
61
62
|
* Routing component
|
|
62
63
|
*/
|
|
@@ -76,4 +77,4 @@ import * as Locale from './constants/Locale';
|
|
|
76
77
|
/**
|
|
77
78
|
* List all exports
|
|
78
79
|
*/
|
|
79
|
-
export { SCContext, SCUserContext, SCThemeContext, SCRoutingContext, SCLocaleContext, SCPreferencesContext, useSCContext, SCContextProvider, SCUserProvider, useSCUser, useSCPreferences, SCThemeProvider, useSCTheme, withSCTheme, SCRoutingProvider, useSCRouting, SCLocaleProvider, useSCLocale, withSCLocale, SCPreferencesProvider, SCPreferences, SCFeatures, SCNotification, SCNotificationProvider, SCNotificationContext, useSCNotification, SCAlertMessagesProvider, SCAlertMessagesContext, useSCAlertMessages, Link, SCRoutes, SCCache, UserUtils, Locale, useSCFetchUser, useSCFetchUserProviders, useSCFetchVote, useSCFetchFeedObject, useSCFetchCommentObject, useSCFetchCommentObjects, useSCFetchCustomAdv, useSCFetchTag, useSCFetchAddressingTagList, useSCFetchCategory, useSCFetchCategories, useSCFetchIncubator, useSCMediaClick, useSCFetchContributors, useSCFetchFeed, useIsComponentMountedRef, usePreviousValue, useIsomorphicLayoutEffect, useEffectOnce, useNoInitialEffect, useSCFetchPrivateMessageSnippets, useSCFetchBroadcastMessages, useSCFetchUserBlockedBy, useSCUserIsBlocked, useSCFetchGroup, };
|
|
80
|
+
export { SCContext, SCUserContext, SCThemeContext, SCRoutingContext, SCLocaleContext, SCPreferencesContext, useSCContext, SCContextProvider, SCUserProvider, useSCUser, useSCPreferences, SCThemeProvider, useSCTheme, withSCTheme, SCRoutingProvider, useSCRouting, SCLocaleProvider, useSCLocale, withSCLocale, SCPreferencesProvider, SCPreferences, SCFeatures, SCNotification, SCNotificationProvider, SCNotificationContext, useSCNotification, SCAlertMessagesProvider, SCAlertMessagesContext, useSCAlertMessages, Link, SCRoutes, SCCache, UserUtils, Locale, useSCFetchUser, useSCFetchUserProviders, useSCFetchVote, useSCFetchFeedObject, useSCFetchCommentObject, useSCFetchCommentObjects, useSCFetchCustomAdv, useSCFetchTag, useSCFetchAddressingTagList, useSCFetchCategory, useSCFetchCategories, useSCFetchIncubator, useSCMediaClick, useSCFetchContributors, useSCFetchFeed, useIsComponentMountedRef, usePreviousValue, useIsomorphicLayoutEffect, useEffectOnce, useNoInitialEffect, useSCFetchPrivateMessageSnippets, useSCFetchBroadcastMessages, useSCFetchUserBlockedBy, useSCUserIsBlocked, useSCFetchGroup, useSCFetchGroups, };
|
|
@@ -266,6 +266,10 @@ export interface SCSubscribedGroupsManagerType {
|
|
|
266
266
|
* Handle user subscription to a group
|
|
267
267
|
*/
|
|
268
268
|
subscribe?: (group: SCGroupType) => Promise<any>;
|
|
269
|
+
/**
|
|
270
|
+
* Handle user unsubscription from a group
|
|
271
|
+
*/
|
|
272
|
+
unsubscribe?: (group: SCGroupType) => Promise<any>;
|
|
269
273
|
/**
|
|
270
274
|
* Handles a user subscription status to a group, caching data
|
|
271
275
|
*/
|