From c84911564f6e452a44abfb57949357af5f7fcd41 Mon Sep 17 00:00:00 2001 From: Arif-tekdi-technologies Date: Tue, 15 Oct 2024 13:59:53 +0530 Subject: [PATCH 1/6] Issue # 2195 feat: Use admin app authorisation token in workspace --- src/pages/api/fileUpload.ts | 2 +- src/pages/api/proxy.ts | 2 +- src/utils/app.constant.ts | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/pages/api/fileUpload.ts b/src/pages/api/fileUpload.ts index 153c605..be0bf39 100644 --- a/src/pages/api/fileUpload.ts +++ b/src/pages/api/fileUpload.ts @@ -81,7 +81,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) // Set your base URL const baseURL = process.env.BASE_URL as string; - const authApiToken = process.env.AUTH_API_TOKEN as string; + const authApiToken = localStorage.getItem('token') || process.env.AUTH_API_TOKEN as string; const tenantId = process.env.TENANT_ID as string; // Extract the relative URL from the incoming request (after /action) diff --git a/src/pages/api/proxy.ts b/src/pages/api/proxy.ts index 16c59de..f0970e9 100644 --- a/src/pages/api/proxy.ts +++ b/src/pages/api/proxy.ts @@ -8,7 +8,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) let pathString = Array.isArray(path) ? path.join('/') : (path as string); const BASE_URL = process.env.BASE_URL as string; - const API_KEY = process.env.AUTH_API_TOKEN as string; + const API_KEY = localStorage.getItem('token') || process.env.AUTH_API_TOKEN as string; const TENANT_ID = process.env.TENANT_ID as string; if (pathString === '/action/data/v3/telemetry') { diff --git a/src/utils/app.constant.ts b/src/utils/app.constant.ts index 4c2fff0..22a728e 100644 --- a/src/utils/app.constant.ts +++ b/src/utils/app.constant.ts @@ -7,3 +7,7 @@ export enum Status { export enum ContentType { QUESTION_SET = "QuestionSet", } + +export enum TOKEN { + TOKEN = 'token' +} \ No newline at end of file From aafebb1a8f8dcd269313d1ed984252b670038c8e Mon Sep 17 00:00:00 2001 From: Arif-tekdi-technologies Date: Tue, 15 Oct 2024 14:07:19 +0530 Subject: [PATCH 2/6] fixes --- src/pages/api/fileUpload.ts | 3 ++- src/pages/api/proxy.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/pages/api/fileUpload.ts b/src/pages/api/fileUpload.ts index be0bf39..904227f 100644 --- a/src/pages/api/fileUpload.ts +++ b/src/pages/api/fileUpload.ts @@ -2,6 +2,7 @@ import multer, { MulterError } from 'multer'; import FormData from 'form-data'; import axios from 'axios'; import type { NextApiRequest, NextApiResponse } from 'next'; +import { TOKEN } from '@/utils/app.constant'; const upload = multer({ limits: { @@ -81,7 +82,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) // Set your base URL const baseURL = process.env.BASE_URL as string; - const authApiToken = localStorage.getItem('token') || process.env.AUTH_API_TOKEN as string; + const authApiToken = localStorage.getItem(TOKEN.TOKEN) || process.env.AUTH_API_TOKEN as string; const tenantId = process.env.TENANT_ID as string; // Extract the relative URL from the incoming request (after /action) diff --git a/src/pages/api/proxy.ts b/src/pages/api/proxy.ts index f0970e9..e3cdee5 100644 --- a/src/pages/api/proxy.ts +++ b/src/pages/api/proxy.ts @@ -1,6 +1,7 @@ import { NextApiRequest, NextApiResponse } from 'next'; import { genericEditorSaveFormResponse, telemetryResponse, creatLockResponse, genericEditorReviewFormResponse } from './mocked-response'; +import { TOKEN } from '@/utils/app.constant'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { const { method, query } = req; const { path } = query; @@ -8,7 +9,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) let pathString = Array.isArray(path) ? path.join('/') : (path as string); const BASE_URL = process.env.BASE_URL as string; - const API_KEY = localStorage.getItem('token') || process.env.AUTH_API_TOKEN as string; + const API_KEY = localStorage.getItem(TOKEN.TOKEN) || process.env.AUTH_API_TOKEN as string; const TENANT_ID = process.env.TENANT_ID as string; if (pathString === '/action/data/v3/telemetry') { From aa7d097091711cc159a99942bf2bdc791a93b636 Mon Sep 17 00:00:00 2001 From: Arif-tekdi-technologies Date: Tue, 15 Oct 2024 14:31:52 +0530 Subject: [PATCH 3/6] Dynamic script import --- src/components/QuestionSetEditor.tsx | 91 +++++++++++++++++----------- src/pages/_document.tsx | 8 --- 2 files changed, 56 insertions(+), 43 deletions(-) diff --git a/src/components/QuestionSetEditor.tsx b/src/components/QuestionSetEditor.tsx index d4b5014..c4ebbd1 100644 --- a/src/components/QuestionSetEditor.tsx +++ b/src/components/QuestionSetEditor.tsx @@ -1,10 +1,9 @@ -import React, { useEffect, useRef } from "react"; +import React, { useEffect, useRef, useState } from "react"; import { useRouter } from "next/router"; const QuestionSetEditor: React.FC = () => { const router = useRouter(); - const { identifier } = router.query; - const { mode } = router.query; + const { identifier, mode } = router.query; const questionSetEditorConfig = { context: { @@ -126,46 +125,68 @@ const QuestionSetEditor: React.FC = () => { const editorRef = useRef(null); const isAppendedRef = useRef(false); + const [assetsLoaded, setAssetsLoaded] = useState(false); useEffect(() => { - const fetchData = async () => { - try { - if (!isAppendedRef.current && editorRef.current) { - const editorConfig = questionSetEditorConfig; - const questionsetEditorElement = document.createElement( - "lib-questionset-editor" - ); - - questionsetEditorElement.setAttribute( - "editor-config", - JSON.stringify(editorConfig) - ); - - questionsetEditorElement.addEventListener( - "editorEmitter", - (event: CustomEvent) => { - console.log("On editorEvent", event); - if (event.detail?.action === "backContent") { - window.history.back(); - } - } - ); + const loadAssets = () => { + if (!document.getElementById("sunbird-editor-css")) { + const link = document.createElement("link"); + link.id = "sunbird-editor-css"; + link.rel = "stylesheet"; + link.href = + "https://cdn.jsdelivr.net/npm/@tekdi/sunbird-questionset-editor-web-component@3.0.1/styles.css"; + document.head.appendChild(link); + } - if (editorRef.current) { - console.log("Element appended"); - editorRef.current.appendChild(questionsetEditorElement); - isAppendedRef.current = true; - } - } - } catch (error) { - console.error("Error creating question set:", error); + if (!document.getElementById("sunbird-editor-js")) { + const script = document.createElement("script"); + script.id = "sunbird-editor-js"; + script.src = + "https://cdn.jsdelivr.net/npm/@tekdi/sunbird-questionset-editor-web-component@3.0.1/sunbird-questionset-editor.js"; + script.async = true; + script.onload = () => setAssetsLoaded(true); + document.body.appendChild(script); + } else { + setAssetsLoaded(true); } }; - fetchData(); + loadAssets(); }, []); - return
; + // Initialize the editor only after assets are loaded + useEffect(() => { + if (assetsLoaded && editorRef.current && !isAppendedRef.current) { + const questionsetEditorElement = document.createElement( + "lib-questionset-editor" + ); + + questionsetEditorElement.setAttribute( + "editor-config", + JSON.stringify(questionSetEditorConfig) + ); + + questionsetEditorElement.addEventListener("editorEmitter", (event: any) => { + console.log("Editor event:", event); + if (event.detail?.action === "backContent") { + window.history.back(); + } + }); + + editorRef.current.appendChild(questionsetEditorElement); + isAppendedRef.current = true; + } + }, [assetsLoaded]); + + return ( +
+ {assetsLoaded ? ( +
+ ) : ( +

Loading editor...

+ )} +
+ ); }; export default QuestionSetEditor; diff --git a/src/pages/_document.tsx b/src/pages/_document.tsx index f04623a..687e71c 100644 --- a/src/pages/_document.tsx +++ b/src/pages/_document.tsx @@ -6,14 +6,6 @@ export default function Document() { return ( - -
From dc8e3048b64cf53902d084fcee9c385aa4a446f4 Mon Sep 17 00:00:00 2001 From: Arif-tekdi-technologies Date: Tue, 15 Oct 2024 14:33:44 +0530 Subject: [PATCH 4/6] update --- src/pages/api/fileUpload.ts | 3 +-- src/pages/api/proxy.ts | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/pages/api/fileUpload.ts b/src/pages/api/fileUpload.ts index 904227f..153c605 100644 --- a/src/pages/api/fileUpload.ts +++ b/src/pages/api/fileUpload.ts @@ -2,7 +2,6 @@ import multer, { MulterError } from 'multer'; import FormData from 'form-data'; import axios from 'axios'; import type { NextApiRequest, NextApiResponse } from 'next'; -import { TOKEN } from '@/utils/app.constant'; const upload = multer({ limits: { @@ -82,7 +81,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) // Set your base URL const baseURL = process.env.BASE_URL as string; - const authApiToken = localStorage.getItem(TOKEN.TOKEN) || process.env.AUTH_API_TOKEN as string; + const authApiToken = process.env.AUTH_API_TOKEN as string; const tenantId = process.env.TENANT_ID as string; // Extract the relative URL from the incoming request (after /action) diff --git a/src/pages/api/proxy.ts b/src/pages/api/proxy.ts index e3cdee5..590e049 100644 --- a/src/pages/api/proxy.ts +++ b/src/pages/api/proxy.ts @@ -1,7 +1,7 @@ import { NextApiRequest, NextApiResponse } from 'next'; import { genericEditorSaveFormResponse, telemetryResponse, creatLockResponse, genericEditorReviewFormResponse } from './mocked-response'; -import { TOKEN } from '@/utils/app.constant'; + export default async function handler(req: NextApiRequest, res: NextApiResponse) { const { method, query } = req; const { path } = query; @@ -9,7 +9,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) let pathString = Array.isArray(path) ? path.join('/') : (path as string); const BASE_URL = process.env.BASE_URL as string; - const API_KEY = localStorage.getItem(TOKEN.TOKEN) || process.env.AUTH_API_TOKEN as string; + const API_KEY = process.env.AUTH_API_TOKEN as string; const TENANT_ID = process.env.TENANT_ID as string; if (pathString === '/action/data/v3/telemetry') { From 14bff88477f7a70380c99b7c28e3a5be03290fad Mon Sep 17 00:00:00 2001 From: Arif-tekdi-technologies Date: Tue, 15 Oct 2024 14:35:10 +0530 Subject: [PATCH 5/6] fix --- src/utils/app.constant.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/utils/app.constant.ts b/src/utils/app.constant.ts index 22a728e..4c2fff0 100644 --- a/src/utils/app.constant.ts +++ b/src/utils/app.constant.ts @@ -7,7 +7,3 @@ export enum Status { export enum ContentType { QUESTION_SET = "QuestionSet", } - -export enum TOKEN { - TOKEN = 'token' -} \ No newline at end of file From 5719b8a1d12b1033810a725e48e28764807d1e7e Mon Sep 17 00:00:00 2001 From: Arif-tekdi-technologies Date: Tue, 15 Oct 2024 14:36:12 +0530 Subject: [PATCH 6/6] fix2 --- src/pages/api/proxy.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pages/api/proxy.ts b/src/pages/api/proxy.ts index 590e049..16c59de 100644 --- a/src/pages/api/proxy.ts +++ b/src/pages/api/proxy.ts @@ -1,7 +1,6 @@ import { NextApiRequest, NextApiResponse } from 'next'; import { genericEditorSaveFormResponse, telemetryResponse, creatLockResponse, genericEditorReviewFormResponse } from './mocked-response'; - export default async function handler(req: NextApiRequest, res: NextApiResponse) { const { method, query } = req; const { path } = query;