This commit is contained in:
Markos Gogoulos
2026-02-20 22:38:52 +02:00
parent 86fa084391
commit b39789c2c4
20 changed files with 103 additions and 21 deletions

View File

@@ -33,3 +33,49 @@ export function isSelectMediaMode() {
export function inSelectMediaEmbedMode() {
return inEmbeddedApp() && isSelectMediaMode();
}
export function getLtiContextId(): string | null {
try {
const params = new URL(globalThis.location.href).searchParams;
const contextId = params.get('lti_context_id');
if (contextId) {
sessionStorage.setItem('lti_context_id', contextId);
return contextId;
}
return sessionStorage.getItem('lti_context_id');
} catch (e) {
return null;
}
}
export function associateMediaWithLtiCategory(mediaId: string): void {
const ltiContextId = getLtiContextId();
if (!ltiContextId || !mediaId) {
return;
}
const csrfMatch = document.cookie.match(/(?:^|;\s*)csrftoken=([^;]+)/);
const csrfToken = csrfMatch ? csrfMatch[1] : '';
fetch('/api/v1/media/user/bulk_actions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrfToken,
},
body: JSON.stringify({
action: 'add_to_category',
media_ids: [mediaId],
lti_context_id: ltiContextId,
}),
}).then(response => {
if (!response.ok) {
console.warn('[MediaCMS LTI] Failed to associate media with course category:', response.statusText);
}
}).catch(error => {
console.warn('[MediaCMS LTI] Failed to associate media with course category:', error);
});
}

View File

@@ -14,4 +14,4 @@ export * from './quickSort';
export * from './requests';
export { translateString } from './translate';
export { replaceString } from './replacementStrings';
export { inEmbeddedApp, inSelectMediaEmbedMode, isSelectMediaMode } from './embeddedApp';
export { inEmbeddedApp, inSelectMediaEmbedMode, isSelectMediaMode, associateMediaWithLtiCategory } from './embeddedApp';