@@ -33,20 +33,22 @@ document.addEventListener('DOMContentLoaded', () => {
33
33
saveButton . disabled = editorTextarea . value . length === 0
34
34
} )
35
35
36
- saveButton . addEventListener ( 'click' , ( ) => {
36
+ saveButton . addEventListener ( 'click' , async ( ) => {
37
37
savedAnswer = editorTextarea . value
38
- createAnswer ( savedAnswer , questionId )
39
- editorTextarea . value = ''
40
- answerEditorPreview . innerHTML = markdownInitializer . render (
41
- editorTextarea . value
42
- )
43
- saveButton . disabled = true
44
- updateAnswerCount ( true )
45
- updateWatchable ( questionId )
46
- if ( previewTab . classList . contains ( 'is-active' ) ) {
47
- toggleVisibility ( tabElements , 'is-active' )
38
+ const answerCreated = await createAnswer ( savedAnswer , questionId )
39
+ if ( answerCreated ) {
40
+ editorTextarea . value = ''
41
+ answerEditorPreview . innerHTML = markdownInitializer . render (
42
+ editorTextarea . value
43
+ )
44
+ saveButton . disabled = true
45
+ updateAnswerCount ( true )
46
+ updateWatchable ( questionId )
47
+ if ( previewTab . classList . contains ( 'is-active' ) ) {
48
+ toggleVisibility ( tabElements , 'is-active' )
49
+ }
50
+ resizeTextarea ( editorTextarea , defaultTextareaSize )
48
51
}
49
- resizeTextarea ( editorTextarea , defaultTextareaSize )
50
52
} )
51
53
52
54
const editTab = answerEditor . querySelector ( '.edit-answer-tab' )
@@ -70,43 +72,42 @@ document.addEventListener('DOMContentLoaded', () => {
70
72
}
71
73
} )
72
74
73
- function createAnswer ( description , questionId ) {
75
+ async function createAnswer ( description , questionId ) {
74
76
if ( description . length < 1 ) {
75
- return null
77
+ return false
76
78
}
77
79
const params = {
78
80
question_id : questionId ,
79
81
answer : {
80
82
description : description
81
83
}
82
84
}
83
- fetch ( '/api/answers' , {
84
- method : 'POST' ,
85
- headers : {
86
- 'Content-Type' : 'application/json; charset=utf-8' ,
87
- 'X-Requested-With' : 'XMLHttpRequest' ,
88
- 'X-CSRF-Token' : CSRF . getToken ( )
89
- } ,
90
- credentials : 'same-origin' ,
91
- redirect : 'manual' ,
92
- body : JSON . stringify ( params )
93
- } )
94
- . then ( ( response ) => {
95
- if ( response . ok ) {
96
- return response . text ( )
97
- } else {
98
- return response . json ( ) . then ( ( data ) => {
99
- throw new Error ( data . errors . join ( ', ' ) )
100
- } )
101
- }
85
+ try {
86
+ const response = await fetch ( '/api/answers' , {
87
+ method : 'POST' ,
88
+ headers : {
89
+ 'Content-Type' : 'application/json; charset=utf-8' ,
90
+ 'X-Requested-With' : 'XMLHttpRequest' ,
91
+ 'X-CSRF-Token' : CSRF . getToken ( )
92
+ } ,
93
+ credentials : 'same-origin' ,
94
+ redirect : 'manual' ,
95
+ body : JSON . stringify ( params )
102
96
} )
103
- . then ( ( html ) => {
97
+
98
+ if ( response . ok ) {
99
+ const html = await response . text ( )
104
100
initializeNewAnswer ( html )
105
101
toast ( '回答を投稿しました!' )
106
- } )
107
- . catch ( ( error ) => {
108
- console . warn ( error )
109
- } )
102
+ return true
103
+ } else {
104
+ const data = await response . json ( )
105
+ throw new Error ( data . errors . join ( ', ' ) )
106
+ }
107
+ } catch ( error ) {
108
+ console . warn ( error )
109
+ return false
110
+ }
110
111
}
111
112
112
113
function toggleVisibility ( elements , className ) {
0 commit comments