Browse Source

refactored async call into a same file

Skylsmoi 5 years ago
parent
commit
153a33b547
2 changed files with 74 additions and 43 deletions
  1. 47 0
      src/action.async.js
  2. 27 43
      src/container/HtmlDocument.jsx

+ 47 - 0
src/action.async.js View File

@@ -0,0 +1,47 @@
1
+import { FETCH_CONFIG } from './helper.js'
2
+
3
+export const getHtmlDocContent = (apiUrl, idWorkspace, idContent) =>
4
+  fetch(`${apiUrl}/workspaces/${idWorkspace}/html-documents/${idContent}`, {
5
+    ...FETCH_CONFIG,
6
+    method: 'GET'
7
+  })
8
+
9
+export const getHtmlDocComment = (apiUrl, idWorkspace, idContent) =>
10
+  fetch(`${apiUrl}/workspaces/${idWorkspace}/contents/${idContent}/comments`, {
11
+    ...FETCH_CONFIG,
12
+    method: 'GET'
13
+  })
14
+
15
+export const getHtmlDocRevision = (apiUrl, idWorkspace, idContent) =>
16
+  fetch(`${apiUrl}/workspaces/${idWorkspace}/html-documents/${idContent}/revisions`, {
17
+    ...FETCH_CONFIG,
18
+    method: 'GET'
19
+  })
20
+
21
+export const postHtmlDocNewComment = (apiUrl, idWorkspace, idContent, newComment) =>
22
+  fetch(`${apiUrl}/workspaces/${idWorkspace}/contents/${idContent}/comments`, {
23
+    ...FETCH_CONFIG,
24
+    method: 'POST',
25
+    body: JSON.stringify({
26
+      raw_content: newComment
27
+    })
28
+  })
29
+
30
+export const putHtmlDocContent = (apiUrl, idWorkspace, idContent, label, newContent) =>
31
+  fetch(`${apiUrl}/workspaces/${idWorkspace}/html-documents/${idContent}`, {
32
+    ...FETCH_CONFIG,
33
+    method: 'PUT',
34
+    body: JSON.stringify({
35
+      label: label,
36
+      raw_content: newContent
37
+    })
38
+  })
39
+
40
+export const putHtmlDocStatus = (apiUrl, idWorkspace, idContent, newStatus) =>
41
+  fetch(`${apiUrl}/workspaces/${idWorkspace}/html-documents/${idContent}/status`, {
42
+    ...FETCH_CONFIG,
43
+    method: 'PUT',
44
+    body: JSON.stringify({
45
+      status: newStatus
46
+    })
47
+  })

+ 27 - 43
src/container/HtmlDocument.jsx View File

@@ -11,7 +11,15 @@ import {
11 11
   ArchiveDeleteContent,
12 12
   SelectStatus
13 13
 } from 'tracim_lib'
14
-import { FETCH_CONFIG, MODE, debug } from '../helper.js'
14
+import { MODE, debug } from '../helper.js'
15
+import {
16
+  getHtmlDocContent,
17
+  getHtmlDocComment,
18
+  getHtmlDocRevision,
19
+  postHtmlDocNewComment,
20
+  putHtmlDocContent,
21
+  putHtmlDocStatus
22
+} from '../action.async.js'
15 23
 import i18n from '../i18n.js'
16 24
 
17 25
 class HtmlDocument extends React.Component {
@@ -68,26 +76,17 @@ class HtmlDocument extends React.Component {
68 76
   loadContent = async () => {
69 77
     const { content, config } = this.state
70 78
 
71
-    const fetchResultHtmlDocument = await fetch(`${config.apiUrl}/workspaces/${content.workspace_id}/html-documents/${content.content_id}`, { // ${content.workspace_id} ${content.content_id}
72
-      ...FETCH_CONFIG,
73
-      method: 'GET'
74
-    })
75
-    const fetchResultComment = await fetch(`${config.apiUrl}/workspaces/${content.workspace_id}/contents/${content.content_id}/comments`, { // ${content.workspace_id} ${content.content_id}
76
-      ...FETCH_CONFIG,
77
-      method: 'GET'
78
-    })
79
-    const fetchResultRevision = await fetch(`${config.apiUrl}/workspaces/${content.workspace_id}/html-documents/${content.content_id}/revisions`, { // ${content.workspace_id} ${content.content_id}
80
-      ...FETCH_CONFIG,
81
-      method: 'GET'
82
-    })
83
-
84
-    handleFetchResult(fetchResultHtmlDocument)
79
+    const fetchResultHtmlDocument = getHtmlDocContent(config.apiUrl, content.workspace_id, content.content_id)
80
+    const fetchResultComment = getHtmlDocComment(config.apiUrl, content.workspace_id, content.content_id)
81
+    const fetchResultRevision = getHtmlDocRevision(config.apiUrl, content.workspace_id, content.content_id)
82
+
83
+    handleFetchResult(await fetchResultHtmlDocument)
85 84
       .then(resHtmlDocument => this.setState({content: resHtmlDocument.body}))
86 85
       .catch(e => console.log('Error loading content.', e))
87 86
 
88 87
     Promise.all([
89
-      handleFetchResult(fetchResultComment),
90
-      handleFetchResult(fetchResultRevision)
88
+      handleFetchResult(await fetchResultComment),
89
+      handleFetchResult(await fetchResultRevision)
91 90
     ])
92 91
       .then(([resComment, resRevision]) => {
93 92
         const resCommentWithProperDate = resComment.body.map(c => ({...c, created: (new Date(c.created)).toLocaleString()}))
@@ -121,25 +120,20 @@ class HtmlDocument extends React.Component {
121 120
       })
122 121
   }
123 122
 
124
-  saveEditHtmlDocument = (label, rawContent) =>
125
-    fetch(`${this.state.config.apiUrl}/workspaces/${this.state.content.workspace_id}/html-documents/${this.state.content.content_id}`, {
126
-      ...FETCH_CONFIG,
127
-      method: 'PUT',
128
-      body: JSON.stringify({label: label, raw_content: rawContent})
129
-    })
130
-
131 123
   handleClickBtnCloseApp = () => {
132 124
     this.setState({ isVisible: false })
133 125
     GLOBAL_dispatchEvent({type: 'appClosed', data: {}})
134 126
   }
135 127
 
136 128
   handleSaveEditTitle = async newTitle => {
137
-    const fetchResultSaveHtmlDoc = await this.saveEditHtmlDocument(newTitle, this.state.content.raw_content)
129
+    const { config, content } = this.state
130
+
131
+    const fetchResultSaveHtmlDoc = putHtmlDocContent(config.apiUrl, content.workspace_id, content.content_id, newTitle, content.raw_content)
138 132
 
139
-    handleFetchResult(fetchResultSaveHtmlDoc)
133
+    handleFetchResult(await fetchResultSaveHtmlDoc)
140 134
       .then(resSave => {
141 135
         if (resSave.apiResponse.status === 200) this.loadContent()
142
-        else console.warn('Error saving html-document. Result:', resSave, 'content:', this.state.content, 'config:', this.state.config)
136
+        else console.warn('Error saving html-document. Result:', resSave, 'content:', content, 'config:', config)
143 137
       })
144 138
   }
145 139
 
@@ -153,9 +147,9 @@ class HtmlDocument extends React.Component {
153 147
   handleSaveHtmlDocument = async () => {
154 148
     const { content, config } = this.state
155 149
 
156
-    const fetchResultSaveHtmlDoc = await this.saveEditHtmlDocument(content.label, content.raw_content)
150
+    const fetchResultSaveHtmlDoc = putHtmlDocContent(config.apiUrl, content.workspace_id, content.content_id, content.label, content.raw_content)
157 151
 
158
-    handleFetchResult(fetchResultSaveHtmlDoc)
152
+    handleFetchResult(await fetchResultSaveHtmlDoc)
159 153
       .then(resSave => {
160 154
         if (resSave.apiResponse.status === 200) {
161 155
           this.handleCloseNewVersion()
@@ -179,15 +173,9 @@ class HtmlDocument extends React.Component {
179 173
   handleClickValidateNewCommentBtn = async () => {
180 174
     const { config, content, newComment } = this.state
181 175
 
182
-    const fetchResultSaveNewComment = await fetch(`${config.apiUrl}/workspaces/${content.workspace_id}/contents/${content.content_id}/comments`, {
183
-      ...FETCH_CONFIG,
184
-      method: 'POST',
185
-      body: JSON.stringify({
186
-        raw_content: newComment
187
-      })
188
-    })
176
+    const fetchResultSaveNewComment = await postHtmlDocNewComment(config.apiUrl, content.workspace_id, content.content_id, newComment)
189 177
 
190
-    handleFetchResult(fetchResultSaveNewComment)
178
+    handleFetchResult(await fetchResultSaveNewComment)
191 179
       .then(resSave => {
192 180
         if (resSave.apiResponse.status === 200) {
193 181
           this.setState({newComment: ''})
@@ -204,13 +192,9 @@ class HtmlDocument extends React.Component {
204 192
   handleChangeStatus = async newStatus => {
205 193
     const { config, content } = this.state
206 194
 
207
-    const fetchResultSaveEditStatus = await fetch(`${config.apiUrl}/workspaces/${content.workspace_id}/html-documents/${content.content_id}/status`, {
208
-      ...FETCH_CONFIG,
209
-      method: 'PUT',
210
-      body: JSON.stringify({status: newStatus})
211
-    })
195
+    const fetchResultSaveEditStatus = putHtmlDocStatus(config.apiUrl, content.workspace_id, content.content_id, newStatus)
212 196
 
213
-    handleFetchResult(fetchResultSaveEditStatus)
197
+    handleFetchResult(await fetchResultSaveEditStatus)
214 198
       .then(resSave => {
215 199
         if (resSave.status !== 204) { // 204 no content so dont take status from resSave.apiResponse.status
216 200
           console.warn('Error saving html-document comment. Result:', resSave, 'content:', content, 'config:', config)