@selfcommunity/react-core 0.1.2-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.
- package/LICENSE.md +21 -0
- package/README.md +13 -0
- package/lib/cjs/components/provider/SCAlertMessagesProvider/index.js +67 -0
- package/lib/cjs/components/provider/SCContextProvider/index.js +117 -0
- package/lib/cjs/components/provider/SCLocaleProvider/index.js +158 -0
- package/lib/cjs/components/provider/SCNotificationProvider/index.js +73 -0
- package/lib/cjs/components/provider/SCPreferencesProvider/index.js +93 -0
- package/lib/cjs/components/provider/SCRoutingProvider/index.js +146 -0
- package/lib/cjs/components/provider/SCThemeProvider/index.js +123 -0
- package/lib/cjs/components/provider/SCUserProvider/index.js +310 -0
- package/lib/cjs/components/router/index.js +91 -0
- package/lib/cjs/constants/Actions.js +6 -0
- package/lib/cjs/constants/ContextProviders.js +29 -0
- package/lib/cjs/constants/Device.js +11 -0
- package/lib/cjs/constants/Errors.js +10 -0
- package/lib/cjs/constants/Features.js +84 -0
- package/lib/cjs/constants/Locale.js +19 -0
- package/lib/cjs/constants/Notification.js +67 -0
- package/lib/cjs/constants/Notifications.js +27 -0
- package/lib/cjs/constants/Preferences.js +344 -0
- package/lib/cjs/constants/Routes.js +68 -0
- package/lib/cjs/constants/Session.js +22 -0
- package/lib/cjs/constants/Theme.js +6 -0
- package/lib/cjs/constants/WebSocket.js +10 -0
- package/lib/cjs/hooks/useSCAuth.js +340 -0
- package/lib/cjs/hooks/useSCCachingManager.js +58 -0
- package/lib/cjs/hooks/useSCConnectionsManager.js +285 -0
- package/lib/cjs/hooks/useSCFetchCategories.js +71 -0
- package/lib/cjs/hooks/useSCFetchCategory.js +68 -0
- package/lib/cjs/hooks/useSCFetchCommentObject.js +68 -0
- package/lib/cjs/hooks/useSCFetchCommentObjects.js +296 -0
- package/lib/cjs/hooks/useSCFetchCustomAdv.js +84 -0
- package/lib/cjs/hooks/useSCFetchFeedObject.js +79 -0
- package/lib/cjs/hooks/useSCFetchIncubator.js +68 -0
- package/lib/cjs/hooks/useSCFetchTag.js +68 -0
- package/lib/cjs/hooks/useSCFetchUser.js +68 -0
- package/lib/cjs/hooks/useSCFollowedCategoriesManager.js +162 -0
- package/lib/cjs/hooks/useSCFollowersManager.js +232 -0
- package/lib/cjs/hooks/useSCMediaClick.js +27 -0
- package/lib/cjs/hooks/useSCSubscribedIncubatorsManager.js +165 -0
- package/lib/cjs/hooks/useSCWebPushMessaging.js +286 -0
- package/lib/cjs/hooks/useSCWebSocket.js +120 -0
- package/lib/cjs/index.js +169 -0
- package/lib/cjs/scss/styles.scss +1 -0
- package/lib/cjs/themes/theme.js +165 -0
- package/lib/cjs/types/context.js +3 -0
- package/lib/cjs/types/index.js +22 -0
- package/lib/cjs/utils/errors.js +196 -0
- package/lib/cjs/utils/locale.js +56 -0
- package/lib/cjs/utils/logger.js +32 -0
- package/lib/cjs/utils/object.js +44 -0
- package/lib/cjs/utils/session.js +17 -0
- package/lib/cjs/utils/string.js +57 -0
- package/lib/cjs/utils/url.js +70 -0
- package/lib/cjs/utils/user.js +83 -0
- package/lib/cjs/utils/validator.js +771 -0
- package/lib/cjs/utils/webPushMessaging.js +110 -0
- package/lib/cjs/utils/websocket.js +307 -0
- package/lib/esm/api-services/src/client/index.d.ts +115 -0
- package/lib/esm/api-services/src/client/index.d.ts.map +1 -0
- package/lib/esm/api-services/src/constants/Endpoints.d.ts +10 -0
- package/lib/esm/api-services/src/constants/Endpoints.d.ts.map +1 -0
- package/lib/esm/api-services/src/index.d.ts +24 -0
- package/lib/esm/api-services/src/index.d.ts.map +1 -0
- package/lib/esm/api-services/src/services/category/index.d.ts +13 -0
- package/lib/esm/api-services/src/services/category/index.d.ts.map +1 -0
- package/lib/esm/api-services/src/services/feature/index.d.ts +10 -0
- package/lib/esm/api-services/src/services/feature/index.d.ts.map +1 -0
- package/lib/esm/api-services/src/services/preference/index.d.ts +16 -0
- package/lib/esm/api-services/src/services/preference/index.d.ts.map +1 -0
- package/lib/esm/api-services/src/services/user/index.d.ts +16 -0
- package/lib/esm/api-services/src/services/user/index.d.ts.map +1 -0
- package/lib/esm/api-services/src/utils/http.d.ts +6 -0
- package/lib/esm/api-services/src/utils/http.d.ts.map +1 -0
- package/lib/esm/api-services/src/utils/token.d.ts +16 -0
- package/lib/esm/api-services/src/utils/token.d.ts.map +1 -0
- package/lib/esm/components/provider/SCAlertMessagesProvider/index.js +67 -0
- package/lib/esm/components/provider/SCContextProvider/index.js +117 -0
- package/lib/esm/components/provider/SCLocaleProvider/index.js +158 -0
- package/lib/esm/components/provider/SCNotificationProvider/index.js +73 -0
- package/lib/esm/components/provider/SCPreferencesProvider/index.js +93 -0
- package/lib/esm/components/provider/SCRoutingProvider/index.js +146 -0
- package/lib/esm/components/provider/SCThemeProvider/index.js +123 -0
- package/lib/esm/components/provider/SCUserProvider/index.js +310 -0
- package/lib/esm/components/router/index.js +91 -0
- package/lib/esm/constants/Actions.js +6 -0
- package/lib/esm/constants/ContextProviders.js +29 -0
- package/lib/esm/constants/Device.js +11 -0
- package/lib/esm/constants/Errors.js +10 -0
- package/lib/esm/constants/Features.js +84 -0
- package/lib/esm/constants/Locale.js +19 -0
- package/lib/esm/constants/Notification.js +67 -0
- package/lib/esm/constants/Notifications.js +27 -0
- package/lib/esm/constants/Preferences.js +344 -0
- package/lib/esm/constants/Routes.js +68 -0
- package/lib/esm/constants/Session.js +22 -0
- package/lib/esm/constants/Theme.js +6 -0
- package/lib/esm/constants/WebSocket.js +10 -0
- package/lib/esm/hooks/useSCAuth.js +340 -0
- package/lib/esm/hooks/useSCCachingManager.js +58 -0
- package/lib/esm/hooks/useSCConnectionsManager.js +285 -0
- package/lib/esm/hooks/useSCFetchCategories.js +71 -0
- package/lib/esm/hooks/useSCFetchCategory.js +68 -0
- package/lib/esm/hooks/useSCFetchCommentObject.js +68 -0
- package/lib/esm/hooks/useSCFetchCommentObjects.js +296 -0
- package/lib/esm/hooks/useSCFetchCustomAdv.js +84 -0
- package/lib/esm/hooks/useSCFetchFeedObject.js +79 -0
- package/lib/esm/hooks/useSCFetchIncubator.js +68 -0
- package/lib/esm/hooks/useSCFetchTag.js +68 -0
- package/lib/esm/hooks/useSCFetchUser.js +68 -0
- package/lib/esm/hooks/useSCFollowedCategoriesManager.js +162 -0
- package/lib/esm/hooks/useSCFollowersManager.js +232 -0
- package/lib/esm/hooks/useSCMediaClick.js +27 -0
- package/lib/esm/hooks/useSCSubscribedIncubatorsManager.js +165 -0
- package/lib/esm/hooks/useSCWebPushMessaging.js +286 -0
- package/lib/esm/hooks/useSCWebSocket.js +120 -0
- package/lib/esm/index.js +169 -0
- package/lib/esm/react-core/src/components/provider/SCAlertMessagesProvider/index.d.ts +37 -0
- package/lib/esm/react-core/src/components/provider/SCAlertMessagesProvider/index.d.ts.map +1 -0
- package/lib/esm/react-core/src/components/provider/SCContextProvider/index.d.ts +35 -0
- package/lib/esm/react-core/src/components/provider/SCContextProvider/index.d.ts.map +1 -0
- package/lib/esm/react-core/src/components/provider/SCLocaleProvider/index.d.ts +42 -0
- package/lib/esm/react-core/src/components/provider/SCLocaleProvider/index.d.ts.map +1 -0
- package/lib/esm/react-core/src/components/provider/SCNotificationProvider/index.d.ts +37 -0
- package/lib/esm/react-core/src/components/provider/SCNotificationProvider/index.d.ts.map +1 -0
- package/lib/esm/react-core/src/components/provider/SCPreferencesProvider/index.d.ts +38 -0
- package/lib/esm/react-core/src/components/provider/SCPreferencesProvider/index.d.ts.map +1 -0
- package/lib/esm/react-core/src/components/provider/SCRoutingProvider/index.d.ts +39 -0
- package/lib/esm/react-core/src/components/provider/SCRoutingProvider/index.d.ts.map +1 -0
- package/lib/esm/react-core/src/components/provider/SCThemeProvider/index.d.ts +49 -0
- package/lib/esm/react-core/src/components/provider/SCThemeProvider/index.d.ts.map +1 -0
- package/lib/esm/react-core/src/components/provider/SCUserProvider/index.d.ts +38 -0
- package/lib/esm/react-core/src/components/provider/SCUserProvider/index.d.ts.map +1 -0
- package/lib/esm/react-core/src/components/router/index.d.ts +34 -0
- package/lib/esm/react-core/src/components/router/index.d.ts.map +1 -0
- package/lib/esm/react-core/src/constants/Actions.d.ts +2 -0
- package/lib/esm/react-core/src/constants/Actions.d.ts.map +1 -0
- package/lib/esm/react-core/src/constants/ContextProviders.d.ts +6 -0
- package/lib/esm/react-core/src/constants/ContextProviders.d.ts.map +1 -0
- package/lib/esm/react-core/src/constants/Device.d.ts +6 -0
- package/lib/esm/react-core/src/constants/Device.d.ts.map +1 -0
- package/lib/esm/react-core/src/constants/Errors.d.ts +5 -0
- package/lib/esm/react-core/src/constants/Errors.d.ts.map +1 -0
- package/lib/esm/react-core/src/constants/Features.d.ts +41 -0
- package/lib/esm/react-core/src/constants/Features.d.ts.map +1 -0
- package/lib/esm/react-core/src/constants/Locale.d.ts +8 -0
- package/lib/esm/react-core/src/constants/Locale.d.ts.map +1 -0
- package/lib/esm/react-core/src/constants/Notification.d.ts +55 -0
- package/lib/esm/react-core/src/constants/Notification.d.ts.map +1 -0
- package/lib/esm/react-core/src/constants/Notifications.d.ts +11 -0
- package/lib/esm/react-core/src/constants/Notifications.d.ts.map +1 -0
- package/lib/esm/react-core/src/constants/Routes.d.ts +29 -0
- package/lib/esm/react-core/src/constants/Routes.d.ts.map +1 -0
- package/lib/esm/react-core/src/constants/Session.d.ts +10 -0
- package/lib/esm/react-core/src/constants/Session.d.ts.map +1 -0
- package/lib/esm/react-core/src/constants/Theme.d.ts +2 -0
- package/lib/esm/react-core/src/constants/Theme.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCAuth.d.ts +56 -0
- package/lib/esm/react-core/src/hooks/useSCAuth.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCCachingManager.d.ts +19 -0
- package/lib/esm/react-core/src/hooks/useSCCachingManager.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCConnectionsManager.d.ts +40 -0
- package/lib/esm/react-core/src/hooks/useSCConnectionsManager.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCFetchCategories.d.ts +17 -0
- package/lib/esm/react-core/src/hooks/useSCFetchCategories.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCFetchCategory.d.ts +19 -0
- package/lib/esm/react-core/src/hooks/useSCFetchCategory.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCFetchCommentObject.d.ts +19 -0
- package/lib/esm/react-core/src/hooks/useSCFetchCommentObject.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCFetchCommentObjects.d.ts +54 -0
- package/lib/esm/react-core/src/hooks/useSCFetchCommentObjects.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCFetchCustomAdv.d.ts +19 -0
- package/lib/esm/react-core/src/hooks/useSCFetchCustomAdv.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCFetchFeedObject.d.ts +21 -0
- package/lib/esm/react-core/src/hooks/useSCFetchFeedObject.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCFetchIncubator.d.ts +19 -0
- package/lib/esm/react-core/src/hooks/useSCFetchIncubator.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCFetchTag.d.ts +19 -0
- package/lib/esm/react-core/src/hooks/useSCFetchTag.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCFetchUser.d.ts +19 -0
- package/lib/esm/react-core/src/hooks/useSCFetchUser.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCFollowedCategoriesManager.d.ts +37 -0
- package/lib/esm/react-core/src/hooks/useSCFollowedCategoriesManager.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCFollowersManager.d.ts +37 -0
- package/lib/esm/react-core/src/hooks/useSCFollowersManager.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCMediaClick.d.ts +4 -0
- package/lib/esm/react-core/src/hooks/useSCMediaClick.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCSubscribedIncubatorsManager.d.ts +27 -0
- package/lib/esm/react-core/src/hooks/useSCSubscribedIncubatorsManager.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCWebPushMessaging.d.ts +9 -0
- package/lib/esm/react-core/src/hooks/useSCWebPushMessaging.d.ts.map +1 -0
- package/lib/esm/react-core/src/hooks/useSCWebSocket.d.ts +12 -0
- package/lib/esm/react-core/src/hooks/useSCWebSocket.d.ts.map +1 -0
- package/lib/esm/react-core/src/index.d.ts +77 -0
- package/lib/esm/react-core/src/index.d.ts.map +1 -0
- package/lib/esm/react-core/src/types/context.d.ts +428 -0
- package/lib/esm/react-core/src/types/context.d.ts.map +1 -0
- package/lib/esm/react-core/src/types/index.d.ts +3 -0
- package/lib/esm/react-core/src/types/index.d.ts.map +1 -0
- package/lib/esm/react-core/src/utils/errors.d.ts +98 -0
- package/lib/esm/react-core/src/utils/errors.d.ts.map +1 -0
- package/lib/esm/react-core/src/utils/locale.d.ts +10 -0
- package/lib/esm/react-core/src/utils/locale.d.ts.map +1 -0
- package/lib/esm/react-core/src/utils/logger.d.ts +11 -0
- package/lib/esm/react-core/src/utils/logger.d.ts.map +1 -0
- package/lib/esm/react-core/src/utils/object.d.ts +13 -0
- package/lib/esm/react-core/src/utils/object.d.ts.map +1 -0
- package/lib/esm/react-core/src/utils/session.d.ts +6 -0
- package/lib/esm/react-core/src/utils/session.d.ts.map +1 -0
- package/lib/esm/react-core/src/utils/string.d.ts +21 -0
- package/lib/esm/react-core/src/utils/string.d.ts.map +1 -0
- package/lib/esm/react-core/src/utils/url.d.ts +22 -0
- package/lib/esm/react-core/src/utils/url.d.ts.map +1 -0
- package/lib/esm/react-core/src/utils/user.d.ts +41 -0
- package/lib/esm/react-core/src/utils/user.d.ts.map +1 -0
- package/lib/esm/react-core/src/utils/validator.d.ts +239 -0
- package/lib/esm/react-core/src/utils/validator.d.ts.map +1 -0
- package/lib/esm/react-core/src/utils/webPushMessaging.d.ts +14 -0
- package/lib/esm/react-core/src/utils/webPushMessaging.d.ts.map +1 -0
- package/lib/esm/react-core/src/utils/websocket.d.ts +177 -0
- package/lib/esm/react-core/src/utils/websocket.d.ts.map +1 -0
- package/lib/esm/react-i18n/src/index.d.ts +815 -0
- package/lib/esm/react-i18n/src/index.d.ts.map +1 -0
- package/lib/esm/scss/styles.scss +1 -0
- package/lib/esm/themes/theme.js +165 -0
- package/lib/esm/types/context.js +3 -0
- package/lib/esm/types/index.js +22 -0
- package/lib/esm/types/src/index.d.ts +9 -0
- package/lib/esm/types/src/index.d.ts.map +1 -0
- package/lib/esm/types/src/types/auth.d.ts +26 -0
- package/lib/esm/types/src/types/auth.d.ts.map +1 -0
- package/lib/esm/types/src/types/broadcastMessage.d.ts +73 -0
- package/lib/esm/types/src/types/broadcastMessage.d.ts.map +1 -0
- package/lib/esm/types/src/types/category.d.ts +100 -0
- package/lib/esm/types/src/types/category.d.ts.map +1 -0
- package/lib/esm/types/src/types/comment.d.ts +97 -0
- package/lib/esm/types/src/types/comment.d.ts.map +1 -0
- package/lib/esm/types/src/types/customAdv.d.ts +53 -0
- package/lib/esm/types/src/types/customAdv.d.ts.map +1 -0
- package/lib/esm/types/src/types/customNotification.d.ts +28 -0
- package/lib/esm/types/src/types/customNotification.d.ts.map +1 -0
- package/lib/esm/types/src/types/embed.d.ts +33 -0
- package/lib/esm/types/src/types/embed.d.ts.map +1 -0
- package/lib/esm/types/src/types/feed.d.ts +208 -0
- package/lib/esm/types/src/types/feed.d.ts.map +1 -0
- package/lib/esm/types/src/types/incubator.d.ts +53 -0
- package/lib/esm/types/src/types/incubator.d.ts.map +1 -0
- package/lib/esm/types/src/types/index.d.ts +22 -0
- package/lib/esm/types/src/types/index.d.ts.map +1 -0
- package/lib/esm/types/src/types/location.d.ts +61 -0
- package/lib/esm/types/src/types/location.d.ts.map +1 -0
- package/lib/esm/types/src/types/media.d.ts +52 -0
- package/lib/esm/types/src/types/media.d.ts.map +1 -0
- package/lib/esm/types/src/types/notification.d.ts +416 -0
- package/lib/esm/types/src/types/notification.d.ts.map +1 -0
- package/lib/esm/types/src/types/poll.d.ts +77 -0
- package/lib/esm/types/src/types/poll.d.ts.map +1 -0
- package/lib/esm/types/src/types/privateMessage.d.ts +109 -0
- package/lib/esm/types/src/types/privateMessage.d.ts.map +1 -0
- package/lib/esm/types/src/types/prize.d.ts +48 -0
- package/lib/esm/types/src/types/prize.d.ts.map +1 -0
- package/lib/esm/types/src/types/tag.d.ts +47 -0
- package/lib/esm/types/src/types/tag.d.ts.map +1 -0
- package/lib/esm/types/src/types/user.d.ts +227 -0
- package/lib/esm/types/src/types/user.d.ts.map +1 -0
- package/lib/esm/utils/errors.js +196 -0
- package/lib/esm/utils/locale.js +56 -0
- package/lib/esm/utils/logger.js +32 -0
- package/lib/esm/utils/object.js +44 -0
- package/lib/esm/utils/session.js +17 -0
- package/lib/esm/utils/src/index.d.ts +7 -0
- package/lib/esm/utils/src/index.d.ts.map +1 -0
- package/lib/esm/utils/src/utils/string.d.ts +21 -0
- package/lib/esm/utils/src/utils/string.d.ts.map +1 -0
- package/lib/esm/utils/src/utils/url.d.ts +22 -0
- package/lib/esm/utils/src/utils/url.d.ts.map +1 -0
- package/lib/esm/utils/string.js +57 -0
- package/lib/esm/utils/url.js +70 -0
- package/lib/esm/utils/user.js +83 -0
- package/lib/esm/utils/validator.js +771 -0
- package/lib/esm/utils/webPushMessaging.js +110 -0
- package/lib/esm/utils/websocket.js +307 -0
- package/lib/umd/react-core.js +2 -0
- package/lib/umd/react-core.js.LICENSE.txt +1 -0
- package/package.json +128 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = useAuth;
|
|
5
|
+
exports.userActionTypes = void 0;
|
|
6
|
+
|
|
7
|
+
var _apiServices = require("@selfcommunity/api-services");
|
|
8
|
+
|
|
9
|
+
var _react = require("react");
|
|
10
|
+
|
|
11
|
+
var Session = _interopRequireWildcard(require("../constants/Session"));
|
|
12
|
+
|
|
13
|
+
var _useDeepCompareEffect = _interopRequireDefault(require("use-deep-compare-effect"));
|
|
14
|
+
|
|
15
|
+
var _logger = require("../utils/logger");
|
|
16
|
+
|
|
17
|
+
var _Errors = require("../constants/Errors");
|
|
18
|
+
|
|
19
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
20
|
+
|
|
21
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
22
|
+
|
|
23
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @hidden
|
|
27
|
+
* We have complex state logic that involves multiple sub-values,
|
|
28
|
+
* so useReducer is preferable to useState.
|
|
29
|
+
* Define all possible auth action types label
|
|
30
|
+
* Use this to export actions and dispatch an action
|
|
31
|
+
*/
|
|
32
|
+
const userActionTypes = {
|
|
33
|
+
LOGIN_LOADING: '_login_loading',
|
|
34
|
+
LOGIN_SUCCESS: '_login_success',
|
|
35
|
+
LOGIN_FAILURE: '_login_failure',
|
|
36
|
+
LOGOUT: '_logout',
|
|
37
|
+
REFRESH_TOKEN_SUCCESS: '_refresh_token_success',
|
|
38
|
+
REFRESH_TOKEN_FAILURE: '_invalid_token_failure',
|
|
39
|
+
REFRESH_SESSION: '_refresh_token',
|
|
40
|
+
UPDATE_USER: '_change_user'
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* userReducer:
|
|
44
|
+
* - manage the state of authentication
|
|
45
|
+
* - update the state base on action type
|
|
46
|
+
* @param state
|
|
47
|
+
* @param action
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
exports.userActionTypes = userActionTypes;
|
|
51
|
+
|
|
52
|
+
function userReducer(state, action) {
|
|
53
|
+
switch (action.type) {
|
|
54
|
+
case userActionTypes.LOGIN_LOADING:
|
|
55
|
+
return {
|
|
56
|
+
user: null,
|
|
57
|
+
session: Object.assign({}, state.session),
|
|
58
|
+
error: null,
|
|
59
|
+
loading: true
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
case userActionTypes.LOGIN_SUCCESS:
|
|
63
|
+
return {
|
|
64
|
+
user: action.payload.user,
|
|
65
|
+
error: null,
|
|
66
|
+
session: Object.assign({}, state.session),
|
|
67
|
+
loading: false
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
case userActionTypes.LOGIN_FAILURE:
|
|
71
|
+
return {
|
|
72
|
+
user: null,
|
|
73
|
+
session: Object.assign({}, state.session),
|
|
74
|
+
error: action.payload.error,
|
|
75
|
+
loading: false
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
case userActionTypes.REFRESH_TOKEN_SUCCESS:
|
|
79
|
+
const newAuthToken = Object.assign({}, state.session.authToken, Object.assign({}, state.session.authToken, {
|
|
80
|
+
accessToken: action.payload.token.accessToken
|
|
81
|
+
}, action.payload.token.refreshToken ? {
|
|
82
|
+
refreshToken: action.payload.token.refreshToken
|
|
83
|
+
} : {}, action.payload.token.expiresIn ? {
|
|
84
|
+
expiresIn: action.payload.token.expiresIn
|
|
85
|
+
} : {}));
|
|
86
|
+
const newSession = Object.assign({}, state.session, {
|
|
87
|
+
authToken: newAuthToken
|
|
88
|
+
}); // Update current client config
|
|
89
|
+
|
|
90
|
+
_apiServices.http.setAuthorizeToken(newAuthToken.accessToken);
|
|
91
|
+
|
|
92
|
+
return Object.assign({}, state, {
|
|
93
|
+
session: newSession,
|
|
94
|
+
error: null,
|
|
95
|
+
loading: false
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
case userActionTypes.REFRESH_TOKEN_FAILURE:
|
|
99
|
+
return {
|
|
100
|
+
user: null,
|
|
101
|
+
session: Object.assign({}, state.session),
|
|
102
|
+
loading: null,
|
|
103
|
+
error: action.payload.error
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
case userActionTypes.LOGOUT:
|
|
107
|
+
return {
|
|
108
|
+
user: null,
|
|
109
|
+
session: null,
|
|
110
|
+
error: null,
|
|
111
|
+
loading: null
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
case userActionTypes.UPDATE_USER:
|
|
115
|
+
return Object.assign({}, state, {
|
|
116
|
+
user: Object.assign({}, state.user, action.payload)
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
case userActionTypes.REFRESH_SESSION:
|
|
120
|
+
return Object.assign({}, state, action.payload.conf);
|
|
121
|
+
|
|
122
|
+
default:
|
|
123
|
+
throw new Error(`Unhandled type: ${action.type}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Define initial context auth session
|
|
128
|
+
* @param session
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
function stateInitializer(session) {
|
|
133
|
+
let _session = Object.assign({}, session);
|
|
134
|
+
|
|
135
|
+
let _isLoading = false;
|
|
136
|
+
/**
|
|
137
|
+
* Set http authorization if session type is OAuth or JWT
|
|
138
|
+
* Configure http object (Authorization, etc...)
|
|
139
|
+
*/
|
|
140
|
+
|
|
141
|
+
if ([Session.OAUTH_SESSION, Session.JWT_SESSION].includes(_session.type) && _session.authToken && _session.authToken.accessToken) {
|
|
142
|
+
_apiServices.http.setAuthorizeToken(_session.authToken.accessToken);
|
|
143
|
+
|
|
144
|
+
_isLoading = true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
_apiServices.http.setSupportWithCredentials(_session.type === Session.COOKIE_SESSION);
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
user: null,
|
|
151
|
+
session: _session,
|
|
152
|
+
error: null,
|
|
153
|
+
loading: _isLoading,
|
|
154
|
+
isSessionRefreshing: false,
|
|
155
|
+
refreshSession: false
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
:::info
|
|
160
|
+
This component is used to navigate through the application.
|
|
161
|
+
:::
|
|
162
|
+
|
|
163
|
+
#### Usage
|
|
164
|
+
|
|
165
|
+
In order to use router you need to import this components first:
|
|
166
|
+
|
|
167
|
+
```jsx
|
|
168
|
+
import {SCRoutingContextType, useSCRouting, Link, SCRoutes} from '@selfcommunity/react-core';
|
|
169
|
+
````
|
|
170
|
+
|
|
171
|
+
:::tipUsage Example:
|
|
172
|
+
|
|
173
|
+
```jsx
|
|
174
|
+
const scRoutingContext: SCRoutingContextType = useSCRouting();
|
|
175
|
+
<Button component={Link} to={scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, {id: user.id})>Go to profile</Button>
|
|
176
|
+
````
|
|
177
|
+
or
|
|
178
|
+
|
|
179
|
+
```jsx
|
|
180
|
+
const scRoutingContext: SCRoutingContextType = useSCRouting();
|
|
181
|
+
<Link to={scRoutingContext.url('profile', {id: user.id})}>Go to profile</Link>
|
|
182
|
+
````
|
|
183
|
+
:::
|
|
184
|
+
* @param initialSession
|
|
185
|
+
*/
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
function useAuth(initialSession) {
|
|
189
|
+
const [state, dispatch] = (0, _react.useReducer)(userReducer, {}, () => stateInitializer(initialSession));
|
|
190
|
+
let authInterceptor = (0, _react.useRef)(null);
|
|
191
|
+
let isSessionRefreshing = (0, _react.useRef)(false);
|
|
192
|
+
let failedQueue = (0, _react.useRef)([]); // CONST
|
|
193
|
+
|
|
194
|
+
const userId = state.user ? state.user.id : null;
|
|
195
|
+
const accessToken = state.session.authToken && state.session.authToken.accessToken ? state.session.authToken.accessToken : null;
|
|
196
|
+
/**
|
|
197
|
+
* Refresh session
|
|
198
|
+
*/
|
|
199
|
+
|
|
200
|
+
const refreshSession = (0, _react.useMemo)(() => () => {
|
|
201
|
+
const session = state.session;
|
|
202
|
+
|
|
203
|
+
if (!isSessionRefreshing.current && session.handleRefreshToken) {
|
|
204
|
+
isSessionRefreshing.current = true;
|
|
205
|
+
return session.handleRefreshToken(state.session).then(res => {
|
|
206
|
+
isSessionRefreshing.current = false;
|
|
207
|
+
dispatch({
|
|
208
|
+
type: userActionTypes.REFRESH_TOKEN_SUCCESS,
|
|
209
|
+
payload: {
|
|
210
|
+
token: res
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
return Promise.resolve(res);
|
|
214
|
+
}).catch(error => {
|
|
215
|
+
_logger.Logger.error(_Errors.SCOPE_SC_CORE, 'Unable to refresh user session.');
|
|
216
|
+
|
|
217
|
+
if (error.response && error.response.data) {
|
|
218
|
+
dispatch({
|
|
219
|
+
type: userActionTypes.REFRESH_TOKEN_FAILURE,
|
|
220
|
+
payload: {
|
|
221
|
+
error: error.response.toString()
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return Promise.reject(error);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return Promise.reject(new Error('Unable to refresh session. Unauthenticated user.'));
|
|
231
|
+
}, [accessToken]);
|
|
232
|
+
/**
|
|
233
|
+
* Manages multiple request during refresh session
|
|
234
|
+
* Save concurrent requests and retry them again
|
|
235
|
+
* at the end of refreshing session
|
|
236
|
+
*/
|
|
237
|
+
|
|
238
|
+
const processQueue = (0, _react.useMemo)(() => (error, token = null) => {
|
|
239
|
+
failedQueue.current.forEach(prom => {
|
|
240
|
+
if (error) {
|
|
241
|
+
prom.reject(error);
|
|
242
|
+
} else {
|
|
243
|
+
prom.resolve(token);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
failedQueue.current = [];
|
|
247
|
+
}, [failedQueue.current]);
|
|
248
|
+
/**
|
|
249
|
+
* Add/remove an http request interceptor.
|
|
250
|
+
* When the component unmounted the interceptor will be detached
|
|
251
|
+
* The interceptor check if the token is expiring
|
|
252
|
+
*/
|
|
253
|
+
|
|
254
|
+
(0, _react.useEffect)(() => {
|
|
255
|
+
if (userId !== null) {
|
|
256
|
+
authInterceptor.current = _apiServices.http.getClientInstance().interceptors.response.use(response => {
|
|
257
|
+
return response;
|
|
258
|
+
}, async error => {
|
|
259
|
+
let originalConfig = error.config;
|
|
260
|
+
|
|
261
|
+
if (error.response) {
|
|
262
|
+
if (error.response.status === 401) {
|
|
263
|
+
/**
|
|
264
|
+
* if other requests arrive at the same time
|
|
265
|
+
* as the token refresh, we save them for later
|
|
266
|
+
*/
|
|
267
|
+
if (isSessionRefreshing.current) {
|
|
268
|
+
return new Promise(function (resolve, reject) {
|
|
269
|
+
failedQueue.current.push({
|
|
270
|
+
resolve,
|
|
271
|
+
reject
|
|
272
|
+
});
|
|
273
|
+
}).then(token => {
|
|
274
|
+
originalConfig.headers['Authorization'] = 'Bearer ' + token;
|
|
275
|
+
return _apiServices.http.request(originalConfig);
|
|
276
|
+
}).catch(err => {
|
|
277
|
+
_logger.Logger.error(_Errors.SCOPE_SC_CORE, 'Unable to resolve promises in failedQueue.');
|
|
278
|
+
|
|
279
|
+
return Promise.reject(err);
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* we mark the request as retried,
|
|
284
|
+
* we avoid doing it again
|
|
285
|
+
*/
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
const session = state.session;
|
|
289
|
+
const authToken = session && 'authToken' in session ? session.authToken : null;
|
|
290
|
+
|
|
291
|
+
if (session.type !== Session.COOKIE_SESSION && !isSessionRefreshing.current && state.user && session && session.handleRefreshToken && Boolean(authToken && authToken.refreshToken)) {
|
|
292
|
+
/**
|
|
293
|
+
* set refreshing mode,
|
|
294
|
+
* save all concurrent request in the meantime
|
|
295
|
+
*/
|
|
296
|
+
try {
|
|
297
|
+
const res = await refreshSession();
|
|
298
|
+
originalConfig.headers.Authorization = `Bearer ${res['accessToken']}`;
|
|
299
|
+
processQueue(null, res['accessToken']);
|
|
300
|
+
return Promise.resolve(_apiServices.http.request(originalConfig));
|
|
301
|
+
} catch (_error) {
|
|
302
|
+
if (_error.response && _error.response.data) {
|
|
303
|
+
processQueue(_error, null);
|
|
304
|
+
return Promise.reject(_error.response.data);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return Promise.reject(error);
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return () => {
|
|
316
|
+
if (authInterceptor.current !== null) {
|
|
317
|
+
_apiServices.http.getClientInstance().interceptors.response.eject(authInterceptor.current);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
}, [userId, accessToken]);
|
|
321
|
+
/**
|
|
322
|
+
* Reset session if initial conf changed
|
|
323
|
+
*/
|
|
324
|
+
|
|
325
|
+
(0, _useDeepCompareEffect.default)(() => {
|
|
326
|
+
dispatch({
|
|
327
|
+
type: userActionTypes.REFRESH_SESSION,
|
|
328
|
+
payload: {
|
|
329
|
+
conf: stateInitializer(initialSession)
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}, [initialSession]);
|
|
333
|
+
return {
|
|
334
|
+
state,
|
|
335
|
+
dispatch,
|
|
336
|
+
helpers: {
|
|
337
|
+
refreshSession
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = useSCCachingManager;
|
|
5
|
+
|
|
6
|
+
var _react = require("react");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
:::info
|
|
10
|
+
This custom hook manages cached data and the loading state.
|
|
11
|
+
:::
|
|
12
|
+
*/
|
|
13
|
+
function useSCCachingManager() {
|
|
14
|
+
const cache = (0, _react.useRef)([]);
|
|
15
|
+
const [loading, setLoading] = (0, _react.useState)([]);
|
|
16
|
+
const [data, setData] = (0, _react.useState)([]);
|
|
17
|
+
/**
|
|
18
|
+
* Update cache
|
|
19
|
+
* @param ids
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const updateCache = (0, _react.useMemo)(() => ids => {
|
|
23
|
+
ids.map(c => {
|
|
24
|
+
if (!cache.current.includes(c)) {
|
|
25
|
+
cache.current.push(c);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}, [cache]);
|
|
29
|
+
/**
|
|
30
|
+
* Empty cache
|
|
31
|
+
* emptying the cache each isFollow request
|
|
32
|
+
* results in a request to the server
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
const emptyCache = (0, _react.useMemo)(() => () => {
|
|
36
|
+
cache.current = [];
|
|
37
|
+
}, [cache]);
|
|
38
|
+
/**
|
|
39
|
+
* Category is checking
|
|
40
|
+
* Return true if the manager is checking
|
|
41
|
+
* the follow status of the obj
|
|
42
|
+
* @param category
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
const isLoading = (0, _react.useMemo)(() => obj => {
|
|
46
|
+
return loading.includes(obj.id);
|
|
47
|
+
}, [loading]);
|
|
48
|
+
return {
|
|
49
|
+
cache: cache.current,
|
|
50
|
+
updateCache,
|
|
51
|
+
emptyCache,
|
|
52
|
+
data,
|
|
53
|
+
setData,
|
|
54
|
+
loading,
|
|
55
|
+
setLoading,
|
|
56
|
+
isLoading
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = useSCConnectionsManager;
|
|
5
|
+
|
|
6
|
+
var _react = require("react");
|
|
7
|
+
|
|
8
|
+
var _apiServices = require("@selfcommunity/api-services");
|
|
9
|
+
|
|
10
|
+
var _types = require("@selfcommunity/types");
|
|
11
|
+
|
|
12
|
+
var _logger = require("../utils/logger");
|
|
13
|
+
|
|
14
|
+
var _Errors = require("../constants/Errors");
|
|
15
|
+
|
|
16
|
+
var _SCPreferencesProvider = require("../components/provider/SCPreferencesProvider");
|
|
17
|
+
|
|
18
|
+
var _Preferences = require("../constants/Preferences");
|
|
19
|
+
|
|
20
|
+
var _useSCCachingManager = _interopRequireDefault(require("./useSCCachingManager"));
|
|
21
|
+
|
|
22
|
+
var _pubsubJs = _interopRequireDefault(require("pubsub-js"));
|
|
23
|
+
|
|
24
|
+
var _Notification = require("../constants/Notification");
|
|
25
|
+
|
|
26
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Used on refresh and in status method
|
|
30
|
+
* Check if the user status is 'connected', 'sent_connection_request'
|
|
31
|
+
* 'received_connection_request', to update the cache and data
|
|
32
|
+
*/
|
|
33
|
+
const STATUS_CONNECTED = 'connected';
|
|
34
|
+
const STATUS_DISCONNECTED = 'disconnected';
|
|
35
|
+
const STATUS_CONNECTION_REQUEST_SENT = 'sent_connection_request';
|
|
36
|
+
const STATUS_CONNECTION_REQUEST_RECEIVED = 'received_connection_request';
|
|
37
|
+
/**
|
|
38
|
+
:::info
|
|
39
|
+
This custom hook is used to manage to manage friends.
|
|
40
|
+
:::
|
|
41
|
+
|
|
42
|
+
:::tipHow to use it:
|
|
43
|
+
|
|
44
|
+
Follow these steps:
|
|
45
|
+
```jsx
|
|
46
|
+
1. const scUserContext: SCUserContextType = useSCUser();
|
|
47
|
+
2. const scConnectionsManager: SCConnectionsManagerType = scUserContext.manager.connections;
|
|
48
|
+
3. scConnectionsManager.status(user)
|
|
49
|
+
```
|
|
50
|
+
:::
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
function useSCConnectionsManager(user) {
|
|
54
|
+
const {
|
|
55
|
+
cache,
|
|
56
|
+
updateCache,
|
|
57
|
+
emptyCache,
|
|
58
|
+
data,
|
|
59
|
+
setData,
|
|
60
|
+
loading,
|
|
61
|
+
setLoading,
|
|
62
|
+
isLoading
|
|
63
|
+
} = (0, _useSCCachingManager.default)();
|
|
64
|
+
const scPreferencesContext = (0, _SCPreferencesProvider.useSCPreferences)();
|
|
65
|
+
const connectionsDisabled = _Preferences.CONFIGURATIONS_FOLLOW_ENABLED in scPreferencesContext.preferences && scPreferencesContext.preferences[_Preferences.CONFIGURATIONS_FOLLOW_ENABLED].value;
|
|
66
|
+
const notificationConnAcceptSubscription = (0, _react.useRef)(null);
|
|
67
|
+
const notificationConnRequestSubscription = (0, _react.useRef)(null);
|
|
68
|
+
const notificationConnRemoveSubscription = (0, _react.useRef)(null);
|
|
69
|
+
/**
|
|
70
|
+
* Notification subscriber only for FOLLOW
|
|
71
|
+
* @param msg
|
|
72
|
+
* @param data
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
const notificationSubscriber = (msg, data) => {
|
|
76
|
+
if (data.connection && data.connection_id !== undefined) {
|
|
77
|
+
updateCache([data.data.connection.id]);
|
|
78
|
+
let _data = [];
|
|
79
|
+
|
|
80
|
+
if (_Notification.SCNotificationMapping[data.data.activity_type] === _types.SCNotificationTypologyType.CONNECTION_REQUEST) {
|
|
81
|
+
_data = data.map(([k, v]) => ({
|
|
82
|
+
k: k === data.data.connection_id ? STATUS_CONNECTION_REQUEST_SENT : v
|
|
83
|
+
}));
|
|
84
|
+
} else if (_Notification.SCNotificationMapping[data.data.activity_type] === _types.SCNotificationTypologyType.CONNECTION_ACCEPT) {
|
|
85
|
+
_data = data.map(([k, v]) => ({
|
|
86
|
+
k: k === data.data.connection_id ? STATUS_CONNECTED : v
|
|
87
|
+
}));
|
|
88
|
+
} else if (_Notification.SCNotificationMapping[data.data.activity_type] === _types.SCNotificationTypologyType.CONNECTION_REMOVE) {
|
|
89
|
+
_data = data.map(([k, v]) => ({
|
|
90
|
+
k: k === data.data.connection_id ? STATUS_DISCONNECTED : v
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
setData(_data);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Subscribe to notification types user_follow, user_unfollow
|
|
99
|
+
*/
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
(0, _react.useEffect)(() => {
|
|
103
|
+
notificationConnAcceptSubscription.current = _pubsubJs.default.subscribe(`${_types.SCNotificationTopicType.INTERACTION}.${_types.SCNotificationTypologyType.CONNECTION_ACCEPT}`, notificationSubscriber);
|
|
104
|
+
notificationConnRequestSubscription.current = _pubsubJs.default.subscribe(`${_types.SCNotificationTopicType.INTERACTION}.${_types.SCNotificationTypologyType.CONNECTION_REQUEST}`, notificationSubscriber);
|
|
105
|
+
notificationConnRemoveSubscription.current = _pubsubJs.default.subscribe(`${_types.SCNotificationTopicType.INTERACTION}.${_types.SCNotificationTypologyType.CONNECTION_REMOVE}`, notificationSubscriber);
|
|
106
|
+
return () => {
|
|
107
|
+
_pubsubJs.default.unsubscribe(notificationConnAcceptSubscription.current);
|
|
108
|
+
|
|
109
|
+
_pubsubJs.default.unsubscribe(notificationConnRequestSubscription.current);
|
|
110
|
+
|
|
111
|
+
_pubsubJs.default.unsubscribe(notificationConnRemoveSubscription.current);
|
|
112
|
+
};
|
|
113
|
+
}, []);
|
|
114
|
+
/**
|
|
115
|
+
* Memoized refresh all connections
|
|
116
|
+
* It makes a single request to the server and retrieves
|
|
117
|
+
* all the users connected by the authenticated user in a single solution
|
|
118
|
+
* It might be useful for multi-tab sync
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
const refresh = (0, _react.useMemo)(() => () => {
|
|
122
|
+
emptyCache();
|
|
123
|
+
|
|
124
|
+
if (user && cache.length > 0) {
|
|
125
|
+
// Only if user is authenticated
|
|
126
|
+
_apiServices.http.request({
|
|
127
|
+
url: _apiServices.Endpoints.UserConnectionStatuses.url({}),
|
|
128
|
+
method: _apiServices.Endpoints.UserConnectionStatuses.method,
|
|
129
|
+
data: {
|
|
130
|
+
users: cache
|
|
131
|
+
}
|
|
132
|
+
}).then(res => {
|
|
133
|
+
if (res.status >= 300) {
|
|
134
|
+
return Promise.reject(res);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
updateCache(Object.keys(res.data.connection_statuses).map(id => parseInt(id)));
|
|
138
|
+
setData(Object.entries(res.data.connection_statuses).filter(([k, v]) => v !== null).map(([k, v]) => ({
|
|
139
|
+
[parseInt(k)]: v
|
|
140
|
+
})));
|
|
141
|
+
return Promise.resolve(res.data);
|
|
142
|
+
}).catch(e => {
|
|
143
|
+
_logger.Logger.error(_Errors.SCOPE_SC_CORE, 'Unable to refresh users Connections by the authenticated user.');
|
|
144
|
+
|
|
145
|
+
_logger.Logger.error(_Errors.SCOPE_SC_CORE, e);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}, [data, user, cache]);
|
|
149
|
+
/**
|
|
150
|
+
* Memoized Request connection
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
const requestConnection = (0, _react.useMemo)(() => user => {
|
|
154
|
+
setLoading(prev => [...prev, ...[user.id]]);
|
|
155
|
+
|
|
156
|
+
if (getCurrentStatus(user) === STATUS_CONNECTION_REQUEST_RECEIVED) {
|
|
157
|
+
return acceptConnection(user);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return _apiServices.http.request({
|
|
161
|
+
url: _apiServices.Endpoints.UserRequestConnection.url({
|
|
162
|
+
id: user.id
|
|
163
|
+
}),
|
|
164
|
+
method: _apiServices.Endpoints.UserRequestConnection.method
|
|
165
|
+
}).then(res => {
|
|
166
|
+
if (res.status >= 300) {
|
|
167
|
+
return Promise.reject(res);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
updateCache([user.id]);
|
|
171
|
+
|
|
172
|
+
const _data = data.map(([k, v]) => ({
|
|
173
|
+
k: k === user.id ? STATUS_CONNECTION_REQUEST_SENT : v
|
|
174
|
+
}));
|
|
175
|
+
|
|
176
|
+
setData(_data);
|
|
177
|
+
setLoading(prev => prev.filter(u => u !== user.id));
|
|
178
|
+
return Promise.resolve(res.data);
|
|
179
|
+
});
|
|
180
|
+
}, [data, loading, cache]);
|
|
181
|
+
/**
|
|
182
|
+
* Memoized Accept Request connection
|
|
183
|
+
*/
|
|
184
|
+
|
|
185
|
+
const acceptConnection = (0, _react.useMemo)(() => user => {
|
|
186
|
+
setLoading(prev => [...prev, ...[user.id]]);
|
|
187
|
+
|
|
188
|
+
if (getCurrentStatus(user) === STATUS_CONNECTION_REQUEST_RECEIVED) {
|
|
189
|
+
return _apiServices.http.request({
|
|
190
|
+
url: _apiServices.Endpoints.UserAcceptRequestConnection.url({
|
|
191
|
+
id: user.id
|
|
192
|
+
}),
|
|
193
|
+
method: _apiServices.Endpoints.UserAcceptRequestConnection.method
|
|
194
|
+
}).then(res => {
|
|
195
|
+
if (res.status >= 300) {
|
|
196
|
+
return Promise.reject(res);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
updateCache([user.id]);
|
|
200
|
+
|
|
201
|
+
const _data = data.map(([k, v]) => ({
|
|
202
|
+
k: k === user.id ? STATUS_CONNECTED : v
|
|
203
|
+
}));
|
|
204
|
+
|
|
205
|
+
setData(_data);
|
|
206
|
+
setLoading(prev => prev.filter(u => u !== user.id));
|
|
207
|
+
return Promise.resolve(res.data);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}, [data, loading, cache]);
|
|
211
|
+
/**
|
|
212
|
+
* Return current user status if exist,
|
|
213
|
+
* otherwise return null
|
|
214
|
+
*/
|
|
215
|
+
|
|
216
|
+
const getCurrentStatus = (0, _react.useMemo)(() => user => {
|
|
217
|
+
const d = data.filter(([id, v]) => id === user.id);
|
|
218
|
+
return d.length ? d[0][user.id] : null;
|
|
219
|
+
}, [data]);
|
|
220
|
+
/**
|
|
221
|
+
* Check if the authenticated user is connected with the user
|
|
222
|
+
* Update the users cached
|
|
223
|
+
* Update user statuses
|
|
224
|
+
* @param user
|
|
225
|
+
*/
|
|
226
|
+
|
|
227
|
+
const checkUserConnectionStatus = user => {
|
|
228
|
+
setLoading(prev => prev.includes(user.id) ? prev : [...prev, ...[user.id]]);
|
|
229
|
+
|
|
230
|
+
_apiServices.http.request({
|
|
231
|
+
url: _apiServices.Endpoints.UserCheckConnection.url({
|
|
232
|
+
id: user.id
|
|
233
|
+
}),
|
|
234
|
+
method: _apiServices.Endpoints.UserCheckConnection.method
|
|
235
|
+
}).then(res => {
|
|
236
|
+
if (res.status >= 300) {
|
|
237
|
+
return Promise.reject(res);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
updateCache([user.id]);
|
|
241
|
+
setData(prev => res.data.is_connection ? [...prev, ...[{
|
|
242
|
+
[user.id]: STATUS_CONNECTED
|
|
243
|
+
}]] : prev.filter(([id, v]) => id !== user.id));
|
|
244
|
+
setLoading(prev => prev.filter(u => u !== user.id));
|
|
245
|
+
return Promise.resolve(res.data);
|
|
246
|
+
});
|
|
247
|
+
};
|
|
248
|
+
/**
|
|
249
|
+
* Memoized status
|
|
250
|
+
* If user is already in cache -> check data user statuses,
|
|
251
|
+
* otherwise, check if auth user is connected with user
|
|
252
|
+
*/
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
const status = (0, _react.useMemo)(() => user => {
|
|
256
|
+
if (cache.includes(user.id)) {
|
|
257
|
+
return data.filter((k, v) => k === user.id)[0][user.id];
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (!loading.includes(user.id)) {
|
|
261
|
+
checkUserConnectionStatus(user);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return null;
|
|
265
|
+
}, [data, loading, cache]);
|
|
266
|
+
|
|
267
|
+
if (connectionsDisabled || !user) {
|
|
268
|
+
return {
|
|
269
|
+
connections: data,
|
|
270
|
+
loading,
|
|
271
|
+
isLoading
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
connections: data,
|
|
277
|
+
loading,
|
|
278
|
+
isLoading,
|
|
279
|
+
status,
|
|
280
|
+
requestConnection,
|
|
281
|
+
acceptConnection,
|
|
282
|
+
refresh,
|
|
283
|
+
emptyCache
|
|
284
|
+
};
|
|
285
|
+
}
|