@elice/material-shared-utils 1.230217.0-sepmatlec.3 → 1.230220.1

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/cjs/index.js CHANGED
@@ -9,7 +9,7 @@ var useMaterialConfig = require('./material-config/useMaterialConfig.js');
9
9
  var useMaterialConfigApiClientUpdate = require('./material-config/useMaterialConfigApiClientUpdate.js');
10
10
  var utils = require('./material-config/utils.js');
11
11
  var useMaterialFetch = require('./material-fetch/useMaterialFetch.js');
12
- var useMaterialFetchRaw = require('./material-fetch/useMaterialFetchRaw.js');
12
+ var useMaterialInitFetch = require('./material-fetch/useMaterialInitFetch.js');
13
13
  var withMaterial = require('./material-hoc/withMaterial.js');
14
14
 
15
15
 
@@ -21,6 +21,6 @@ exports.useMaterialConfig = useMaterialConfig.useMaterialConfig;
21
21
  exports.useMaterialConfigApiClientUpdate = useMaterialConfigApiClientUpdate.useMaterialConfigApiClientUpdate;
22
22
  exports.updateMaterialConfigSessionKey = utils.updateMaterialConfigSessionKey;
23
23
  exports.useMaterialFetch = useMaterialFetch.useMaterialFetch;
24
- exports.useMaterialFetchRaw = useMaterialFetchRaw.useMaterialFetchRaw;
24
+ exports.useMaterialInitFetch = useMaterialInitFetch.useMaterialInitFetch;
25
25
  exports.withForwardRefMaterial = withMaterial.withForwardRefMaterial;
26
26
  exports.withMaterial = withMaterial.withMaterial;
@@ -1,2 +1,2 @@
1
1
  export * from './useMaterialFetch';
2
- export * from './useMaterialFetchRaw';
2
+ export * from './useMaterialInitFetch';
@@ -1,71 +1,21 @@
1
- import type { EliceApiClientConfigs, EliceApiResponse } from '@elice/api-client';
2
- import type { enums } from '@elice/types';
3
- import type { FetchKeyMap } from './useMaterialFetchRaw';
4
- declare type FetchClientInitializer = (config: EliceApiClientConfigs) => void;
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>>;
5
4
  /**
6
5
  * Init api client and fetch material for contaner.
7
6
  *
7
+ * @param fn API client wrapped with `React.useCallback` to fetch material.
8
+ * @param isReady Is ready to fetch material.
9
+ *
8
10
  * @example
9
- * const { materialPdf, materialLecturePage, status ] =
10
- * useMaterialFetch(
11
- * enums.LectureMaterialType.PDF,
11
+ * const [materialPdf, materialPdfStatus] =
12
+ * useMaterialFetch<GetOrgMaterialPdfGetResponses>(
12
13
  * React.useCallback(
13
- * signal =>
14
- * getOrgMaterialPdfGet<GetOrgMaterialPdfGetResponses>(
15
- * { materialPdfId },
16
- * { signal }
17
- * ),
14
+ * signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
18
15
  * [materialPdfId]
19
16
  * ),
20
- * eliceApiClientConfigs.init
17
+ * isReady // boolean
21
18
  * );
22
19
  */
23
- export declare const useMaterialFetch: <MT extends enums.LectureMaterialType, R extends Record<FetchKeyMap[MT], Record<string, any>> = Record<FetchKeyMap[MT], Record<string, any>>>(materialType: MT, materialFetchFn: (signal?: AbortSignal | undefined) => Promise<EliceApiResponse<R>>, init: FetchClientInitializer) => Record<FetchKeyMap[MT], R[FetchKeyMap[MT]] | null> & {
24
- materialLecturePage: {
25
- certificateDescription: string | null;
26
- certificateFileUrl: string | null;
27
- certificateTitle: string | null;
28
- closeDatetime: number | null;
29
- closeScheduleDatetime: number | null;
30
- courseId: number;
31
- createdDatetime: number;
32
- description: string;
33
- id: number;
34
- isCertificateEnabled: boolean;
35
- isCompleted: boolean;
36
- isEnrolledStudentResponded: boolean;
37
- isForStats: boolean;
38
- isGraded: boolean;
39
- isLibraryInstance: boolean;
40
- isOpened: boolean;
41
- isResponded: boolean;
42
- lectureGroupType: enums.LectureGroupType | null;
43
- lectureId: number;
44
- mainOrderNo: number;
45
- materialId: number;
46
- materialKey: string;
47
- materialType: enums.LectureMaterialType;
48
- modifiedDatetime: number;
49
- openScheduleDatetime: number | null;
50
- originCourse: {
51
- id: number;
52
- title: string;
53
- organization: {
54
- id: number;
55
- name: string;
56
- nameShort: string;
57
- };
58
- } | null;
59
- originLecturePageId: number | null;
60
- point: number;
61
- score: number;
62
- subLecturePageIds: number[];
63
- subOrderNo: number | null;
64
- taglist: string[];
65
- title: string;
66
- } | null;
67
- } & {
68
- status: import("@elice/material-shared-types").APIStatus;
69
- refetch: (signal?: AbortSignal | undefined) => Promise<void>;
70
- };
20
+ export declare const useMaterialFetch: <R = any>(fn: FetchFn<R>, isReady?: boolean) => [R | null, APIStatus];
71
21
  export {};
@@ -2,36 +2,50 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- require('react');
6
- require('../material-config/MaterialConfigContext.js');
7
- var useMaterialConfigApiClientUpdate = require('../material-config/useMaterialConfigApiClientUpdate.js');
8
- var useMaterialFetchRaw = require('./useMaterialFetchRaw.js');
9
-
10
- //
11
- //
5
+ var React = require('react');
12
6
 
13
7
  /**
14
8
  * Init api client and fetch material for contaner.
15
9
  *
10
+ * @param fn API client wrapped with `React.useCallback` to fetch material.
11
+ * @param isReady Is ready to fetch material.
12
+ *
16
13
  * @example
17
- * const { materialPdf, materialLecturePage, status ] =
18
- * useMaterialFetch(
19
- * enums.LectureMaterialType.PDF,
14
+ * const [materialPdf, materialPdfStatus] =
15
+ * useMaterialFetch<GetOrgMaterialPdfGetResponses>(
20
16
  * React.useCallback(
21
- * signal =>
22
- * getOrgMaterialPdfGet<GetOrgMaterialPdfGetResponses>(
23
- * { materialPdfId },
24
- * { signal }
25
- * ),
17
+ * signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
26
18
  * [materialPdfId]
27
19
  * ),
28
- * eliceApiClientConfigs.init
20
+ * isReady // boolean
29
21
  * );
30
22
  */
31
23
 
32
- const useMaterialFetch = (materialType, materialFetchFn, init) => {
33
- const isReady = useMaterialConfigApiClientUpdate.useMaterialConfigApiClientUpdate(init);
34
- return useMaterialFetchRaw.useMaterialFetchRaw(materialType, materialFetchFn, isReady);
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];
35
49
  };
36
50
 
37
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;
package/es/index.js CHANGED
@@ -5,5 +5,5 @@ export { useMaterialConfig } from './material-config/useMaterialConfig.js';
5
5
  export { useMaterialConfigApiClientUpdate } from './material-config/useMaterialConfigApiClientUpdate.js';
6
6
  export { updateMaterialConfigSessionKey } from './material-config/utils.js';
7
7
  export { useMaterialFetch } from './material-fetch/useMaterialFetch.js';
8
- export { useMaterialFetchRaw } from './material-fetch/useMaterialFetchRaw.js';
8
+ export { useMaterialInitFetch } from './material-fetch/useMaterialInitFetch.js';
9
9
  export { withForwardRefMaterial, withMaterial } from './material-hoc/withMaterial.js';
@@ -1,2 +1,2 @@
1
1
  export * from './useMaterialFetch';
2
- export * from './useMaterialFetchRaw';
2
+ export * from './useMaterialInitFetch';
@@ -1,71 +1,21 @@
1
- import type { EliceApiClientConfigs, EliceApiResponse } from '@elice/api-client';
2
- import type { enums } from '@elice/types';
3
- import type { FetchKeyMap } from './useMaterialFetchRaw';
4
- declare type FetchClientInitializer = (config: EliceApiClientConfigs) => void;
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>>;
5
4
  /**
6
5
  * Init api client and fetch material for contaner.
7
6
  *
7
+ * @param fn API client wrapped with `React.useCallback` to fetch material.
8
+ * @param isReady Is ready to fetch material.
9
+ *
8
10
  * @example
9
- * const { materialPdf, materialLecturePage, status ] =
10
- * useMaterialFetch(
11
- * enums.LectureMaterialType.PDF,
11
+ * const [materialPdf, materialPdfStatus] =
12
+ * useMaterialFetch<GetOrgMaterialPdfGetResponses>(
12
13
  * React.useCallback(
13
- * signal =>
14
- * getOrgMaterialPdfGet<GetOrgMaterialPdfGetResponses>(
15
- * { materialPdfId },
16
- * { signal }
17
- * ),
14
+ * signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
18
15
  * [materialPdfId]
19
16
  * ),
20
- * eliceApiClientConfigs.init
17
+ * isReady // boolean
21
18
  * );
22
19
  */
23
- export declare const useMaterialFetch: <MT extends enums.LectureMaterialType, R extends Record<FetchKeyMap[MT], Record<string, any>> = Record<FetchKeyMap[MT], Record<string, any>>>(materialType: MT, materialFetchFn: (signal?: AbortSignal | undefined) => Promise<EliceApiResponse<R>>, init: FetchClientInitializer) => Record<FetchKeyMap[MT], R[FetchKeyMap[MT]] | null> & {
24
- materialLecturePage: {
25
- certificateDescription: string | null;
26
- certificateFileUrl: string | null;
27
- certificateTitle: string | null;
28
- closeDatetime: number | null;
29
- closeScheduleDatetime: number | null;
30
- courseId: number;
31
- createdDatetime: number;
32
- description: string;
33
- id: number;
34
- isCertificateEnabled: boolean;
35
- isCompleted: boolean;
36
- isEnrolledStudentResponded: boolean;
37
- isForStats: boolean;
38
- isGraded: boolean;
39
- isLibraryInstance: boolean;
40
- isOpened: boolean;
41
- isResponded: boolean;
42
- lectureGroupType: enums.LectureGroupType | null;
43
- lectureId: number;
44
- mainOrderNo: number;
45
- materialId: number;
46
- materialKey: string;
47
- materialType: enums.LectureMaterialType;
48
- modifiedDatetime: number;
49
- openScheduleDatetime: number | null;
50
- originCourse: {
51
- id: number;
52
- title: string;
53
- organization: {
54
- id: number;
55
- name: string;
56
- nameShort: string;
57
- };
58
- } | null;
59
- originLecturePageId: number | null;
60
- point: number;
61
- score: number;
62
- subLecturePageIds: number[];
63
- subOrderNo: number | null;
64
- taglist: string[];
65
- title: string;
66
- } | null;
67
- } & {
68
- status: import("@elice/material-shared-types").APIStatus;
69
- refetch: (signal?: AbortSignal | undefined) => Promise<void>;
70
- };
20
+ export declare const useMaterialFetch: <R = any>(fn: FetchFn<R>, isReady?: boolean) => [R | null, APIStatus];
71
21
  export {};
@@ -1,33 +1,47 @@
1
- import 'react';
2
- import '../material-config/MaterialConfigContext.js';
3
- import { useMaterialConfigApiClientUpdate } from '../material-config/useMaterialConfigApiClientUpdate.js';
4
- import { useMaterialFetchRaw } from './useMaterialFetchRaw.js';
5
-
6
- //
7
- //
1
+ import { useState, useEffect } from 'react';
8
2
 
9
3
  /**
10
4
  * Init api client and fetch material for contaner.
11
5
  *
6
+ * @param fn API client wrapped with `React.useCallback` to fetch material.
7
+ * @param isReady Is ready to fetch material.
8
+ *
12
9
  * @example
13
- * const { materialPdf, materialLecturePage, status ] =
14
- * useMaterialFetch(
15
- * enums.LectureMaterialType.PDF,
10
+ * const [materialPdf, materialPdfStatus] =
11
+ * useMaterialFetch<GetOrgMaterialPdfGetResponses>(
16
12
  * React.useCallback(
17
- * signal =>
18
- * getOrgMaterialPdfGet<GetOrgMaterialPdfGetResponses>(
19
- * { materialPdfId },
20
- * { signal }
21
- * ),
13
+ * signal => getOrgMaterialPdfGet({ materialPdfId }, { signal }),
22
14
  * [materialPdfId]
23
15
  * ),
24
- * eliceApiClientConfigs.init
16
+ * isReady // boolean
25
17
  * );
26
18
  */
27
19
 
28
- const useMaterialFetch = (materialType, materialFetchFn, init) => {
29
- const isReady = useMaterialConfigApiClientUpdate(init);
30
- return useMaterialFetchRaw(materialType, materialFetchFn, isReady);
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];
31
45
  };
32
46
 
33
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elice/material-shared-utils",
3
- "version": "1.230217.0-sepmatlec.3",
3
+ "version": "1.230220.1",
4
4
  "description": "Shared typings for elice material projects",
5
5
  "repository": "https://git.elicer.io/elice/frontend/library/elice-material",
6
6
  "license": "UNLICENSED",
@@ -29,7 +29,7 @@
29
29
  "clean": "del-cli \"es/*\" \"cjs/*\" \"dist/*\""
30
30
  },
31
31
  "peerDependencies": {
32
- "@elice/api-client": "1.230126.0-sepmatlec.4",
32
+ "@elice/api-client": "1.230215.1",
33
33
  "@elice/material-shared-types": "22.7.2-1",
34
34
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
35
35
  "react-use": "^17.0.0",
@@ -39,11 +39,11 @@
39
39
  "tslib": "^2.3.1"
40
40
  },
41
41
  "devDependencies": {
42
- "@elice/api-client": "1.230126.0-sepmatlec.4",
43
- "@elice/material-shared-types": "1.230217.0-sepmatlec.3",
44
- "@elice/types": "1.230126.0-sepmatlec.5",
42
+ "@elice/api-client": "1.230215.1",
43
+ "@elice/material-shared-types": "1.230220.1",
44
+ "@elice/types": "1.230215.1",
45
45
  "@types/react": "~17.0.9",
46
46
  "react-use": "^17.2.4"
47
47
  },
48
- "gitHead": "9256c4bde755c55a27550e7036c56bdae5104f50"
48
+ "gitHead": "f8c978d25b5efb10d7c75a2023e9a32ca864754b"
49
49
  }
@@ -1,43 +0,0 @@
1
- import { enums } from '@elice/types';
2
- import type { EliceApiResponse } from '@elice/api-client';
3
- import type { APIStatus } from '@elice/material-shared-types';
4
- import type { GetOrgLecturePageGetResponses } from '@elice/types';
5
- export interface FetchKeyMap {
6
- [enums.LectureMaterialType.Nothing]: 'materialNothing';
7
- [enums.LectureMaterialType.Note]: 'materialNote';
8
- [enums.LectureMaterialType.Video]: 'materialVideo';
9
- [enums.LectureMaterialType.PDF]: 'materialPdf';
10
- [enums.LectureMaterialType.Exercise]: 'materialExercise';
11
- [enums.LectureMaterialType.Quiz]: 'materialQuiz';
12
- [enums.LectureMaterialType.Runbox]: 'materialRunbox';
13
- [enums.LectureMaterialType.VideoFile]: 'materialVideoFile';
14
- [enums.LectureMaterialType.Live]: 'materialLive';
15
- [enums.LectureMaterialType.Assignment]: 'materialAssignment';
16
- [enums.LectureMaterialType.LMSPublicContent]: 'materialLmsPublicContent';
17
- [enums.LectureMaterialType.External]: 'materialExternal';
18
- [enums.LectureMaterialType.Survey]: 'materialSurvey';
19
- }
20
- /**
21
- * Init api client and fetch material for contaner.
22
- *
23
- * @example
24
- * const { materialPdf, materialLecturePage, status ] =
25
- * useMaterialFetchRaw(
26
- * enums.LectureMaterialType.PDF,
27
- * React.useCallback(
28
- * signal =>
29
- * getOrgMaterialPdfGet<GetOrgMaterialPdfGetResponses>(
30
- * { materialPdfId },
31
- * { signal }
32
- * ),
33
- * [materialPdfId]
34
- * ),
35
- * isReady // boolean
36
- * );
37
- */
38
- export declare const useMaterialFetchRaw: <MT extends enums.LectureMaterialType, R extends Record<FetchKeyMap[MT], Record<string, any>> = Record<FetchKeyMap[MT], Record<string, any>>>(materialType: MT, materialFetchFn: (signal?: AbortSignal | undefined) => Promise<EliceApiResponse<R>>, isReady?: boolean) => Record<FetchKeyMap[MT], R[FetchKeyMap[MT]] | null> & {
39
- materialLecturePage: GetOrgLecturePageGetResponses['lecturePage'] | null;
40
- } & {
41
- status: APIStatus;
42
- refetch: (signal?: AbortSignal | undefined) => Promise<void>;
43
- };
@@ -1,112 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var React = require('react');
6
- var apiClient = require('@elice/api-client');
7
- var types = require('@elice/types');
8
-
9
- const MATERIAL_FETCH_KEY_MAP = Object.freeze({
10
- [types.enums.LectureMaterialType.Nothing]: 'materialNothing',
11
- [types.enums.LectureMaterialType.Note]: 'materialNote',
12
- [types.enums.LectureMaterialType.Video]: 'materialVideo',
13
- [types.enums.LectureMaterialType.PDF]: 'materialPdf',
14
- [types.enums.LectureMaterialType.Exercise]: 'materialExercise',
15
- [types.enums.LectureMaterialType.Quiz]: 'materialQuiz',
16
- [types.enums.LectureMaterialType.Runbox]: 'materialRunbox',
17
- [types.enums.LectureMaterialType.VideoFile]: 'materialVideoFile',
18
- [types.enums.LectureMaterialType.Live]: 'materialLive',
19
- [types.enums.LectureMaterialType.Assignment]: 'materialAssignment',
20
- [types.enums.LectureMaterialType.LMSPublicContent]: 'materialLmsPublicContent',
21
- [types.enums.LectureMaterialType.External]: 'materialExternal',
22
- [types.enums.LectureMaterialType.Survey]: 'materialSurvey'
23
- }); //
24
- //
25
- //
26
-
27
- /**
28
- * Init api client and fetch material for contaner.
29
- *
30
- * @example
31
- * const { materialPdf, materialLecturePage, status ] =
32
- * useMaterialFetchRaw(
33
- * enums.LectureMaterialType.PDF,
34
- * React.useCallback(
35
- * signal =>
36
- * getOrgMaterialPdfGet<GetOrgMaterialPdfGetResponses>(
37
- * { materialPdfId },
38
- * { signal }
39
- * ),
40
- * [materialPdfId]
41
- * ),
42
- * isReady // boolean
43
- * );
44
- */
45
-
46
- const useMaterialFetchRaw = (materialType, materialFetchFn, isReady = true) => {
47
- const _return = React.useRef({});
48
-
49
- const materialKey = MATERIAL_FETCH_KEY_MAP[materialType];
50
-
51
- const _createEmptyFetchRes = () => ({
52
- [materialKey]: null,
53
- materialLecturePage: null
54
- });
55
-
56
- const [matFetchRes, setMatFetchRes] = React.useState(_createEmptyFetchRes);
57
- const [matFetchStatus, setMatFetchStatus] = React.useState('idle');
58
- /**
59
- * fetch material and lecture page
60
- */
61
-
62
- const fetch = React.useCallback(async signal => {
63
- setMatFetchStatus('pending');
64
- return materialFetchFn(signal).then(resMaterialGet => resMaterialGet[materialKey]).then(material => Promise.all([material, apiClient.getOrgLecturePageResolve({
65
- materialId: material.id,
66
- materialType
67
- }, {
68
- signal: signal
69
- }).then(resLecturePageResolve => apiClient.getOrgLecturePageGet({
70
- lecturePageId: resLecturePageResolve.lecturePageId
71
- }, {
72
- signal: signal
73
- })).then(resLecturePageGet => resLecturePageGet.lecturePage)])).then(([material, materialLecturePage]) => {
74
- setMatFetchRes({
75
- [materialKey]: material,
76
- materialLecturePage
77
- });
78
- setMatFetchStatus('resolved');
79
- }).catch(err => {
80
- if (err.name !== 'AbortError') {
81
- setMatFetchRes(_createEmptyFetchRes);
82
- setMatFetchStatus('rejected');
83
- }
84
- });
85
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
86
- [materialKey, materialFetchFn]); //
87
- // emit `fetch` when `isReady` is changed
88
- //
89
-
90
- React.useEffect(() => {
91
- if (!isReady) {
92
- return;
93
- }
94
-
95
- const abortCtrl = new AbortController();
96
- fetch(abortCtrl.signal).catch(() => void 0);
97
- return () => {
98
- abortCtrl.abort();
99
- };
100
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
101
- [isReady, fetch]); //
102
- //
103
- //
104
-
105
- _return.current[materialKey] = matFetchRes[materialKey];
106
- _return.current.materialLecturePage = matFetchRes.materialLecturePage;
107
- _return.current.status = matFetchStatus;
108
- _return.current.refetch = fetch;
109
- return _return.current;
110
- };
111
-
112
- exports.useMaterialFetchRaw = useMaterialFetchRaw;
@@ -1,43 +0,0 @@
1
- import { enums } from '@elice/types';
2
- import type { EliceApiResponse } from '@elice/api-client';
3
- import type { APIStatus } from '@elice/material-shared-types';
4
- import type { GetOrgLecturePageGetResponses } from '@elice/types';
5
- export interface FetchKeyMap {
6
- [enums.LectureMaterialType.Nothing]: 'materialNothing';
7
- [enums.LectureMaterialType.Note]: 'materialNote';
8
- [enums.LectureMaterialType.Video]: 'materialVideo';
9
- [enums.LectureMaterialType.PDF]: 'materialPdf';
10
- [enums.LectureMaterialType.Exercise]: 'materialExercise';
11
- [enums.LectureMaterialType.Quiz]: 'materialQuiz';
12
- [enums.LectureMaterialType.Runbox]: 'materialRunbox';
13
- [enums.LectureMaterialType.VideoFile]: 'materialVideoFile';
14
- [enums.LectureMaterialType.Live]: 'materialLive';
15
- [enums.LectureMaterialType.Assignment]: 'materialAssignment';
16
- [enums.LectureMaterialType.LMSPublicContent]: 'materialLmsPublicContent';
17
- [enums.LectureMaterialType.External]: 'materialExternal';
18
- [enums.LectureMaterialType.Survey]: 'materialSurvey';
19
- }
20
- /**
21
- * Init api client and fetch material for contaner.
22
- *
23
- * @example
24
- * const { materialPdf, materialLecturePage, status ] =
25
- * useMaterialFetchRaw(
26
- * enums.LectureMaterialType.PDF,
27
- * React.useCallback(
28
- * signal =>
29
- * getOrgMaterialPdfGet<GetOrgMaterialPdfGetResponses>(
30
- * { materialPdfId },
31
- * { signal }
32
- * ),
33
- * [materialPdfId]
34
- * ),
35
- * isReady // boolean
36
- * );
37
- */
38
- export declare const useMaterialFetchRaw: <MT extends enums.LectureMaterialType, R extends Record<FetchKeyMap[MT], Record<string, any>> = Record<FetchKeyMap[MT], Record<string, any>>>(materialType: MT, materialFetchFn: (signal?: AbortSignal | undefined) => Promise<EliceApiResponse<R>>, isReady?: boolean) => Record<FetchKeyMap[MT], R[FetchKeyMap[MT]] | null> & {
39
- materialLecturePage: GetOrgLecturePageGetResponses['lecturePage'] | null;
40
- } & {
41
- status: APIStatus;
42
- refetch: (signal?: AbortSignal | undefined) => Promise<void>;
43
- };
@@ -1,108 +0,0 @@
1
- import { useRef, useState, useCallback, useEffect } from 'react';
2
- import { getOrgLecturePageResolve, getOrgLecturePageGet } from '@elice/api-client';
3
- import { enums } from '@elice/types';
4
-
5
- const MATERIAL_FETCH_KEY_MAP = Object.freeze({
6
- [enums.LectureMaterialType.Nothing]: 'materialNothing',
7
- [enums.LectureMaterialType.Note]: 'materialNote',
8
- [enums.LectureMaterialType.Video]: 'materialVideo',
9
- [enums.LectureMaterialType.PDF]: 'materialPdf',
10
- [enums.LectureMaterialType.Exercise]: 'materialExercise',
11
- [enums.LectureMaterialType.Quiz]: 'materialQuiz',
12
- [enums.LectureMaterialType.Runbox]: 'materialRunbox',
13
- [enums.LectureMaterialType.VideoFile]: 'materialVideoFile',
14
- [enums.LectureMaterialType.Live]: 'materialLive',
15
- [enums.LectureMaterialType.Assignment]: 'materialAssignment',
16
- [enums.LectureMaterialType.LMSPublicContent]: 'materialLmsPublicContent',
17
- [enums.LectureMaterialType.External]: 'materialExternal',
18
- [enums.LectureMaterialType.Survey]: 'materialSurvey'
19
- }); //
20
- //
21
- //
22
-
23
- /**
24
- * Init api client and fetch material for contaner.
25
- *
26
- * @example
27
- * const { materialPdf, materialLecturePage, status ] =
28
- * useMaterialFetchRaw(
29
- * enums.LectureMaterialType.PDF,
30
- * React.useCallback(
31
- * signal =>
32
- * getOrgMaterialPdfGet<GetOrgMaterialPdfGetResponses>(
33
- * { materialPdfId },
34
- * { signal }
35
- * ),
36
- * [materialPdfId]
37
- * ),
38
- * isReady // boolean
39
- * );
40
- */
41
-
42
- const useMaterialFetchRaw = (materialType, materialFetchFn, isReady = true) => {
43
- const _return = useRef({});
44
-
45
- const materialKey = MATERIAL_FETCH_KEY_MAP[materialType];
46
-
47
- const _createEmptyFetchRes = () => ({
48
- [materialKey]: null,
49
- materialLecturePage: null
50
- });
51
-
52
- const [matFetchRes, setMatFetchRes] = useState(_createEmptyFetchRes);
53
- const [matFetchStatus, setMatFetchStatus] = useState('idle');
54
- /**
55
- * fetch material and lecture page
56
- */
57
-
58
- const fetch = useCallback(async signal => {
59
- setMatFetchStatus('pending');
60
- return materialFetchFn(signal).then(resMaterialGet => resMaterialGet[materialKey]).then(material => Promise.all([material, getOrgLecturePageResolve({
61
- materialId: material.id,
62
- materialType
63
- }, {
64
- signal: signal
65
- }).then(resLecturePageResolve => getOrgLecturePageGet({
66
- lecturePageId: resLecturePageResolve.lecturePageId
67
- }, {
68
- signal: signal
69
- })).then(resLecturePageGet => resLecturePageGet.lecturePage)])).then(([material, materialLecturePage]) => {
70
- setMatFetchRes({
71
- [materialKey]: material,
72
- materialLecturePage
73
- });
74
- setMatFetchStatus('resolved');
75
- }).catch(err => {
76
- if (err.name !== 'AbortError') {
77
- setMatFetchRes(_createEmptyFetchRes);
78
- setMatFetchStatus('rejected');
79
- }
80
- });
81
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
82
- [materialKey, materialFetchFn]); //
83
- // emit `fetch` when `isReady` is changed
84
- //
85
-
86
- useEffect(() => {
87
- if (!isReady) {
88
- return;
89
- }
90
-
91
- const abortCtrl = new AbortController();
92
- fetch(abortCtrl.signal).catch(() => void 0);
93
- return () => {
94
- abortCtrl.abort();
95
- };
96
- }, // eslint-disable-next-line react-hooks/exhaustive-deps
97
- [isReady, fetch]); //
98
- //
99
- //
100
-
101
- _return.current[materialKey] = matFetchRes[materialKey];
102
- _return.current.materialLecturePage = matFetchRes.materialLecturePage;
103
- _return.current.status = matFetchStatus;
104
- _return.current.refetch = fetch;
105
- return _return.current;
106
- };
107
-
108
- export { useMaterialFetchRaw };