@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.
- package/lib/cjs/components/provider/SCPreferencesProvider/index.js +1 -1
- package/lib/cjs/constants/Cache.d.ts +1 -0
- package/lib/cjs/constants/Cache.js +2 -1
- package/lib/cjs/constants/Notification.d.ts +4 -0
- package/lib/cjs/constants/Notification.js +4 -0
- package/lib/cjs/constants/Routes.d.ts +3 -0
- package/lib/cjs/constants/Routes.js +7 -1
- package/lib/cjs/hooks/useSCFetchGroup.d.ts +1 -2
- package/lib/cjs/hooks/useSCFetchGroup.js +10 -9
- package/lib/cjs/hooks/useSCFetchGroups.js +2 -2
- package/lib/cjs/hooks/useSCSubscribedGroupsManager.d.ts +1 -1
- package/lib/cjs/hooks/useSCSubscribedGroupsManager.js +61 -36
- package/lib/cjs/types/context.d.ts +1 -1
- package/lib/esm/components/provider/SCPreferencesProvider/index.js +1 -1
- package/lib/esm/constants/Cache.d.ts +1 -0
- package/lib/esm/constants/Cache.js +1 -0
- package/lib/esm/constants/Notification.d.ts +4 -0
- package/lib/esm/constants/Notification.js +4 -0
- package/lib/esm/constants/Routes.d.ts +3 -0
- package/lib/esm/constants/Routes.js +6 -0
- package/lib/esm/hooks/useSCFetchGroup.d.ts +1 -2
- package/lib/esm/hooks/useSCFetchGroup.js +10 -9
- package/lib/esm/hooks/useSCFetchGroups.js +2 -2
- package/lib/esm/hooks/useSCSubscribedGroupsManager.d.ts +1 -1
- package/lib/esm/hooks/useSCSubscribedGroupsManager.js +62 -37
- package/lib/esm/types/context.d.ts +1 -1
- package/lib/umd/react-core.js +1 -1
- package/package.json +5 -5
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { useEffect, useMemo } from 'react';
|
|
2
|
-
import {
|
|
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(
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
}, [
|
|
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
|
*/
|