Browse Source

added fetch from tracim mock api + fix build

Skylsmoi 6 years ago
parent
commit
9f85fca26e
5 changed files with 256 additions and 43 deletions
  1. 12 6
      dist/index.html
  2. 75 31
      src/container/PageHtml.jsx
  3. 160 0
      src/css/pageHtml.styl
  4. 6 5
      src/index.js
  5. 3 1
      webpack.config.js

+ 12 - 6
dist/index.html View File

@@ -20,10 +20,12 @@
20 20
   <script src='./pageHtml.app.dev.js'></script>
21 21
 
22 22
   <script type='text/javascript'>
23
-    GLOBAL_renderApp = appName => {
24
-      if (appName === 'PageHtml') {
25
-        appPageHtml.renderApp('appContainer')
26
-        console.log('app pagehtml rendered')
23
+    GLOBAL_renderApp = app => {
24
+      switch (app.appData.name) {
25
+        case 'PageHtml':
26
+          appPageHtml.renderApp('appContainer'); break
27
+        case 'Thread':
28
+          appThread.renderApp('appContainer'); break
27 29
       }
28 30
     }
29 31
 
@@ -33,8 +35,12 @@
33 35
     }
34 36
 
35 37
     GLOBAL_unmountApp = () => {
36
-      console.log('btn close clicked')
37
-      appPageHtml.hideApp('appContainer')
38
+      switch (appName) {
39
+        case 'PageHtml':
40
+          appPageHtml.hideApp('appContainer'); break
41
+        case 'Thread':
42
+          appThread.hideApp('appContainer'); break
43
+      }
38 44
     }
39 45
 
40 46
     // only usefull if app doesn't handle fileContent himself

+ 75 - 31
src/container/PageHtml.jsx View File

@@ -7,67 +7,111 @@ import {
7 7
   PopinFixedContent,
8 8
   Timeline
9 9
 } from 'tracim_lib'
10
+import { FETCH_CONFIG } from '../helper.js'
11
+
12
+const debug = {
13
+  workspace: {
14
+    id: '-1',
15
+    title: 'Test debugg workspace'
16
+  },
17
+  content: {
18
+    id: '-1',
19
+    type: 'pageHtml',
20
+    title: 'Test debugg pageHtml',
21
+    status: 'validated',
22
+    version: '-1',
23
+    text: 'This is the default pageHtml content for debug purpose'
24
+  },
25
+  appConfig: {
26
+    name: 'PageHtml',
27
+    customClass: 'wsFilePageHtml',
28
+    icon: 'fa fa-file-word-o',
29
+    apiUrl: 'http://localhost:3001'
30
+  }
31
+}
10 32
 
11 33
 class pageHtml extends React.Component {
12 34
   constructor (props) {
13 35
     super(props)
14 36
     this.state = {
15 37
       appName: 'PageHtml',
16
-      data: props.data
17
-        ? props.data
18
-        : { // for debugg purpose
19
-          file: {
20
-            version: '3',
21
-            text: 'Bonjour ?'
22
-          },
23
-          appData: {
24
-            name: 'PageHtml',
25
-            componentLeft: 'PageHtml',
26
-            componentRight: 'Timeline',
27
-            customClass: 'wsFilePageHtml',
28
-            icon: 'fa fa-file-word-o'
29
-          }
30
-        }
38
+      isVisible: true,
39
+      workspace: props.app ? props.app.workspace : debug.workspace,
40
+      content: props.app ? props.app.content : debug.content,
41
+      appConfig: props.app ? props.app.appConfig : debug.appConfig
31 42
     }
32 43
 
33
-    document.addEventListener('appCustomEvent', this.customEventReducer, false)
44
+    document.addEventListener('appCustomEvent', this.customEventReducer)
34 45
   }
35 46
 
36
-  customEventReducer = ({detail}) => {
37
-    switch (detail.type) {
38
-      case 'PageHtml_showMsg': // unused for now, for testing purpose
39
-        this.setState({inputText: detail.content})
47
+  async componentDidMount () {
48
+    const { workspace, content, appConfig } = this.state
49
+    if (content.id === '-1') return
50
+
51
+    const fetchResult = await fetch(`${appConfig.apiUrl}/workspace/${workspace.id}/content/${content.id}`, {
52
+      ...FETCH_CONFIG,
53
+      method: 'GET'
54
+    })
55
+
56
+    fetchResult.json = await (async () => {
57
+      switch (fetchResult.status) {
58
+        case 200:
59
+        case 304:
60
+          return fetchResult.json()
61
+        case 204:
62
+        case 400:
63
+        case 404:
64
+        case 409:
65
+        case 500:
66
+        case 501:
67
+        case 502:
68
+        case 503:
69
+        case 504:
70
+          return `Error: ${fetchResult.status}` // @TODO : handle errors from api result
71
+      }
72
+    })()
73
+
74
+    this.setState({content: fetchResult.json})
75
+  }
76
+
77
+  customEventReducer = action => { // action: { type: '', data: {} }
78
+    switch (action.type) {
79
+      case 'PageHtml_dummyTest':
80
+        this.setState({dummy: true})
40 81
         break
41 82
     }
42 83
   }
43 84
 
44 85
   handleClickBtnCloseApp = () => {
45
-    GLOBAL_unmountApp(this.state.appName)
86
+    // GLOBAL_unmountApp(this.state.appName)
87
+    this.setState({ isVisible: false })
46 88
   }
47 89
 
48 90
   render () {
49
-    const { file, appData } = this.state.data
91
+    const { isVisible, content, appConfig } = this.state
92
+
93
+    if (!isVisible) return null
50 94
 
51 95
     return (
52
-      <PopinFixed customClass={`${appData.customClass}`}>
96
+      <PopinFixed customClass={`${appConfig.customClass}`}>
53 97
         <PopinFixedHeader
54
-          customClass={`${appData.customClass}`}
55
-          icon={appData.icon}
56
-          name={file.title}
98
+          customClass={`${appConfig.customClass}`}
99
+          icon={appConfig.icon}
100
+          name={content.title}
57 101
           onClickCloseBtn={this.handleClickBtnCloseApp}
58 102
         />
59 103
 
60
-        <PopinFixedOption customClass={`${appData.customClass}`} />
104
+        <PopinFixedOption customClass={`${appConfig.customClass}`} />
61 105
 
62
-        <PopinFixedContent customClass={`${appData.customClass}__contentpage`}>
106
+        <PopinFixedContent customClass={`${appConfig.customClass}__contentpage`}>
63 107
           <PageHtmlComponent
64
-            version={file.version}
65
-            text={file.text}
108
+            version={content.version}
109
+            text={content.text}
66 110
             key={'PageHtml'}
67 111
           />
68 112
 
69 113
           <Timeline
70
-            customClass={`${appData.customClass}__contentpage`}
114
+            customClass={`${appConfig.customClass}__contentpage`}
71 115
             key={'pageHtml__timeline'}
72 116
           />
73 117
         </PopinFixedContent>

+ 160 - 0
src/css/pageHtml.styl View File

@@ -0,0 +1,160 @@
1
+
2
+.wsFilePageHtml
3
+  &__header
4
+    background-color htmlColor
5
+    &__editionmode
6
+      display none
7
+    &__icon
8
+      .fa-file-word-o
9
+        color white
10
+  &__contentpage
11
+    &__textnote
12
+      margin 10px
13
+      border-radius 10px
14
+      padding 25px
15
+      height 100%
16
+      background-color off-white
17
+      &__latestversion
18
+        text-align right
19
+        opacity 0.7
20
+      &__text
21
+        font-size 14px
22
+      &__edition
23
+        display none
24
+    &__messagelist
25
+      min-height 300px
26
+      &__item
27
+        &__content
28
+          color darkGrey
29
+      &__version
30
+        margin-top 40px
31
+        background-color grey-hover
32
+        &__btn
33
+          padding 5px 25px
34
+          border-radius 5px
35
+          width 145px
36
+          color white
37
+          font-size 17px
38
+          background-color htmlColor
39
+          & > i
40
+            margin-right 10px
41
+            color darkGrey
42
+            font-size 22px
43
+          &:hover
44
+            background-color darkHtmlColor
45
+          &:focus
46
+            background-color darkHtmlColor
47
+        &__date
48
+          color fontColor
49
+          font-size 17px
50
+    &__texteditor
51
+      &__simpletext
52
+        &__input
53
+          &:focus
54
+            color htmlColor
55
+            border-color htmlColor
56
+      &__submit
57
+        &__btn
58
+          border-color htmlColor
59
+          background-color htmlColor
60
+          color white
61
+          &:hover
62
+            border-color htmlColor
63
+            background-color htmlColor
64
+          &:focus
65
+            border-color htmlColor
66
+            background-color htmlColor
67
+
68
+.messagelistOpen
69
+  .wsFilePageHtml__contentpage__messagelist
70
+    flex 0
71
+    min-height 0
72
+
73
+.received
74
+  .wsFilePageHtml__contentpage__messagelist__item__content
75
+    background-color htmlColor
76
+
77
+@media (min-width: max-xs) and (max-width: max-lg)
78
+  .wsFilePageHtml
79
+    &__contentpage
80
+      display block
81
+      overflow-Y scroll
82
+      &__textnote
83
+        margin-right 10px
84
+        padding 10px 20px
85
+        width auto
86
+        overflow-Y hidden
87
+        &__latestversion
88
+          padding-top 10px
89
+      &__wrapper
90
+        width auto
91
+      &__texteditor
92
+        &__simpletext
93
+          display inline-flex
94
+          width 60%
95
+        &__submit
96
+          display inline-flex
97
+          margin 10px 0
98
+          &__btn
99
+            display flex
100
+            margin-left 35px
101
+            &__icon
102
+              margin-left 15px
103
+
104
+@media (min-width: min-lg) and (max-width: max-lg)
105
+  .wsFilePageHtml
106
+    width 692px
107
+    &__contentpage__texteditor__simpletext
108
+      margin-left 15px
109
+
110
+@media (min-width: min-md) and (max-width: max-md)
111
+  .wsFilePageHtml
112
+    width 768px
113
+    &__contentpage__texteditor__simpletext
114
+      margin-left 25px
115
+
116
+@media (min-width: min-sm) and (max-width: max-sm)
117
+  .wsFilePageHtml
118
+    top 69px
119
+    width 576px
120
+    height calc(100% - 69px)
121
+    &__contentpage__texteditor__simpletext
122
+      margin-left 10px
123
+
124
+@media (max-width: max-xs)
125
+  .wsFilePageHtml
126
+    top 70px
127
+    height calc(100% - 70px)
128
+    width 100%
129
+    box-shadow none
130
+    &__contentpage
131
+      display block
132
+      overflow-Y scroll
133
+      &__textnote
134
+        margin-right 10px
135
+        width auto
136
+        overflow-Y hidden
137
+        font-size 15px
138
+      &__wrapper
139
+        width auto
140
+      &__messagelist
141
+        &__item
142
+          padding 0 35px 20px 30px
143
+        &__version
144
+          &__btn
145
+            font-size 17px
146
+            vertical-align middle
147
+          &__dateversion
148
+            font-size 15px
149
+      &__texteditor
150
+        &__simpletext
151
+          margin 10px 0
152
+          display flex
153
+          width 95%
154
+          margin-left 10px
155
+        &__submit
156
+          &__btn
157
+            display flex
158
+            margin 0 auto
159
+            &__icon
160
+              margin-left 10px

+ 6 - 5
src/index.js View File

@@ -4,14 +4,15 @@ import PageHtml from './container/PageHtml.jsx'
4 4
 
5 5
 require('./css/index.styl')
6 6
 
7
-const appInterface = {
8
-  renderApp: (domId, data) => {
7
+const appInterface = class appInterface {
8
+  renderApp = (domId, app) => {
9 9
     return ReactDOM.render(
10
-      <PageHtml file={data} />
10
+      <PageHtml app={app} />
11 11
       , document.getElementById(domId)
12 12
     )
13
-  },
14
-  hideApp: domId => {
13
+  }
14
+
15
+  unountApp = domId => {
15 16
     return ReactDOM.unmountComponentAtNode(document.getElementById(domId)) // returns bool
16 17
   }
17 18
 }

+ 3 - 1
webpack.config.js View File

@@ -5,7 +5,9 @@ const isProduction = process.env.NODE_ENV === 'production'
5 5
 console.log('isProduction : ', isProduction)
6 6
 
7 7
 module.exports = {
8
-  entry: isProduction ? './src/index.js' : './src/index.dev.js',
8
+  entry: isProduction
9
+    ? './src/index.js' // only one instance of babel-polyfill is allowed
10
+    : ['babel-polyfill', './src/index.dev.js'],
9 11
   output: {
10 12
     path: path.resolve(__dirname, 'dist'),
11 13
     filename: isProduction ? 'pageHtml.app.js' : 'pageHtml.app.dev.js',