@@ -76,50 +76,58 @@ const SUBMIT_ACTION_DELTA: Record<SubmitSnippetAction, Partial<Snippet>> = {
7676}
7777
7878export interface UseSubmitSnippet {
79- submitSnippet : ( action ?: SubmitSnippetAction ) => Promise < Snippet | undefined >
79+ submitSnippet : ( action ?: SubmitSnippetAction , snippetOverride ?: Snippet ) => Promise < Snippet | undefined >
8080}
8181
8282export const useSubmitSnippet = ( ) : UseSubmitSnippet => {
8383 const { snippetsAPI } = useRestAPI ( )
8484 const { setIsWorking, setCurrentNotice, snippet, setSnippet } = useSnippetForm ( )
8585
86- const submitSnippet = useCallback ( async ( action : SubmitSnippetAction = SubmitSnippetAction . SAVE ) => {
86+ const submitSnippet = useCallback ( async (
87+ action : SubmitSnippetAction = SubmitSnippetAction . SAVE ,
88+ snippetOverride ?: Snippet
89+ ) => {
8790 setCurrentNotice ( undefined )
91+ setIsWorking ( true )
92+
93+ // Use the override if provided (prevents stale state issues), otherwise use current context state
94+ const activeSnippet = snippetOverride ?? snippet
8895
8996 const result = await ( async ( ) : Promise < Snippet | string | undefined > => {
9097 try {
91- const request : Snippet = { ...snippet , ...SUBMIT_ACTION_DELTA [ action ] }
98+ const request : Snippet = { ...activeSnippet , ...SUBMIT_ACTION_DELTA [ action ] }
9299 const response = await ( 0 === request . id ? snippetsAPI . create ( request ) : snippetsAPI . update ( request ) )
93- setIsWorking ( false )
94100 return response . id ? response : undefined
95101 } catch ( error ) {
96- setIsWorking ( false )
97102 return isAxiosError ( error ) ? error . message : undefined
103+ } finally {
104+ setIsWorking ( false )
98105 }
99106 } ) ( )
100107
101- const messages = isCondition ( snippet ) ? conditionMessages : snippetMessages
108+ const messages = isCondition ( activeSnippet ) ? conditionMessages : snippetMessages
102109
103110 if ( undefined === result || 'string' === typeof result ) {
104111 const message = [
105- snippet . id ? messages . failedUpdate : messages . failedCreate ,
112+ activeSnippet . id ? messages . failedUpdate : messages . failedCreate ,
106113 result ?? __ ( 'The server did not send a valid response.' , 'code-snippets' )
107114 ]
108115
109116 setCurrentNotice ( [ 'error' , message . filter ( Boolean ) . join ( ' ' ) ] )
110117 return undefined
111118 } else {
112- setSnippet ( createSnippetObject ( result ) )
113- setCurrentNotice ( [ 'updated' , getSuccessNotice ( snippet , result , action ) ] )
119+ const updatedSnippet = createSnippetObject ( result )
120+ setSnippet ( updatedSnippet )
121+ setCurrentNotice ( [ 'updated' , getSuccessNotice ( activeSnippet , updatedSnippet , action ) ] )
114122
115- if ( snippet . id && result . id ) {
123+ if ( activeSnippet . id && updatedSnippet . id ) {
116124 window . document . title = window . document . title . replace ( snippetMessages . addNew , messages . edit )
117- window . history . replaceState ( { } , '' , addQueryArgs ( window . CODE_SNIPPETS ?. urls . edit , { id : result . id } ) )
125+ window . history . replaceState ( { } , '' , addQueryArgs ( window . CODE_SNIPPETS ?. urls . edit , { id : updatedSnippet . id } ) )
118126 }
119127
120- return result
128+ return updatedSnippet
121129 }
122130 } , [ snippetsAPI , setIsWorking , setCurrentNotice , snippet , setSnippet ] )
123131
124132 return { submitSnippet }
125- }
133+ }
0 commit comments