Skip to content

Commit fe6dc25

Browse files
authored
Merge pull request #688 from 10up/fix/686
Show error message if title or excerpt generation fails in Classic Editor
2 parents b37526d + d3ed8fa commit fe6dc25

3 files changed

+97
-54
lines changed

src/js/openai/classic-editor-excerpt-generator.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ const classifaiExcerptData = window.classifaiGenerateExcerpt || {};
4040
)
4141
.insertAfter( excerptContainer );
4242

43+
$( '<p>', {
44+
class: 'classifai-openai__excerpt-generate-error',
45+
} ).insertAfter(
46+
document.getElementById( 'classifai-openai__excerpt-generate-btn' )
47+
);
48+
4349
// Append disable feature link.
4450
if (
4551
ClassifAI?.opt_out_enabled_features?.includes(
@@ -79,25 +85,34 @@ const classifaiExcerptData = window.classifaiGenerateExcerpt || {};
7985
const spinnerEl = $(
8086
'.classifai-openai__excerpt-generate-btn--spinner'
8187
);
88+
const errorEl = $( '.classifai-openai__excerpt-generate-error' );
8289

8390
generateTextEl.css( 'opacity', '0' );
8491
spinnerEl.show();
92+
errorEl.text( '' ).hide();
8593
isProcessing = true;
8694

8795
const path = classifaiExcerptData?.path + postId;
8896

8997
apiFetch( {
9098
path,
91-
} ).then( ( result ) => {
92-
generateTextEl.css( 'opacity', '1' );
93-
spinnerEl.hide();
94-
isProcessing = false;
99+
} )
100+
.then( ( result ) => {
101+
generateTextEl.css( 'opacity', '1' );
102+
spinnerEl.hide();
103+
isProcessing = false;
95104

96-
$( excerptContainer ).val( result ).trigger( 'input' );
97-
generateTextEl.text(
98-
classifaiExcerptData?.regenerateText ?? ''
99-
);
100-
} );
105+
$( excerptContainer ).val( result ).trigger( 'input' );
106+
generateTextEl.text(
107+
classifaiExcerptData?.regenerateText ?? ''
108+
);
109+
} )
110+
.catch( ( error ) => {
111+
generateTextEl.css( 'opacity', '1' );
112+
spinnerEl.hide();
113+
isProcessing = false;
114+
errorEl.text( error?.message ).show();
115+
} );
101116
};
102117

103118
// Event handler registration to generate the excerpt.

src/js/openai/classic-editor-title-generator.js

+62-45
Original file line numberDiff line numberDiff line change
@@ -104,54 +104,71 @@ const scriptData = classifaiChatGPTData.enabledFeatures.reduce(
104104

105105
apiFetch( {
106106
path,
107-
} ).then( ( result ) => {
108-
generateTextEl.css( 'opacity', '1' );
109-
spinnerEl.hide();
110-
isProcessing = false;
111-
112-
result.forEach( ( title ) => {
113-
$( '<textarea>', {
114-
text: title,
115-
} )
116-
.wrap( `<div class="classifai-openai__result-item" />` )
117-
.parent()
118-
.append(
119-
$( '<button />', {
120-
text: scriptData.title.selectBtnText,
121-
type: 'button',
122-
class: 'button classifai-openai__select-title',
123-
} )
107+
} )
108+
.then( ( result ) => {
109+
generateTextEl.css( 'opacity', '1' );
110+
spinnerEl.hide();
111+
isProcessing = false;
112+
113+
result.forEach( ( title ) => {
114+
$( '<textarea>', {
115+
text: title,
116+
} )
117+
.wrap(
118+
`<div class="classifai-openai__result-item" />`
119+
)
120+
.parent()
121+
.append(
122+
$( '<button />', {
123+
text: scriptData.title.selectBtnText,
124+
type: 'button',
125+
class: 'button classifai-openai__select-title',
126+
} )
127+
)
128+
.appendTo( '#classifai-openai__results-content' );
129+
} );
130+
131+
// Append disable feature link.
132+
if (
133+
ClassifAI?.opt_out_enabled_features?.includes(
134+
'feature_title_generation'
124135
)
136+
) {
137+
$( '<a>', {
138+
text: __(
139+
'Disable this ClassifAI feature',
140+
'classifai'
141+
),
142+
href: ClassifAI?.profile_url,
143+
target: '_blank',
144+
rel: 'noopener noreferrer',
145+
class: 'classifai-disable-feature-link',
146+
} )
147+
.wrap(
148+
`<div class="classifai-openai__result-disable-link" />`
149+
)
150+
.parent()
151+
.appendTo( '#classifai-openai__modal' );
152+
}
153+
154+
$( '#classifai-openai__results' )
155+
.show()
156+
.addClass( 'classifai-openai--fade-in' );
157+
} )
158+
.catch( ( error ) => {
159+
generateTextEl.css( 'opacity', '1' );
160+
spinnerEl.hide();
161+
isProcessing = false;
162+
163+
$( '<span class="error">' )
164+
.text( error?.message )
165+
.wrap( `<div class="classifai-openai__result-item" />` )
125166
.appendTo( '#classifai-openai__results-content' );
126-
} );
127167

128-
// Append disable feature link.
129-
if (
130-
ClassifAI?.opt_out_enabled_features?.includes(
131-
'feature_title_generation'
132-
)
133-
) {
134-
$( '<a>', {
135-
text: __(
136-
'Disable this ClassifAI feature',
137-
'classifai'
138-
),
139-
href: ClassifAI?.profile_url,
140-
target: '_blank',
141-
rel: 'noopener noreferrer',
142-
class: 'classifai-disable-feature-link',
143-
} )
144-
.wrap(
145-
`<div class="classifai-openai__result-disable-link" />`
146-
)
147-
.parent()
148-
.appendTo( '#classifai-openai__modal' );
149-
}
150-
151-
$( '#classifai-openai__results' )
152-
.show()
153-
.addClass( 'classifai-openai--fade-in' );
154-
} );
168+
$( '#classifai-openai__results' )
169+
.show()
170+
.addClass( 'classifai-openai--fade-in' );
171+
} );
155172
};
156173

157174
// Event handler registration to generate the title.

src/scss/openai/classic-editor-title-generator.scss

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
}
3838
}
3939

40+
.classifai-openai__excerpt-generate-error {
41+
display: none;
42+
color: #dc3232;
43+
text-align: right;
44+
}
45+
4046
.classifai-openai__excerpt-generate-disable-link {
4147
text-align: right;
4248
margin-top: 6px;
@@ -81,6 +87,11 @@
8187
padding: 1rem;
8288
padding-bottom: 1.5rem;
8389
gap: 1rem;
90+
91+
& .error {
92+
color: #dc3232;
93+
font-size: 1rem;
94+
}
8495
}
8596

8697
&__close-modal-button {

0 commit comments

Comments
 (0)