@selfcommunity/react-core 0.7.0-alpha.10 → 0.7.0-alpha.11
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.
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { SCUserAutocompleteType } from '@selfcommunity/types';
|
|
2
|
-
import { CacheStrategies } from '@selfcommunity/utils';
|
|
3
2
|
/**
|
|
4
3
|
:::info
|
|
5
4
|
This custom hook is used to fetch users.
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
|
|
8
6
|
:::tip Context can be consumed in this way:
|
|
9
7
|
|
|
10
8
|
```jsx
|
|
@@ -14,7 +12,6 @@ import { CacheStrategies } from '@selfcommunity/utils';
|
|
|
14
12
|
* @param props
|
|
15
13
|
*/
|
|
16
14
|
declare const useSCFetchUsers: (props?: {
|
|
17
|
-
cacheStrategy?: CacheStrategies;
|
|
18
15
|
search?: string;
|
|
19
16
|
}) => {
|
|
20
17
|
users: SCUserAutocompleteType[];
|
|
@@ -5,25 +5,10 @@ const react_1 = require("react");
|
|
|
5
5
|
const Errors_1 = require("../constants/Errors");
|
|
6
6
|
const api_services_1 = require("@selfcommunity/api-services");
|
|
7
7
|
const utils_1 = require("@selfcommunity/utils");
|
|
8
|
-
const Cache_1 = require("../constants/Cache");
|
|
9
|
-
// Hydrate cache
|
|
10
|
-
const hydrate = (ids) => {
|
|
11
|
-
if (!ids)
|
|
12
|
-
return null;
|
|
13
|
-
const users = ids.map((id) => {
|
|
14
|
-
const __userCacheKey = (0, Cache_1.getUserObjectCacheKey)(id);
|
|
15
|
-
return utils_1.LRUCache.get(__userCacheKey);
|
|
16
|
-
});
|
|
17
|
-
if (users.filter((u) => !u).length > 0) {
|
|
18
|
-
return null; // revalidate cache
|
|
19
|
-
}
|
|
20
|
-
return users;
|
|
21
|
-
};
|
|
22
8
|
/**
|
|
23
9
|
:::info
|
|
24
10
|
This custom hook is used to fetch users.
|
|
25
|
-
|
|
26
|
-
|
|
11
|
+
|
|
27
12
|
:::tip Context can be consumed in this way:
|
|
28
13
|
|
|
29
14
|
```jsx
|
|
@@ -33,10 +18,8 @@ const hydrate = (ids) => {
|
|
|
33
18
|
* @param props
|
|
34
19
|
*/
|
|
35
20
|
const useSCFetchUsers = (props) => {
|
|
36
|
-
const {
|
|
37
|
-
const
|
|
38
|
-
const cachedUsers = cacheStrategy !== utils_1.CacheStrategies.NETWORK_ONLY ? hydrate(utils_1.LRUCache.get(__usersCacheKey, null)) : null;
|
|
39
|
-
const [data, setData] = (0, react_1.useState)(cachedUsers !== null ? { users: cachedUsers, isLoading: false } : { users: [], isLoading: false });
|
|
21
|
+
const { search = '' } = props || {};
|
|
22
|
+
const [data, setData] = (0, react_1.useState)({ users: [], isLoading: false });
|
|
40
23
|
const fetchUsers = (next = api_services_1.Endpoints.UserAutocomplete.url(), searchParam) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
41
24
|
const response = yield api_services_1.http.request({
|
|
42
25
|
url: next,
|
|
@@ -52,16 +35,9 @@ const useSCFetchUsers = (props) => {
|
|
|
52
35
|
(0, react_1.useEffect)(() => {
|
|
53
36
|
if (!search)
|
|
54
37
|
return;
|
|
55
|
-
if (cacheStrategy === utils_1.CacheStrategies.CACHE_FIRST && cachedUsers)
|
|
56
|
-
return;
|
|
57
38
|
fetchUsers(undefined, search)
|
|
58
39
|
.then((data) => {
|
|
59
40
|
setData({ users: data, isLoading: false });
|
|
60
|
-
utils_1.LRUCache.set(__usersCacheKey, data.map((u) => {
|
|
61
|
-
const __userCacheKey = (0, Cache_1.getUserObjectCacheKey)(u.id);
|
|
62
|
-
utils_1.LRUCache.set(__userCacheKey, u);
|
|
63
|
-
return u.id;
|
|
64
|
-
}));
|
|
65
41
|
})
|
|
66
42
|
.catch((error) => {
|
|
67
43
|
console.error(error);
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { SCUserAutocompleteType } from '@selfcommunity/types';
|
|
2
|
-
import { CacheStrategies } from '@selfcommunity/utils';
|
|
3
2
|
/**
|
|
4
3
|
:::info
|
|
5
4
|
This custom hook is used to fetch users.
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
|
|
8
6
|
:::tip Context can be consumed in this way:
|
|
9
7
|
|
|
10
8
|
```jsx
|
|
@@ -14,7 +12,6 @@ import { CacheStrategies } from '@selfcommunity/utils';
|
|
|
14
12
|
* @param props
|
|
15
13
|
*/
|
|
16
14
|
declare const useSCFetchUsers: (props?: {
|
|
17
|
-
cacheStrategy?: CacheStrategies;
|
|
18
15
|
search?: string;
|
|
19
16
|
}) => {
|
|
20
17
|
users: SCUserAutocompleteType[];
|
|
@@ -2,26 +2,11 @@ import { __awaiter } from "tslib";
|
|
|
2
2
|
import { useEffect, useState } from 'react';
|
|
3
3
|
import { SCOPE_SC_CORE } from '../constants/Errors';
|
|
4
4
|
import { Endpoints, http } from '@selfcommunity/api-services';
|
|
5
|
-
import {
|
|
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
|
-
};
|
|
5
|
+
import { Logger } from '@selfcommunity/utils';
|
|
20
6
|
/**
|
|
21
7
|
:::info
|
|
22
8
|
This custom hook is used to fetch users.
|
|
23
|
-
|
|
24
|
-
|
|
9
|
+
|
|
25
10
|
:::tip Context can be consumed in this way:
|
|
26
11
|
|
|
27
12
|
```jsx
|
|
@@ -31,10 +16,8 @@ const hydrate = (ids) => {
|
|
|
31
16
|
* @param props
|
|
32
17
|
*/
|
|
33
18
|
const useSCFetchUsers = (props) => {
|
|
34
|
-
const {
|
|
35
|
-
const
|
|
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 });
|
|
19
|
+
const { search = '' } = props || {};
|
|
20
|
+
const [data, setData] = useState({ users: [], isLoading: false });
|
|
38
21
|
const fetchUsers = (next = Endpoints.UserAutocomplete.url(), searchParam) => __awaiter(void 0, void 0, void 0, function* () {
|
|
39
22
|
const response = yield http.request({
|
|
40
23
|
url: next,
|
|
@@ -50,16 +33,9 @@ const useSCFetchUsers = (props) => {
|
|
|
50
33
|
useEffect(() => {
|
|
51
34
|
if (!search)
|
|
52
35
|
return;
|
|
53
|
-
if (cacheStrategy === CacheStrategies.CACHE_FIRST && cachedUsers)
|
|
54
|
-
return;
|
|
55
36
|
fetchUsers(undefined, search)
|
|
56
37
|
.then((data) => {
|
|
57
38
|
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
39
|
})
|
|
64
40
|
.catch((error) => {
|
|
65
41
|
console.error(error);
|