Browse Source

bugfix + added missing refactor for fetch call

Skylsmoi 5 years ago
parent
commit
ebb9e76625
3 changed files with 30 additions and 25 deletions
  1. 11 0
      src/action.async.js
  2. 1 1
      src/container/HtmlDocument.jsx
  3. 18 24
      src/container/PopupCreateHtmlDocument.jsx

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

@@ -45,3 +45,14 @@ export const putHtmlDocStatus = (apiUrl, idWorkspace, idContent, newStatus) =>
45 45
       status: newStatus
46 46
     })
47 47
   })
48
+
49
+export const postHtmlDocContent = (apiUrl, idWorkspace, idFolder, contentType, newContentName) =>
50
+  fetch(`${apiUrl}/workspaces/${idWorkspace}/contents`, {
51
+    ...FETCH_CONFIG,
52
+    method: 'POST',
53
+    body: JSON.stringify({
54
+      parent_id: idFolder,
55
+      content_type: contentType,
56
+      label: newContentName
57
+    })
58
+  })

+ 1 - 1
src/container/HtmlDocument.jsx View File

@@ -196,7 +196,7 @@ class HtmlDocument extends React.Component {
196 196
 
197 197
     handleFetchResult(await fetchResultSaveEditStatus)
198 198
       .then(resSave => {
199
-        if (resSave.status !== 204) { // 204 no content so dont take status from resSave.apiResponse.status
199
+        if (resSave.apiResponse.status !== 204) { // 204 no content so dont take status from resSave.apiResponse.status
200 200
           console.warn('Error saving html-document comment. Result:', resSave, 'content:', content, 'config:', config)
201 201
         } else {
202 202
           this.loadContent()

+ 18 - 24
src/container/PopupCreateHtmlDocument.jsx View File

@@ -1,10 +1,10 @@
1 1
 import React from 'react'
2 2
 import {
3
-  CardPopupCreateContent
3
+  CardPopupCreateContent, handleFetchResult
4 4
 } from 'tracim_lib'
5
-import { FETCH_CONFIG } from '../helper.js'
5
+import { postHtmlDocContent } from '../action.async.js'
6 6
 
7
-const debug = {
7
+const debug = { // outdated
8 8
   config: {
9 9
     label: 'Text Document',
10 10
     slug: 'html-documents',
@@ -43,7 +43,6 @@ class PopupCreateHtmlDocument extends React.Component {
43 43
       idFolder: props.data ? props.data.idFolder : debug.idFolder,
44 44
       newContentName: ''
45 45
     }
46
-    this.handleValidate = this.handleValidate.bind(this)
47 46
   }
48 47
 
49 48
   handleChangeNewContentName = e => this.setState({newContentName: e.target.value})
@@ -55,31 +54,26 @@ class PopupCreateHtmlDocument extends React.Component {
55 54
     }
56 55
   })
57 56
 
58
-  async handleValidate () {
59
-    const fetchSaveNewHtmlDoc = await fetch(`${this.state.config.apiUrl}/workspaces/${this.state.idWorkspace}/contents`, {
60
-      ...FETCH_CONFIG,
61
-      method: 'POST',
62
-      body: JSON.stringify({
63
-        parent_id: this.state.idFolder,
64
-        content_type: this.state.config.slug,
65
-        label: this.state.newContentName
66
-      })
67
-    })
57
+  handleValidate = async () => {
58
+    const { config, appName, idWorkspace, idFolder, newContentName } = this.state
68 59
 
69
-    if (fetchSaveNewHtmlDoc.status === 200) {
70
-      const jsonSaveNewHtmlDoc = await fetchSaveNewHtmlDoc.json()
60
+    const fetchSaveNewHtmlDoc = postHtmlDocContent(config.apiUrl, idWorkspace, idFolder, config.slug, newContentName)
71 61
 
72
-      this.handleClose()
62
+    handleFetchResult(await fetchSaveNewHtmlDoc)
63
+      .then(resSave => {
64
+        if (resSave.apiResponse.status === 200) {
65
+          this.handleClose()
73 66
 
74
-      GLOBAL_dispatchEvent({
75
-        type: 'openContentUrl', // handled by tracim_front:src/container/WorkspaceContent.jsx
76
-        data: {
77
-          idWorkspace: jsonSaveNewHtmlDoc.workspace_id,
78
-          contentType: this.state.appName,
79
-          idContent: jsonSaveNewHtmlDoc.id
67
+          GLOBAL_dispatchEvent({
68
+            type: 'openContentUrl', // handled by tracim_front:src/container/WorkspaceContent.jsx
69
+            data: {
70
+              idWorkspace: resSave.body.workspace_id,
71
+              contentType: appName,
72
+              idContent: resSave.body.content_id
73
+            }
74
+          })
80 75
         }
81 76
       })
82
-    }
83 77
   }
84 78
 
85 79
   render () {