@elice/material-shared-utils 1.220803.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/README.md +3 -0
- package/cjs/components/MaterialIntlProvider.d.ts +14 -0
- package/cjs/components/MaterialIntlProvider.js +30 -0
- package/cjs/components/index.d.ts +2 -0
- package/cjs/hooks/index.d.ts +1 -0
- package/cjs/hooks/useCallbackProp.d.ts +2 -0
- package/cjs/hooks/useCallbackProp.js +19 -0
- package/cjs/index.d.ts +5 -0
- package/cjs/index.js +26 -0
- package/cjs/material-config/MaterialConfigContext.d.ts +15 -0
- package/cjs/material-config/MaterialConfigContext.js +12 -0
- package/cjs/material-config/MaterialConfigProvider.d.ts +5 -0
- package/cjs/material-config/MaterialConfigProvider.js +45 -0
- package/cjs/material-config/index.d.ts +4 -0
- package/cjs/material-config/useMaterialConfig.d.ts +1 -0
- package/cjs/material-config/useMaterialConfig.js +18 -0
- package/cjs/material-config/useMaterialConfigApiClientUpdate.d.ts +3 -0
- package/cjs/material-config/useMaterialConfigApiClientUpdate.js +30 -0
- package/cjs/material-config/utils.d.ts +5 -0
- package/cjs/material-config/utils.js +15 -0
- package/cjs/material-fetch/index.d.ts +2 -0
- package/cjs/material-fetch/useMaterialFetch.d.ts +21 -0
- package/cjs/material-fetch/useMaterialFetch.js +51 -0
- package/cjs/material-fetch/useMaterialInitFetch.d.ts +22 -0
- package/cjs/material-fetch/useMaterialInitFetch.js +54 -0
- package/cjs/material-hoc/index.d.ts +1 -0
- package/cjs/material-hoc/withMaterial.d.ts +41 -0
- package/cjs/material-hoc/withMaterial.js +89 -0
- package/es/components/MaterialIntlProvider.d.ts +14 -0
- package/es/components/MaterialIntlProvider.js +24 -0
- package/es/components/index.d.ts +2 -0
- package/es/hooks/index.d.ts +1 -0
- package/es/hooks/useCallbackProp.d.ts +2 -0
- package/es/hooks/useCallbackProp.js +11 -0
- package/es/index.d.ts +5 -0
- package/es/index.js +9 -0
- package/es/material-config/MaterialConfigContext.d.ts +15 -0
- package/es/material-config/MaterialConfigContext.js +7 -0
- package/es/material-config/MaterialConfigProvider.d.ts +5 -0
- package/es/material-config/MaterialConfigProvider.js +39 -0
- package/es/material-config/index.d.ts +4 -0
- package/es/material-config/useMaterialConfig.d.ts +1 -0
- package/es/material-config/useMaterialConfig.js +14 -0
- package/es/material-config/useMaterialConfigApiClientUpdate.d.ts +3 -0
- package/es/material-config/useMaterialConfigApiClientUpdate.js +26 -0
- package/es/material-config/utils.d.ts +5 -0
- package/es/material-config/utils.js +11 -0
- package/es/material-fetch/index.d.ts +2 -0
- package/es/material-fetch/useMaterialFetch.d.ts +21 -0
- package/es/material-fetch/useMaterialFetch.js +47 -0
- package/es/material-fetch/useMaterialInitFetch.d.ts +22 -0
- package/es/material-fetch/useMaterialInitFetch.js +50 -0
- package/es/material-hoc/index.d.ts +1 -0
- package/es/material-hoc/withMaterial.d.ts +41 -0
- package/es/material-hoc/withMaterial.js +80 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export declare type MaterialIntlProviderPhrases = Record<string, Record<string, string>>;
|
|
3
|
+
export interface MaterialIntlProviderProps {
|
|
4
|
+
children?: React.ReactNode;
|
|
5
|
+
/** Locale code. (default: `ko`) */
|
|
6
|
+
locale?: string;
|
|
7
|
+
/** I18n phrases. */
|
|
8
|
+
phrases?: MaterialIntlProviderPhrases;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* IntlProvider for material components.
|
|
12
|
+
*/
|
|
13
|
+
declare const MaterialIntlProvider: React.FC<MaterialIntlProviderProps>;
|
|
14
|
+
export default MaterialIntlProvider;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var reactIntl = require('react-intl');
|
|
5
|
+
|
|
6
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
7
|
+
|
|
8
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
9
|
+
|
|
10
|
+
const INTL_DEFAULT_LOCALE = 'ko';
|
|
11
|
+
const INTL_DEFAULT_PHRASES = {};
|
|
12
|
+
/**
|
|
13
|
+
* IntlProvider for material components.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const MaterialIntlProvider = ({
|
|
17
|
+
children,
|
|
18
|
+
locale = INTL_DEFAULT_LOCALE,
|
|
19
|
+
phrases = INTL_DEFAULT_PHRASES
|
|
20
|
+
}) => {
|
|
21
|
+
const messages = React__default["default"].useMemo(() => locale in phrases ? phrases[locale] : phrases[INTL_DEFAULT_LOCALE], [locale, phrases]);
|
|
22
|
+
return React__default["default"].createElement(reactIntl.IntlProvider, {
|
|
23
|
+
children: children,
|
|
24
|
+
locale: locale,
|
|
25
|
+
defaultLocale: INTL_DEFAULT_LOCALE,
|
|
26
|
+
messages: messages
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
module.exports = MaterialIntlProvider;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useCallbackProp';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
10
|
+
|
|
11
|
+
const useCallbackProp = cb => {
|
|
12
|
+
const cbRef = React__default["default"].useRef(cb);
|
|
13
|
+
React__default["default"].useEffect(() => {
|
|
14
|
+
cbRef.current = cb;
|
|
15
|
+
}, [cb]);
|
|
16
|
+
return cbRef;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
exports.useCallbackProp = useCallbackProp;
|
package/cjs/index.d.ts
ADDED
package/cjs/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var MaterialIntlProvider = require('./components/MaterialIntlProvider.js');
|
|
6
|
+
var useCallbackProp = require('./hooks/useCallbackProp.js');
|
|
7
|
+
var MaterialConfigProvider = require('./material-config/MaterialConfigProvider.js');
|
|
8
|
+
var useMaterialConfig = require('./material-config/useMaterialConfig.js');
|
|
9
|
+
var useMaterialConfigApiClientUpdate = require('./material-config/useMaterialConfigApiClientUpdate.js');
|
|
10
|
+
var utils = require('./material-config/utils.js');
|
|
11
|
+
var useMaterialFetch = require('./material-fetch/useMaterialFetch.js');
|
|
12
|
+
var useMaterialInitFetch = require('./material-fetch/useMaterialInitFetch.js');
|
|
13
|
+
var withMaterial = require('./material-hoc/withMaterial.js');
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
exports.MaterialIntlProvider = MaterialIntlProvider;
|
|
18
|
+
exports.useCallbackProp = useCallbackProp.useCallbackProp;
|
|
19
|
+
exports.MaterialConfigProvider = MaterialConfigProvider;
|
|
20
|
+
exports.useMaterialConfig = useMaterialConfig.useMaterialConfig;
|
|
21
|
+
exports.useMaterialConfigApiClientUpdate = useMaterialConfigApiClientUpdate.useMaterialConfigApiClientUpdate;
|
|
22
|
+
exports.updateMaterialConfigSessionKey = utils.updateMaterialConfigSessionKey;
|
|
23
|
+
exports.useMaterialFetch = useMaterialFetch.useMaterialFetch;
|
|
24
|
+
exports.useMaterialInitFetch = useMaterialInitFetch.useMaterialInitFetch;
|
|
25
|
+
exports.withForwardRefMaterial = withMaterial.withForwardRefMaterial;
|
|
26
|
+
exports.withMaterial = withMaterial.withMaterial;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { BehaviorSubject } from 'rxjs';
|
|
3
|
+
export declare type MaterialConfigSessionKey = null | string;
|
|
4
|
+
export interface MaterialConfig {
|
|
5
|
+
/** Origin of API server. Should start with `http(s?)://` */
|
|
6
|
+
apiOriginUrl: string;
|
|
7
|
+
/** Origin of API websocket server. Should start with `ws(s?)://` */
|
|
8
|
+
apiWsOriginUrl: string;
|
|
9
|
+
/** Organization name short. */
|
|
10
|
+
orgNameShort: string;
|
|
11
|
+
/** Elice API session key. */
|
|
12
|
+
sessionKey: MaterialConfigSessionKey;
|
|
13
|
+
}
|
|
14
|
+
export declare const MaterialConfigContext: import("react").Context<MaterialConfig>;
|
|
15
|
+
export declare const sessionKeySubject$: BehaviorSubject<MaterialConfigSessionKey>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var rxjs = require('rxjs');
|
|
7
|
+
|
|
8
|
+
const MaterialConfigContext = React.createContext(null);
|
|
9
|
+
const sessionKeySubject$ = new rxjs.BehaviorSubject(null);
|
|
10
|
+
|
|
11
|
+
exports.MaterialConfigContext = MaterialConfigContext;
|
|
12
|
+
exports.sessionKeySubject$ = sessionKeySubject$;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { MaterialConfig } from './MaterialConfigContext';
|
|
3
|
+
declare type MaterialConfigProviderProps = Omit<MaterialConfig, 'sessionKey'>;
|
|
4
|
+
declare const MaterialConfigProvider: React.FC<MaterialConfigProviderProps>;
|
|
5
|
+
export default MaterialConfigProvider;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var MaterialConfigContext = require('./MaterialConfigContext.js');
|
|
5
|
+
|
|
6
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
7
|
+
|
|
8
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
9
|
+
|
|
10
|
+
const MaterialConfigProvider = ({
|
|
11
|
+
children,
|
|
12
|
+
apiOriginUrl,
|
|
13
|
+
apiWsOriginUrl,
|
|
14
|
+
orgNameShort: orgNameShortProp = 'academy'
|
|
15
|
+
}) => {
|
|
16
|
+
const [orgNameShort, setOrgNameShort] = React__default["default"].useState(orgNameShortProp);
|
|
17
|
+
const [sessionKey, setSessionKey] = React__default["default"].useState(null); //
|
|
18
|
+
// Update org name short.
|
|
19
|
+
//
|
|
20
|
+
|
|
21
|
+
React__default["default"].useEffect(() => setOrgNameShort(orgNameShortProp), [orgNameShortProp]); //
|
|
22
|
+
// Update session key when session key subject emits.
|
|
23
|
+
//
|
|
24
|
+
|
|
25
|
+
React__default["default"].useEffect(() => {
|
|
26
|
+
const sub = MaterialConfigContext.sessionKeySubject$.subscribe(setSessionKey);
|
|
27
|
+
return () => {
|
|
28
|
+
sub.unsubscribe();
|
|
29
|
+
};
|
|
30
|
+
}, []); //
|
|
31
|
+
//
|
|
32
|
+
//
|
|
33
|
+
|
|
34
|
+
return React__default["default"].createElement(MaterialConfigContext.MaterialConfigContext.Provider, {
|
|
35
|
+
children: children,
|
|
36
|
+
value: {
|
|
37
|
+
apiOriginUrl,
|
|
38
|
+
apiWsOriginUrl,
|
|
39
|
+
orgNameShort,
|
|
40
|
+
sessionKey
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
module.exports = MaterialConfigProvider;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useMaterialConfig: () => import("./MaterialConfigContext").MaterialConfig;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var MaterialConfigContext = require('./MaterialConfigContext.js');
|
|
7
|
+
|
|
8
|
+
const useMaterialConfig = () => {
|
|
9
|
+
const context = React.useContext(MaterialConfigContext.MaterialConfigContext);
|
|
10
|
+
|
|
11
|
+
if (context === undefined) {
|
|
12
|
+
throw new Error('Oops, `useMaterialConfig` must be used within a `MaterialConfigProvider`');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return context;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
exports.useMaterialConfig = useMaterialConfig;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { config as eliceApiClientConfig } from '@elice/api-client';
|
|
2
|
+
export declare type MaterialConfigApiClientUpdateCallback = typeof eliceApiClientConfig.init;
|
|
3
|
+
export declare const useMaterialConfigApiClientUpdate: (callback: MaterialConfigApiClientUpdateCallback) => boolean;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var reactUse = require('react-use');
|
|
7
|
+
var MaterialConfigContext = require('./MaterialConfigContext.js');
|
|
8
|
+
|
|
9
|
+
const useMaterialConfigApiClientUpdate = callback => {
|
|
10
|
+
const context = React.useContext(MaterialConfigContext.MaterialConfigContext);
|
|
11
|
+
|
|
12
|
+
if (!context) {
|
|
13
|
+
throw new Error('Oops, Elice material component must be used within a `MaterialConfigProvider`');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const [isReady, setReady] = React.useState(false);
|
|
17
|
+
reactUse.useDeepCompareEffect(() => {
|
|
18
|
+
var _a;
|
|
19
|
+
|
|
20
|
+
callback({
|
|
21
|
+
apiBaseUrl: context.apiOriginUrl,
|
|
22
|
+
orgNameShort: context.orgNameShort,
|
|
23
|
+
sessionkey: (_a = context.sessionKey) !== null && _a !== void 0 ? _a : undefined
|
|
24
|
+
});
|
|
25
|
+
setReady(Boolean(context.apiOriginUrl && context.orgNameShort && context.sessionKey));
|
|
26
|
+
}, [context]);
|
|
27
|
+
return isReady;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
exports.useMaterialConfigApiClientUpdate = useMaterialConfigApiClientUpdate;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var MaterialConfigContext = require('./MaterialConfigContext.js');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Update session key.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
function updateMaterialConfigSessionKey(sessionKey) {
|
|
12
|
+
MaterialConfigContext.sessionKeySubject$.next(sessionKey);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
exports.updateMaterialConfigSessionKey = updateMaterialConfigSessionKey;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { EliceApiResponse } from '@elice/api-client';
|
|
2
|
+
import type { APIStatus } from '@elice/material-shared-types';
|
|
3
|
+
declare type FetchFn<R> = (signal: AbortSignal) => Promise<EliceApiResponse<R>>;
|
|
4
|
+
/**
|
|
5
|
+
* Init api client and fetch material for contaner.
|
|
6
|
+
*
|
|
7
|
+
* @param fn API client wrapped with `React.useCallback` to fetch material.
|
|
8
|
+
* @param isReady Is ready to fetch material.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const [materialPdf, materialPdfStatus] =
|
|
12
|
+
* useMaterialFetch<GetOrgMaterialPdfGetResponses>(
|
|
13
|
+
* React.useCallback(
|
|
14
|
+
* signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
|
|
15
|
+
* [materialPdfId]
|
|
16
|
+
* ),
|
|
17
|
+
* isReady // boolean
|
|
18
|
+
* );
|
|
19
|
+
*/
|
|
20
|
+
export declare const useMaterialFetch: <R = any>(fn: FetchFn<R>, isReady?: boolean) => [R | null, APIStatus];
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Init api client and fetch material for contaner.
|
|
9
|
+
*
|
|
10
|
+
* @param fn API client wrapped with `React.useCallback` to fetch material.
|
|
11
|
+
* @param isReady Is ready to fetch material.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const [materialPdf, materialPdfStatus] =
|
|
15
|
+
* useMaterialFetch<GetOrgMaterialPdfGetResponses>(
|
|
16
|
+
* React.useCallback(
|
|
17
|
+
* signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
|
|
18
|
+
* [materialPdfId]
|
|
19
|
+
* ),
|
|
20
|
+
* isReady // boolean
|
|
21
|
+
* );
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const useMaterialFetch = (fn, isReady = true) => {
|
|
25
|
+
const [material, setMaterial] = React.useState(null);
|
|
26
|
+
const [materialStatus, setMaterialStatus] = React.useState('idle');
|
|
27
|
+
React.useEffect(() => {
|
|
28
|
+
if (!isReady) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
setMaterial(null);
|
|
33
|
+
setMaterialStatus('pending');
|
|
34
|
+
const abortCtrl = new AbortController();
|
|
35
|
+
fn(abortCtrl.signal).then(res => {
|
|
36
|
+
setMaterial(res);
|
|
37
|
+
setMaterialStatus('resolved');
|
|
38
|
+
}).catch(err => {
|
|
39
|
+
if (err.name !== 'AbortError') {
|
|
40
|
+
setMaterial(null);
|
|
41
|
+
setMaterialStatus('rejected');
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return () => {
|
|
45
|
+
abortCtrl.abort();
|
|
46
|
+
};
|
|
47
|
+
}, [isReady, fn]);
|
|
48
|
+
return [material, materialStatus];
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
exports.useMaterialFetch = useMaterialFetch;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { EliceApiClientConfigs, EliceApiResponse } from '@elice/api-client';
|
|
2
|
+
import type { APIStatus } from '@elice/material-shared-types';
|
|
3
|
+
declare type ApiInitFn = (config: EliceApiClientConfigs) => void;
|
|
4
|
+
declare type FetchFn<R> = (signal: AbortSignal) => Promise<EliceApiResponse<R>>;
|
|
5
|
+
/**
|
|
6
|
+
* Init api client and fetch material for contaner.
|
|
7
|
+
*
|
|
8
|
+
* @param init Init api client.
|
|
9
|
+
* @param fn API client wrapped with `React.useCallback` to fetch material.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const [materialPdf, materialPdfStatus] =
|
|
13
|
+
* useMaterialInitFetch<GetOrgMaterialPdfGetResponses>(
|
|
14
|
+
* eliceApiClientConfigs.init,
|
|
15
|
+
* React.useCallback(
|
|
16
|
+
* signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
|
|
17
|
+
* [materialPdfId]
|
|
18
|
+
* ),
|
|
19
|
+
* );
|
|
20
|
+
*/
|
|
21
|
+
export declare const useMaterialInitFetch: <R = any>(init: ApiInitFn, fn: FetchFn<R>) => [R | null, APIStatus];
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
require('../material-config/MaterialConfigContext.js');
|
|
7
|
+
var useMaterialConfigApiClientUpdate = require('../material-config/useMaterialConfigApiClientUpdate.js');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Init api client and fetch material for contaner.
|
|
11
|
+
*
|
|
12
|
+
* @param init Init api client.
|
|
13
|
+
* @param fn API client wrapped with `React.useCallback` to fetch material.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const [materialPdf, materialPdfStatus] =
|
|
17
|
+
* useMaterialInitFetch<GetOrgMaterialPdfGetResponses>(
|
|
18
|
+
* eliceApiClientConfigs.init,
|
|
19
|
+
* React.useCallback(
|
|
20
|
+
* signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
|
|
21
|
+
* [materialPdfId]
|
|
22
|
+
* ),
|
|
23
|
+
* );
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const useMaterialInitFetch = (init, fn) => {
|
|
27
|
+
const [material, setMaterial] = React.useState(null);
|
|
28
|
+
const [materialStatus, setMaterialStatus] = React.useState('idle');
|
|
29
|
+
const isReady = useMaterialConfigApiClientUpdate.useMaterialConfigApiClientUpdate(init);
|
|
30
|
+
React.useEffect(() => {
|
|
31
|
+
if (!isReady) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
setMaterial(null);
|
|
36
|
+
setMaterialStatus('pending');
|
|
37
|
+
const abortCtrl = new AbortController();
|
|
38
|
+
fn(abortCtrl.signal).then(res => {
|
|
39
|
+
setMaterial(res);
|
|
40
|
+
setMaterialStatus('resolved');
|
|
41
|
+
}).catch(err => {
|
|
42
|
+
if (err.name !== 'AbortError') {
|
|
43
|
+
setMaterial(null);
|
|
44
|
+
setMaterialStatus('rejected');
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return () => {
|
|
48
|
+
abortCtrl.abort();
|
|
49
|
+
};
|
|
50
|
+
}, [isReady, fn]);
|
|
51
|
+
return [material, materialStatus];
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
exports.useMaterialInitFetch = useMaterialInitFetch;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './withMaterial';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { MaterialIntlProviderPhrases } from '../components/MaterialIntlProvider';
|
|
3
|
+
import type { MaterialConfigApiClientUpdateCallback } from '../material-config';
|
|
4
|
+
declare type AnyObject = Record<string, any>;
|
|
5
|
+
/**
|
|
6
|
+
* Options for `withMaterial` HOC.
|
|
7
|
+
*/
|
|
8
|
+
export interface WithMaterialOptions {
|
|
9
|
+
/**
|
|
10
|
+
* If override is true, this root will create a new Recoil scope.
|
|
11
|
+
* - ref: https://recoiljs.org/docs/api-reference/core/RecoilRoot/#props -> `override`
|
|
12
|
+
*/
|
|
13
|
+
overrideRecoilScope?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Phrases for `IntlProvider`.
|
|
16
|
+
*/
|
|
17
|
+
phrases?: MaterialIntlProviderPhrases;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Common props for material components.
|
|
21
|
+
*/
|
|
22
|
+
export interface MaterialCommponentCommonProps {
|
|
23
|
+
locale?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* HOC for material components.
|
|
27
|
+
*
|
|
28
|
+
* wrapped with:
|
|
29
|
+
* - `<RecoilRoot />`
|
|
30
|
+
* - `<IntlProvider />`
|
|
31
|
+
*/
|
|
32
|
+
export declare const withMaterial: <P extends AnyObject>(Component: React.VFC<P & MaterialCommponentCommonProps>, eliceApiClientConfigInit: MaterialConfigApiClientUpdateCallback, options?: WithMaterialOptions) => React.VFC<P & MaterialCommponentCommonProps>;
|
|
33
|
+
/**
|
|
34
|
+
* HOC for material forward ref exotic components.
|
|
35
|
+
*
|
|
36
|
+
* wrapped with:
|
|
37
|
+
* - `<RecoilRoot />`
|
|
38
|
+
* - `<IntlProvider />`
|
|
39
|
+
*/
|
|
40
|
+
export declare const withForwardRefMaterial: <T, P extends AnyObject>(Component: React.ForwardRefExoticComponent<P & MaterialCommponentCommonProps & React.RefAttributes<T>>, eliceApiClientConfigInit: MaterialConfigApiClientUpdateCallback, options?: WithMaterialOptions) => React.ForwardRefExoticComponent<React.PropsWithoutRef<P & MaterialCommponentCommonProps> & React.RefAttributes<T>>;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var blocks = require('@elice/blocks');
|
|
7
|
+
var recoil = require('recoil');
|
|
8
|
+
var MaterialIntlProvider = require('../components/MaterialIntlProvider.js');
|
|
9
|
+
require('../material-config/MaterialConfigContext.js');
|
|
10
|
+
var useMaterialConfigApiClientUpdate = require('../material-config/useMaterialConfigApiClientUpdate.js');
|
|
11
|
+
|
|
12
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
+
|
|
14
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
15
|
+
|
|
16
|
+
//
|
|
17
|
+
//
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* HOC for material components.
|
|
21
|
+
*
|
|
22
|
+
* wrapped with:
|
|
23
|
+
* - `<RecoilRoot />`
|
|
24
|
+
* - `<IntlProvider />`
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
const withMaterial = (Component, eliceApiClientConfigInit, options = {}) => {
|
|
28
|
+
return props => {
|
|
29
|
+
var _a;
|
|
30
|
+
|
|
31
|
+
const isReady = useMaterialConfigApiClientUpdate.useMaterialConfigApiClientUpdate(eliceApiClientConfigInit); // do not render component, if material config is not ready
|
|
32
|
+
|
|
33
|
+
if (!isReady) {
|
|
34
|
+
return React__default["default"].createElement(blocks.Flex, {
|
|
35
|
+
justify: "center",
|
|
36
|
+
align: "center",
|
|
37
|
+
width: "100%",
|
|
38
|
+
height: "100%"
|
|
39
|
+
}, React__default["default"].createElement(blocks.Spinner, {
|
|
40
|
+
size: "small"
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return React__default["default"].createElement(recoil.RecoilRoot, {
|
|
45
|
+
override: (_a = options.overrideRecoilScope) !== null && _a !== void 0 ? _a : false
|
|
46
|
+
}, React__default["default"].createElement(MaterialIntlProvider, {
|
|
47
|
+
locale: props.locale,
|
|
48
|
+
phrases: options.phrases
|
|
49
|
+
}, React__default["default"].createElement(Component, Object.assign({}, props))));
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* HOC for material forward ref exotic components.
|
|
54
|
+
*
|
|
55
|
+
* wrapped with:
|
|
56
|
+
* - `<RecoilRoot />`
|
|
57
|
+
* - `<IntlProvider />`
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
const withForwardRefMaterial = (Component, eliceApiClientConfigInit, options = {}) => {
|
|
61
|
+
return React.forwardRef((props, ref) => {
|
|
62
|
+
var _a;
|
|
63
|
+
|
|
64
|
+
const isReady = useMaterialConfigApiClientUpdate.useMaterialConfigApiClientUpdate(eliceApiClientConfigInit); // do not render component, if material config is not ready
|
|
65
|
+
|
|
66
|
+
if (!isReady) {
|
|
67
|
+
return React__default["default"].createElement(blocks.Flex, {
|
|
68
|
+
justify: "center",
|
|
69
|
+
align: "center",
|
|
70
|
+
width: "100%",
|
|
71
|
+
height: "100%"
|
|
72
|
+
}, React__default["default"].createElement(blocks.Spinner, {
|
|
73
|
+
size: "small"
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return React__default["default"].createElement(recoil.RecoilRoot, {
|
|
78
|
+
override: (_a = options.overrideRecoilScope) !== null && _a !== void 0 ? _a : false
|
|
79
|
+
}, React__default["default"].createElement(MaterialIntlProvider, {
|
|
80
|
+
locale: props.locale,
|
|
81
|
+
phrases: options.phrases
|
|
82
|
+
}, React__default["default"].createElement(Component, Object.assign({}, props, {
|
|
83
|
+
ref: ref
|
|
84
|
+
}))));
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
exports.withForwardRefMaterial = withForwardRefMaterial;
|
|
89
|
+
exports.withMaterial = withMaterial;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export declare type MaterialIntlProviderPhrases = Record<string, Record<string, string>>;
|
|
3
|
+
export interface MaterialIntlProviderProps {
|
|
4
|
+
children?: React.ReactNode;
|
|
5
|
+
/** Locale code. (default: `ko`) */
|
|
6
|
+
locale?: string;
|
|
7
|
+
/** I18n phrases. */
|
|
8
|
+
phrases?: MaterialIntlProviderPhrases;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* IntlProvider for material components.
|
|
12
|
+
*/
|
|
13
|
+
declare const MaterialIntlProvider: React.FC<MaterialIntlProviderProps>;
|
|
14
|
+
export default MaterialIntlProvider;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { IntlProvider } from 'react-intl';
|
|
3
|
+
|
|
4
|
+
const INTL_DEFAULT_LOCALE = 'ko';
|
|
5
|
+
const INTL_DEFAULT_PHRASES = {};
|
|
6
|
+
/**
|
|
7
|
+
* IntlProvider for material components.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const MaterialIntlProvider = ({
|
|
11
|
+
children,
|
|
12
|
+
locale = INTL_DEFAULT_LOCALE,
|
|
13
|
+
phrases = INTL_DEFAULT_PHRASES
|
|
14
|
+
}) => {
|
|
15
|
+
const messages = React.useMemo(() => locale in phrases ? phrases[locale] : phrases[INTL_DEFAULT_LOCALE], [locale, phrases]);
|
|
16
|
+
return React.createElement(IntlProvider, {
|
|
17
|
+
children: children,
|
|
18
|
+
locale: locale,
|
|
19
|
+
defaultLocale: INTL_DEFAULT_LOCALE,
|
|
20
|
+
messages: messages
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { MaterialIntlProvider as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useCallbackProp';
|
package/es/index.d.ts
ADDED
package/es/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { default as MaterialIntlProvider } from './components/MaterialIntlProvider.js';
|
|
2
|
+
export { useCallbackProp } from './hooks/useCallbackProp.js';
|
|
3
|
+
export { default as MaterialConfigProvider } from './material-config/MaterialConfigProvider.js';
|
|
4
|
+
export { useMaterialConfig } from './material-config/useMaterialConfig.js';
|
|
5
|
+
export { useMaterialConfigApiClientUpdate } from './material-config/useMaterialConfigApiClientUpdate.js';
|
|
6
|
+
export { updateMaterialConfigSessionKey } from './material-config/utils.js';
|
|
7
|
+
export { useMaterialFetch } from './material-fetch/useMaterialFetch.js';
|
|
8
|
+
export { useMaterialInitFetch } from './material-fetch/useMaterialInitFetch.js';
|
|
9
|
+
export { withForwardRefMaterial, withMaterial } from './material-hoc/withMaterial.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { BehaviorSubject } from 'rxjs';
|
|
3
|
+
export declare type MaterialConfigSessionKey = null | string;
|
|
4
|
+
export interface MaterialConfig {
|
|
5
|
+
/** Origin of API server. Should start with `http(s?)://` */
|
|
6
|
+
apiOriginUrl: string;
|
|
7
|
+
/** Origin of API websocket server. Should start with `ws(s?)://` */
|
|
8
|
+
apiWsOriginUrl: string;
|
|
9
|
+
/** Organization name short. */
|
|
10
|
+
orgNameShort: string;
|
|
11
|
+
/** Elice API session key. */
|
|
12
|
+
sessionKey: MaterialConfigSessionKey;
|
|
13
|
+
}
|
|
14
|
+
export declare const MaterialConfigContext: import("react").Context<MaterialConfig>;
|
|
15
|
+
export declare const sessionKeySubject$: BehaviorSubject<MaterialConfigSessionKey>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { MaterialConfig } from './MaterialConfigContext';
|
|
3
|
+
declare type MaterialConfigProviderProps = Omit<MaterialConfig, 'sessionKey'>;
|
|
4
|
+
declare const MaterialConfigProvider: React.FC<MaterialConfigProviderProps>;
|
|
5
|
+
export default MaterialConfigProvider;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { sessionKeySubject$, MaterialConfigContext } from './MaterialConfigContext.js';
|
|
3
|
+
|
|
4
|
+
const MaterialConfigProvider = ({
|
|
5
|
+
children,
|
|
6
|
+
apiOriginUrl,
|
|
7
|
+
apiWsOriginUrl,
|
|
8
|
+
orgNameShort: orgNameShortProp = 'academy'
|
|
9
|
+
}) => {
|
|
10
|
+
const [orgNameShort, setOrgNameShort] = React.useState(orgNameShortProp);
|
|
11
|
+
const [sessionKey, setSessionKey] = React.useState(null); //
|
|
12
|
+
// Update org name short.
|
|
13
|
+
//
|
|
14
|
+
|
|
15
|
+
React.useEffect(() => setOrgNameShort(orgNameShortProp), [orgNameShortProp]); //
|
|
16
|
+
// Update session key when session key subject emits.
|
|
17
|
+
//
|
|
18
|
+
|
|
19
|
+
React.useEffect(() => {
|
|
20
|
+
const sub = sessionKeySubject$.subscribe(setSessionKey);
|
|
21
|
+
return () => {
|
|
22
|
+
sub.unsubscribe();
|
|
23
|
+
};
|
|
24
|
+
}, []); //
|
|
25
|
+
//
|
|
26
|
+
//
|
|
27
|
+
|
|
28
|
+
return React.createElement(MaterialConfigContext.Provider, {
|
|
29
|
+
children: children,
|
|
30
|
+
value: {
|
|
31
|
+
apiOriginUrl,
|
|
32
|
+
apiWsOriginUrl,
|
|
33
|
+
orgNameShort,
|
|
34
|
+
sessionKey
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export { MaterialConfigProvider as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useMaterialConfig: () => import("./MaterialConfigContext").MaterialConfig;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { MaterialConfigContext } from './MaterialConfigContext.js';
|
|
3
|
+
|
|
4
|
+
const useMaterialConfig = () => {
|
|
5
|
+
const context = useContext(MaterialConfigContext);
|
|
6
|
+
|
|
7
|
+
if (context === undefined) {
|
|
8
|
+
throw new Error('Oops, `useMaterialConfig` must be used within a `MaterialConfigProvider`');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return context;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { useMaterialConfig };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { config as eliceApiClientConfig } from '@elice/api-client';
|
|
2
|
+
export declare type MaterialConfigApiClientUpdateCallback = typeof eliceApiClientConfig.init;
|
|
3
|
+
export declare const useMaterialConfigApiClientUpdate: (callback: MaterialConfigApiClientUpdateCallback) => boolean;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useContext, useState } from 'react';
|
|
2
|
+
import { useDeepCompareEffect } from 'react-use';
|
|
3
|
+
import { MaterialConfigContext } from './MaterialConfigContext.js';
|
|
4
|
+
|
|
5
|
+
const useMaterialConfigApiClientUpdate = callback => {
|
|
6
|
+
const context = useContext(MaterialConfigContext);
|
|
7
|
+
|
|
8
|
+
if (!context) {
|
|
9
|
+
throw new Error('Oops, Elice material component must be used within a `MaterialConfigProvider`');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const [isReady, setReady] = useState(false);
|
|
13
|
+
useDeepCompareEffect(() => {
|
|
14
|
+
var _a;
|
|
15
|
+
|
|
16
|
+
callback({
|
|
17
|
+
apiBaseUrl: context.apiOriginUrl,
|
|
18
|
+
orgNameShort: context.orgNameShort,
|
|
19
|
+
sessionkey: (_a = context.sessionKey) !== null && _a !== void 0 ? _a : undefined
|
|
20
|
+
});
|
|
21
|
+
setReady(Boolean(context.apiOriginUrl && context.orgNameShort && context.sessionKey));
|
|
22
|
+
}, [context]);
|
|
23
|
+
return isReady;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { useMaterialConfigApiClientUpdate };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { EliceApiResponse } from '@elice/api-client';
|
|
2
|
+
import type { APIStatus } from '@elice/material-shared-types';
|
|
3
|
+
declare type FetchFn<R> = (signal: AbortSignal) => Promise<EliceApiResponse<R>>;
|
|
4
|
+
/**
|
|
5
|
+
* Init api client and fetch material for contaner.
|
|
6
|
+
*
|
|
7
|
+
* @param fn API client wrapped with `React.useCallback` to fetch material.
|
|
8
|
+
* @param isReady Is ready to fetch material.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const [materialPdf, materialPdfStatus] =
|
|
12
|
+
* useMaterialFetch<GetOrgMaterialPdfGetResponses>(
|
|
13
|
+
* React.useCallback(
|
|
14
|
+
* signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
|
|
15
|
+
* [materialPdfId]
|
|
16
|
+
* ),
|
|
17
|
+
* isReady // boolean
|
|
18
|
+
* );
|
|
19
|
+
*/
|
|
20
|
+
export declare const useMaterialFetch: <R = any>(fn: FetchFn<R>, isReady?: boolean) => [R | null, APIStatus];
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Init api client and fetch material for contaner.
|
|
5
|
+
*
|
|
6
|
+
* @param fn API client wrapped with `React.useCallback` to fetch material.
|
|
7
|
+
* @param isReady Is ready to fetch material.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const [materialPdf, materialPdfStatus] =
|
|
11
|
+
* useMaterialFetch<GetOrgMaterialPdfGetResponses>(
|
|
12
|
+
* React.useCallback(
|
|
13
|
+
* signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
|
|
14
|
+
* [materialPdfId]
|
|
15
|
+
* ),
|
|
16
|
+
* isReady // boolean
|
|
17
|
+
* );
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const useMaterialFetch = (fn, isReady = true) => {
|
|
21
|
+
const [material, setMaterial] = useState(null);
|
|
22
|
+
const [materialStatus, setMaterialStatus] = useState('idle');
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
if (!isReady) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setMaterial(null);
|
|
29
|
+
setMaterialStatus('pending');
|
|
30
|
+
const abortCtrl = new AbortController();
|
|
31
|
+
fn(abortCtrl.signal).then(res => {
|
|
32
|
+
setMaterial(res);
|
|
33
|
+
setMaterialStatus('resolved');
|
|
34
|
+
}).catch(err => {
|
|
35
|
+
if (err.name !== 'AbortError') {
|
|
36
|
+
setMaterial(null);
|
|
37
|
+
setMaterialStatus('rejected');
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return () => {
|
|
41
|
+
abortCtrl.abort();
|
|
42
|
+
};
|
|
43
|
+
}, [isReady, fn]);
|
|
44
|
+
return [material, materialStatus];
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export { useMaterialFetch };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { EliceApiClientConfigs, EliceApiResponse } from '@elice/api-client';
|
|
2
|
+
import type { APIStatus } from '@elice/material-shared-types';
|
|
3
|
+
declare type ApiInitFn = (config: EliceApiClientConfigs) => void;
|
|
4
|
+
declare type FetchFn<R> = (signal: AbortSignal) => Promise<EliceApiResponse<R>>;
|
|
5
|
+
/**
|
|
6
|
+
* Init api client and fetch material for contaner.
|
|
7
|
+
*
|
|
8
|
+
* @param init Init api client.
|
|
9
|
+
* @param fn API client wrapped with `React.useCallback` to fetch material.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const [materialPdf, materialPdfStatus] =
|
|
13
|
+
* useMaterialInitFetch<GetOrgMaterialPdfGetResponses>(
|
|
14
|
+
* eliceApiClientConfigs.init,
|
|
15
|
+
* React.useCallback(
|
|
16
|
+
* signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
|
|
17
|
+
* [materialPdfId]
|
|
18
|
+
* ),
|
|
19
|
+
* );
|
|
20
|
+
*/
|
|
21
|
+
export declare const useMaterialInitFetch: <R = any>(init: ApiInitFn, fn: FetchFn<R>) => [R | null, APIStatus];
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
import '../material-config/MaterialConfigContext.js';
|
|
3
|
+
import { useMaterialConfigApiClientUpdate } from '../material-config/useMaterialConfigApiClientUpdate.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Init api client and fetch material for contaner.
|
|
7
|
+
*
|
|
8
|
+
* @param init Init api client.
|
|
9
|
+
* @param fn API client wrapped with `React.useCallback` to fetch material.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const [materialPdf, materialPdfStatus] =
|
|
13
|
+
* useMaterialInitFetch<GetOrgMaterialPdfGetResponses>(
|
|
14
|
+
* eliceApiClientConfigs.init,
|
|
15
|
+
* React.useCallback(
|
|
16
|
+
* signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
|
|
17
|
+
* [materialPdfId]
|
|
18
|
+
* ),
|
|
19
|
+
* );
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const useMaterialInitFetch = (init, fn) => {
|
|
23
|
+
const [material, setMaterial] = useState(null);
|
|
24
|
+
const [materialStatus, setMaterialStatus] = useState('idle');
|
|
25
|
+
const isReady = useMaterialConfigApiClientUpdate(init);
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!isReady) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
setMaterial(null);
|
|
32
|
+
setMaterialStatus('pending');
|
|
33
|
+
const abortCtrl = new AbortController();
|
|
34
|
+
fn(abortCtrl.signal).then(res => {
|
|
35
|
+
setMaterial(res);
|
|
36
|
+
setMaterialStatus('resolved');
|
|
37
|
+
}).catch(err => {
|
|
38
|
+
if (err.name !== 'AbortError') {
|
|
39
|
+
setMaterial(null);
|
|
40
|
+
setMaterialStatus('rejected');
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return () => {
|
|
44
|
+
abortCtrl.abort();
|
|
45
|
+
};
|
|
46
|
+
}, [isReady, fn]);
|
|
47
|
+
return [material, materialStatus];
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export { useMaterialInitFetch };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './withMaterial';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { MaterialIntlProviderPhrases } from '../components/MaterialIntlProvider';
|
|
3
|
+
import type { MaterialConfigApiClientUpdateCallback } from '../material-config';
|
|
4
|
+
declare type AnyObject = Record<string, any>;
|
|
5
|
+
/**
|
|
6
|
+
* Options for `withMaterial` HOC.
|
|
7
|
+
*/
|
|
8
|
+
export interface WithMaterialOptions {
|
|
9
|
+
/**
|
|
10
|
+
* If override is true, this root will create a new Recoil scope.
|
|
11
|
+
* - ref: https://recoiljs.org/docs/api-reference/core/RecoilRoot/#props -> `override`
|
|
12
|
+
*/
|
|
13
|
+
overrideRecoilScope?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Phrases for `IntlProvider`.
|
|
16
|
+
*/
|
|
17
|
+
phrases?: MaterialIntlProviderPhrases;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Common props for material components.
|
|
21
|
+
*/
|
|
22
|
+
export interface MaterialCommponentCommonProps {
|
|
23
|
+
locale?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* HOC for material components.
|
|
27
|
+
*
|
|
28
|
+
* wrapped with:
|
|
29
|
+
* - `<RecoilRoot />`
|
|
30
|
+
* - `<IntlProvider />`
|
|
31
|
+
*/
|
|
32
|
+
export declare const withMaterial: <P extends AnyObject>(Component: React.VFC<P & MaterialCommponentCommonProps>, eliceApiClientConfigInit: MaterialConfigApiClientUpdateCallback, options?: WithMaterialOptions) => React.VFC<P & MaterialCommponentCommonProps>;
|
|
33
|
+
/**
|
|
34
|
+
* HOC for material forward ref exotic components.
|
|
35
|
+
*
|
|
36
|
+
* wrapped with:
|
|
37
|
+
* - `<RecoilRoot />`
|
|
38
|
+
* - `<IntlProvider />`
|
|
39
|
+
*/
|
|
40
|
+
export declare const withForwardRefMaterial: <T, P extends AnyObject>(Component: React.ForwardRefExoticComponent<P & MaterialCommponentCommonProps & React.RefAttributes<T>>, eliceApiClientConfigInit: MaterialConfigApiClientUpdateCallback, options?: WithMaterialOptions) => React.ForwardRefExoticComponent<React.PropsWithoutRef<P & MaterialCommponentCommonProps> & React.RefAttributes<T>>;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import React, { forwardRef } from 'react';
|
|
2
|
+
import { Flex, Spinner } from '@elice/blocks';
|
|
3
|
+
import { RecoilRoot } from 'recoil';
|
|
4
|
+
import MaterialIntlProvider from '../components/MaterialIntlProvider.js';
|
|
5
|
+
import '../material-config/MaterialConfigContext.js';
|
|
6
|
+
import { useMaterialConfigApiClientUpdate } from '../material-config/useMaterialConfigApiClientUpdate.js';
|
|
7
|
+
|
|
8
|
+
//
|
|
9
|
+
//
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* HOC for material components.
|
|
13
|
+
*
|
|
14
|
+
* wrapped with:
|
|
15
|
+
* - `<RecoilRoot />`
|
|
16
|
+
* - `<IntlProvider />`
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const withMaterial = (Component, eliceApiClientConfigInit, options = {}) => {
|
|
20
|
+
return props => {
|
|
21
|
+
var _a;
|
|
22
|
+
|
|
23
|
+
const isReady = useMaterialConfigApiClientUpdate(eliceApiClientConfigInit); // do not render component, if material config is not ready
|
|
24
|
+
|
|
25
|
+
if (!isReady) {
|
|
26
|
+
return React.createElement(Flex, {
|
|
27
|
+
justify: "center",
|
|
28
|
+
align: "center",
|
|
29
|
+
width: "100%",
|
|
30
|
+
height: "100%"
|
|
31
|
+
}, React.createElement(Spinner, {
|
|
32
|
+
size: "small"
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return React.createElement(RecoilRoot, {
|
|
37
|
+
override: (_a = options.overrideRecoilScope) !== null && _a !== void 0 ? _a : false
|
|
38
|
+
}, React.createElement(MaterialIntlProvider, {
|
|
39
|
+
locale: props.locale,
|
|
40
|
+
phrases: options.phrases
|
|
41
|
+
}, React.createElement(Component, Object.assign({}, props))));
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* HOC for material forward ref exotic components.
|
|
46
|
+
*
|
|
47
|
+
* wrapped with:
|
|
48
|
+
* - `<RecoilRoot />`
|
|
49
|
+
* - `<IntlProvider />`
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
const withForwardRefMaterial = (Component, eliceApiClientConfigInit, options = {}) => {
|
|
53
|
+
return forwardRef((props, ref) => {
|
|
54
|
+
var _a;
|
|
55
|
+
|
|
56
|
+
const isReady = useMaterialConfigApiClientUpdate(eliceApiClientConfigInit); // do not render component, if material config is not ready
|
|
57
|
+
|
|
58
|
+
if (!isReady) {
|
|
59
|
+
return React.createElement(Flex, {
|
|
60
|
+
justify: "center",
|
|
61
|
+
align: "center",
|
|
62
|
+
width: "100%",
|
|
63
|
+
height: "100%"
|
|
64
|
+
}, React.createElement(Spinner, {
|
|
65
|
+
size: "small"
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return React.createElement(RecoilRoot, {
|
|
70
|
+
override: (_a = options.overrideRecoilScope) !== null && _a !== void 0 ? _a : false
|
|
71
|
+
}, React.createElement(MaterialIntlProvider, {
|
|
72
|
+
locale: props.locale,
|
|
73
|
+
phrases: options.phrases
|
|
74
|
+
}, React.createElement(Component, Object.assign({}, props, {
|
|
75
|
+
ref: ref
|
|
76
|
+
}))));
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export { withForwardRefMaterial, withMaterial };
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@elice/material-shared-utils",
|
|
3
|
+
"version": "1.220803.0",
|
|
4
|
+
"description": "Shared typings for elice material projects",
|
|
5
|
+
"repository": "https://git.elicer.io/elice/frontend/library/elice-material",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"main": "./cjs/index.js",
|
|
8
|
+
"module": "./es/index.js",
|
|
9
|
+
"types": "./es/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"es",
|
|
12
|
+
"cjs",
|
|
13
|
+
"package.json",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": "~16.14",
|
|
19
|
+
"yarn": ">=1.20 <2"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"start": "run-s watch",
|
|
23
|
+
"prebuild": "run-s clean",
|
|
24
|
+
"build": "run-p build:es build:cjs",
|
|
25
|
+
"build:es": "rollup -c ../../rollup.config.ts -f es",
|
|
26
|
+
"build:cjs": "rollup -c ../../rollup.config.ts -f cjs",
|
|
27
|
+
"watch": "rollup -c ../../rollup.config.ts -f es -w",
|
|
28
|
+
"lint": "eslint --ext .ts,.tsx ./src --max-warnings=0",
|
|
29
|
+
"clean": "del-cli \"es/*\" \"cjs/*\" \"dist/*\""
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@elice/api-client": "^1.220802.1",
|
|
33
|
+
"@elice/material-shared-types": "22.7.2-1",
|
|
34
|
+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
|
|
35
|
+
"react-use": "^17.0.0",
|
|
36
|
+
"recoil": "~0.7.3"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"tslib": "^2.3.1"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@elice/api-client": "^1.220802.1",
|
|
43
|
+
"@elice/material-shared-types": "1.220803.0",
|
|
44
|
+
"@elice/types": "^1.220802.1",
|
|
45
|
+
"@types/react": "~17.0.9",
|
|
46
|
+
"react-use": "^17.2.4"
|
|
47
|
+
},
|
|
48
|
+
"gitHead": "21c6b42ae6550e7899c57bf39fa78cea0f47e64c"
|
|
49
|
+
}
|