Skip to content

Commit

Permalink
fix js linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidsector9 committed Jun 23, 2023
1 parent 95d210f commit c4c22f7
Showing 1 changed file with 62 additions and 31 deletions.
93 changes: 62 additions & 31 deletions src/js/openai/classic-editor-title-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import apiFetch from '@wordpress/api-fetch';
import '../../scss/openai/classic-editor-title-generator.scss';

const classifaiChatGPTData = window.classifaiChatGPTData || {};
let scriptData = classifaiChatGPTData.enabledFeatures.reduce( ( acc, cur ) => ( { [ cur.feature ] : cur } ), {} );
const scriptData = classifaiChatGPTData.enabledFeatures.reduce(
( acc, cur ) => ( { [ cur.feature ]: cur } ),
{}
);

( function( $ ) {
( function ( $ ) {
$( document ).ready( () => {
if ( scriptData?.title ) {
generateTitleInit();
Expand All @@ -22,31 +25,40 @@ let scriptData = classifaiChatGPTData.enabledFeatures.reduce( ( acc, cur ) => (
// Creates and appens the "Generate titles" button.
$( '<span />', {
text: scriptData?.title?.buttonText ?? '',
'class': 'classifai-openai__title-generate-btn--text',
class: 'classifai-openai__title-generate-btn--text',
} )
.wrap( '<div class="button" id="classifai-openai__title-generate-btn" />' )
.parent()
.append( $( '<span />', {
'class': 'classifai-openai__title-generate-btn--spinner',
} ) )
.appendTo( '#titlewrap' )
.wrap(
'<div class="button" id="classifai-openai__title-generate-btn" />'
)
.parent()
.append(
$( '<span />', {
class: 'classifai-openai__title-generate-btn--spinner',
} )
)
.appendTo( '#titlewrap' );

// The current post ID.
const postId = $( '#post_ID' ).val();

// Callback to hide the popup.
const hidePopup = () => {
$( '#classifai-openai__results' ).removeClass( 'classifai-openai--fade-in' ).delay(300).fadeOut(0);
}
$( '#classifai-openai__results' )
.removeClass( 'classifai-openai--fade-in' )
.delay( 300 )
.fadeOut( 0 );
};

// Callback to apply the title from the result to the post title.
const applyTitle = ( e ) => {
const selectBtnEl = $( e.target );
const textarea = selectBtnEl.closest( '.classifai-openai__result-item' ).find( 'textarea' );
const textarea = selectBtnEl
.closest( '.classifai-openai__result-item' )
.find( 'textarea' );

$( '#title' ).val( textarea.val() ).trigger( 'input' );
hidePopup();
}
};

// Callback to generate the title.
const generateTitle = () => {
Expand All @@ -55,8 +67,12 @@ let scriptData = classifaiChatGPTData.enabledFeatures.reduce( ( acc, cur ) => (
}

$( '#classifai-openai__results-content' ).html( '' );
const generateTextEl = $( '.classifai-openai__title-generate-btn--text' );
const spinnerEl = $( '.classifai-openai__title-generate-btn--spinner' );
const generateTextEl = $(
'.classifai-openai__title-generate-btn--text'
);
const spinnerEl = $(
'.classifai-openai__title-generate-btn--spinner'
);

generateTextEl.css( 'opacity', '0' );
spinnerEl.show();
Expand All @@ -66,44 +82,59 @@ let scriptData = classifaiChatGPTData.enabledFeatures.reduce( ( acc, cur ) => (

apiFetch( {
path,
} )
.then( ( result ) => {
} ).then( ( result ) => {
generateTextEl.css( 'opacity', '1' );
spinnerEl.hide();
isProcessing = false;

result.forEach( ( title ) => {
$( '<textarea>', {
text: title
text: title,
} )
.wrap( `<div class="classifai-openai__result-item" />` )
.parent()
.append( $( '<button />', {
text: scriptData.title.selectBtnText,
type: 'button',
'class': 'button classifai-openai__select-title',
} ) )
.appendTo( '#classifai-openai__results-content' );
.wrap( `<div class="classifai-openai__result-item" />` )
.parent()
.append(
$( '<button />', {
text: scriptData.title.selectBtnText,
type: 'button',
class: 'button classifai-openai__select-title',
} )
)
.appendTo( '#classifai-openai__results-content' );
} );

$( '#classifai-openai__results' ).show().addClass( 'classifai-openai--fade-in' );
$( '#classifai-openai__results' )
.show()
.addClass( 'classifai-openai--fade-in' );
} );
};

// Event handler registration to generate the title.
$( document ).on( 'click', '#classifai-openai__title-generate-btn', generateTitle );
$( document ).on(
'click',
'#classifai-openai__title-generate-btn',
generateTitle
);

// Event handler registration to hide the popup.
$( document ).on( 'click', '#classifai-openai__overlay', hidePopup );
$( document ).on( 'click', '#classifai-openai__close-modal-button', hidePopup );
$( document ).on(
'click',
'#classifai-openai__close-modal-button',
hidePopup
);

// Event handler registration to apply the selected title to the post title.
$( document ).on( 'click', '.classifai-openai__select-title', applyTitle );
$( document ).on(
'click',
'.classifai-openai__select-title',
applyTitle
);

// Sets the modal title.
const resultWrapper = $( '#classifai-openai__results' );
resultWrapper
.find( '#classifai-openai__results-title' )
.text( scriptData.title.modalTitle );
}
} ( jQuery ) );
} )( jQuery );

0 comments on commit c4c22f7

Please sign in to comment.