UNPKG

@defikitdotnet/education-module-ai

Version:
305 lines (304 loc) 14.4 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useCourseContent = void 0; var react_1 = require("react"); var useCourseContent = function (agentId, apiUrl) { if (apiUrl === void 0) { apiUrl = "/api"; } var _a = (0, react_1.useState)(false), isLoading = _a[0], setIsLoading = _a[1]; var _b = (0, react_1.useState)(null), error = _b[0], setError = _b[1]; var _c = (0, react_1.useState)(null), content = _c[0], setContent = _c[1]; var getHeaders = function () { return { "Content-Type": "application/json", "X-Agent-Id": agentId, }; }; var fetchCourseContents = function (courseId) { return __awaiter(void 0, void 0, void 0, function () { var response, data, err_1, errorMessage; return __generator(this, function (_a) { switch (_a.label) { case 0: setIsLoading(true); setError(null); _a.label = 1; case 1: _a.trys.push([1, 4, 5, 6]); return [4 /*yield*/, fetch("".concat(process.env.NEXT_PUBLIC_BACKEND_URL, "/api/courses/").concat(courseId, "/contents"), { method: "GET", headers: getHeaders(), })]; case 2: response = _a.sent(); if (!response.ok) { throw new Error("Error fetching course contents: ".concat(response.statusText)); } return [4 /*yield*/, response.json()]; case 3: data = _a.sent(); return [2 /*return*/, data.contents || []]; case 4: err_1 = _a.sent(); errorMessage = err_1 instanceof Error ? err_1.message : "Failed to fetch course contents"; setError(errorMessage); return [2 /*return*/, []]; case 5: setIsLoading(false); return [7 /*endfinally*/]; case 6: return [2 /*return*/]; } }); }); }; var fetchContentById = function (courseId, contentId) { return __awaiter(void 0, void 0, void 0, function () { var response, err_2, errorMessage; return __generator(this, function (_a) { switch (_a.label) { case 0: setIsLoading(true); setError(null); _a.label = 1; case 1: _a.trys.push([1, 4, 5, 6]); return [4 /*yield*/, fetch("".concat(process.env.NEXT_PUBLIC_BACKEND_URL, "/api/courses/").concat(courseId, "/contents/").concat(contentId), { method: "GET", headers: getHeaders(), })]; case 2: response = _a.sent(); if (!response.ok) { throw new Error("Error fetching content: ".concat(response.statusText)); } return [4 /*yield*/, response.json()]; case 3: return [2 /*return*/, _a.sent()]; case 4: err_2 = _a.sent(); errorMessage = err_2 instanceof Error ? err_2.message : "Failed to fetch content"; setError(errorMessage); return [2 /*return*/, null]; case 5: setIsLoading(false); return [7 /*endfinally*/]; case 6: return [2 /*return*/]; } }); }); }; var fetchContent = function (courseId, contentId) { return __awaiter(void 0, void 0, void 0, function () { var contentData, err_3, errorMessage; return __generator(this, function (_a) { switch (_a.label) { case 0: setIsLoading(true); setError(null); _a.label = 1; case 1: _a.trys.push([1, 3, 4, 5]); return [4 /*yield*/, fetchContentById(courseId, contentId)]; case 2: contentData = _a.sent(); setContent(contentData); return [3 /*break*/, 5]; case 3: err_3 = _a.sent(); errorMessage = err_3 instanceof Error ? err_3.message : "Failed to fetch content"; setError(errorMessage); return [3 /*break*/, 5]; case 4: setIsLoading(false); return [7 /*endfinally*/]; case 5: return [2 /*return*/]; } }); }); }; var createContent = function (courseId, content) { return __awaiter(void 0, void 0, void 0, function () { var response, err_4, errorMessage; return __generator(this, function (_a) { switch (_a.label) { case 0: setIsLoading(true); setError(null); _a.label = 1; case 1: _a.trys.push([1, 4, 5, 6]); return [4 /*yield*/, fetch("".concat(process.env.NEXT_PUBLIC_BACKEND_URL, "/api/courses/").concat(courseId, "/contents"), { method: "POST", headers: getHeaders(), body: JSON.stringify(content), })]; case 2: response = _a.sent(); if (!response.ok) { throw new Error("Error creating content: ".concat(response.statusText)); } return [4 /*yield*/, response.json()]; case 3: return [2 /*return*/, _a.sent()]; case 4: err_4 = _a.sent(); errorMessage = err_4 instanceof Error ? err_4.message : "Failed to create content"; setError(errorMessage); throw err_4; case 5: setIsLoading(false); return [7 /*endfinally*/]; case 6: return [2 /*return*/]; } }); }); }; var updateContent = function (courseId, contentId, content) { return __awaiter(void 0, void 0, void 0, function () { var response, err_5, errorMessage; return __generator(this, function (_a) { switch (_a.label) { case 0: setIsLoading(true); setError(null); _a.label = 1; case 1: _a.trys.push([1, 4, 5, 6]); return [4 /*yield*/, fetch("".concat(apiUrl, "/courses/").concat(courseId, "/contents/").concat(contentId), { method: "PUT", headers: getHeaders(), body: JSON.stringify(__assign(__assign({}, content), { updatedAt: new Date().toISOString() })), })]; case 2: response = _a.sent(); if (!response.ok) { throw new Error("Error updating content: ".concat(response.statusText)); } return [4 /*yield*/, response.json()]; case 3: return [2 /*return*/, _a.sent()]; case 4: err_5 = _a.sent(); errorMessage = err_5 instanceof Error ? err_5.message : "Failed to update content"; setError(errorMessage); throw err_5; case 5: setIsLoading(false); return [7 /*endfinally*/]; case 6: return [2 /*return*/]; } }); }); }; var deleteContent = function (courseId, contentId) { return __awaiter(void 0, void 0, void 0, function () { var response, err_6, errorMessage; return __generator(this, function (_a) { switch (_a.label) { case 0: setIsLoading(true); setError(null); _a.label = 1; case 1: _a.trys.push([1, 3, 4, 5]); return [4 /*yield*/, fetch("".concat(apiUrl, "/courses/").concat(courseId, "/contents/").concat(contentId), { method: "DELETE", headers: getHeaders(), })]; case 2: response = _a.sent(); if (!response.ok) { throw new Error("Error deleting content: ".concat(response.statusText)); } return [2 /*return*/, true]; case 3: err_6 = _a.sent(); errorMessage = err_6 instanceof Error ? err_6.message : "Failed to delete content"; setError(errorMessage); throw err_6; case 4: setIsLoading(false); return [7 /*endfinally*/]; case 5: return [2 /*return*/]; } }); }); }; var reorderContents = function (courseId, contentOrders) { return __awaiter(void 0, void 0, void 0, function () { var response, err_7, errorMessage; return __generator(this, function (_a) { switch (_a.label) { case 0: setIsLoading(true); setError(null); _a.label = 1; case 1: _a.trys.push([1, 3, 4, 5]); return [4 /*yield*/, fetch("".concat(apiUrl, "/courses/").concat(courseId, "/contents/reorder"), { method: "PUT", headers: getHeaders(), body: JSON.stringify({ contentOrders: contentOrders }), })]; case 2: response = _a.sent(); if (!response.ok) { throw new Error("Error reordering contents: ".concat(response.statusText)); } return [2 /*return*/, true]; case 3: err_7 = _a.sent(); errorMessage = err_7 instanceof Error ? err_7.message : "Failed to reorder contents"; setError(errorMessage); throw err_7; case 4: setIsLoading(false); return [7 /*endfinally*/]; case 5: return [2 /*return*/]; } }); }); }; return { content: content, isLoading: isLoading, error: error, fetchContent: fetchContent, fetchCourseContents: fetchCourseContents, fetchContentById: fetchContentById, createContent: createContent, updateContent: updateContent, deleteContent: deleteContent, reorderContents: reorderContents, }; }; exports.useCourseContent = useCourseContent;