@selfcommunity/react-core 0.4.9-alpha.2 → 0.4.9-alpha.21

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.
@@ -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.POLL_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = exports.INCUBATOR_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = 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.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 = 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}`;
@@ -32,6 +32,8 @@ export declare const RECOVER_ROUTE_NAME = "recover";
32
32
  export declare const CUSTOM_PAGES_ROUTE_NAME = "custom_pages";
33
33
  export declare const LEGAL_PAGES_ROUTE_NAME = "legal_pages";
34
34
  export declare const GROUP_ROUTE_NAME = "group";
35
+ export declare const GROUP_MEMBERS_ROUTE_NAME = "group_members";
36
+ export declare const GROUP_MESSAGES_ROUTE_NAME = "group_messages";
35
37
  export declare const GROUPS_ROUTE_NAME = "groups";
36
38
  /**
37
39
  * Default Routes
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.defaultRoutes = exports.GROUPS_ROUTE_NAME = exports.GROUP_ROUTE_NAME = exports.LEGAL_PAGES_ROUTE_NAME = exports.CUSTOM_PAGES_ROUTE_NAME = exports.RECOVER_ROUTE_NAME = exports.SIGNUP_ROUTE_NAME = exports.SIGNIN_ROUTE_NAME = exports.INCUBATOR_ROUTE_NAME = exports.LOYALTY_ROUTE_NAME = exports.USER_PROFILE_FOLLOWED_DISCUSSIONS_ROUTE_NAME = exports.USER_PROFILE_FOLLOWED_POSTS_ROUTE_NAME = exports.USER_PROFILE_CATEGORIES_ROUTE_NAME = exports.USER_PROFILE_CONNECTIONS_REQUESTS_SENT_ROUTE_NAME = exports.USER_PROFILE_CONNECTIONS_REQUESTS_ROUTE_NAME = exports.USER_PROFILE_CONNECTIONS_ROUTE_NAME = exports.USER_PROFILE_FOLLOWERS_ROUTE_NAME = exports.USER_PROFILE_FOLLOWINGS_ROUTE_NAME = exports.USER_PRIVATE_MESSAGES_ROUTE_NAME = exports.USER_NOTIFICATIONS_ROUTE_NAME = exports.USER_PROFILE_SETTINGS_ROUTE_NAME = exports.USER_PROFILE_ROUTE_NAME = exports.CATEGORIES_LIST_ROUTE_NAME = exports.CATEGORY_TRENDING_FEED_ROUTE_NAME = exports.CATEGORY_ROUTE_NAME = exports.COMMENT_ROUTE_NAME = exports.STATUS_ROUTE_NAME = exports.DISCUSSION_ROUTE_NAME = exports.POST_ROUTE_NAME = exports.EXPLORE_ROUTE_NAME = exports.HOME_ROUTE_NAME = exports.ROUTER_OPTION = exports.PORTAL_OPTION = void 0;
3
+ exports.defaultRoutes = exports.GROUPS_ROUTE_NAME = exports.GROUP_MESSAGES_ROUTE_NAME = exports.GROUP_MEMBERS_ROUTE_NAME = exports.GROUP_ROUTE_NAME = exports.LEGAL_PAGES_ROUTE_NAME = exports.CUSTOM_PAGES_ROUTE_NAME = exports.RECOVER_ROUTE_NAME = exports.SIGNUP_ROUTE_NAME = exports.SIGNIN_ROUTE_NAME = exports.INCUBATOR_ROUTE_NAME = exports.LOYALTY_ROUTE_NAME = exports.USER_PROFILE_FOLLOWED_DISCUSSIONS_ROUTE_NAME = exports.USER_PROFILE_FOLLOWED_POSTS_ROUTE_NAME = exports.USER_PROFILE_CATEGORIES_ROUTE_NAME = exports.USER_PROFILE_CONNECTIONS_REQUESTS_SENT_ROUTE_NAME = exports.USER_PROFILE_CONNECTIONS_REQUESTS_ROUTE_NAME = exports.USER_PROFILE_CONNECTIONS_ROUTE_NAME = exports.USER_PROFILE_FOLLOWERS_ROUTE_NAME = exports.USER_PROFILE_FOLLOWINGS_ROUTE_NAME = exports.USER_PRIVATE_MESSAGES_ROUTE_NAME = exports.USER_NOTIFICATIONS_ROUTE_NAME = exports.USER_PROFILE_SETTINGS_ROUTE_NAME = exports.USER_PROFILE_ROUTE_NAME = exports.CATEGORIES_LIST_ROUTE_NAME = exports.CATEGORY_TRENDING_FEED_ROUTE_NAME = exports.CATEGORY_ROUTE_NAME = exports.COMMENT_ROUTE_NAME = exports.STATUS_ROUTE_NAME = exports.DISCUSSION_ROUTE_NAME = exports.POST_ROUTE_NAME = exports.EXPLORE_ROUTE_NAME = exports.HOME_ROUTE_NAME = exports.ROUTER_OPTION = exports.PORTAL_OPTION = void 0;
4
4
  exports.PORTAL_OPTION = 'portal';
5
5
  exports.ROUTER_OPTION = 'router';
6
6
  /**
@@ -35,6 +35,8 @@ exports.RECOVER_ROUTE_NAME = 'recover';
35
35
  exports.CUSTOM_PAGES_ROUTE_NAME = 'custom_pages';
36
36
  exports.LEGAL_PAGES_ROUTE_NAME = 'legal_pages';
37
37
  exports.GROUP_ROUTE_NAME = 'group';
38
+ exports.GROUP_MEMBERS_ROUTE_NAME = 'group_members';
39
+ exports.GROUP_MESSAGES_ROUTE_NAME = 'group_messages';
38
40
  exports.GROUPS_ROUTE_NAME = 'groups';
39
41
  /**
40
42
  * Default Routes
@@ -69,6 +71,8 @@ exports.defaultRoutes = {
69
71
  [exports.RECOVER_ROUTE_NAME]: '/recover/',
70
72
  [exports.CUSTOM_PAGES_ROUTE_NAME]: '/:id/:slug/',
71
73
  [exports.LEGAL_PAGES_ROUTE_NAME]: '/legal/:policy/',
72
- [exports.GROUP_ROUTE_NAME]: '/group/:id/',
74
+ [exports.GROUP_ROUTE_NAME]: '/group/:id/:slug/',
75
+ [exports.GROUP_MEMBERS_ROUTE_NAME]: '/group/:id/:slug/members/',
76
+ [exports.GROUP_MESSAGES_ROUTE_NAME]: '/group/:id/:slug/messages/',
73
77
  [exports.GROUPS_ROUTE_NAME]: '/groups/',
74
78
  };
@@ -60,14 +60,14 @@ function useSCFetchGroup({ id = null, group = null, cacheStrategy = utils_1.Cach
60
60
  utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, err.message);
61
61
  });
62
62
  }
63
- }, [__groupId]);
63
+ }, [__groupId, authUserId]);
64
64
  (0, use_deep_compare_effect_1.useDeepCompareEffectNoCheck)(() => {
65
65
  if (group) {
66
66
  const _c = authUserId ? group : (0, utils_1.objectWithoutProperties)(group, ['subscription_status']);
67
67
  setSCGroup(_c);
68
68
  utils_1.LRUCache.set(__groupCacheKey, _c);
69
69
  }
70
- }, [group]);
70
+ }, [group, authUserId]);
71
71
  return { scGroup, setSCGroup, error };
72
72
  }
73
73
  exports.default = useSCFetchGroup;
@@ -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.GetUserGroups.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.GetUserGroups.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;
@@ -29,7 +30,8 @@ export default function useSCSubscribedGroupsManager(user?: SCUserType): {
29
30
  isLoading: (v: number | {
30
31
  id: number;
31
32
  }) => boolean;
32
- subscribe: (group: SCGroupType) => Promise<any>;
33
+ subscribe: (group: SCGroupType, userId?: number) => Promise<any>;
34
+ unsubscribe: (group: SCGroupType) => Promise<any>;
33
35
  subscriptionStatus: (group: SCGroupType) => string;
34
36
  refresh: () => void;
35
37
  emptyCache: () => void;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  const react_1 = require("react");
5
5
  const api_services_1 = require("@selfcommunity/api-services");
6
+ const types_1 = require("@selfcommunity/types");
6
7
  const useSCCachingManager_1 = tslib_1.__importDefault(require("./useSCCachingManager"));
7
8
  const Errors_1 = require("../constants/Errors");
8
9
  const utils_1 = require("@selfcommunity/utils");
@@ -57,23 +58,66 @@ function useSCSubscribedGroupsManager(user) {
57
58
  * Memoized subscribe Group
58
59
  * Toggle action
59
60
  */
60
- const subscribe = (0, react_1.useMemo)(() => (group) => {
61
+ const subscribe = (0, react_1.useMemo)(() => (group, userId) => {
61
62
  setLoading(group.id);
62
- return api_services_1.http
63
- .request({
64
- url: api_services_1.Endpoints.SubscribeToGroup.url({ id: group.id }),
65
- method: api_services_1.Endpoints.SubscribeToGroup.method,
66
- })
67
- .then((res) => {
68
- if (res.status >= 300) {
69
- return Promise.reject(res);
70
- }
71
- updateCache([group.id]);
72
- const isSubscribed = data.includes(group.id);
73
- setData((prev) => (isSubscribed ? prev.filter((id) => id !== group.id) : [...[group.id], ...prev]));
74
- setUnLoading(group.id);
75
- return Promise.resolve(res.data);
76
- });
63
+ if (userId) {
64
+ return api_services_1.http
65
+ .request({
66
+ url: api_services_1.Endpoints.InviteOrAcceptGroupRequest.url({ id: group.id }),
67
+ method: api_services_1.Endpoints.InviteOrAcceptGroupRequest.method,
68
+ data: { users: [userId] },
69
+ })
70
+ .then((res) => {
71
+ if (res.status >= 300) {
72
+ return Promise.reject(res);
73
+ }
74
+ updateCache([group.id]);
75
+ setData((prev) => getDataUpdated(prev, group.id, types_1.SCGroupSubscriptionStatusType.SUBSCRIBED));
76
+ setUnLoading(group.id);
77
+ return Promise.resolve(res.data);
78
+ });
79
+ }
80
+ else {
81
+ return api_services_1.http
82
+ .request({
83
+ url: api_services_1.Endpoints.SubscribeToGroup.url({ id: group.id }),
84
+ method: api_services_1.Endpoints.SubscribeToGroup.method,
85
+ })
86
+ .then((res) => {
87
+ if (res.status >= 300) {
88
+ return Promise.reject(res);
89
+ }
90
+ updateCache([group.id]);
91
+ setData((prev) => getDataUpdated(prev, group.id, group.privacy === types_1.SCGroupPrivacyType.PRIVATE && group.subscription_status !== types_1.SCGroupSubscriptionStatusType.INVITED
92
+ ? types_1.SCGroupSubscriptionStatusType.REQUESTED
93
+ : types_1.SCGroupSubscriptionStatusType.SUBSCRIBED));
94
+ setUnLoading(group.id);
95
+ return Promise.resolve(res.data);
96
+ });
97
+ }
98
+ }, [data, loading, cache]);
99
+ /**
100
+ * Memoized subscribe Group
101
+ * Toggle action
102
+ */
103
+ const unsubscribe = (0, react_1.useMemo)(() => (group) => {
104
+ if (group.subscription_status !== types_1.SCGroupSubscriptionStatusType.REQUESTED) {
105
+ setLoading(group.id);
106
+ return api_services_1.http
107
+ .request({
108
+ url: api_services_1.Endpoints.UnsubscribeFromGroup.url({ id: group.id }),
109
+ method: api_services_1.Endpoints.UnsubscribeFromGroup.method,
110
+ })
111
+ .then((res) => {
112
+ if (res.status >= 300) {
113
+ return Promise.reject(res);
114
+ }
115
+ updateCache([group.id]);
116
+ setData((prev) => getDataUpdated(prev, group.id, null));
117
+ setUnLoading(group.id);
118
+ return Promise.resolve(res.data);
119
+ });
120
+ }
77
121
  }, [data, loading, cache]);
78
122
  /**
79
123
  * Check the authenticated user subscription status to the group
@@ -171,6 +215,6 @@ function useSCSubscribedGroupsManager(user) {
171
215
  if (!user) {
172
216
  return { groups: data, loading, isLoading };
173
217
  }
174
- return { groups: data, loading, isLoading, subscribe, subscriptionStatus, refresh, emptyCache };
218
+ return { groups: data, loading, isLoading, subscribe, unsubscribe, subscriptionStatus, refresh, emptyCache };
175
219
  }
176
220
  exports.default = useSCSubscribedGroupsManager;
@@ -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
  */
@@ -265,7 +265,11 @@ export interface SCSubscribedGroupsManagerType {
265
265
  /**
266
266
  * Handle user subscription to a group
267
267
  */
268
- subscribe?: (group: SCGroupType) => Promise<any>;
268
+ subscribe?: (group: SCGroupType, userId?: number) => 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}`;
@@ -32,6 +32,8 @@ export declare const RECOVER_ROUTE_NAME = "recover";
32
32
  export declare const CUSTOM_PAGES_ROUTE_NAME = "custom_pages";
33
33
  export declare const LEGAL_PAGES_ROUTE_NAME = "legal_pages";
34
34
  export declare const GROUP_ROUTE_NAME = "group";
35
+ export declare const GROUP_MEMBERS_ROUTE_NAME = "group_members";
36
+ export declare const GROUP_MESSAGES_ROUTE_NAME = "group_messages";
35
37
  export declare const GROUPS_ROUTE_NAME = "groups";
36
38
  /**
37
39
  * Default Routes
@@ -32,6 +32,8 @@ export const RECOVER_ROUTE_NAME = 'recover';
32
32
  export const CUSTOM_PAGES_ROUTE_NAME = 'custom_pages';
33
33
  export const LEGAL_PAGES_ROUTE_NAME = 'legal_pages';
34
34
  export const GROUP_ROUTE_NAME = 'group';
35
+ export const GROUP_MEMBERS_ROUTE_NAME = 'group_members';
36
+ export const GROUP_MESSAGES_ROUTE_NAME = 'group_messages';
35
37
  export const GROUPS_ROUTE_NAME = 'groups';
36
38
  /**
37
39
  * Default Routes
@@ -66,6 +68,8 @@ export const defaultRoutes = {
66
68
  [RECOVER_ROUTE_NAME]: '/recover/',
67
69
  [CUSTOM_PAGES_ROUTE_NAME]: '/:id/:slug/',
68
70
  [LEGAL_PAGES_ROUTE_NAME]: '/legal/:policy/',
69
- [GROUP_ROUTE_NAME]: '/group/:id/',
71
+ [GROUP_ROUTE_NAME]: '/group/:id/:slug/',
72
+ [GROUP_MEMBERS_ROUTE_NAME]: '/group/:id/:slug/members/',
73
+ [GROUP_MESSAGES_ROUTE_NAME]: '/group/:id/:slug/messages/',
70
74
  [GROUPS_ROUTE_NAME]: '/groups/',
71
75
  };
@@ -58,13 +58,13 @@ export default function useSCFetchGroup({ id = null, group = null, cacheStrategy
58
58
  Logger.error(SCOPE_SC_CORE, err.message);
59
59
  });
60
60
  }
61
- }, [__groupId]);
61
+ }, [__groupId, authUserId]);
62
62
  useDeepCompareEffectNoCheck(() => {
63
63
  if (group) {
64
64
  const _c = authUserId ? group : objectWithoutProperties(group, ['subscription_status']);
65
65
  setSCGroup(_c);
66
66
  LRUCache.set(__groupCacheKey, _c);
67
67
  }
68
- }, [group]);
68
+ }, [group, authUserId]);
69
69
  return { scGroup, setSCGroup, error };
70
70
  }
@@ -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.GetUserGroups.url()) => __awaiter(void 0, void 0, void 0, function* () {
48
+ const response = yield http.request({
49
+ url: next,
50
+ method: Endpoints.GetUserGroups.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;
@@ -29,7 +30,8 @@ export default function useSCSubscribedGroupsManager(user?: SCUserType): {
29
30
  isLoading: (v: number | {
30
31
  id: number;
31
32
  }) => boolean;
32
- subscribe: (group: SCGroupType) => Promise<any>;
33
+ subscribe: (group: SCGroupType, userId?: number) => Promise<any>;
34
+ unsubscribe: (group: SCGroupType) => Promise<any>;
33
35
  subscriptionStatus: (group: SCGroupType) => string;
34
36
  refresh: () => void;
35
37
  emptyCache: () => void;