@@ -7,119 +7,169 @@ import { useRestAPI } from './useRestAPI'
77import { useSnippetForm } from './useSnippetForm'
88import type { Snippet } from '../types/Snippet'
99
10- const snippetMessages = < const > {
11- addNew : __ ( 'Add New Snippet' , 'code-snippets' ) ,
12- edit : __ ( 'Edit Snippet' , 'code-snippets' ) ,
13- created : __ ( 'Snippet <strong>created</strong>.' , 'code-snippets' ) ,
14- updated : __ ( 'Snippet <strong>updated</strong>.' , 'code-snippets' ) ,
15- createdActivated : __ ( 'Snippet <strong>created</strong> and <strong>activated</strong>.' , 'code-snippets' ) ,
16- updatedActivated : __ ( 'Snippet <strong>updated</strong> and <strong>activated</strong>.' , 'code-snippets' ) ,
17- updatedDeactivated : __ ( 'Snippet <strong>updated</strong> and <strong>deactivated</strong>' ) ,
18- updatedExecuted : __ ( 'Snippet <strong>updated</strong> and <strong>executed</strong>.' , 'code-snippets' ) ,
19- failedCreate : __ ( 'Could not create snippet.' , 'code-snippets' ) ,
20- failedUpdate : __ ( 'Could not update snippet.' , 'code-snippets' )
10+ const snippetMessages = < const > {
11+ addNew : __ ( 'Add New Snippet' , 'code-snippets' ) ,
12+ edit : __ ( 'Edit Snippet' , 'code-snippets' ) ,
13+ created : __ ( 'Snippet <strong>created</strong>.' , 'code-snippets' ) ,
14+ updated : __ ( 'Snippet <strong>updated</strong>.' , 'code-snippets' ) ,
15+ createdActivated : __ (
16+ 'Snippet <strong>created</strong> and <strong>activated</strong>.' ,
17+ 'code-snippets'
18+ ) ,
19+ updatedActivated : __ (
20+ 'Snippet <strong>updated</strong> and <strong>activated</strong>.' ,
21+ 'code-snippets'
22+ ) ,
23+ updatedDeactivated : __ (
24+ 'Snippet <strong>updated</strong> and <strong>deactivated</strong>'
25+ ) ,
26+ updatedExecuted : __ (
27+ 'Snippet <strong>updated</strong> and <strong>executed</strong>.' ,
28+ 'code-snippets'
29+ ) ,
30+ failedCreate : __ ( 'Could not create snippet.' , 'code-snippets' ) ,
31+ failedUpdate : __ ( 'Could not update snippet.' , 'code-snippets' ) ,
2132}
2233
23- const conditionCreated = __ ( 'Condition <strong>created</strong>.' , 'code-snippets' )
24- const conditionUpdated = __ ( 'Condition <strong>updated</strong>.' , 'code-snippets' )
34+ const conditionCreated = __ (
35+ 'Condition <strong>created</strong>.' ,
36+ 'code-snippets'
37+ )
38+ const conditionUpdated = __ (
39+ 'Condition <strong>updated</strong>.' ,
40+ 'code-snippets'
41+ )
2542
2643const conditionMessages : typeof snippetMessages = {
27- addNew : __ ( 'Add New Condition' , 'code-snippets' ) ,
28- edit : __ ( 'Edit Condition' , 'code-snippets' ) ,
29- created : conditionCreated ,
30- updated : conditionUpdated ,
31- createdActivated : conditionCreated ,
32- updatedActivated : conditionUpdated ,
33- updatedDeactivated : conditionUpdated ,
34- updatedExecuted : conditionUpdated ,
35- failedCreate : __ ( 'Could not create condition.' , 'code-snippets' ) ,
36- failedUpdate : __ ( 'Could not update condition.' , 'code-snippets' )
44+ addNew : __ ( 'Add New Condition' , 'code-snippets' ) ,
45+ edit : __ ( 'Edit Condition' , 'code-snippets' ) ,
46+ created : conditionCreated ,
47+ updated : conditionUpdated ,
48+ createdActivated : conditionCreated ,
49+ updatedActivated : conditionUpdated ,
50+ updatedDeactivated : conditionUpdated ,
51+ updatedExecuted : conditionUpdated ,
52+ failedCreate : __ ( 'Could not create condition.' , 'code-snippets' ) ,
53+ failedUpdate : __ ( 'Could not update condition.' , 'code-snippets' ) ,
3754}
3855
3956export enum SubmitSnippetAction {
40- SAVE = 'save_snippet' ,
41- SAVE_AND_ACTIVATE = 'save_snippet_activate' ,
42- SAVE_AND_EXECUTE = 'save_snippet_execute' ,
43- SAVE_AND_DEACTIVATE = 'save_snippet_deactivate'
57+ SAVE = 'save_snippet' ,
58+ SAVE_AND_ACTIVATE = 'save_snippet_activate' ,
59+ SAVE_AND_EXECUTE = 'save_snippet_execute' ,
60+ SAVE_AND_DEACTIVATE = 'save_snippet_deactivate' ,
4461}
4562
46- const getSuccessNotice = ( request : Snippet , response : Snippet , action : SubmitSnippetAction ) : string => {
47- const messages = 'condition' === request . scope ? conditionMessages : snippetMessages
48- const wasCreated = 0 === request . id
49-
50- switch ( action ) {
51- case SubmitSnippetAction . SAVE :
52- return wasCreated ? messages . created : messages . updated
53-
54- case SubmitSnippetAction . SAVE_AND_EXECUTE :
55- return messages . updatedExecuted
56-
57- case SubmitSnippetAction . SAVE_AND_ACTIVATE :
58- if ( 'single-use' === response . scope ) {
59- return messages . updatedExecuted
60- } else {
61- return wasCreated
62- ? messages . createdActivated
63- : messages . updatedActivated
64- }
65-
66- case SubmitSnippetAction . SAVE_AND_DEACTIVATE :
67- return messages . updatedDeactivated
68- }
63+ const getSuccessNotice = (
64+ request : Snippet ,
65+ response : Snippet ,
66+ action : SubmitSnippetAction
67+ ) : string => {
68+ const messages =
69+ 'condition' === request . scope ? conditionMessages : snippetMessages
70+ const wasCreated = 0 === request . id
71+
72+ switch ( action ) {
73+ case SubmitSnippetAction . SAVE :
74+ return wasCreated ? messages . created : messages . updated
75+
76+ case SubmitSnippetAction . SAVE_AND_EXECUTE :
77+ return messages . updatedExecuted
78+
79+ case SubmitSnippetAction . SAVE_AND_ACTIVATE :
80+ if ( 'single-use' === response . scope ) {
81+ return messages . updatedExecuted
82+ } else {
83+ return wasCreated
84+ ? messages . createdActivated
85+ : messages . updatedActivated
86+ }
87+
88+ case SubmitSnippetAction . SAVE_AND_DEACTIVATE :
89+ return messages . updatedDeactivated
90+ }
6991}
7092
7193const SUBMIT_ACTION_DELTA : Record < SubmitSnippetAction , Partial < Snippet > > = {
72- [ SubmitSnippetAction . SAVE ] : { } ,
73- [ SubmitSnippetAction . SAVE_AND_ACTIVATE ] : { active : true } ,
74- [ SubmitSnippetAction . SAVE_AND_DEACTIVATE ] : { active : false } ,
75- [ SubmitSnippetAction . SAVE_AND_EXECUTE ] : { active : true }
94+ [ SubmitSnippetAction . SAVE ] : { } ,
95+ [ SubmitSnippetAction . SAVE_AND_ACTIVATE ] : { active : true } ,
96+ [ SubmitSnippetAction . SAVE_AND_DEACTIVATE ] : { active : false } ,
97+ [ SubmitSnippetAction . SAVE_AND_EXECUTE ] : { active : true } ,
7698}
7799
78100export interface UseSubmitSnippet {
79- submitSnippet : ( action ?: SubmitSnippetAction ) => Promise < Snippet | undefined >
101+ submitSnippet : ( action ?: SubmitSnippetAction ) => Promise < Snippet | undefined >
80102}
81103
82104export const useSubmitSnippet = ( ) : UseSubmitSnippet => {
83- const { snippetsAPI } = useRestAPI ( )
84- const { setIsWorking, setCurrentNotice, snippet, setSnippet } = useSnippetForm ( )
85-
86- const submitSnippet = useCallback ( async ( action : SubmitSnippetAction = SubmitSnippetAction . SAVE ) => {
87- setCurrentNotice ( undefined )
88-
89- const result = await ( async ( ) : Promise < Snippet | string | undefined > => {
90- try {
91- const request : Snippet = { ...snippet , ...SUBMIT_ACTION_DELTA [ action ] }
92- const response = await ( 0 === request . id ? snippetsAPI . create ( request ) : snippetsAPI . update ( request ) )
93- setIsWorking ( false )
94- return response . id ? response : undefined
95- } catch ( error ) {
96- setIsWorking ( false )
97- return isAxiosError ( error ) ? error . message : undefined
98- }
99- } ) ( )
100-
101- const messages = isCondition ( snippet ) ? conditionMessages : snippetMessages
102-
103- if ( undefined === result || 'string' === typeof result ) {
104- const message = [
105- snippet . id ? messages . failedUpdate : messages . failedCreate ,
106- result ?? __ ( 'The server did not send a valid response.' , 'code-snippets' )
107- ]
108-
109- setCurrentNotice ( [ 'error' , message . filter ( Boolean ) . join ( ' ' ) ] )
110- return undefined
111- } else {
112- setSnippet ( createSnippetObject ( result ) )
113- setCurrentNotice ( [ 'updated' , getSuccessNotice ( snippet , result , action ) ] )
114-
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- }
119-
120- return result
121- }
122- } , [ snippetsAPI , setIsWorking , setCurrentNotice , snippet , setSnippet ] )
123-
124- return { submitSnippet }
105+ const { snippetsAPI } = useRestAPI ( )
106+ const { setIsWorking, setCurrentNotice, snippet, setSnippet } =
107+ useSnippetForm ( )
108+
109+ const submitSnippet = useCallback (
110+ async ( action : SubmitSnippetAction = SubmitSnippetAction . SAVE ) => {
111+ setCurrentNotice ( undefined )
112+
113+ const result = await ( async ( ) : Promise < Snippet | string | undefined > => {
114+ try {
115+ const request : Snippet = {
116+ ...snippet ,
117+ ...SUBMIT_ACTION_DELTA [ action ] ,
118+ }
119+ const response = await ( 0 === request . id
120+ ? snippetsAPI . create ( request )
121+ : snippetsAPI . update ( request ) )
122+ setIsWorking ( false )
123+ return response . id ? response : undefined
124+ } catch ( error ) {
125+ setIsWorking ( false )
126+ return isAxiosError ( error ) ? error . message : undefined
127+ }
128+ } ) ( )
129+
130+ const messages = isCondition ( snippet )
131+ ? conditionMessages
132+ : snippetMessages
133+
134+ if ( undefined === result || 'string' === typeof result ) {
135+ const message = [
136+ snippet . id ? messages . failedUpdate : messages . failedCreate ,
137+ result ??
138+ __ ( 'The server did not send a valid response.' , 'code-snippets' ) ,
139+ ]
140+
141+ setCurrentNotice ( [ 'error' , message . filter ( Boolean ) . join ( ' ' ) ] )
142+ return undefined
143+ } else {
144+ setSnippet ( createSnippetObject ( result ) )
145+
146+ // Check for code_error and show error if present
147+ if ( result . code_error ) {
148+ setCurrentNotice ( [ 'error' , result . code_error [ 0 ] ] )
149+ } else {
150+ setCurrentNotice ( [
151+ 'updated' ,
152+ getSuccessNotice ( snippet , result , action ) ,
153+ ] )
154+ }
155+
156+ if ( snippet . id && result . id ) {
157+ window . document . title = window . document . title . replace (
158+ snippetMessages . addNew ,
159+ messages . edit
160+ )
161+ window . history . replaceState (
162+ { } ,
163+ '' ,
164+ addQueryArgs ( window . CODE_SNIPPETS ?. urls . edit , { id : result . id } )
165+ )
166+ }
167+
168+ return result
169+ }
170+ } ,
171+ [ snippetsAPI , setIsWorking , setCurrentNotice , snippet , setSnippet ]
172+ )
173+
174+ return { submitSnippet }
125175}
0 commit comments