@selfcommunity/react-core 0.7.0-alpha.0 → 0.7.0-alpha.10

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.
Files changed (85) hide show
  1. package/lib/cjs/components/provider/SCThemeProvider/index.js +3 -3
  2. package/lib/cjs/constants/Cache.d.ts +12 -0
  3. package/lib/cjs/constants/Cache.js +17 -1
  4. package/lib/cjs/constants/Preferences.d.ts +16 -0
  5. package/lib/cjs/constants/Preferences.js +31 -2
  6. package/lib/cjs/constants/Routes.d.ts +6 -0
  7. package/lib/cjs/constants/Routes.js +14 -1
  8. package/lib/cjs/hooks/useSCFetchCategory.d.ts +1 -0
  9. package/lib/cjs/hooks/useSCFetchCategory.js +18 -1
  10. package/lib/cjs/hooks/useSCFetchCourse.js +15 -15
  11. package/lib/cjs/hooks/useSCFetchEvent.d.ts +1 -0
  12. package/lib/cjs/hooks/useSCFetchEvent.js +16 -1
  13. package/lib/cjs/hooks/useSCFetchGroup.d.ts +1 -0
  14. package/lib/cjs/hooks/useSCFetchGroup.js +16 -1
  15. package/lib/cjs/hooks/useSCFetchPaymentOrder.d.ts +20 -0
  16. package/lib/cjs/hooks/useSCFetchPaymentOrder.js +70 -0
  17. package/lib/cjs/hooks/useSCFetchPaymentProduct.d.ts +20 -0
  18. package/lib/cjs/hooks/useSCFetchPaymentProduct.js +70 -0
  19. package/lib/cjs/hooks/useSCFetchUsers.d.ts +23 -0
  20. package/lib/cjs/hooks/useSCFetchUsers.js +74 -0
  21. package/lib/cjs/hooks/useSCJoinedCoursesManager.d.ts +1 -1
  22. package/lib/cjs/hooks/useSCJoinedCoursesManager.js +36 -68
  23. package/lib/cjs/hooks/useSCPaymentsEnabled.d.ts +18 -0
  24. package/lib/cjs/hooks/useSCPaymentsEnabled.js +35 -0
  25. package/lib/cjs/hooks/useSCPreferenceEnabled.d.ts +6 -0
  26. package/lib/cjs/hooks/useSCPreferenceEnabled.js +16 -0
  27. package/lib/cjs/hooks/useSCSubscribedEventsManager.js +4 -14
  28. package/lib/cjs/hooks/useSCSubscribedGroupsManager.d.ts +1 -1
  29. package/lib/cjs/hooks/useSCSubscribedGroupsManager.js +29 -54
  30. package/lib/cjs/hooks/useSCWebPushMessaging.js +2 -2
  31. package/lib/cjs/index.d.ts +7 -2
  32. package/lib/cjs/index.js +13 -2
  33. package/lib/cjs/themes/theme.js +14 -2
  34. package/lib/cjs/types/theme.d.ts +53 -1
  35. package/lib/cjs/utils/hooks/index.d.ts +2 -1
  36. package/lib/cjs/utils/hooks/index.js +3 -1
  37. package/lib/cjs/utils/hooks/useResizeObserver.d.ts +1 -0
  38. package/lib/cjs/utils/hooks/useResizeObserver.js +16 -0
  39. package/lib/cjs/utils/user.d.ts +8 -1
  40. package/lib/cjs/utils/user.js +15 -2
  41. package/lib/esm/components/provider/SCThemeProvider/index.js +1 -1
  42. package/lib/esm/constants/Cache.d.ts +12 -0
  43. package/lib/esm/constants/Cache.js +12 -0
  44. package/lib/esm/constants/Preferences.d.ts +16 -0
  45. package/lib/esm/constants/Preferences.js +28 -0
  46. package/lib/esm/constants/Routes.d.ts +6 -0
  47. package/lib/esm/constants/Routes.js +12 -0
  48. package/lib/esm/hooks/useSCFetchCategory.d.ts +1 -0
  49. package/lib/esm/hooks/useSCFetchCategory.js +18 -1
  50. package/lib/esm/hooks/useSCFetchCourse.js +15 -15
  51. package/lib/esm/hooks/useSCFetchEvent.d.ts +1 -0
  52. package/lib/esm/hooks/useSCFetchEvent.js +16 -1
  53. package/lib/esm/hooks/useSCFetchGroup.d.ts +1 -0
  54. package/lib/esm/hooks/useSCFetchGroup.js +16 -1
  55. package/lib/esm/hooks/useSCFetchPaymentOrder.d.ts +20 -0
  56. package/lib/esm/hooks/useSCFetchPaymentOrder.js +67 -0
  57. package/lib/esm/hooks/useSCFetchPaymentProduct.d.ts +20 -0
  58. package/lib/esm/hooks/useSCFetchPaymentProduct.js +67 -0
  59. package/lib/esm/hooks/useSCFetchUsers.d.ts +23 -0
  60. package/lib/esm/hooks/useSCFetchUsers.js +72 -0
  61. package/lib/esm/hooks/useSCJoinedCoursesManager.d.ts +1 -1
  62. package/lib/esm/hooks/useSCJoinedCoursesManager.js +37 -68
  63. package/lib/esm/hooks/useSCPaymentsEnabled.d.ts +18 -0
  64. package/lib/esm/hooks/useSCPaymentsEnabled.js +31 -0
  65. package/lib/esm/hooks/useSCPreferenceEnabled.d.ts +6 -0
  66. package/lib/esm/hooks/useSCPreferenceEnabled.js +13 -0
  67. package/lib/esm/hooks/useSCSubscribedEventsManager.js +4 -14
  68. package/lib/esm/hooks/useSCSubscribedGroupsManager.d.ts +1 -1
  69. package/lib/esm/hooks/useSCSubscribedGroupsManager.js +29 -54
  70. package/lib/esm/hooks/useSCWebPushMessaging.js +1 -1
  71. package/lib/esm/index.d.ts +7 -2
  72. package/lib/esm/index.js +7 -2
  73. package/lib/esm/themes/theme.js +13 -1
  74. package/lib/esm/types/theme.d.ts +53 -1
  75. package/lib/esm/utils/hooks/index.d.ts +2 -1
  76. package/lib/esm/utils/hooks/index.js +2 -1
  77. package/lib/esm/utils/hooks/useResizeObserver.d.ts +1 -0
  78. package/lib/esm/utils/hooks/useResizeObserver.js +13 -0
  79. package/lib/esm/utils/user.d.ts +8 -1
  80. package/lib/esm/utils/user.js +13 -1
  81. package/lib/umd/440.js +2 -0
  82. package/lib/umd/react-core.js +1 -1
  83. package/package.json +6 -6
  84. package/lib/umd/258.js +0 -2
  85. /package/lib/umd/{258.js.LICENSE.txt → 440.js.LICENSE.txt} +0 -0
@@ -46,6 +46,21 @@ export default function useSCFetchCourse({ id = null, course = null, cacheStrate
46
46
  return Promise.resolve(res.data);
47
47
  });
48
48
  }, []);
49
+ /**
50
+ * Refresh course
51
+ */
52
+ const refreshCourse = useMemo(() => () => {
53
+ fetchCourse(id)
54
+ .then((e) => {
55
+ setSCCourse(e);
56
+ })
57
+ .catch((err) => {
58
+ LRUCache.delete(__courseCacheKey);
59
+ setError(`Error on refresh course with id ${id}`);
60
+ Logger.error(SCOPE_SC_CORE, `Error on refresh course with id ${id}`);
61
+ Logger.error(SCOPE_SC_CORE, err.message);
62
+ });
63
+ }, [id, __courseCacheKey, setSCCourse]);
49
64
  /**
50
65
  * If id attempt to get the course by id
51
66
  */
@@ -74,20 +89,5 @@ export default function useSCFetchCourse({ id = null, course = null, cacheStrate
74
89
  setSCCourse(course);
75
90
  }
76
91
  }, [course, authUserId]);
77
- /**
78
- * Refreshes course data from the network
79
- */
80
- const refreshCourse = useCallback(() => {
81
- if (id) {
82
- fetchCourse(id)
83
- .then((e) => {
84
- setSCCourse(e);
85
- })
86
- .catch((err) => {
87
- setError(`Failed to refresh course data: ${err.message}`);
88
- Logger.error(SCOPE_SC_CORE, err.message);
89
- });
90
- }
91
- }, [id, fetchCourse]);
92
92
  return { scCourse, setSCCourse, error, refreshCourse };
93
93
  }
@@ -19,4 +19,5 @@ export default function useSCFetchEvent({ id, event, autoSubscribe, cacheStrateg
19
19
  scEvent: SCEventType;
20
20
  setSCEvent: (e: SCEventType) => void;
21
21
  error: string;
22
+ refreshEvent: () => () => void;
22
23
  };
@@ -66,6 +66,21 @@ export default function useSCFetchEvent({ id = null, event = null, autoSubscribe
66
66
  return Promise.resolve(res.data);
67
67
  });
68
68
  }, []);
69
+ /**
70
+ * Refresh event
71
+ */
72
+ const refreshEvent = useMemo(() => () => () => {
73
+ fetchEvent(id)
74
+ .then((e) => {
75
+ setSCEvent(e);
76
+ })
77
+ .catch((err) => {
78
+ LRUCache.delete(__eventCacheKey);
79
+ setError(`Error on refresh event with id ${id}`);
80
+ Logger.error(SCOPE_SC_CORE, `Error on refresh event with id ${id}`);
81
+ Logger.error(SCOPE_SC_CORE, err.message);
82
+ });
83
+ }, [id, setSCEvent, __eventCacheKey]);
69
84
  /**
70
85
  * If id attempt to get the event by id
71
86
  */
@@ -88,5 +103,5 @@ export default function useSCFetchEvent({ id = null, event = null, autoSubscribe
88
103
  setSCEvent(event);
89
104
  }
90
105
  }, [event, authUserId]);
91
- return { scEvent, setSCEvent, error };
106
+ return { scEvent, setSCEvent, error, refreshEvent };
92
107
  }
@@ -17,4 +17,5 @@ export default function useSCFetchGroup({ id, group, cacheStrategy, }: {
17
17
  scGroup: SCGroupType;
18
18
  setSCGroup: (group: SCGroupType) => void;
19
19
  error: string;
20
+ refreshGroup: () => void;
20
21
  };
@@ -45,6 +45,21 @@ export default function useSCFetchGroup({ id = null, group = null, cacheStrategy
45
45
  return Promise.resolve(res.data);
46
46
  });
47
47
  }, [__groupId]);
48
+ /**
49
+ * Refresh group
50
+ */
51
+ const refreshGroup = useMemo(() => () => {
52
+ fetchGroup()
53
+ .then((obj) => {
54
+ setSCGroup(obj);
55
+ })
56
+ .catch((err) => {
57
+ LRUCache.delete(__groupCacheKey);
58
+ setError(`Error on refresh group with id ${id}`);
59
+ Logger.error(SCOPE_SC_CORE, `Error on refresh group with id ${id}`);
60
+ Logger.error(SCOPE_SC_CORE, err.message);
61
+ });
62
+ }, [__groupCacheKey, setSCGroup]);
48
63
  /**
49
64
  * If id attempt to get the group by id
50
65
  */
@@ -67,5 +82,5 @@ export default function useSCFetchGroup({ id = null, group = null, cacheStrategy
67
82
  setSCGroup(group);
68
83
  }
69
84
  }, [group, authUserId]);
70
- return { scGroup, setSCGroup, error };
85
+ return { scGroup, setSCGroup, error, refreshGroup };
71
86
  }
@@ -0,0 +1,20 @@
1
+ import { SCPaymentOrder } from '@selfcommunity/types';
2
+ import { CacheStrategies } from '@selfcommunity/utils';
3
+ /**
4
+ :::info
5
+ This custom hook is used to fetch a payment order.
6
+ :::
7
+ * @param object
8
+ * @param object.id
9
+ * @param object.paymentOrder
10
+ * @param object.cacheStrategy
11
+ */
12
+ export default function useSCFetchPaymentOrder({ id, paymentOrder, cacheStrategy, }: {
13
+ id?: number | string;
14
+ paymentOrder?: SCPaymentOrder;
15
+ cacheStrategy?: CacheStrategies;
16
+ }): {
17
+ scPaymentOrder: SCPaymentOrder;
18
+ setSCPaymentOrder: (c: SCPaymentOrder) => void;
19
+ error: string;
20
+ };
@@ -0,0 +1,67 @@
1
+ import { Endpoints, http } from '@selfcommunity/api-services';
2
+ import { CacheStrategies, Logger, LRUCache } from '@selfcommunity/utils';
3
+ import { useCallback, useEffect, useMemo, useState } from 'react';
4
+ import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect';
5
+ import { useSCUser } from '../components/provider/SCUserProvider';
6
+ import { getPaymentOrderObjectCacheKey } from '../constants/Cache';
7
+ import { SCOPE_SC_CORE } from '../constants/Errors';
8
+ /**
9
+ :::info
10
+ This custom hook is used to fetch a payment order.
11
+ :::
12
+ * @param object
13
+ * @param object.id
14
+ * @param object.paymentOrder
15
+ * @param object.cacheStrategy
16
+ */
17
+ export default function useSCFetchPaymentOrder({ id = null, paymentOrder = null, cacheStrategy = CacheStrategies.NETWORK_ONLY, }) {
18
+ const __paymentOrderId = useMemo(() => (paymentOrder === null || paymentOrder === void 0 ? void 0 : paymentOrder.id) || id, [paymentOrder, id]);
19
+ // CONTEXT
20
+ const scUserContext = useSCUser();
21
+ const authUserId = useMemo(() => { var _a; return ((_a = scUserContext.user) === null || _a === void 0 ? void 0 : _a.id) || null; }, [scUserContext.user]);
22
+ // CACHE
23
+ const __paymentOrderCacheKey = useMemo(() => getPaymentOrderObjectCacheKey(__paymentOrderId), [__paymentOrderId]);
24
+ const [scPaymentOrder, setScPaymentOrder] = useState(cacheStrategy !== CacheStrategies.NETWORK_ONLY ? LRUCache.get(__paymentOrderCacheKey, paymentOrder) : null);
25
+ const [error, setError] = useState(null);
26
+ const setSCPaymentOrder = useCallback((c) => {
27
+ setScPaymentOrder(c);
28
+ LRUCache.set(__paymentOrderCacheKey, c);
29
+ }, [setScPaymentOrder, __paymentOrderCacheKey]);
30
+ /**
31
+ * Memoized fetch order
32
+ */
33
+ const fetchPaymentOrder = useMemo(() => (id) => {
34
+ return http
35
+ .request({
36
+ url: Endpoints.GetPaymentOrder.url({ id }),
37
+ method: Endpoints.GetPaymentOrder.method,
38
+ })
39
+ .then((res) => {
40
+ if (res.status >= 300) {
41
+ return Promise.reject(res);
42
+ }
43
+ return Promise.resolve(res.data);
44
+ });
45
+ }, []);
46
+ /**
47
+ * If id attempt to get the course by id
48
+ */
49
+ useEffect(() => {
50
+ if (id !== null && id !== undefined && !paymentOrder) {
51
+ fetchPaymentOrder(id)
52
+ .then((e) => {
53
+ setSCPaymentOrder(e);
54
+ })
55
+ .catch((err) => {
56
+ LRUCache.delete(__paymentOrderCacheKey);
57
+ Logger.error(SCOPE_SC_CORE, err.message);
58
+ });
59
+ }
60
+ }, [id, paymentOrder, authUserId]);
61
+ useDeepCompareEffectNoCheck(() => {
62
+ if (paymentOrder) {
63
+ setSCPaymentOrder(paymentOrder);
64
+ }
65
+ }, [paymentOrder, authUserId]);
66
+ return { scPaymentOrder, setSCPaymentOrder, error };
67
+ }
@@ -0,0 +1,20 @@
1
+ import { SCPaymentProduct } from '@selfcommunity/types';
2
+ import { CacheStrategies } from '@selfcommunity/utils';
3
+ /**
4
+ :::info
5
+ This custom hook is used to fetch a payment product.
6
+ :::
7
+ * @param object
8
+ * @param object.id
9
+ * @param object.paymentProduct
10
+ * @param object.cacheStrategy
11
+ */
12
+ export default function useSCFetchPaymentProduct({ id, paymentProduct, cacheStrategy, }: {
13
+ id?: number | string;
14
+ paymentProduct?: SCPaymentProduct;
15
+ cacheStrategy?: CacheStrategies;
16
+ }): {
17
+ scPaymentProduct: SCPaymentProduct;
18
+ setSCPaymentProduct: (c: SCPaymentProduct) => void;
19
+ error: string;
20
+ };
@@ -0,0 +1,67 @@
1
+ import { Endpoints, http } from '@selfcommunity/api-services';
2
+ import { CacheStrategies, Logger, LRUCache } from '@selfcommunity/utils';
3
+ import { useCallback, useEffect, useMemo, useState } from 'react';
4
+ import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect';
5
+ import { useSCUser } from '../components/provider/SCUserProvider';
6
+ import { getPaymentProductObjectCacheKey } from '../constants/Cache';
7
+ import { SCOPE_SC_CORE } from '../constants/Errors';
8
+ /**
9
+ :::info
10
+ This custom hook is used to fetch a payment product.
11
+ :::
12
+ * @param object
13
+ * @param object.id
14
+ * @param object.paymentProduct
15
+ * @param object.cacheStrategy
16
+ */
17
+ export default function useSCFetchPaymentProduct({ id = null, paymentProduct = null, cacheStrategy = CacheStrategies.NETWORK_ONLY, }) {
18
+ const __paymentProductId = useMemo(() => (paymentProduct === null || paymentProduct === void 0 ? void 0 : paymentProduct.id) || id, [paymentProduct, id]);
19
+ // CONTEXT
20
+ const scUserContext = useSCUser();
21
+ const authUserId = useMemo(() => { var _a; return ((_a = scUserContext.user) === null || _a === void 0 ? void 0 : _a.id) || null; }, [scUserContext.user]);
22
+ // CACHE
23
+ const __paymentProductCacheKey = useMemo(() => getPaymentProductObjectCacheKey(__paymentProductId), [__paymentProductId]);
24
+ const [scPaymentProduct, setScPaymentProduct] = useState(cacheStrategy !== CacheStrategies.NETWORK_ONLY ? LRUCache.get(__paymentProductCacheKey, paymentProduct) : null);
25
+ const [error, setError] = useState(null);
26
+ const setSCPaymentProduct = useCallback((c) => {
27
+ setScPaymentProduct(c);
28
+ LRUCache.set(__paymentProductCacheKey, c);
29
+ }, [setScPaymentProduct, __paymentProductCacheKey]);
30
+ /**
31
+ * Memoized fetch product
32
+ */
33
+ const fetchPaymentProduct = useMemo(() => (id) => {
34
+ return http
35
+ .request({
36
+ url: Endpoints.GetPaymentProduct.url({ id }),
37
+ method: Endpoints.GetPaymentProduct.method,
38
+ })
39
+ .then((res) => {
40
+ if (res.status >= 300) {
41
+ return Promise.reject(res);
42
+ }
43
+ return Promise.resolve(res.data);
44
+ });
45
+ }, []);
46
+ /**
47
+ * If id attempt to get the course by id
48
+ */
49
+ useEffect(() => {
50
+ if (id !== null && id !== undefined && !paymentProduct) {
51
+ fetchPaymentProduct(id)
52
+ .then((e) => {
53
+ setSCPaymentProduct(e);
54
+ })
55
+ .catch((err) => {
56
+ LRUCache.delete(__paymentProductCacheKey);
57
+ Logger.error(SCOPE_SC_CORE, err.message);
58
+ });
59
+ }
60
+ }, [id, paymentProduct, authUserId]);
61
+ useDeepCompareEffectNoCheck(() => {
62
+ if (paymentProduct) {
63
+ setSCPaymentProduct(paymentProduct);
64
+ }
65
+ }, [paymentProduct, authUserId]);
66
+ return { scPaymentProduct, setSCPaymentProduct, error };
67
+ }
@@ -0,0 +1,23 @@
1
+ import { SCUserAutocompleteType } from '@selfcommunity/types';
2
+ import { CacheStrategies } from '@selfcommunity/utils';
3
+ /**
4
+ :::info
5
+ This custom hook is used to fetch users.
6
+ @param object.cacheStrategy
7
+
8
+ :::tip Context can be consumed in this way:
9
+
10
+ ```jsx
11
+ const {users, isLoading} = useSCFetchUsers();
12
+ ```
13
+ :::
14
+ * @param props
15
+ */
16
+ declare const useSCFetchUsers: (props?: {
17
+ cacheStrategy?: CacheStrategies;
18
+ search?: string;
19
+ }) => {
20
+ users: SCUserAutocompleteType[];
21
+ isLoading: boolean;
22
+ };
23
+ export default useSCFetchUsers;
@@ -0,0 +1,72 @@
1
+ import { __awaiter } from "tslib";
2
+ import { useEffect, useState } from 'react';
3
+ import { SCOPE_SC_CORE } from '../constants/Errors';
4
+ import { Endpoints, http } from '@selfcommunity/api-services';
5
+ import { CacheStrategies, Logger, LRUCache } from '@selfcommunity/utils';
6
+ import { getUserObjectCacheKey, getUsersObjectCacheKey } from '../constants/Cache';
7
+ // Hydrate cache
8
+ const hydrate = (ids) => {
9
+ if (!ids)
10
+ return null;
11
+ const users = ids.map((id) => {
12
+ const __userCacheKey = getUserObjectCacheKey(id);
13
+ return LRUCache.get(__userCacheKey);
14
+ });
15
+ if (users.filter((u) => !u).length > 0) {
16
+ return null; // revalidate cache
17
+ }
18
+ return users;
19
+ };
20
+ /**
21
+ :::info
22
+ This custom hook is used to fetch users.
23
+ @param object.cacheStrategy
24
+
25
+ :::tip Context can be consumed in this way:
26
+
27
+ ```jsx
28
+ const {users, isLoading} = useSCFetchUsers();
29
+ ```
30
+ :::
31
+ * @param props
32
+ */
33
+ const useSCFetchUsers = (props) => {
34
+ const { cacheStrategy = CacheStrategies.CACHE_FIRST, search = '' } = props || {};
35
+ const __usersCacheKey = getUsersObjectCacheKey(search);
36
+ const cachedUsers = cacheStrategy !== CacheStrategies.NETWORK_ONLY ? hydrate(LRUCache.get(__usersCacheKey, null)) : null;
37
+ const [data, setData] = useState(cachedUsers !== null ? { users: cachedUsers, isLoading: false } : { users: [], isLoading: false });
38
+ const fetchUsers = (next = Endpoints.UserAutocomplete.url(), searchParam) => __awaiter(void 0, void 0, void 0, function* () {
39
+ const response = yield http.request({
40
+ url: next,
41
+ method: Endpoints.UserAutocomplete.method,
42
+ params: searchParam ? { search: searchParam } : undefined,
43
+ });
44
+ const data = response.data;
45
+ if (data.next) {
46
+ return data.results.concat(yield fetchUsers(data.next, searchParam));
47
+ }
48
+ return data.results;
49
+ });
50
+ useEffect(() => {
51
+ if (!search)
52
+ return;
53
+ if (cacheStrategy === CacheStrategies.CACHE_FIRST && cachedUsers)
54
+ return;
55
+ fetchUsers(undefined, search)
56
+ .then((data) => {
57
+ setData({ users: data, isLoading: false });
58
+ LRUCache.set(__usersCacheKey, data.map((u) => {
59
+ const __userCacheKey = getUserObjectCacheKey(u.id);
60
+ LRUCache.set(__userCacheKey, u);
61
+ return u.id;
62
+ }));
63
+ })
64
+ .catch((error) => {
65
+ console.error(error);
66
+ Logger.error(SCOPE_SC_CORE, 'Unable to retrieve users');
67
+ setData((prev) => (Object.assign(Object.assign({}, prev), { isLoading: false })));
68
+ });
69
+ }, [search]);
70
+ return data;
71
+ };
72
+ export default useSCFetchUsers;
@@ -30,7 +30,7 @@ export default function useSCJoinedCoursesManager(user?: SCUserType): {
30
30
  isLoading: (v: number | {
31
31
  id: number;
32
32
  }) => boolean;
33
- join: (course: SCCourseType, userId?: number) => Promise<any>;
33
+ join: (course: SCCourseType) => Promise<any>;
34
34
  leave: (course: SCCourseType) => Promise<any>;
35
35
  joinStatus: (course: SCCourseType) => string;
36
36
  refresh: () => void;
@@ -1,3 +1,4 @@
1
+ import { __awaiter } from "tslib";
1
2
  import { useEffect, useMemo, useRef } from 'react';
2
3
  import { Endpoints, http } from '@selfcommunity/api-services';
3
4
  import { SCFeatureName, SCCoursePrivacyType, SCCourseJoinStatusType, SCNotificationTopicType, SCNotificationTypologyType, } from '@selfcommunity/types';
@@ -29,26 +30,19 @@ export default function useSCJoinedCoursesManager(user) {
29
30
  const authUserId = user ? user.id : null;
30
31
  const coursesEnabled = useMemo(() => preferences &&
31
32
  features &&
32
- features.includes(SCFeatureName.TAGGING) &&
33
33
  features.includes(SCFeatureName.COURSE) &&
34
34
  CONFIGURATIONS_COURSES_ENABLED in preferences &&
35
35
  preferences[CONFIGURATIONS_COURSES_ENABLED].value, [preferences, features]);
36
36
  const notificationInvitedToJoinCourse = useRef(null);
37
- const notificationRequestedToJoinCourse = useRef(null);
38
- const notificationAcceptedToJoinCourse = useRef(null);
39
37
  const notificationAddedToCourse = useRef(null);
40
38
  /**
41
39
  * Subscribe to notification types user_follow, user_unfollow
42
40
  */
43
41
  useDeepCompareEffectNoCheck(() => {
44
42
  notificationInvitedToJoinCourse.current = PubSub.subscribe(`${SCNotificationTopicType.INTERACTION}.${SCNotificationTypologyType.USER_INVITED_TO_JOIN_COURSE}`, notificationSubscriber);
45
- notificationRequestedToJoinCourse.current = PubSub.subscribe(`${SCNotificationTopicType.INTERACTION}.${SCNotificationTypologyType.USER_REQUESTED_TO_JOIN_COURSE}`, notificationSubscriber);
46
- notificationAcceptedToJoinCourse.current = PubSub.subscribe(`${SCNotificationTopicType.INTERACTION}.${SCNotificationTypologyType.USER_ACCEPTED_TO_JOIN_COURSE}`, notificationSubscriber);
47
43
  notificationAddedToCourse.current = PubSub.subscribe(`${SCNotificationTopicType.INTERACTION}.${SCNotificationTypologyType.USER_ADDED_TO_COURSE}`, notificationSubscriber);
48
44
  return () => {
49
45
  PubSub.unsubscribe(notificationInvitedToJoinCourse.current);
50
- PubSub.unsubscribe(notificationRequestedToJoinCourse.current);
51
- PubSub.unsubscribe(notificationAcceptedToJoinCourse.current);
52
46
  PubSub.unsubscribe(notificationAddedToCourse.current);
53
47
  };
54
48
  }, [data]);
@@ -58,20 +52,20 @@ export default function useSCJoinedCoursesManager(user) {
58
52
  * @param dataMsg
59
53
  */
60
54
  const notificationSubscriber = (msg, dataMsg) => {
55
+ var _a;
61
56
  if (dataMsg.data.course !== undefined) {
62
57
  let _status;
63
58
  switch (SCNotificationMapping[dataMsg.data.activity_type]) {
64
59
  case SCNotificationTypologyType.USER_INVITED_TO_JOIN_COURSE:
65
60
  _status = SCCourseJoinStatusType.INVITED;
66
61
  break;
67
- case SCNotificationTypologyType.USER_REQUESTED_TO_JOIN_COURSE:
68
- _status = SCCourseJoinStatusType.REQUESTED;
69
- break;
70
- case SCNotificationTypologyType.USER_ACCEPTED_TO_JOIN_COURSE:
71
- _status = SCCourseJoinStatusType.JOINED;
72
- break;
73
62
  case SCNotificationTypologyType.USER_ADDED_TO_COURSE:
74
- _status = SCCourseJoinStatusType.JOINED;
63
+ if (dataMsg.data.notification_obj.course && ((_a = dataMsg.data.notification_obj.course.paywalls) === null || _a === void 0 ? void 0 : _a.length)) {
64
+ _status = SCCourseJoinStatusType.PAYMENT_WAITING;
65
+ }
66
+ else {
67
+ _status = SCCourseJoinStatusType.JOINED;
68
+ }
75
69
  break;
76
70
  }
77
71
  updateCache([dataMsg.data.course]);
@@ -90,8 +84,8 @@ export default function useSCJoinedCoursesManager(user) {
90
84
  // Only if user is authenticated
91
85
  http
92
86
  .request({
93
- url: Endpoints.GetJoinedCourses.url(),
94
- method: Endpoints.GetJoinedCourses.method,
87
+ url: Endpoints.GetUserJoinedCourses.url({ id: user.id }),
88
+ method: Endpoints.GetUserJoinedCourses.method,
95
89
  })
96
90
  .then((res) => {
97
91
  if (res.status >= 300) {
@@ -112,67 +106,42 @@ export default function useSCJoinedCoursesManager(user) {
112
106
  * Memoized join Course
113
107
  * Toggle action
114
108
  */
115
- const join = useMemo(() => (course, userId) => {
109
+ const join = useMemo(() => (course) => __awaiter(this, void 0, void 0, function* () {
116
110
  setLoading(course.id);
117
- if (userId) {
118
- return http
119
- .request({
120
- url: Endpoints.InviteOrAcceptUsersToCourse.url({ id: course.id }),
121
- method: Endpoints.InviteOrAcceptUsersToCourse.method,
122
- data: { users: [userId] },
123
- })
124
- .then((res) => {
125
- if (res.status >= 300) {
126
- return Promise.reject(res);
127
- }
128
- updateCache([course.id]);
129
- setData((prev) => getDataUpdated(prev, course.id, SCCourseJoinStatusType.JOINED));
130
- setUnLoading(course.id);
131
- return Promise.resolve(res.data);
132
- });
133
- }
134
- else {
135
- return http
136
- .request({
137
- url: Endpoints.JoinOrAcceptInviteToCourse.url({ id: course.id }),
138
- method: Endpoints.JoinOrAcceptInviteToCourse.method,
139
- })
140
- .then((res) => {
141
- if (res.status >= 300) {
142
- return Promise.reject(res);
143
- }
144
- updateCache([course.id]);
145
- setData((prev) => getDataUpdated(prev, course.id, course.privacy === SCCoursePrivacyType.PRIVATE && course.join_status !== SCCourseJoinStatusType.INVITED
146
- ? SCCourseJoinStatusType.REQUESTED
147
- : SCCourseJoinStatusType.JOINED));
148
- setUnLoading(course.id);
149
- return Promise.resolve(res.data);
150
- });
111
+ const res = yield http.request({
112
+ url: Endpoints.JoinOrAcceptInviteToCourse.url({ id: course.id }),
113
+ method: Endpoints.JoinOrAcceptInviteToCourse.method,
114
+ });
115
+ if (res.status >= 300) {
116
+ return Promise.reject(res);
151
117
  }
152
- }, [data, loading, cache]);
118
+ updateCache([course.id]);
119
+ setData((prev) => getDataUpdated(prev, course.id, course.privacy === SCCoursePrivacyType.PRIVATE && course.join_status !== SCCourseJoinStatusType.INVITED
120
+ ? SCCourseJoinStatusType.REQUESTED
121
+ : SCCourseJoinStatusType.JOINED));
122
+ setUnLoading(course.id);
123
+ return yield Promise.resolve(res.data);
124
+ }), [data, loading, cache]);
153
125
  /**
154
126
  * Memoized leave Course
155
127
  * Toggle action
156
128
  */
157
- const leave = useMemo(() => (course) => {
158
- if (course.join_status !== SCCourseJoinStatusType.REQUESTED) {
129
+ const leave = useMemo(() => (course) => __awaiter(this, void 0, void 0, function* () {
130
+ if (data[course.id] !== SCCourseJoinStatusType.REQUESTED) {
159
131
  setLoading(course.id);
160
- return http
161
- .request({
132
+ const res = yield http.request({
162
133
  url: Endpoints.LeaveOrRemoveCourseRequest.url({ id: course.id }),
163
134
  method: Endpoints.LeaveOrRemoveCourseRequest.method,
164
- })
165
- .then((res) => {
166
- if (res.status >= 300) {
167
- return Promise.reject(res);
168
- }
169
- updateCache([course.id]);
170
- setData((prev) => getDataUpdated(prev, course.id, null));
171
- setUnLoading(course.id);
172
- return Promise.resolve(res.data);
173
135
  });
136
+ if (res.status >= 300) {
137
+ return Promise.reject(res);
138
+ }
139
+ updateCache([course.id]);
140
+ setData((prev) => getDataUpdated(prev, course.id, null));
141
+ setUnLoading(course.id);
142
+ return yield Promise.resolve(res.data);
174
143
  }
175
- }, [data, loading, cache]);
144
+ }), [data, loading, cache]);
176
145
  /**
177
146
  * Check the authenticated user subscription status to the course
178
147
  * Update the courses cached
@@ -248,7 +217,7 @@ export default function useSCJoinedCoursesManager(user) {
248
217
  if (cache.includes(course.id)) {
249
218
  return getCurrentCourseCacheStatus(course);
250
219
  }
251
- if (authUserId) {
220
+ if (authUserId && course) {
252
221
  if ('join_status' in course) {
253
222
  return getJoinStatus(course);
254
223
  }
@@ -257,7 +226,7 @@ export default function useSCJoinedCoursesManager(user) {
257
226
  }
258
227
  }
259
228
  return null;
260
- }, [loading, cache, authUserId]);
229
+ }, [loading, cache, authUserId, getJoinStatus, getCurrentCourseCacheStatus]);
261
230
  /**
262
231
  * Empty cache on logout
263
232
  */
@@ -0,0 +1,18 @@
1
+ /**
2
+ :::info
3
+ This custom hook is used to check if the payments are enabled
4
+ :::
5
+ :::tip How to use it:
6
+
7
+ Follow these steps:
8
+ ```jsx
9
+ 1. import useSCPaymentsEnabled from '@selfcommunity/react-core';
10
+ 2. const {isPaymentsEnabled, stripePublicKey, stripeConnectedAccountId} = useSCPaymentsEnabled();
11
+ ```
12
+ :::
13
+ */
14
+ export default function useSCPaymentsEnabled(): {
15
+ isPaymentsEnabled: any;
16
+ stripePublicKey: any;
17
+ stripeConnectedAccountId: any;
18
+ };
@@ -0,0 +1,31 @@
1
+ import { useMemo } from 'react';
2
+ import { SCFeatureName } from '@selfcommunity/types';
3
+ import { useSCPreferences } from '../components/provider/SCPreferencesProvider';
4
+ import * as SCPreferences from '../constants/Preferences';
5
+ /**
6
+ :::info
7
+ This custom hook is used to check if the payments are enabled
8
+ :::
9
+ :::tip How to use it:
10
+
11
+ Follow these steps:
12
+ ```jsx
13
+ 1. import useSCPaymentsEnabled from '@selfcommunity/react-core';
14
+ 2. const {isPaymentsEnabled, stripePublicKey, stripeConnectedAccountId} = useSCPaymentsEnabled();
15
+ ```
16
+ :::
17
+ */
18
+ export default function useSCPaymentsEnabled() {
19
+ // CONTEXT
20
+ const { preferences, features } = useSCPreferences();
21
+ const isPaymentsEnabled = useMemo(() => preferences &&
22
+ features &&
23
+ features.includes(SCFeatureName.PAYMENTS) &&
24
+ SCPreferences.CONFIGURATIONS_PAYMENTS_ENABLED in preferences &&
25
+ preferences[SCPreferences.CONFIGURATIONS_PAYMENTS_ENABLED].value, [preferences]);
26
+ const stripePublicKey = useMemo(() => preferences && SCPreferences.STATIC_STRIPE_PUBLIC_KEY in preferences && preferences[SCPreferences.STATIC_STRIPE_PUBLIC_KEY].value, [preferences]);
27
+ const stripeConnectedAccountId = useMemo(() => preferences &&
28
+ SCPreferences.CONFIGURATIONS_STRIPE_CONNECTED_ACCOUNT_ID in preferences &&
29
+ preferences[SCPreferences.CONFIGURATIONS_STRIPE_CONNECTED_ACCOUNT_ID].value, [preferences]);
30
+ return { isPaymentsEnabled, stripePublicKey, stripeConnectedAccountId };
31
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Custom hook to check if a given preference is present and has a value.
3
+ * @param preference - full preference key (e.g., 'section.name')
4
+ * @returns boolean - true if the preference exists and has a value
5
+ */
6
+ export default function useSCPreferenceEnabled(preference: string): boolean;
@@ -0,0 +1,13 @@
1
+ import { useMemo } from 'react';
2
+ import { useSCPreferences } from '../components/provider/SCPreferencesProvider';
3
+ /**
4
+ * Custom hook to check if a given preference is present and has a value.
5
+ * @param preference - full preference key (e.g., 'section.name')
6
+ * @returns boolean - true if the preference exists and has a value
7
+ */
8
+ export default function useSCPreferenceEnabled(preference) {
9
+ const { preferences } = useSCPreferences();
10
+ return useMemo(() => {
11
+ return preferences && preference in preferences && preferences[preference].value;
12
+ }, [preferences, preference]);
13
+ }