浏览代码

Merge branch 'develop' of github.com:tracim/app_PageHtml into develop

AlexiCauvin 6 年前
父节点
当前提交
5a6d04f38f
共有 2 个文件被更改,包括 74 次插入43 次删除
  1. 47 0
      src/action.async.js
  2. 27 43
      src/container/HtmlDocument.jsx

+ 47 - 0
src/action.async.js 查看文件

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 查看文件

11
   ArchiveDeleteContent,
11
   ArchiveDeleteContent,
12
   SelectStatus
12
   SelectStatus
13
 } from 'tracim_lib'
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
 import i18n from '../i18n.js'
23
 import i18n from '../i18n.js'
16
 
24
 
17
 class HtmlDocument extends React.Component {
25
 class HtmlDocument extends React.Component {
68
   loadContent = async () => {
76
   loadContent = async () => {
69
     const { content, config } = this.state
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
       .then(resHtmlDocument => this.setState({content: resHtmlDocument.body}))
84
       .then(resHtmlDocument => this.setState({content: resHtmlDocument.body}))
86
       .catch(e => console.log('Error loading content.', e))
85
       .catch(e => console.log('Error loading content.', e))
87
 
86
 
88
     Promise.all([
87
     Promise.all([
89
-      handleFetchResult(fetchResultComment),
90
-      handleFetchResult(fetchResultRevision)
88
+      handleFetchResult(await fetchResultComment),
89
+      handleFetchResult(await fetchResultRevision)
91
     ])
90
     ])
92
       .then(([resComment, resRevision]) => {
91
       .then(([resComment, resRevision]) => {
93
         const resCommentWithProperDate = resComment.body.map(c => ({...c, created: (new Date(c.created)).toLocaleString()}))
92
         const resCommentWithProperDate = resComment.body.map(c => ({...c, created: (new Date(c.created)).toLocaleString()}))
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
   handleClickBtnCloseApp = () => {
123
   handleClickBtnCloseApp = () => {
132
     this.setState({ isVisible: false })
124
     this.setState({ isVisible: false })
133
     GLOBAL_dispatchEvent({type: 'appClosed', data: {}})
125
     GLOBAL_dispatchEvent({type: 'appClosed', data: {}})
134
   }
126
   }
135
 
127
 
136
   handleSaveEditTitle = async newTitle => {
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
       .then(resSave => {
134
       .then(resSave => {
141
         if (resSave.apiResponse.status === 200) this.loadContent()
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
   handleSaveHtmlDocument = async () => {
147
   handleSaveHtmlDocument = async () => {
154
     const { content, config } = this.state
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
       .then(resSave => {
153
       .then(resSave => {
160
         if (resSave.apiResponse.status === 200) {
154
         if (resSave.apiResponse.status === 200) {
161
           this.handleCloseNewVersion()
155
           this.handleCloseNewVersion()
179
   handleClickValidateNewCommentBtn = async () => {
173
   handleClickValidateNewCommentBtn = async () => {
180
     const { config, content, newComment } = this.state
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
       .then(resSave => {
179
       .then(resSave => {
192
         if (resSave.apiResponse.status === 200) {
180
         if (resSave.apiResponse.status === 200) {
193
           this.setState({newComment: ''})
181
           this.setState({newComment: ''})
204
   handleChangeStatus = async newStatus => {
192
   handleChangeStatus = async newStatus => {
205
     const { config, content } = this.state
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
       .then(resSave => {
198
       .then(resSave => {
215
         if (resSave.status !== 204) { // 204 no content so dont take status from resSave.apiResponse.status
199
         if (resSave.status !== 204) { // 204 no content so dont take status from resSave.apiResponse.status
216
           console.warn('Error saving html-document comment. Result:', resSave, 'content:', content, 'config:', config)
200
           console.warn('Error saving html-document comment. Result:', resSave, 'content:', content, 'config:', config)