@selfcommunity/react-core 0.4.9-alpha.4 → 0.4.9-alpha.41

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.
@@ -1,8 +1,10 @@
1
1
  import { useEffect, useMemo } from 'react';
2
- import { http, Endpoints } from '@selfcommunity/api-services';
2
+ import { Endpoints, http } from '@selfcommunity/api-services';
3
+ import { SCFeatureName, SCGroupPrivacyType, SCGroupSubscriptionStatusType } from '@selfcommunity/types';
3
4
  import useSCCachingManager from './useSCCachingManager';
4
5
  import { SCOPE_SC_CORE } from '../constants/Errors';
5
6
  import { Logger } from '@selfcommunity/utils';
7
+ import { useSCPreferences } from '../components/provider/SCPreferencesProvider';
6
8
  /**
7
9
  :::info
8
10
  This custom hook is used to manage the groups followed.
@@ -19,7 +21,9 @@ import { Logger } from '@selfcommunity/utils';
19
21
  */
20
22
  export default function useSCSubscribedGroupsManager(user) {
21
23
  const { cache, updateCache, emptyCache, data, setData, loading, setLoading, setUnLoading, isLoading } = useSCCachingManager();
24
+ const scPreferencesContext = useSCPreferences();
22
25
  const authUserId = user ? user.id : null;
26
+ const groupsDisabled = scPreferencesContext.features && !scPreferencesContext.features.includes(SCFeatureName.GROUPING);
23
27
  /**
24
28
  * Memoized refresh all groups
25
29
  * It makes a single request to the server and retrieves
@@ -39,9 +43,9 @@ export default function useSCSubscribedGroupsManager(user) {
39
43
  if (res.status >= 300) {
40
44
  return Promise.reject(res);
41
45
  }
42
- const groupsIds = res.data.map((g) => g.id);
46
+ const groupsIds = res.data.results.map((g) => g.id);
43
47
  updateCache(groupsIds);
44
- setData(groupsIds);
48
+ setData(res.data.results.map((g) => ({ [g.id]: g.subscription_status })));
45
49
  return Promise.resolve(res.data);
46
50
  })
47
51
  .catch((e) => {
@@ -54,45 +58,66 @@ export default function useSCSubscribedGroupsManager(user) {
54
58
  * Memoized subscribe Group
55
59
  * Toggle action
56
60
  */
57
- const subscribe = useMemo(() => (group) => {
61
+ const subscribe = useMemo(() => (group, userId) => {
58
62
  setLoading(group.id);
59
- return http
60
- .request({
61
- url: Endpoints.SubscribeToGroup.url({ id: group.id }),
62
- method: Endpoints.SubscribeToGroup.method,
63
- })
64
- .then((res) => {
65
- if (res.status >= 300) {
66
- return Promise.reject(res);
67
- }
68
- updateCache([group.id]);
69
- const isSubscribed = data.includes(group.id);
70
- setData((prev) => (isSubscribed ? prev.filter((id) => id !== group.id) : [...[group.id], ...prev]));
71
- setUnLoading(group.id);
72
- return Promise.resolve(res.data);
73
- });
63
+ if (userId) {
64
+ return http
65
+ .request({
66
+ url: Endpoints.InviteOrAcceptGroupRequest.url({ id: group.id }),
67
+ method: 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, SCGroupSubscriptionStatusType.SUBSCRIBED));
76
+ setUnLoading(group.id);
77
+ return Promise.resolve(res.data);
78
+ });
79
+ }
80
+ else {
81
+ return http
82
+ .request({
83
+ url: Endpoints.SubscribeToGroup.url({ id: group.id }),
84
+ method: 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 === SCGroupPrivacyType.PRIVATE && group.subscription_status !== SCGroupSubscriptionStatusType.INVITED
92
+ ? SCGroupSubscriptionStatusType.REQUESTED
93
+ : SCGroupSubscriptionStatusType.SUBSCRIBED));
94
+ setUnLoading(group.id);
95
+ return Promise.resolve(res.data);
96
+ });
97
+ }
74
98
  }, [data, loading, cache]);
75
99
  /**
76
100
  * Memoized subscribe Group
77
101
  * Toggle action
78
102
  */
79
103
  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
- });
104
+ if (group.subscription_status !== SCGroupSubscriptionStatusType.REQUESTED) {
105
+ setLoading(group.id);
106
+ return http
107
+ .request({
108
+ url: Endpoints.UnsubscribeFromGroup.url({ id: group.id }),
109
+ method: 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
+ }
96
121
  }, [data, loading, cache]);
97
122
  /**
98
123
  * Check the authenticated user subscription status to the group
@@ -178,7 +203,7 @@ export default function useSCSubscribedGroupsManager(user) {
178
203
  }
179
204
  }
180
205
  return null;
181
- }, [data, loading, cache, authUserId]);
206
+ }, [loading, cache, authUserId]);
182
207
  /**
183
208
  * Empty cache on logout
184
209
  */
@@ -187,7 +212,7 @@ export default function useSCSubscribedGroupsManager(user) {
187
212
  emptyCache();
188
213
  }
189
214
  }, [authUserId]);
190
- if (!user) {
215
+ if (groupsDisabled || !user) {
191
216
  return { groups: data, loading, isLoading };
192
217
  }
193
218
  return { groups: data, loading, isLoading, subscribe, unsubscribe, subscriptionStatus, refresh, emptyCache };
@@ -265,7 +265,7 @@ 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
269
  /**
270
270
  * Handle user unsubscription from a group
271
271
  */