Explorar el Código

integration with real api

Skylsmoi hace 5 años
padre
commit
4e4b456c15

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 40 - 0
dist/html-documents.app.js


+ 1 - 1
dist/index.html Ver fichero

17
 
17
 
18
   <div id='content'></div>
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
   <script type='text/javascript'>
22
   <script type='text/javascript'>
23
     GLOBAL_renderApp = app => {
23
     GLOBAL_renderApp = app => {

+ 1 - 0
package.json Ver fichero

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

+ 93 - 49
src/container/PageHtml.jsx Ver fichero

14
 
14
 
15
 const debug = {
15
 const debug = {
16
   config: {
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
     domContainer: 'appContainer',
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
   loggedUser: {
31
   loggedUser: {
32
     id: 5,
32
     id: 5,
33
     username: 'Smoi',
33
     username: 'Smoi',
37
     avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
37
     avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
38
   },
38
   },
39
   content: {
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
   timeline: timelineDebugData
66
   timeline: timelineDebugData
53
 }
67
 }
56
   constructor (props) {
70
   constructor (props) {
57
     super(props)
71
     super(props)
58
     this.state = {
72
     this.state = {
59
-      appName: 'PageHtml',
73
+      appName: 'page',
60
       isVisible: true,
74
       isVisible: true,
61
       config: props.data ? props.data.config : debug.config,
75
       config: props.data ? props.data.config : debug.config,
62
       loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
76
       loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
70
 
84
 
71
   customEventReducer = ({ detail: { type, data } }) => { // action: { type: '', data: {} }
85
   customEventReducer = ({ detail: { type, data } }) => { // action: { type: '', data: {} }
72
     switch (type) {
86
     switch (type) {
73
-      case 'PageHtml_showApp':
87
+      case 'page_showApp':
74
         this.setState({isVisible: true})
88
         this.setState({isVisible: true})
75
         break
89
         break
76
-      case 'PageHtml_hideApp':
90
+      case 'page_hideApp':
77
         this.setState({isVisible: false})
91
         this.setState({isVisible: false})
78
         break
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
     const { content, config } = this.state
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
       ...FETCH_CONFIG,
120
       ...FETCH_CONFIG,
88
       method: 'GET'
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
       ...FETCH_CONFIG,
124
       ...FETCH_CONFIG,
92
       method: 'GET'
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
   handleClickBtnCloseApp = () => {
150
   handleClickBtnCloseApp = () => {
124
     if (!isVisible) return null
168
     if (!isVisible) return null
125
 
169
 
126
     return (
170
     return (
127
-      <PopinFixed customClass={`${config.customClass}`}>
171
+      <PopinFixed customClass={`${config.slug}`}>
128
         <PopinFixedHeader
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
           onClickCloseBtn={this.handleClickBtnCloseApp}
176
           onClickCloseBtn={this.handleClickBtnCloseApp}
133
           onChangeTitle={this.handleChangeTitle}
177
           onChangeTitle={this.handleChangeTitle}
134
         />
178
         />
135
 
179
 
136
         <PopinFixedOption
180
         <PopinFixedOption
137
-          customClass={`${config.customClass}`}
181
+          customClass={`${config.slug}`}
138
           onClickNewVersionBtn={this.handleClickNewVersion}
182
           onClickNewVersionBtn={this.handleClickNewVersion}
139
           i18n={i18n}
183
           i18n={i18n}
140
         />
184
         />
141
 
185
 
142
-        <PopinFixedContent customClass={`${config.customClass}__contentpage`}>
186
+        <PopinFixedContent customClass={`${config.slug}__contentpage`}>
143
           <PageHtmlComponent
187
           <PageHtmlComponent
144
             mode={this.state.mode}
188
             mode={this.state.mode}
145
             onClickCloseEditMode={this.handleCloseNewVersion}
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
           <Timeline
195
           <Timeline
152
-            customClass={`${config.customClass}__contentpage`}
196
+            customClass={`${config.slug}__contentpage`}
153
             loggedUser={loggedUser}
197
             loggedUser={loggedUser}
154
             timelineData={timeline}
198
             timelineData={timeline}
155
           />
199
           />

+ 69 - 15
src/container/PopupCreatePageHtml.jsx Ver fichero

1
 import React from 'react'
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
 class PopupCreatePageHtml extends React.Component {
36
 class PopupCreatePageHtml extends React.Component {
5
   constructor (props) {
37
   constructor (props) {
6
     super(props)
38
     super(props)
7
     this.state = {
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
       newContentName: ''
45
       newContentName: ''
9
     }
46
     }
47
+    this.handleValidate = this.handleValidate.bind(this)
10
   }
48
   }
11
 
49
 
12
   handleChangeNewContentName = e => this.setState({newContentName: e.target.value})
50
   handleChangeNewContentName = e => this.setState({newContentName: e.target.value})
14
   handleClose = () => GLOBAL_dispatchEvent({
52
   handleClose = () => GLOBAL_dispatchEvent({
15
     type: 'hide_popupCreateContent',
53
     type: 'hide_popupCreateContent',
16
     data: {
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
   render () {
88
   render () {
36
       <CardPopupCreateContent
90
       <CardPopupCreateContent
37
         onClose={this.handleClose}
91
         onClose={this.handleClose}
38
         onValidate={this.handleValidate}
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
         contentName={this.state.newContentName}
96
         contentName={this.state.newContentName}
43
         onChangeContentName={this.handleChangeNewContentName}
97
         onChangeContentName={this.handleChangeNewContentName}
44
         btnValidateLabel='Valider et créer'
98
         btnValidateLabel='Valider et créer'

+ 8 - 8
src/css/index.styl Ver fichero

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

+ 2 - 1
src/helper.js Ver fichero

1
 export const FETCH_CONFIG = {
1
 export const FETCH_CONFIG = {
2
   headers: {
2
   headers: {
3
     'Accept': 'application/json',
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 Ver fichero

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

+ 1 - 1
webpack.config.js Ver fichero

10
     : ['babel-polyfill', './src/index.dev.js'],
10
     : ['babel-polyfill', './src/index.dev.js'],
11
   output: {
11
   output: {
12
     path: path.resolve(__dirname, 'dist'),
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
     pathinfo: !isProduction,
14
     pathinfo: !isProduction,
15
     library: isProduction ? 'appPageHtml' : undefined,
15
     library: isProduction ? 'appPageHtml' : undefined,
16
     libraryTarget: isProduction ? 'var' : undefined
16
     libraryTarget: isProduction ? 'var' : undefined