@selfcommunity/react-core 0.3.9-alpha.4 → 0.3.9-alpha.6
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/lib/cjs/constants/Cache.d.ts +4 -2
- package/lib/cjs/constants/Cache.d.ts.map +1 -1
- package/lib/cjs/constants/Cache.js +8 -3
- package/lib/cjs/constants/Cache.js.map +1 -1
- package/lib/cjs/constants/ContextProviders.d.ts +0 -1
- package/lib/cjs/constants/ContextProviders.d.ts.map +1 -1
- package/lib/cjs/hooks/useSCFetchCommentObjects.d.ts.map +1 -1
- package/lib/cjs/hooks/useSCFetchCommentObjects.js +11 -10
- package/lib/cjs/hooks/useSCFetchCommentObjects.js.map +1 -1
- package/lib/cjs/hooks/useSCFetchCustomAdv.d.ts +5 -1
- package/lib/cjs/hooks/useSCFetchCustomAdv.d.ts.map +1 -1
- package/lib/cjs/hooks/useSCFetchCustomAdv.js +51 -27
- package/lib/cjs/hooks/useSCFetchCustomAdv.js.map +1 -1
- package/lib/esm/constants/Cache.d.ts +4 -2
- package/lib/esm/constants/Cache.d.ts.map +1 -1
- package/lib/esm/constants/Cache.js +6 -2
- package/lib/esm/constants/Cache.js.map +1 -1
- package/lib/esm/constants/ContextProviders.d.ts +0 -1
- package/lib/esm/constants/ContextProviders.d.ts.map +1 -1
- package/lib/esm/hooks/useSCFetchCommentObjects.d.ts.map +1 -1
- package/lib/esm/hooks/useSCFetchCommentObjects.js +11 -10
- package/lib/esm/hooks/useSCFetchCommentObjects.js.map +1 -1
- package/lib/esm/hooks/useSCFetchCustomAdv.d.ts +5 -1
- package/lib/esm/hooks/useSCFetchCustomAdv.d.ts.map +1 -1
- package/lib/esm/hooks/useSCFetchCustomAdv.js +51 -30
- package/lib/esm/hooks/useSCFetchCustomAdv.js.map +1 -1
- package/lib/umd/react-core.js +1 -1
- package/package.json +3 -3
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { useEffect, useMemo,
|
|
1
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
2
2
|
import { SCOPE_SC_CORE } from '../constants/Errors';
|
|
3
|
-
import {
|
|
4
|
-
import { Logger } from '@selfcommunity/utils';
|
|
3
|
+
import { Endpoints, http } from '@selfcommunity/api-services';
|
|
4
|
+
import { CacheStrategies, Logger, LRUCache } from '@selfcommunity/utils';
|
|
5
|
+
import useIsComponentMountedRef from '../utils/hooks/useIsComponentMountedRef';
|
|
6
|
+
import { getAdvObjectCacheKey } from '../constants/Cache';
|
|
5
7
|
/**
|
|
6
8
|
:::info
|
|
7
9
|
This custom hook is used to fetch a custom adv object.
|
|
@@ -9,15 +11,40 @@ import { Logger } from '@selfcommunity/utils';
|
|
|
9
11
|
* @param object
|
|
10
12
|
* @param object.position
|
|
11
13
|
* @param object.categoryId
|
|
14
|
+
* @param cacheStrategy
|
|
12
15
|
*/
|
|
13
|
-
export default function useSCFetchCustomAdv({ position = null, categoriesId = null }) {
|
|
14
|
-
const [scCustomAdv, setSCCustomAdv] = useState(null)
|
|
16
|
+
export default function useSCFetchCustomAdv({ id = null, position = null, categoriesId = null, cacheStrategy = CacheStrategies.CACHE_FIRST, }) {
|
|
17
|
+
const [scCustomAdv, setSCCustomAdv] = useState(id !== null && cacheStrategy === CacheStrategies.CACHE_FIRST && LRUCache.get(getAdvObjectCacheKey(id))
|
|
18
|
+
? LRUCache.get(getAdvObjectCacheKey(id))
|
|
19
|
+
: null);
|
|
15
20
|
const [error, setError] = useState(null);
|
|
16
|
-
|
|
21
|
+
// REFS
|
|
22
|
+
const mounted = useIsComponentMountedRef();
|
|
23
|
+
/**
|
|
24
|
+
* Cache advertising object
|
|
25
|
+
*/
|
|
26
|
+
const storeCache = useMemo(() => (data) => {
|
|
27
|
+
data.map((d) => {
|
|
28
|
+
LRUCache.set(getAdvObjectCacheKey(d.id), d);
|
|
29
|
+
});
|
|
30
|
+
}, []);
|
|
17
31
|
/**
|
|
18
32
|
* Memoized fetchCustomAdv
|
|
19
33
|
*/
|
|
20
34
|
const fetchCustomAdv = useMemo(() => () => {
|
|
35
|
+
if (id !== null) {
|
|
36
|
+
return http
|
|
37
|
+
.request({
|
|
38
|
+
url: Endpoints.CustomAdv.url({ id }),
|
|
39
|
+
method: Endpoints.CustomAdv.method,
|
|
40
|
+
})
|
|
41
|
+
.then((res) => {
|
|
42
|
+
if (res.status >= 300) {
|
|
43
|
+
return Promise.reject(res);
|
|
44
|
+
}
|
|
45
|
+
return Promise.resolve([res.data]);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
21
48
|
return http
|
|
22
49
|
.request({
|
|
23
50
|
url: Endpoints.CustomAdvSearch.url(),
|
|
@@ -30,34 +57,28 @@ export default function useSCFetchCustomAdv({ position = null, categoriesId = nu
|
|
|
30
57
|
}
|
|
31
58
|
return Promise.resolve(res.data.results);
|
|
32
59
|
});
|
|
33
|
-
}, [position, `${categoriesId}`]);
|
|
34
|
-
/**
|
|
35
|
-
* Track mount/unmount
|
|
36
|
-
*/
|
|
37
|
-
useEffect(() => {
|
|
38
|
-
mounted.current = true;
|
|
39
|
-
return () => {
|
|
40
|
-
mounted.current = false;
|
|
41
|
-
};
|
|
42
|
-
}, []);
|
|
60
|
+
}, [id, position, `${categoriesId}`]);
|
|
43
61
|
/**
|
|
44
62
|
* If id attempt to get the category by id
|
|
45
63
|
*/
|
|
46
64
|
useEffect(() => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
65
|
+
if (!scCustomAdv || cacheStrategy !== CacheStrategies.CACHE_FIRST) {
|
|
66
|
+
fetchCustomAdv()
|
|
67
|
+
.then((data) => {
|
|
68
|
+
if (mounted.current) {
|
|
69
|
+
setSCCustomAdv(data[Math.floor(Math.random() * data.length)]);
|
|
70
|
+
}
|
|
71
|
+
storeCache(data);
|
|
72
|
+
})
|
|
73
|
+
.catch((err) => {
|
|
74
|
+
if (mounted.current) {
|
|
75
|
+
setError(`Custom ADV with position ${position} not found`);
|
|
76
|
+
}
|
|
77
|
+
Logger.error(SCOPE_SC_CORE, `Custom ADV with position ${position} not found`);
|
|
78
|
+
Logger.error(SCOPE_SC_CORE, err.message);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}, [id, position, `${categoriesId}`, scCustomAdv]);
|
|
61
82
|
return { scCustomAdv, setSCCustomAdv, error };
|
|
62
83
|
}
|
|
63
84
|
//# sourceMappingURL=useSCFetchCustomAdv.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSCFetchCustomAdv.js","sourceRoot":"","sources":["../../../src/hooks/useSCFetchCustomAdv.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"useSCFetchCustomAdv.js","sourceRoot":"","sources":["../../../src/hooks/useSCFetchCustomAdv.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAC;AACnD,OAAO,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAC,SAAS,EAAE,IAAI,EAAe,MAAM,6BAA6B,CAAC;AAC1E,OAAO,EAAC,eAAe,EAAE,MAAM,EAAE,QAAQ,EAAC,MAAM,sBAAsB,CAAC;AAEvE,OAAO,wBAAwB,MAAM,yCAAyC,CAAC;AAC/E,OAAO,EAAC,oBAAoB,EAAC,MAAM,oBAAoB,CAAC;AAExD;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAC,EAC1C,EAAE,GAAG,IAAI,EACT,QAAQ,GAAG,IAAI,EACf,YAAY,GAAG,IAAI,EACnB,aAAa,GAAG,eAAe,CAAC,WAAW,GAM5C;IACC,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC,GAAG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