@@ -86,7 +86,7 @@ export const useSubmitSnippet = (): UseSubmitSnippet => {
8686 const submitSnippet = useCallback ( async ( action : SubmitSnippetAction = SubmitSnippetAction . SAVE ) => {
8787 setCurrentNotice ( undefined )
8888
89- const result = await ( async ( ) : Promise < Snippet | string | undefined > => {
89+ const rawResult = await ( async ( ) : Promise < Snippet | string | undefined > => {
9090 try {
9191 const request : Snippet = { ...snippet , ...SUBMIT_ACTION_DELTA [ action ] }
9292 const response = await ( 0 === request . id ? snippetsAPI . create ( request ) : snippetsAPI . update ( request ) )
@@ -100,25 +100,38 @@ export const useSubmitSnippet = (): UseSubmitSnippet => {
100100
101101 const messages = isCondition ( snippet ) ? conditionMessages : snippetMessages
102102
103- if ( undefined === result || 'string' === typeof result ) {
103+ if ( undefined === rawResult || 'string' === typeof rawResult ) {
104104 const message = [
105105 snippet . id ? messages . failedUpdate : messages . failedCreate ,
106- result ?? __ ( 'The server did not send a valid response.' , 'code-snippets' )
106+ rawResult ?? __ ( 'The server did not send a valid response.' , 'code-snippets' )
107107 ]
108108
109109 setCurrentNotice ( [ 'error' , message . filter ( Boolean ) . join ( ' ' ) ] )
110110 return undefined
111- } else {
112- setSnippet ( createSnippetObject ( result ) )
113- setCurrentNotice ( [ 'updated' , getSuccessNotice ( snippet , result , action ) ] )
111+ }
114112
115- if ( snippet . id && result . id ) {
116- window . document . title = window . document . title . replace ( snippetMessages . addNew , messages . edit )
117- window . history . replaceState ( { } , '' , addQueryArgs ( window . CODE_SNIPPETS ?. urls . edit , { id : result . id } ) )
118- }
113+ const updatedSnippet = createSnippetObject ( rawResult )
114+ setSnippet ( updatedSnippet )
119115
120- return result
116+ if ( updatedSnippet . code_error ) {
117+ setCurrentNotice ( [
118+ 'error' ,
119+ __ ( 'Snippet could not be activated because the code contains an error. See details below.' , 'code-snippets' )
120+ ] )
121+ } else {
122+ setCurrentNotice ( [ 'updated' , getSuccessNotice ( snippet , updatedSnippet , action ) ] )
121123 }
124+
125+ if ( snippet . id && updatedSnippet . id ) {
126+ window . document . title = window . document . title . replace ( snippetMessages . addNew , messages . edit )
127+ window . history . replaceState (
128+ { } ,
129+ '' ,
130+ addQueryArgs ( window . CODE_SNIPPETS ?. urls . edit , { id : updatedSnippet . id } )
131+ )
132+ }
133+
134+ return updatedSnippet
122135 } , [ snippetsAPI , setIsWorking , setCurrentNotice , snippet , setSnippet ] )
123136
124137 return { submitSnippet }
0 commit comments