@selfcommunity/react-core 0.4.19 → 0.4.20-alpha.1
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.
|
@@ -8,6 +8,9 @@ const useSCCachingManager_1 = tslib_1.__importDefault(require("./useSCCachingMan
|
|
|
8
8
|
const Errors_1 = require("../constants/Errors");
|
|
9
9
|
const utils_1 = require("@selfcommunity/utils");
|
|
10
10
|
const SCPreferencesProvider_1 = require("../components/provider/SCPreferencesProvider");
|
|
11
|
+
const Notification_1 = require("../constants/Notification");
|
|
12
|
+
const use_deep_compare_effect_1 = require("use-deep-compare-effect");
|
|
13
|
+
const pubsub_js_1 = tslib_1.__importDefault(require("pubsub-js"));
|
|
11
14
|
/**
|
|
12
15
|
:::info
|
|
13
16
|
This custom hook is used to manage the groups followed.
|
|
@@ -24,9 +27,54 @@ const SCPreferencesProvider_1 = require("../components/provider/SCPreferencesPro
|
|
|
24
27
|
*/
|
|
25
28
|
function useSCSubscribedGroupsManager(user) {
|
|
26
29
|
const { cache, updateCache, emptyCache, data, setData, loading, setLoading, setUnLoading, isLoading } = (0, useSCCachingManager_1.default)();
|
|
27
|
-
const
|
|
30
|
+
const { features } = (0, SCPreferencesProvider_1.useSCPreferences)();
|
|
28
31
|
const authUserId = user ? user.id : null;
|
|
29
|
-
const groupsDisabled =
|
|
32
|
+
const groupsDisabled = features && !features.includes(types_1.SCFeatureName.GROUPING) && !features.includes(types_1.SCFeatureName.TAGGING);
|
|
33
|
+
const notificationInvitedToJoinGroup = (0, react_1.useRef)(null);
|
|
34
|
+
const notificationRequestedToJoinGroup = (0, react_1.useRef)(null);
|
|
35
|
+
const notificationAcceptedToJoinGroup = (0, react_1.useRef)(null);
|
|
36
|
+
const notificationAddedToGroup = (0, react_1.useRef)(null);
|
|
37
|
+
/**
|
|
38
|
+
* Subscribe to notification types user_follow, user_unfollow
|
|
39
|
+
*/
|
|
40
|
+
(0, use_deep_compare_effect_1.useDeepCompareEffectNoCheck)(() => {
|
|
41
|
+
notificationInvitedToJoinGroup.current = pubsub_js_1.default.subscribe(`${types_1.SCNotificationTopicType.INTERACTION}.${types_1.SCNotificationTypologyType.USER_INVITED_TO_JOIN_GROUP}`, notificationSubscriber);
|
|
42
|
+
notificationRequestedToJoinGroup.current = pubsub_js_1.default.subscribe(`${types_1.SCNotificationTopicType.INTERACTION}.${types_1.SCNotificationTypologyType.USER_REQUESTED_TO_JOIN_GROUP}`, notificationSubscriber);
|
|
43
|
+
notificationAcceptedToJoinGroup.current = pubsub_js_1.default.subscribe(`${types_1.SCNotificationTopicType.INTERACTION}.${types_1.SCNotificationTypologyType.USER_ACCEPTED_TO_JOIN_GROUP}`, notificationSubscriber);
|
|
44
|
+
notificationAddedToGroup.current = pubsub_js_1.default.subscribe(`${types_1.SCNotificationTopicType.INTERACTION}.${types_1.SCNotificationTypologyType.USER_ADDED_TO_GROUP}`, notificationSubscriber);
|
|
45
|
+
return () => {
|
|
46
|
+
pubsub_js_1.default.unsubscribe(notificationInvitedToJoinGroup.current);
|
|
47
|
+
pubsub_js_1.default.unsubscribe(notificationRequestedToJoinGroup.current);
|
|
48
|
+
pubsub_js_1.default.unsubscribe(notificationAcceptedToJoinGroup.current);
|
|
49
|
+
pubsub_js_1.default.unsubscribe(notificationAddedToGroup.current);
|
|
50
|
+
};
|
|
51
|
+
}, [data]);
|
|
52
|
+
/**
|
|
53
|
+
* Notification subscriber handler
|
|
54
|
+
* @param msg
|
|
55
|
+
* @param dataMsg
|
|
56
|
+
*/
|
|
57
|
+
const notificationSubscriber = (msg, dataMsg) => {
|
|
58
|
+
if (dataMsg.data.group !== undefined) {
|
|
59
|
+
let _status;
|
|
60
|
+
switch (Notification_1.SCNotificationMapping[dataMsg.data.activity_type]) {
|
|
61
|
+
case types_1.SCNotificationTypologyType.USER_INVITED_TO_JOIN_GROUP:
|
|
62
|
+
_status = types_1.SCGroupSubscriptionStatusType.INVITED;
|
|
63
|
+
break;
|
|
64
|
+
case types_1.SCNotificationTypologyType.USER_REQUESTED_TO_JOIN_GROUP:
|
|
65
|
+
_status = types_1.SCGroupSubscriptionStatusType.REQUESTED;
|
|
66
|
+
break;
|
|
67
|
+
case types_1.SCNotificationTypologyType.USER_ACCEPTED_TO_JOIN_GROUP:
|
|
68
|
+
_status = types_1.SCGroupSubscriptionStatusType.SUBSCRIBED;
|
|
69
|
+
break;
|
|
70
|
+
case types_1.SCNotificationTypologyType.USER_ADDED_TO_GROUP:
|
|
71
|
+
_status = types_1.SCGroupSubscriptionStatusType.SUBSCRIBED;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
updateCache([dataMsg.data.group.id]);
|
|
75
|
+
setData((prev) => getDataUpdated(prev, dataMsg.data.group.id, _status));
|
|
76
|
+
}
|
|
77
|
+
};
|
|
30
78
|
/**
|
|
31
79
|
* Memoized refresh all groups
|
|
32
80
|
* It makes a single request to the server and retrieves
|
|
@@ -177,7 +225,7 @@ function useSCSubscribedGroupsManager(user) {
|
|
|
177
225
|
*/
|
|
178
226
|
const getCurrentGroupCacheStatus = (0, react_1.useMemo)(() => (group) => {
|
|
179
227
|
const d = data.filter((k) => parseInt(Object.keys(k)[0]) === group.id);
|
|
180
|
-
return d.length ? d[0][group.id] : null;
|
|
228
|
+
return d.length ? d[0][group.id] : !data.length ? group.subscription_status : null;
|
|
181
229
|
}, [data]);
|
|
182
230
|
/**
|
|
183
231
|
* Bypass remote check if the group is subscribed
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { useEffect, useMemo } from 'react';
|
|
1
|
+
import { useEffect, useMemo, useRef } from 'react';
|
|
2
2
|
import { Endpoints, http } from '@selfcommunity/api-services';
|
|
3
|
-
import { SCFeatureName, SCGroupPrivacyType, SCGroupSubscriptionStatusType } from '@selfcommunity/types';
|
|
3
|
+
import { SCFeatureName, SCGroupPrivacyType, SCGroupSubscriptionStatusType, SCNotificationTopicType, SCNotificationTypologyType, } from '@selfcommunity/types';
|
|
4
4
|
import useSCCachingManager from './useSCCachingManager';
|
|
5
5
|
import { SCOPE_SC_CORE } from '../constants/Errors';
|
|
6
6
|
import { Logger } from '@selfcommunity/utils';
|
|
7
7
|
import { useSCPreferences } from '../components/provider/SCPreferencesProvider';
|
|
8
|
+
import { SCNotificationMapping } from '../constants/Notification';
|
|
9
|
+
import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect';
|
|
10
|
+
import PubSub from 'pubsub-js';
|
|
8
11
|
/**
|
|
9
12
|
:::info
|
|
10
13
|
This custom hook is used to manage the groups followed.
|
|
@@ -21,9 +24,54 @@ import { useSCPreferences } from '../components/provider/SCPreferencesProvider';
|
|
|
21
24
|
*/
|
|
22
25
|
export default function useSCSubscribedGroupsManager(user) {
|
|
23
26
|
const { cache, updateCache, emptyCache, data, setData, loading, setLoading, setUnLoading, isLoading } = useSCCachingManager();
|
|
24
|
-
const
|
|
27
|
+
const { features } = useSCPreferences();
|
|
25
28
|
const authUserId = user ? user.id : null;
|
|
26
|
-
const groupsDisabled =
|
|
29
|
+
const groupsDisabled = features && !features.includes(SCFeatureName.GROUPING) && !features.includes(SCFeatureName.TAGGING);
|
|
30
|
+
const notificationInvitedToJoinGroup = useRef(null);
|
|
31
|
+
const notificationRequestedToJoinGroup = useRef(null);
|
|
32
|
+
const notificationAcceptedToJoinGroup = useRef(null);
|
|
33
|
+
const notificationAddedToGroup = useRef(null);
|
|
34
|
+
/**
|
|
35
|
+
* Subscribe to notification types user_follow, user_unfollow
|
|
36
|
+
*/
|
|
37
|
+
useDeepCompareEffectNoCheck(() => {
|
|
38
|
+
notificationInvitedToJoinGroup.current = PubSub.subscribe(`${SCNotificationTopicType.INTERACTION}.${SCNotificationTypologyType.USER_INVITED_TO_JOIN_GROUP}`, notificationSubscriber);
|
|
39
|
+
notificationRequestedToJoinGroup.current = PubSub.subscribe(`${SCNotificationTopicType.INTERACTION}.${SCNotificationTypologyType.USER_REQUESTED_TO_JOIN_GROUP}`, notificationSubscriber);
|
|
40
|
+
notificationAcceptedToJoinGroup.current = PubSub.subscribe(`${SCNotificationTopicType.INTERACTION}.${SCNotificationTypologyType.USER_ACCEPTED_TO_JOIN_GROUP}`, notificationSubscriber);
|
|
41
|
+
notificationAddedToGroup.current = PubSub.subscribe(`${SCNotificationTopicType.INTERACTION}.${SCNotificationTypologyType.USER_ADDED_TO_GROUP}`, notificationSubscriber);
|
|
42
|
+
return () => {
|
|
43
|
+
PubSub.unsubscribe(notificationInvitedToJoinGroup.current);
|
|
44
|
+
PubSub.unsubscribe(notificationRequestedToJoinGroup.current);
|
|
45
|
+
PubSub.unsubscribe(notificationAcceptedToJoinGroup.current);
|
|
46
|
+
PubSub.unsubscribe(notificationAddedToGroup.current);
|
|
47
|
+
};
|
|
48
|
+
}, [data]);
|
|
49
|
+
/**
|
|
50
|
+
* Notification subscriber handler
|
|
51
|
+
* @param msg
|
|
52
|
+
* @param dataMsg
|
|
53
|
+
*/
|
|
54
|
+
const notificationSubscriber = (msg, dataMsg) => {
|
|
55
|
+
if (dataMsg.data.group !== undefined) {
|
|
56
|
+
let _status;
|
|
57
|
+
switch (SCNotificationMapping[dataMsg.data.activity_type]) {
|
|
58
|
+
case SCNotificationTypologyType.USER_INVITED_TO_JOIN_GROUP:
|
|
59
|
+
_status = SCGroupSubscriptionStatusType.INVITED;
|
|
60
|
+
break;
|
|
61
|
+
case SCNotificationTypologyType.USER_REQUESTED_TO_JOIN_GROUP:
|
|
62
|
+
_status = SCGroupSubscriptionStatusType.REQUESTED;
|
|
63
|
+
break;
|
|
64
|
+
case SCNotificationTypologyType.USER_ACCEPTED_TO_JOIN_GROUP:
|
|
65
|
+
_status = SCGroupSubscriptionStatusType.SUBSCRIBED;
|
|
66
|
+
break;
|
|
67
|
+
case SCNotificationTypologyType.USER_ADDED_TO_GROUP:
|
|
68
|
+
_status = SCGroupSubscriptionStatusType.SUBSCRIBED;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
updateCache([dataMsg.data.group.id]);
|
|
72
|
+
setData((prev) => getDataUpdated(prev, dataMsg.data.group.id, _status));
|
|
73
|
+
}
|
|
74
|
+
};
|
|
27
75
|
/**
|
|
28
76
|
* Memoized refresh all groups
|
|
29
77
|
* It makes a single request to the server and retrieves
|
|
@@ -174,7 +222,7 @@ export default function useSCSubscribedGroupsManager(user) {
|
|
|
174
222
|
*/
|
|
175
223
|
const getCurrentGroupCacheStatus = useMemo(() => (group) => {
|
|
176
224
|
const d = data.filter((k) => parseInt(Object.keys(k)[0]) === group.id);
|
|
177
|
-
return d.length ? d[0][group.id] : null;
|
|
225
|
+
return d.length ? d[0][group.id] : !data.length ? group.subscription_status : null;
|
|
178
226
|
}, [data]);
|
|
179
227
|
/**
|
|
180
228
|
* Bypass remote check if the group is subscribed
|