@selfcommunity/react-core 0.4.19 → 0.4.20-alpha.0
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,55 @@ 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
|
+
console.log(msg, dataMsg, 'subscriber');
|
|
59
|
+
if (dataMsg.data.group !== undefined) {
|
|
60
|
+
let _status;
|
|
61
|
+
switch (Notification_1.SCNotificationMapping[dataMsg.data.activity_type]) {
|
|
62
|
+
case types_1.SCNotificationTypologyType.USER_INVITED_TO_JOIN_GROUP:
|
|
63
|
+
_status = types_1.SCGroupSubscriptionStatusType.INVITED;
|
|
64
|
+
break;
|
|
65
|
+
case types_1.SCNotificationTypologyType.USER_REQUESTED_TO_JOIN_GROUP:
|
|
66
|
+
_status = types_1.SCGroupSubscriptionStatusType.REQUESTED;
|
|
67
|
+
break;
|
|
68
|
+
case types_1.SCNotificationTypologyType.USER_ACCEPTED_TO_JOIN_GROUP:
|
|
69
|
+
_status = types_1.SCGroupSubscriptionStatusType.SUBSCRIBED;
|
|
70
|
+
break;
|
|
71
|
+
case types_1.SCNotificationTypologyType.USER_ADDED_TO_GROUP:
|
|
72
|
+
_status = types_1.SCGroupSubscriptionStatusType.SUBSCRIBED;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
updateCache([dataMsg.data.group.id]);
|
|
76
|
+
setData((prev) => getDataUpdated(prev, dataMsg.data.group.id, _status));
|
|
77
|
+
}
|
|
78
|
+
};
|
|
30
79
|
/**
|
|
31
80
|
* Memoized refresh all groups
|
|
32
81
|
* It makes a single request to the server and retrieves
|
|
@@ -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,55 @@ 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
|
+
console.log(msg, dataMsg, 'subscriber');
|
|
56
|
+
if (dataMsg.data.group !== undefined) {
|
|
57
|
+
let _status;
|
|
58
|
+
switch (SCNotificationMapping[dataMsg.data.activity_type]) {
|
|
59
|
+
case SCNotificationTypologyType.USER_INVITED_TO_JOIN_GROUP:
|
|
60
|
+
_status = SCGroupSubscriptionStatusType.INVITED;
|
|
61
|
+
break;
|
|
62
|
+
case SCNotificationTypologyType.USER_REQUESTED_TO_JOIN_GROUP:
|
|
63
|
+
_status = SCGroupSubscriptionStatusType.REQUESTED;
|
|
64
|
+
break;
|
|
65
|
+
case SCNotificationTypologyType.USER_ACCEPTED_TO_JOIN_GROUP:
|
|
66
|
+
_status = SCGroupSubscriptionStatusType.SUBSCRIBED;
|
|
67
|
+
break;
|
|
68
|
+
case SCNotificationTypologyType.USER_ADDED_TO_GROUP:
|
|
69
|
+
_status = SCGroupSubscriptionStatusType.SUBSCRIBED;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
updateCache([dataMsg.data.group.id]);
|
|
73
|
+
setData((prev) => getDataUpdated(prev, dataMsg.data.group.id, _status));
|
|
74
|
+
}
|
|
75
|
+
};
|
|
27
76
|
/**
|
|
28
77
|
* Memoized refresh all groups
|
|
29
78
|
* It makes a single request to the server and retrieves
|