UNPKG

@giancosta86/omnicourse

Version:

Interactive, React-based drill-down analysis of your learning experience

129 lines 6.48 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LearningContextProvider = exports.useLearningContext = void 0; const react_1 = __importStar(require("react")); const omnicourse_core_1 = require("@giancosta86/omnicourse-core"); const learningContext = (0, react_1.createContext)({ taxonomyKeys: undefined, taxonomyPath: undefined, currentLevel: undefined, selectedTaxonomyKey: undefined, onMobile: false, setSelectedTaxonomyId: () => void null, pushToTaxonomyPath: () => void null, revertTaxonomyPathTo: () => void null }); const useLearningContext = () => (0, react_1.useContext)(learningContext); exports.useLearningContext = useLearningContext; const LearningContextProvider = ({ onMobile, taxonomyKeysFetcher, taxonomyRepository, children }) => { const [taxonomyKeys, setTaxonomyKeys] = (0, react_1.useState)(undefined); const taxonomyKeysById = (0, react_1.useMemo)(() => new Map(taxonomyKeys === null || taxonomyKeys === void 0 ? void 0 : taxonomyKeys.map(taxonomyKey => [taxonomyKey.id, taxonomyKey])), [taxonomyKeys]); const [selectedTaxonomyKey, setSelectedTaxonomyKey] = (0, react_1.useState)(undefined); const [taxonomyPath, setTaxonomyPath] = (0, react_1.useState)(undefined); (0, react_1.useEffect)(() => { let canProcessData = true; (() => __awaiter(void 0, void 0, void 0, function* () { try { const fetchedTaxonomyKeys = yield taxonomyKeysFetcher(); if (!canProcessData) { return; } (0, omnicourse_core_1.validateTaxonomyKeys)(fetchedTaxonomyKeys); const firstTaxonomyKey = fetchedTaxonomyKeys[0]; setTaxonomyKeys(fetchedTaxonomyKeys); setSelectedTaxonomyKey(firstTaxonomyKey); } catch (err) { alert(`Cannot load the taxonomy keys. ${(0, omnicourse_core_1.formatError)(err)}`); } }))(); return () => { canProcessData = false; }; }, [taxonomyKeysFetcher]); const taxonomyPathReference = (0, react_1.useRef)(); taxonomyPathReference.current = taxonomyPath; (0, react_1.useEffect)(() => { if (!selectedTaxonomyKey) { return; } let canProcessData = true; const oldTaxonomyPath = taxonomyPathReference.current; setTaxonomyPath(undefined); (() => __awaiter(void 0, void 0, void 0, function* () { var _a; try { const fetchedTaxonomy = yield taxonomyRepository.getByKey(selectedTaxonomyKey); if (!canProcessData) { return; } const newTaxonomyPath = ((_a = oldTaxonomyPath === null || oldTaxonomyPath === void 0 ? void 0 : oldTaxonomyPath.navigateTaxonomy(fetchedTaxonomy)) !== null && _a !== void 0 ? _a : omnicourse_core_1.TaxonomyPath.fromTaxonomy(fetchedTaxonomy)).toMeaningful(); setTaxonomyPath(newTaxonomyPath); } catch (err) { alert(`Cannot load taxonomy '${selectedTaxonomyKey.name}'. ${(0, omnicourse_core_1.formatError)(err)}`); } }))(); return () => { canProcessData = false; }; }, [taxonomyRepository, selectedTaxonomyKey]); const setSelectedTaxonomyId = (0, react_1.useCallback)((taxonomyId) => { const taxonomyKey = taxonomyKeysById === null || taxonomyKeysById === void 0 ? void 0 : taxonomyKeysById.get(taxonomyId); if (!taxonomyKey) { throw new Error(`Cannot find taxonomy key having id '${taxonomyId}'`); } setSelectedTaxonomyKey(taxonomyKey); }, [taxonomyKeysById]); const pushToTaxonomyPath = (0, react_1.useCallback)((subject) => { setTaxonomyPath(taxonomyPath === null || taxonomyPath === void 0 ? void 0 : taxonomyPath.push(subject).toMeaningful()); }, [taxonomyPath]); const revertTaxonomyPathTo = (0, react_1.useCallback)((taxonomyLevel) => { setTaxonomyPath(taxonomyPath === null || taxonomyPath === void 0 ? void 0 : taxonomyPath.revertTo(taxonomyLevel).toMeaningful()); }, [taxonomyPath]); const contextValue = { taxonomyKeys, taxonomyPath, currentLevel: taxonomyPath === null || taxonomyPath === void 0 ? void 0 : taxonomyPath.currentLevel, selectedTaxonomyKey, onMobile, setSelectedTaxonomyId, pushToTaxonomyPath, revertTaxonomyPathTo }; return (react_1.default.createElement(learningContext.Provider, { value: contextValue }, children)); }; exports.LearningContextProvider = LearningContextProvider; //# sourceMappingURL=LearningContext.js.map