@@ -18,6 +18,7 @@ import { Modal } from 'bootstrap';
1818import { pushErrorNotification , pushNotification } from '../../../../assets/src/utils' ;
1919import { Response } from '../interfaces' ;
2020import { renderPageEditor } from './editor' ;
21+ import { Translator } from '../translation/translator' ;
2122
2223interface PageData {
2324 pageTitle : string ;
@@ -193,7 +194,7 @@ export const handleTranslatePage = (): void => {
193194 const form = document . getElementById ( 'pmf-translate-page-form' ) as HTMLFormElement | null ;
194195 if ( ! form ) return ;
195196
196- // Initialize WYSIWYG editor for content field
197+ // Initialize WYSIWYG editor for the content field
197198 renderPageEditor ( ) ;
198199
199200 const titleInput = document . getElementById ( 'pageTitle' ) as HTMLInputElement | null ;
@@ -246,6 +247,53 @@ export const handleTranslatePage = (): void => {
246247 updateCharCounter ( 'seoTitle' , 'seo-title-counter' , 60 ) ;
247248 updateCharCounter ( 'seoDescription' , 'seo-description-counter' , 160 ) ;
248249
250+ // AI Translation integration
251+ const translateButton = document . getElementById ( 'btn-translate-page-ai' ) as HTMLButtonElement | null ;
252+ const originalLangInput = document . getElementById ( 'originalLang' ) as HTMLInputElement | null ;
253+
254+ if ( translateButton && langInput && originalLangInput ) {
255+ // Initialize translator when the target language is selected
256+ langInput . addEventListener ( 'change' , ( ) => {
257+ const sourceLang = originalLangInput . value ;
258+ const targetLang = langInput . value ;
259+
260+ if ( sourceLang && targetLang && sourceLang !== targetLang ) {
261+ // Enable the translate button
262+ translateButton . disabled = false ;
263+
264+ // Initialize the Translator
265+ try {
266+ new Translator ( {
267+ buttonSelector : '#btn-translate-page-ai' ,
268+ contentType : 'customPage' ,
269+ sourceLang : sourceLang ,
270+ targetLang : targetLang ,
271+ fieldMapping : {
272+ pageTitle : '#pageTitle' ,
273+ content : '#content' ,
274+ seoTitle : '#seoTitle' ,
275+ seoDescription : '#seoDescription' ,
276+ } ,
277+ onTranslationSuccess : ( ) => {
278+ pushNotification ( 'Translation completed successfully' ) ;
279+ } ,
280+ onTranslationError : ( error ) => {
281+ pushErrorNotification ( `Translation failed: ${ error } ` ) ;
282+ } ,
283+ } ) ;
284+ } catch ( error ) {
285+ console . error ( 'Failed to initialize translator:' , error ) ;
286+ }
287+ } else {
288+ // Disable the translation button if the same language or no target language
289+ translateButton . disabled = true ;
290+ }
291+ } ) ;
292+
293+ // Initially disable the button
294+ translateButton . disabled = true ;
295+ }
296+
249297 // Form submission
250298 const submitButton = document . getElementById ( 'pmf-submit-page' ) as HTMLButtonElement | null ;
251299 if ( submitButton ) {
@@ -268,14 +316,10 @@ export const handleTranslatePage = (): void => {
268316 } ;
269317
270318 const response = ( await addPage ( data ) ) as unknown as Response ;
271- if ( typeof response . success === 'string' ) {
272- pushNotification ( response . success ) ;
273- setTimeout ( ( ) => {
274- window . location . href = './pages' ;
275- } , 2000 ) ;
276- } else {
277- pushErrorNotification ( response . error || 'An error occurred' ) ;
278- }
319+ pushNotification ( response . success ) ;
320+ setTimeout ( ( ) => {
321+ window . location . href = './pages' ;
322+ } , 2000 ) ;
279323 } ) ;
280324 }
281325} ;
@@ -287,7 +331,7 @@ export const handleEditPage = (): void => {
287331 const form = document . getElementById ( 'pmf-edit-page-form' ) as HTMLFormElement | null ;
288332 if ( ! form ) return ;
289333
290- // Initialize WYSIWYG editor for content field
334+ // Initialize WYSIWYG editor for the content field
291335 renderPageEditor ( ) ;
292336
293337 const slugInput = document . getElementById ( 'slug' ) as HTMLInputElement | null ;
@@ -347,14 +391,10 @@ export const handleEditPage = (): void => {
347391 } ;
348392
349393 const response = ( await updatePage ( data ) ) as unknown as Response ;
350- if ( typeof response . success === 'string' ) {
351- pushNotification ( response . success ) ;
352- setTimeout ( ( ) => {
353- window . location . href = './pages' ;
354- } , 2000 ) ;
355- } else {
356- pushErrorNotification ( response . error || 'An error occurred' ) ;
357- }
394+ pushNotification ( response . success ) ;
395+ setTimeout ( ( ) => {
396+ window . location . href = './pages' ;
397+ } , 2000 ) ;
358398 } ) ;
359399 }
360400} ;
@@ -388,14 +428,10 @@ export const handlePages = (): void => {
388428 const pageLang = ( document . getElementById ( 'pageLang' ) as HTMLInputElement ) . value ;
389429
390430 const response = ( await deletePage ( csrfToken , pageId , pageLang ) ) as unknown as Response ;
391- if ( typeof response . success === 'string' ) {
392- pushNotification ( response . success ) ;
393- setTimeout ( ( ) => {
394- window . location . reload ( ) ;
395- } , 2000 ) ;
396- } else {
397- pushErrorNotification ( response . error || 'An error occurred' ) ;
398- }
431+ pushNotification ( response . success ) ;
432+ setTimeout ( ( ) => {
433+ window . location . reload ( ) ;
434+ } , 2000 ) ;
399435 } ) ;
400436 }
401437 }
@@ -410,12 +446,7 @@ export const handlePages = (): void => {
410446
411447 const response = ( await activatePage ( pageId , status , csrfToken ) ) as unknown as Response ;
412448
413- if ( typeof response . success === 'string' ) {
414- pushNotification ( response . success ) ;
415- } else {
416- pushErrorNotification ( response . error || 'An error occurred' ) ;
417- checkbox . checked = ! status ;
418- }
449+ pushNotification ( response . success ) ;
419450 } ) ;
420451 } ) ;
421452} ;
0 commit comments