/** * Dailymotion Pro – Admin Video Embed: contextual API/actions * * Contains actions related to enabling/disabling contextual matching via API, * and helper to enable the UI state after confirmation. */ import { updateSubmitButtonState } from '../VideoEmbed/State'; import { applyContextualGlobalToggle } from "./State"; import { hidePopup } from '../VideoEmbed/Popup'; import { qs } from '../../Libs/Utils'; import { updateFeedback } from '../../Libs/UiHelper'; import { updateContextualPreviewSW } from './Preview'; import { changeButtonStatus, removeContextualSettings, storeContextualEmbedSettings as apiStoreContextualEmbedSettings } from '../SetupWizard/WizardApi'; import { navigateToWizardStep } from "../SetupWizard/Navigation"; /** * Calls the backend to destroy contextual settings and shows feedback. * Throws when the HTTP request fails. */ export async function destroyContextualSettings(): Promise { try { await removeContextualSettings(); updateFeedback('Contextual video matching disabled.', 'feedback-success'); } catch (err) { updateFeedback('Failed to disable contextual video matching. Please try again.', 'feedback-error'); throw err; } } /** * Enables contextual matching UI after user confirms in the popup. * Also shows a success feedback message. */ export function enableContextual(): void { const toggle = qs('contextual_embed') as HTMLInputElement | null; if (!toggle) return; toggle.checked = true; applyContextualGlobalToggle(); updateContextualPreviewSW(); hidePopup('confirmContextual'); updateFeedback('Contextual video matching enabled', 'feedback-success'); } export async function storeContextualSettingsFlow(): Promise { return storeContextualSettingsFlowInternal({ hidePopupId: 'confirmContextual', navigateOnSuccess: true }); } /** * Saves contextual settings via API for the Video Embed page (no wizard navigation). * Hides confirmSaveContextual popup and shows feedback. */ export async function storeContextualSettingsFlowForEmbed(): Promise { return storeContextualSettingsFlowInternal({ hidePopupId: 'confirmSaveContextual', navigateOnSuccess: false }); } async function storeContextualSettingsFlowInternal(opts: { hidePopupId: string; navigateOnSuccess: boolean }): Promise { changeButtonStatus('contextual-store__button', 'loading', 'Storing...'); const settingsData = { contextual_embed: (qs('contextual_embed') as HTMLInputElement)?.checked, contextual_player_id: (qs('contextual-player-id') as HTMLSelectElement)?.value, position: (document.querySelector('input[name="position"]:checked') as HTMLInputElement)?.value, contextual_video_heading: (qs('contextual-video-heading') as HTMLInputElement)?.checked, contextual_video_heading_text: (qs('contextual-video-heading-text') as HTMLInputElement)?.value, contextual_font_heading: (qs('contextual-font-heading') as HTMLSelectElement)?.value, contextual_heading_size: (qs('contextual-heading-size') as HTMLSelectElement)?.value, contextual_mute: (qs('contextual-mute') as HTMLInputElement)?.checked, contextual_video_title: (qs('contextual-video-title') as HTMLInputElement)?.checked, contextual_font_title: (qs('contextual-font-title') as HTMLSelectElement)?.value, contextual_title_size: (qs('contextual-title-size') as HTMLSelectElement)?.value }; try { const data = await apiStoreContextualEmbedSettings(settingsData); hidePopup(opts.hidePopupId); if (data.data.status === 200) { changeButtonStatus('contextual-store__button', 'success', 'Stored Successfully'); if (opts.navigateOnSuccess) { navigateToWizardStep(5); } updateFeedback(data.message, 'feedback-success'); } else { changeButtonStatus('contextual-store__button', 'error'); updateFeedback(data.message, 'feedback-error'); } } catch (error: any) { hidePopup(opts.hidePopupId); changeButtonStatus('contextual-store__button', 'error'); updateFeedback(error.message, 'feedback-error'); } }