@selfcommunity/react-core 0.4.9-alpha.0 → 0.4.9-alpha.10

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.
Files changed (36) hide show
  1. package/lib/cjs/components/provider/SCUserProvider/index.js +6 -0
  2. package/lib/cjs/constants/Cache.d.ts +9 -0
  3. package/lib/cjs/constants/Cache.js +13 -1
  4. package/lib/cjs/constants/Routes.d.ts +4 -0
  5. package/lib/cjs/constants/Routes.js +9 -1
  6. package/lib/cjs/hooks/useSCFetchGroup.d.ts +21 -0
  7. package/lib/cjs/hooks/useSCFetchGroup.js +73 -0
  8. package/lib/cjs/hooks/useSCFetchGroups.d.ts +22 -0
  9. package/lib/cjs/hooks/useSCFetchGroups.js +83 -0
  10. package/lib/cjs/hooks/useSCSubscribedGroupsManager.d.ts +38 -0
  11. package/lib/cjs/hooks/useSCSubscribedGroupsManager.js +218 -0
  12. package/lib/cjs/index.d.ts +4 -2
  13. package/lib/cjs/index.js +5 -1
  14. package/lib/cjs/themes/theme.js +8 -0
  15. package/lib/cjs/types/context.d.ts +37 -2
  16. package/lib/cjs/types/index.d.ts +2 -2
  17. package/lib/cjs/types/theme.d.ts +13 -0
  18. package/lib/esm/components/provider/SCUserProvider/index.js +6 -0
  19. package/lib/esm/constants/Cache.d.ts +9 -0
  20. package/lib/esm/constants/Cache.js +9 -0
  21. package/lib/esm/constants/Routes.d.ts +4 -0
  22. package/lib/esm/constants/Routes.js +8 -0
  23. package/lib/esm/hooks/useSCFetchGroup.d.ts +21 -0
  24. package/lib/esm/hooks/useSCFetchGroup.js +70 -0
  25. package/lib/esm/hooks/useSCFetchGroups.d.ts +22 -0
  26. package/lib/esm/hooks/useSCFetchGroups.js +81 -0
  27. package/lib/esm/hooks/useSCSubscribedGroupsManager.d.ts +38 -0
  28. package/lib/esm/hooks/useSCSubscribedGroupsManager.js +214 -0
  29. package/lib/esm/index.d.ts +4 -2
  30. package/lib/esm/index.js +3 -1
  31. package/lib/esm/themes/theme.js +8 -0
  32. package/lib/esm/types/context.d.ts +37 -2
  33. package/lib/esm/types/index.d.ts +2 -2
  34. package/lib/esm/types/theme.d.ts +13 -0
  35. package/lib/umd/react-core.js +1 -1
  36. package/package.json +5 -5
@@ -43,6 +43,14 @@ const getTheme = (options, preferences) => {
43
43
  sizeLarge: 50,
44
44
  },
45
45
  },
46
+ group: {
47
+ avatar: {
48
+ sizeSmall: 40,
49
+ sizeMedium: 60,
50
+ sizeLarge: 90,
51
+ sizeXLarge: 120,
52
+ },
53
+ },
46
54
  };
47
55
  const defaultOptions = preferences
48
56
  ? {
@@ -1,5 +1,5 @@
1
1
  import React, { ReactNode } from 'react';
2
- import { SCAuthTokenType, SCIncubatorType, SCCategoryType, SCUserType, SCUserSettingsType, SCReactionType } from "@selfcommunity/types";
2
+ import { SCAuthTokenType, SCIncubatorType, SCCategoryType, SCUserType, SCUserSettingsType, SCReactionType, SCGroupType } from '@selfcommunity/types';
3
3
  import { SCThemeType } from './theme';
4
4
  /**
5
5
  * Interface SCSettingsType
@@ -132,7 +132,7 @@ export interface SCUserContextType {
132
132
  */
133
133
  refreshCounters: () => Promise<any>;
134
134
  /**
135
- * Managers: followed, connections, categories, incubators, etc...
135
+ * Managers: followed, connections, categories, incubators, groups, etc...
136
136
  */
137
137
  managers: {
138
138
  settings?: SCSettingsManagerType;
@@ -142,6 +142,7 @@ export interface SCUserContextType {
142
142
  categories: SCFollowedCategoriesManagerType;
143
143
  incubators?: SCSubscribedIncubatorsManagerType;
144
144
  blockedUsers?: SCBlockedUsersManagerType;
145
+ groups?: SCSubscribedGroupsManagerType;
145
146
  };
146
147
  }
147
148
  export interface SCSettingsManagerType {
@@ -248,6 +249,40 @@ export interface SCFollowedCategoriesManagerType {
248
249
  */
249
250
  emptyCache?: () => void;
250
251
  }
252
+ export interface SCSubscribedGroupsManagerType {
253
+ /**
254
+ * List of all groups ids followed by the authenticated user
255
+ */
256
+ groups: number[];
257
+ /**
258
+ * List of all groups in loading state
259
+ */
260
+ loading: number[];
261
+ /**
262
+ * List of current groups in loading state
263
+ */
264
+ isLoading: (group: SCGroupType) => boolean;
265
+ /**
266
+ * Handle user subscription to a group
267
+ */
268
+ subscribe?: (group: SCGroupType, userId?: number) => Promise<any>;
269
+ /**
270
+ * Handle user unsubscription from a group
271
+ */
272
+ unsubscribe?: (group: SCGroupType) => Promise<any>;
273
+ /**
274
+ * Handles a user subscription status to a group, caching data
275
+ */
276
+ subscriptionStatus?: (group: SCGroupType) => string;
277
+ /**
278
+ * Refresh groups
279
+ */
280
+ refresh?: () => void;
281
+ /**
282
+ * Empty cache to revalidate all groups
283
+ */
284
+ emptyCache?: () => void;
285
+ }
251
286
  export interface SCConnectionsManagerType {
252
287
  /**
253
288
  * List of all users in relations(social graph) with authenticated user
@@ -1,3 +1,3 @@
1
- import { SCUserContextType, SCFollowedManagerType, SCFollowersManagerType, SCFollowedCategoriesManagerType, SCSettingsManagerType, SCSubscribedIncubatorsManagerType, SCConnectionsManagerType, SCBlockedUsersManagerType, SCContextProviderType, SCContextType, SCSettingsType, SCSessionType, SCPreferencesContextType, SCNotificationContextType, SCLocaleType, SCVoteType, SCVoteContextType, SCThemeContextType, SCRoutingContextType, SCLocaleContextType, SCAlertMessagesContextType } from './context';
1
+ import { SCUserContextType, SCFollowedManagerType, SCFollowersManagerType, SCFollowedCategoriesManagerType, SCSettingsManagerType, SCSubscribedIncubatorsManagerType, SCConnectionsManagerType, SCBlockedUsersManagerType, SCContextProviderType, SCContextType, SCSettingsType, SCSessionType, SCPreferencesContextType, SCNotificationContextType, SCLocaleType, SCVoteType, SCVoteContextType, SCThemeContextType, SCRoutingContextType, SCLocaleContextType, SCAlertMessagesContextType, SCSubscribedGroupsManagerType } from './context';
2
2
  import { SCThemeAvatarVariableType, SCThemeUserVariableType, SCThemeCategoryIconVariableType, SCThemeCategoryVariableType, SCThemeVariablesType, SCThemeType } from './theme';
3
- export { SCUserContextType, SCFollowedCategoriesManagerType, SCSettingsManagerType, SCContextProviderType, SCContextType, SCSettingsType, SCSessionType, SCFollowedManagerType, SCFollowersManagerType, SCConnectionsManagerType, SCBlockedUsersManagerType, SCSubscribedIncubatorsManagerType, SCLocaleType, SCVoteType, SCVoteContextType, SCNotificationContextType, SCPreferencesContextType, SCThemeContextType, SCRoutingContextType, SCLocaleContextType, SCAlertMessagesContextType, SCThemeAvatarVariableType, SCThemeUserVariableType, SCThemeCategoryIconVariableType, SCThemeCategoryVariableType, SCThemeVariablesType, SCThemeType, };
3
+ export { SCUserContextType, SCFollowedCategoriesManagerType, SCSettingsManagerType, SCContextProviderType, SCContextType, SCSettingsType, SCSessionType, SCFollowedManagerType, SCFollowersManagerType, SCConnectionsManagerType, SCBlockedUsersManagerType, SCSubscribedIncubatorsManagerType, SCLocaleType, SCVoteType, SCVoteContextType, SCNotificationContextType, SCPreferencesContextType, SCThemeContextType, SCRoutingContextType, SCLocaleContextType, SCAlertMessagesContextType, SCThemeAvatarVariableType, SCThemeUserVariableType, SCThemeCategoryIconVariableType, SCThemeCategoryVariableType, SCThemeVariablesType, SCThemeType, SCSubscribedGroupsManagerType, };
@@ -29,6 +29,15 @@ export interface SCThemeUserVariableType {
29
29
  */
30
30
  avatar: SCThemeAvatarVariableType;
31
31
  }
32
+ /**
33
+ * Interface SCThemeGroupVariableType
34
+ */
35
+ export interface SCThemeGroupVariableType {
36
+ /**
37
+ * Avatar variables
38
+ */
39
+ avatar: SCThemeAvatarVariableType;
40
+ }
32
41
  /**
33
42
  * Interface SCThemeCategoryVariableType
34
43
  */
@@ -67,6 +76,10 @@ export interface SCThemeVariablesType {
67
76
  * Category
68
77
  */
69
78
  category: SCThemeCategoryVariableType;
79
+ /**
80
+ * Group
81
+ */
82
+ group: SCThemeGroupVariableType;
70
83
  }
71
84
  export interface SCThemeType extends MuiTheme {
72
85
  /**
@@ -15,6 +15,7 @@ import { SCNotificationTopicType, SCNotificationTypologyType, SCUserStatus } fro
15
15
  import useSCSubscribedIncubatorsManager from '../../../hooks/useSCSubscribedIncubatorsManager';
16
16
  import useSCBlockedUsersManager from '../../../hooks/useSCBlockedUsersManager';
17
17
  import * as Session from '../../../constants/Session';
18
+ import useSCSubscribedGroupsManager from '../../../hooks/useSCSubscribedGroupsManager';
18
19
  /**
19
20
  * SCUserContext (Authentication Context)
20
21
  *
@@ -66,6 +67,7 @@ export default function SCUserProvider({ children }) {
66
67
  const connectionsManager = useSCConnectionsManager(state.user);
67
68
  const categoriesManager = useSCFollowedCategoriesManager(state.user, updateUser);
68
69
  const blockedUsersManager = useSCBlockedUsersManager(state.user);
70
+ const subscribedGroupsManager = useSCSubscribedGroupsManager(state.user);
69
71
  /**
70
72
  * Ref notifications subscribers: BLOCKED_USER, UNBLOCKED_USER
71
73
  */
@@ -116,6 +118,7 @@ export default function SCUserProvider({ children }) {
116
118
  connectionsManager.refresh && connectionsManager.refresh();
117
119
  subscribedIncubatorsManager.refresh && subscribedIncubatorsManager.refresh();
118
120
  blockedUsersManager.refresh && blockedUsersManager.refresh();
121
+ subscribedGroupsManager.refresh && subscribedGroupsManager.refresh();
119
122
  }
120
123
  }
121
124
  /**
@@ -211,6 +214,7 @@ export default function SCUserProvider({ children }) {
211
214
  connections: connectionsManager,
212
215
  incubators: subscribedIncubatorsManager,
213
216
  blockedUsers: blockedUsersManager,
217
+ groups: subscribedGroupsManager,
214
218
  },
215
219
  }), [
216
220
  state,
@@ -227,6 +231,8 @@ export default function SCUserProvider({ children }) {
227
231
  blockedUsersManager.blocked,
228
232
  subscribedIncubatorsManager.loading,
229
233
  subscribedIncubatorsManager.incubators,
234
+ subscribedGroupsManager.loading,
235
+ subscribedGroupsManager.groups,
230
236
  ]);
231
237
  /**
232
238
  * We only want to render the underlying app after we
@@ -17,6 +17,12 @@ export declare const getCategoriesObjectCacheKey: () => string;
17
17
  /** CATEGORY OBJECT **/
18
18
  export declare const CATEGORY_OBJECT_CACHE_PREFIX_KEY = "_ca_";
19
19
  export declare const getCategoryObjectCacheKey: (id: any) => string;
20
+ /** GROUP OBJECT **/
21
+ export declare const GROUP_OBJECT_CACHE_PREFIX_KEY = "_grp_";
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;
20
26
  /** INCUBATOR OBJECT **/
21
27
  export declare const INCUBATOR_OBJECT_CACHE_PREFIX_KEY = "_inc_";
22
28
  export declare const getIncubatorObjectCacheKey: (id: any) => string;
@@ -62,4 +68,7 @@ export declare const PEOPLE_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = "_pSugWidg
62
68
  export declare const INCUBATOR_LIST_TOOLS_STATE_CACHE_PREFIX_KEY = "_iListWidget_";
63
69
  export declare const INCUBATOR_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = "_iSugWidget_";
64
70
  export declare const POLL_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = "_pSugWidget_";
71
+ export declare const GROUP_MEMBERS_TOOLS_STATE_CACHE_PREFIX_KEY = "_gMemWidget_";
72
+ export declare const GROUP_REQUESTS_TOOLS_STATE_CACHE_PREFIX_KEY = "_gReqWidget_";
73
+ export declare const GROUPS_LIST_TOOLS_STATE_CACHE_PREFIX_KEY = "_glIST_";
65
74
  export declare const getWidgetStateCacheKey: (p: any, id?: any) => string;
@@ -17,6 +17,12 @@ export const getCategoriesObjectCacheKey = () => `${CATEGORIES_OBJECT_CACHE_PREF
17
17
  /** CATEGORY OBJECT **/
18
18
  export const CATEGORY_OBJECT_CACHE_PREFIX_KEY = '_ca_';
19
19
  export const getCategoryObjectCacheKey = (id) => `${CATEGORY_OBJECT_CACHE_PREFIX_KEY}${id}`;
20
+ /** GROUP OBJECT **/
21
+ export const GROUP_OBJECT_CACHE_PREFIX_KEY = '_grp_';
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}`;
20
26
  /** INCUBATOR OBJECT **/
21
27
  export const INCUBATOR_OBJECT_CACHE_PREFIX_KEY = '_inc_';
22
28
  export const getIncubatorObjectCacheKey = (id) => `${INCUBATOR_OBJECT_CACHE_PREFIX_KEY}${id}`;
@@ -66,4 +72,7 @@ export const PEOPLE_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = '_pSugWidget_';
66
72
  export const INCUBATOR_LIST_TOOLS_STATE_CACHE_PREFIX_KEY = '_iListWidget_';
67
73
  export const INCUBATOR_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = '_iSugWidget_';
68
74
  export const POLL_SUGGESTION_TOOLS_STATE_CACHE_PREFIX_KEY = '_pSugWidget_';
75
+ export const GROUP_MEMBERS_TOOLS_STATE_CACHE_PREFIX_KEY = '_gMemWidget_';
76
+ export const GROUP_REQUESTS_TOOLS_STATE_CACHE_PREFIX_KEY = '_gReqWidget_';
77
+ export const GROUPS_LIST_TOOLS_STATE_CACHE_PREFIX_KEY = '_glIST_';
69
78
  export const getWidgetStateCacheKey = (p, id = undefined) => `${p}${id !== undefined ? id : ''}`;
@@ -31,6 +31,10 @@ export declare const SIGNUP_ROUTE_NAME = "signup";
31
31
  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
+ 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";
37
+ export declare const GROUPS_ROUTE_NAME = "groups";
34
38
  /**
35
39
  * Default Routes
36
40
  * @type {{[p: string]: string, '[POST_ROUTE_NAME]': string, '[INCUBATOR_ROUTE_NAME]': string, '[LOYALTY_ROUTE_NAME]': string, '[USER_NOTIFICATION_ROUTE_NAME]': string, '[USER_PRIVATE_MESSAGES_ROUTE_NAME]': string, '[COMMENT_ROUTE_NAME]': string, '[DISCUSSION_ROUTE_NAME]': string, '[CATEGORIES_ROUTE_NAME]': string, '[USER_PROFILE_ROUTE_NAME]': string, '[CATEGORY_ROUTE_NAME]': string, '[USER_PROFILE_SETTINGS_ROUTE_NAME]': string, '[STATUS_ROUTE_NAME]': string}}
@@ -31,6 +31,10 @@ export const SIGNUP_ROUTE_NAME = 'signup';
31
31
  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
+ export const GROUP_ROUTE_NAME = 'group';
35
+ export const GROUP_MEMBERS_ROUTE_NAME = 'group_members';
36
+ export const GROUP_MESSAGES_ROUTE_NAME = 'group_messages';
37
+ export const GROUPS_ROUTE_NAME = 'groups';
34
38
  /**
35
39
  * Default Routes
36
40
  * @type {{[p: string]: string, '[POST_ROUTE_NAME]': string, '[INCUBATOR_ROUTE_NAME]': string, '[LOYALTY_ROUTE_NAME]': string, '[USER_NOTIFICATION_ROUTE_NAME]': string, '[USER_PRIVATE_MESSAGES_ROUTE_NAME]': string, '[COMMENT_ROUTE_NAME]': string, '[DISCUSSION_ROUTE_NAME]': string, '[CATEGORIES_ROUTE_NAME]': string, '[USER_PROFILE_ROUTE_NAME]': string, '[CATEGORY_ROUTE_NAME]': string, '[USER_PROFILE_SETTINGS_ROUTE_NAME]': string, '[STATUS_ROUTE_NAME]': string}}
@@ -64,4 +68,8 @@ export const defaultRoutes = {
64
68
  [RECOVER_ROUTE_NAME]: '/recover/',
65
69
  [CUSTOM_PAGES_ROUTE_NAME]: '/:id/:slug/',
66
70
  [LEGAL_PAGES_ROUTE_NAME]: '/legal/:policy/',
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/',
74
+ [GROUPS_ROUTE_NAME]: '/groups/',
67
75
  };
@@ -0,0 +1,21 @@
1
+ /// <reference types="react" />
2
+ import { SCGroupType } from '@selfcommunity/types';
3
+ import { CacheStrategies } from '@selfcommunity/utils';
4
+ /**
5
+ :::info
6
+ This custom hook is used to fetch a group object.
7
+ :::
8
+ * @param object
9
+ * @param object.id
10
+ * @param object.group
11
+ * @param object.cacheStrategy
12
+ */
13
+ export default function useSCFetchGroup({ id, group, cacheStrategy, }: {
14
+ id?: number | string;
15
+ group?: SCGroupType;
16
+ cacheStrategy?: CacheStrategies;
17
+ }): {
18
+ scGroup: SCGroupType;
19
+ setSCGroup: import("react").Dispatch<import("react").SetStateAction<SCGroupType>>;
20
+ error: string;
21
+ };
@@ -0,0 +1,70 @@
1
+ import { useEffect, useMemo, useState } from 'react';
2
+ import { SCOPE_SC_CORE } from '../constants/Errors';
3
+ import { Endpoints, http } from '@selfcommunity/api-services';
4
+ import { CacheStrategies, Logger, LRUCache, objectWithoutProperties } from '@selfcommunity/utils';
5
+ import { getGroupObjectCacheKey } from '../constants/Cache';
6
+ import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect';
7
+ import { useSCUser } from '../components/provider/SCUserProvider';
8
+ /**
9
+ :::info
10
+ This custom hook is used to fetch a group object.
11
+ :::
12
+ * @param object
13
+ * @param object.id
14
+ * @param object.group
15
+ * @param object.cacheStrategy
16
+ */
17
+ export default function useSCFetchGroup({ id = null, group = null, cacheStrategy = CacheStrategies.CACHE_FIRST, }) {
18
+ const __groupId = group ? group.id : id;
19
+ // CONTEXT
20
+ const scUserContext = useSCUser();
21
+ const authUserId = scUserContext.user ? scUserContext.user.id : null;
22
+ // CACHE
23
+ const __groupCacheKey = getGroupObjectCacheKey(__groupId);
24
+ const __group = authUserId ? group : objectWithoutProperties(group, ['subscription_status']);
25
+ const [scGroup, setSCGroup] = useState(cacheStrategy !== CacheStrategies.NETWORK_ONLY ? LRUCache.get(__groupCacheKey, __group) : null);
26
+ const [error, setError] = useState(null);
27
+ /**
28
+ * Memoized fetchTag
29
+ */
30
+ const fetchGroup = useMemo(() => () => {
31
+ return http
32
+ .request({
33
+ url: Endpoints.GetGroupInfo.url({ id: __groupId }),
34
+ method: Endpoints.GetGroupInfo.method,
35
+ })
36
+ .then((res) => {
37
+ if (res.status >= 300) {
38
+ return Promise.reject(res);
39
+ }
40
+ return Promise.resolve(res.data);
41
+ });
42
+ }, [__groupId]);
43
+ /**
44
+ * If id attempt to get the group by id
45
+ */
46
+ useEffect(() => {
47
+ if (__groupId && (!scGroup || (scGroup && __groupId !== scGroup.id))) {
48
+ fetchGroup()
49
+ .then((obj) => {
50
+ const _c = authUserId ? obj : objectWithoutProperties(obj, ['subscription_status']);
51
+ setSCGroup(_c);
52
+ LRUCache.set(__groupCacheKey, _c);
53
+ })
54
+ .catch((err) => {
55
+ LRUCache.delete(__groupCacheKey);
56
+ setError(`Group with id ${id} not found`);
57
+ Logger.error(SCOPE_SC_CORE, `Group with id ${id} not found`);
58
+ Logger.error(SCOPE_SC_CORE, err.message);
59
+ });
60
+ }
61
+ }, [__groupId, authUserId]);
62
+ useDeepCompareEffectNoCheck(() => {
63
+ if (group) {
64
+ const _c = authUserId ? group : objectWithoutProperties(group, ['subscription_status']);
65
+ setSCGroup(_c);
66
+ LRUCache.set(__groupCacheKey, _c);
67
+ }
68
+ }, [group, authUserId]);
69
+ return { scGroup, setSCGroup, error };
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;
@@ -0,0 +1,38 @@
1
+ import { SCGroupType, SCUserType } from '@selfcommunity/types';
2
+ /**
3
+ :::info
4
+ This custom hook is used to manage the groups followed.
5
+ :::
6
+
7
+ :::tip How to use it:
8
+ Follow these steps:
9
+ ```jsx
10
+ 1. const scUserContext: SCUserContextType = useSCUser();
11
+ 2. const scSubscribedGroupsManager: SCSubscribedGroupsManagerType = scUserContext.manager.groups;
12
+ 3. scSubscribedGroupsManager.isSubscribed(group)
13
+ ```
14
+ :::
15
+ */
16
+ export default function useSCSubscribedGroupsManager(user?: SCUserType): {
17
+ groups: any[];
18
+ loading: any[];
19
+ isLoading: (v: number | {
20
+ id: number;
21
+ }) => boolean;
22
+ subscribe?: undefined;
23
+ unsubscribe?: undefined;
24
+ subscriptionStatus?: undefined;
25
+ refresh?: undefined;
26
+ emptyCache?: undefined;
27
+ } | {
28
+ groups: any[];
29
+ loading: any[];
30
+ isLoading: (v: number | {
31
+ id: number;
32
+ }) => boolean;
33
+ subscribe: (group: SCGroupType, userId?: number) => Promise<any>;
34
+ unsubscribe: (group: SCGroupType) => Promise<any>;
35
+ subscriptionStatus: (group: SCGroupType) => string;
36
+ refresh: () => void;
37
+ emptyCache: () => void;
38
+ };