Browse Source

integration with real api

Skylsmoi 5 years ago
parent
commit
4e4b456c15

File diff suppressed because it is too large
+ 40 - 0
dist/html-documents.app.js


+ 1 - 1
dist/index.html View File

@@ -17,7 +17,7 @@
17 17
 
18 18
   <div id='content'></div>
19 19
 
20
-  <script src='./pageHtml.app.dev.js'></script>
20
+  <script src='./html-documents.app.dev.js'></script>
21 21
 
22 22
   <script type='text/javascript'>
23 23
     GLOBAL_renderApp = app => {

+ 1 - 0
package.json View File

@@ -49,6 +49,7 @@
49 49
     "globals": [
50 50
       "fetch",
51 51
       "history",
52
+      "btoa",
52 53
       "GLOBAL_renderApp",
53 54
       "GLOBAL_unmountApp",
54 55
       "GLOBAL_dispatchEvent"

+ 93 - 49
src/container/PageHtml.jsx View File

@@ -14,20 +14,20 @@ import i18n from '../i18n.js'
14 14
 
15 15
 const debug = {
16 16
   config: {
17
-    name: 'PageHtml',
18
-    label: {
19
-      fr: 'Page Html',
20
-      en: 'Html page'
21
-    },
22
-    componentLeft: 'PageHtml',
23
-    componentRight: 'Timeline',
24
-    customClass: 'wsContentPageHtml',
25
-    icon: 'fa fa-file-text-o',
26
-    color: '#fdfdfd',
17
+    label: 'Text Document',
18
+    slug: 'page',
19
+    faIcon: 'file-text-o',
20
+    hexcolor: '#3f52e3',
21
+    creationLabel: 'Write a document',
27 22
     domContainer: 'appContainer',
28
-    mockApiUrl: 'http://localhost:3001'
23
+    apiUrl: 'localhost:6543/api/v2',
24
+    mockApiUrl: 'localhost:3001',
25
+    apiHeader: {
26
+      'Accept': 'application/json',
27
+      'Content-Type': 'application/json'
28
+      // 'Authorization': 'Basic ' + btoa(`${'admin@admin.admin'}:${'admin@admin.admin'}`)
29
+    }
29 30
   },
30
-  contentAction: 'showContent',
31 31
   loggedUser: {
32 32
     id: 5,
33 33
     username: 'Smoi',
@@ -37,17 +37,31 @@ const debug = {
37 37
     avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
38 38
   },
39 39
   content: {
40
-    id: -1,
41
-    type: 'pageHtml',
42
-    title: 'Test debug pageHtml',
43
-    status: 'validated',
44
-    version: 'version n°1',
45
-    text: 'This is the default pageHtml content for debug purpose',
46
-    workspace: {
47
-      id: -1,
48
-      title: 'Test debug workspace',
49
-      ownerId: 5
50
-    }
40
+    author: {
41
+      avatar_url: null,
42
+      public_name: 'Global manager',
43
+      user_id: 1
44
+    },
45
+    content_id: -1,
46
+    content_type: 'page',
47
+    created: '2018-06-18T14:59:26Z',
48
+    current_revision_id: 11,
49
+    is_archived: false,
50
+    is_deleted: false,
51
+    label: 'Current Menu',
52
+    last_modifier: {
53
+      avatar_url: null,
54
+      public_name: 'Global manager',
55
+      user_id: 1
56
+    },
57
+    modified: '2018-06-18T14:59:26Z',
58
+    parent_id: 2,
59
+    raw_content: '<div>bonjour, je suis un lapin.</div>',
60
+    show_in_ui: true,
61
+    slug: 'current-menu',
62
+    status: 'open',
63
+    sub_content_types: ['thread', 'page', 'file', 'folder'],
64
+    workspace_id: 1
51 65
   },
52 66
   timeline: timelineDebugData
53 67
 }
@@ -56,7 +70,7 @@ class pageHtml extends React.Component {
56 70
   constructor (props) {
57 71
     super(props)
58 72
     this.state = {
59
-      appName: 'PageHtml',
73
+      appName: 'page',
60 74
       isVisible: true,
61 75
       config: props.data ? props.data.config : debug.config,
62 76
       loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
@@ -70,37 +84,67 @@ class pageHtml extends React.Component {
70 84
 
71 85
   customEventReducer = ({ detail: { type, data } }) => { // action: { type: '', data: {} }
72 86
     switch (type) {
73
-      case 'PageHtml_showApp':
87
+      case 'page_showApp':
74 88
         this.setState({isVisible: true})
75 89
         break
76
-      case 'PageHtml_hideApp':
90
+      case 'page_hideApp':
77 91
         this.setState({isVisible: false})
78 92
         break
93
+      case 'page_reloadContent':
94
+        this.setState({content: data})
79 95
     }
80 96
   }
81 97
 
82
-  async componentDidMount () {
98
+  componentDidMount () {
99
+    console.log('pageHtml did mount')
100
+    if (this.state.content.content_id === -1) return // debug case
101
+
102
+    this.loadContent()
103
+    // wysiwyg()
104
+  }
105
+
106
+  componentDidUpdate (prevProps, prevState) {
107
+    console.log('pageHtml did update', prevState, this.state)
108
+    if (!prevState.content || !this.state.content) return
109
+
110
+    if (prevState.content.content_id !== this.state.content.content_id) {
111
+      this.loadContent()
112
+    }
113
+  }
114
+
115
+  loadContent = async () => {
116
+    console.log('loadContent')
83 117
     const { content, config } = this.state
84
-    if (content.id === '-1') return // debug case
85 118
 
86
-    const fetchResultPageHtml = await fetch(`${config.mockApiUrl}/workspace/${content.workspace.id}/content/${content.id}`, {
119
+    const fetchResultPageHtml = await fetch(`${config.apiUrl}/workspaces/${content.workspace_id}/html-documents/${content.content_id}`, {
87 120
       ...FETCH_CONFIG,
88 121
       method: 'GET'
89 122
     })
90
-    const fetchResultTimeline = await fetch(`${config.mockApiUrl}/workspace/${content.workspace.id}/content/${content.id}/timeline`, {
123
+    const fetchResultTimeline = await fetch(`${config.apiUrl}/workspaces/${content.workspace_id}/contents/${content.content_id}/comments`, {
91 124
       ...FETCH_CONFIG,
92 125
       method: 'GET'
93 126
     })
94 127
 
95
-    fetchResultPageHtml.json = await handleFetchResult(fetchResultPageHtml)
96
-    fetchResultTimeline.json = await handleFetchResult(fetchResultTimeline)
97
-
98
-    this.setState({
99
-      content: fetchResultPageHtml.json,
100
-      timeline: fetchResultTimeline.json
101
-    })
102
-
103
-    // wysiwyg()
128
+    // Promise.all([
129
+    //   handleFetchResult(fetchResultPageHtml),
130
+    //   handleFetchResult(fetchResultTimeline)
131
+    // ])
132
+    //   .then(([resPageHtml, resTimeline]) => {
133
+    //     this.setState({
134
+    //       content: resPageHtml,
135
+    //       timeline: resTimeline
136
+    //     })
137
+    //   })
138
+    handleFetchResult(fetchResultPageHtml)
139
+      .then(resPageHtml => this.setState({content: resPageHtml}))
140
+      .catch(e => console.log('Error loading content.', e))
141
+
142
+    handleFetchResult(fetchResultTimeline)
143
+      .then(resTimeline => this.setState({timeline: resTimeline}))
144
+      .catch(e => {
145
+        console.log('Error loading Timeline.', e)
146
+        this.setState({timeline: []})
147
+      })
104 148
   }
105 149
 
106 150
   handleClickBtnCloseApp = () => {
@@ -124,32 +168,32 @@ class pageHtml extends React.Component {
124 168
     if (!isVisible) return null
125 169
 
126 170
     return (
127
-      <PopinFixed customClass={`${config.customClass}`}>
171
+      <PopinFixed customClass={`${config.slug}`}>
128 172
         <PopinFixedHeader
129
-          customClass={`${config.customClass}`}
130
-          icon={config.icon}
131
-          name={content.title}
173
+          customClass={`${config.slug}`}
174
+          icon={config.faIcon}
175
+          name={content.label}
132 176
           onClickCloseBtn={this.handleClickBtnCloseApp}
133 177
           onChangeTitle={this.handleChangeTitle}
134 178
         />
135 179
 
136 180
         <PopinFixedOption
137
-          customClass={`${config.customClass}`}
181
+          customClass={`${config.slug}`}
138 182
           onClickNewVersionBtn={this.handleClickNewVersion}
139 183
           i18n={i18n}
140 184
         />
141 185
 
142
-        <PopinFixedContent customClass={`${config.customClass}__contentpage`}>
186
+        <PopinFixedContent customClass={`${config.slug}__contentpage`}>
143 187
           <PageHtmlComponent
144 188
             mode={this.state.mode}
145 189
             onClickCloseEditMode={this.handleCloseNewVersion}
146
-            version={content.version}
147
-            text={content.text}
148
-            key={'PageHtml'}
190
+            version={content.current_revision_id}
191
+            text={content.raw_content}
192
+            key={'html-documents'}
149 193
           />
150 194
 
151 195
           <Timeline
152
-            customClass={`${config.customClass}__contentpage`}
196
+            customClass={`${config.slug}__contentpage`}
153 197
             loggedUser={loggedUser}
154 198
             timelineData={timeline}
155 199
           />

+ 69 - 15
src/container/PopupCreatePageHtml.jsx View File

@@ -1,12 +1,50 @@
1 1
 import React from 'react'
2
-import { CardPopupCreateContent } from 'tracim_lib'
2
+import {
3
+  CardPopupCreateContent,
4
+  handleFetchResult
5
+} from 'tracim_lib'
6
+import { FETCH_CONFIG } from '../helper.js'
7
+
8
+const debug = {
9
+  config: {
10
+    label: 'Text Document',
11
+    slug: 'page',
12
+    faIcon: 'file-text-o',
13
+    hexcolor: '#3f52e3',
14
+    creationLabel: 'Write a document',
15
+    domContainer: 'appContainer',
16
+    apiUrl: 'http://localhost:3001',
17
+    mockApiUrl: 'http://localhost:8071',
18
+    apiHeader: {
19
+      'Accept': 'application/json',
20
+      'Content-Type': 'application/json',
21
+      'Authorization': 'Basic ' + btoa(`${'admin@admin.admin'}:${'admin@admin.admin'}`)
22
+    }
23
+  },
24
+  loggedUser: {
25
+    id: 5,
26
+    username: 'Smoi',
27
+    firstname: 'Côme',
28
+    lastname: 'Stoilenom',
29
+    email: 'osef@algoo.fr',
30
+    avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
31
+  },
32
+  idWorkspace: 1,
33
+  idFolder: null
34
+}
3 35
 
4 36
 class PopupCreatePageHtml extends React.Component {
5 37
   constructor (props) {
6 38
     super(props)
7 39
     this.state = {
40
+      appName: 'page',
41
+      config: props.data ? props.data.config : debug.config,
42
+      loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
43
+      idWorkspace: props.data ? props.data.idWorkspace : debug.idWorkspace,
44
+      idFolder: props.data ? props.data.idFolder : debug.idFolder,
8 45
       newContentName: ''
9 46
     }
47
+    this.handleValidate = this.handleValidate.bind(this)
10 48
   }
11 49
 
12 50
   handleChangeNewContentName = e => this.setState({newContentName: e.target.value})
@@ -14,21 +52,37 @@ class PopupCreatePageHtml extends React.Component {
14 52
   handleClose = () => GLOBAL_dispatchEvent({
15 53
     type: 'hide_popupCreateContent',
16 54
     data: {
17
-      name: 'PageHtml'
55
+      name: this.state.appName
18 56
     }
19 57
   })
20 58
 
21
-  handleValidate = () => {
22
-    console.log(`fetch(/workspace/:id/content, POST, body:{name: ${this.state.newContentName})`)
23
-    // API return the id of the new content
24
-    this.handleClose()
25
-    GLOBAL_dispatchEvent({
26
-      type: 'openContentUrl',
27
-      data: {
28
-        idWorkspace: this.props.data.folder.workspace_id,
29
-        idContent: '1' // will the id returned by api
30
-      }
59
+  async handleValidate () {
60
+    const fetchSaveNewHtmlDoc = await fetch(`${this.state.config.apiUrl}/workspaces/${this.state.idWorkspace}/contents`, {
61
+      ...FETCH_CONFIG,
62
+      method: 'POST',
63
+      body: JSON.stringify({
64
+        parent_id: this.state.idFolder,
65
+        content_type_slug: this.state.config.slug,
66
+        label: this.state.newContentName
67
+      })
31 68
     })
69
+
70
+    if (fetchSaveNewHtmlDoc.status === 200) {
71
+      const jsonSaveNewHtmlDoc = await fetchSaveNewHtmlDoc.json()
72
+
73
+      console.log(jsonSaveNewHtmlDoc)
74
+
75
+      this.handleClose()
76
+
77
+      GLOBAL_dispatchEvent({
78
+        type: 'openContentUrl',
79
+        data: {
80
+          idWorkspace: jsonSaveNewHtmlDoc.workspace_id,
81
+          contentType: this.state.appName,
82
+          idContent: jsonSaveNewHtmlDoc.id
83
+        }
84
+      })
85
+    }
32 86
   }
33 87
 
34 88
   render () {
@@ -36,9 +90,9 @@ class PopupCreatePageHtml extends React.Component {
36 90
       <CardPopupCreateContent
37 91
         onClose={this.handleClose}
38 92
         onValidate={this.handleValidate}
39
-        title={this.props.data.config.label.fr} // @TODO get the lang of user
40
-        color={this.props.data.config.color}
41
-        icon={this.props.data.config.icon}
93
+        label={this.state.config.label} // @TODO get the lang of user
94
+        hexcolor={this.state.config.hexcolor}
95
+        faIcon={this.state.config.faIcon}
42 96
         contentName={this.state.newContentName}
43 97
         onChangeContentName={this.handleChangeNewContentName}
44 98
         btnValidateLabel='Valider et créer'

+ 8 - 8
src/css/index.styl View File

@@ -5,7 +5,7 @@ btncolor()
5 5
   box-shadow 0 0 1px 2px htmlColor
6 6
   color off-white
7 7
 
8
-.wsContentPageHtml
8
+.page
9 9
   width 1155px
10 10
   &__header
11 11
     background-color htmlColor
@@ -95,16 +95,16 @@ btncolor()
95 95
             background-color htmlColor
96 96
 
97 97
 .messagelistOpen
98
-  .wsContentPageHtml__contentpage__messagelist
98
+  .page__contentpage__messagelist
99 99
     flex 0
100 100
     min-height 0
101 101
 
102 102
 .received
103
-  .wsContentPageHtml__contentpage__messagelist__item__content
103
+  .page__contentpage__messagelist__item__content
104 104
     background-color htmlColor
105 105
 
106 106
 @media (min-width: max-xs) and (max-width: max-lg)
107
-  .wsContentPageHtml
107
+  .page
108 108
     &__contentpage
109 109
       display block
110 110
       overflow-Y scroll
@@ -131,19 +131,19 @@ btncolor()
131 131
               margin-left 15px
132 132
 
133 133
 @media (min-width: min-lg) and (max-width: max-lg)
134
-  .wsContentPageHtml
134
+  .page
135 135
     width 692px
136 136
     &__contentpage__texteditor__simpletext
137 137
       margin-left 15px
138 138
 
139 139
 @media (min-width: min-md) and (max-width: max-md)
140
-  .wsContentPageHtml
140
+  .page
141 141
     width 768px
142 142
     &__contentpage__texteditor__simpletext
143 143
       margin-left 25px
144 144
 
145 145
 @media (min-width: min-sm) and (max-width: max-sm)
146
-  .wsContentPageHtml
146
+  .page
147 147
     top 69px
148 148
     width 576px
149 149
     height calc(100% - 69px)
@@ -151,7 +151,7 @@ btncolor()
151 151
       margin-left 10px
152 152
 
153 153
 @media (max-width: max-xs)
154
-  .wsContentPageHtml
154
+  .page
155 155
     top 70px
156 156
     height calc(100% - 70px)
157 157
     width 100%

+ 2 - 1
src/helper.js View File

@@ -1,7 +1,8 @@
1 1
 export const FETCH_CONFIG = {
2 2
   headers: {
3 3
     'Accept': 'application/json',
4
-    'Content-Type': 'application/json'
4
+    'Content-Type': 'application/json',
5
+    'Authorization': 'Basic ' + btoa(`${'admin@admin.admin'}:${'admin@admin.admin'}`)
5 6
   }
6 7
 }
7 8
 

+ 6 - 1
src/index.dev.js View File

@@ -6,6 +6,11 @@ import PopupCreatePageHtml from './container/PopupCreatePageHtml.jsx'
6 6
 require('./css/index.styl')
7 7
 
8 8
 ReactDOM.render(
9
-  <PopupCreatePageHtml />
9
+  <PageHtml data={undefined} />
10 10
   , document.getElementById('content')
11 11
 )
12
+
13
+// ReactDOM.render(
14
+//   <PopupCreatePageHtml />
15
+//   , document.getElementById('content')
16
+// )

+ 1 - 1
webpack.config.js View File

@@ -10,7 +10,7 @@ module.exports = {
10 10
     : ['babel-polyfill', './src/index.dev.js'],
11 11
   output: {
12 12
     path: path.resolve(__dirname, 'dist'),
13
-    filename: isProduction ? 'pageHtml.app.js' : 'pageHtml.app.dev.js',
13
+    filename: isProduction ? 'html-documents.app.js' : 'html-documents.app.dev.js',
14 14
     pathinfo: !isProduction,
15 15
     library: isProduction ? 'appPageHtml' : undefined,
16 16
     libraryTarget: isProduction ? 'var' : undefined