1
1
import React from 'react' ;
2
2
import styled from 'styled-components' ;
3
- import { Text , ScrollView } from 'react-native' ;
3
+ import { Text , ScrollView , View } from 'react-native' ;
4
4
import axios from 'axios' ;
5
5
import { connect } from 'react-redux' ;
6
+ import isEmpty from 'lodash/isEmpty' ;
6
7
import SyntaxHighlighter from 'react-native-syntax-highlighter' ;
7
8
import { github as GithubStyle } from 'react-syntax-highlighter/dist/styles/hljs' ;
8
9
import Toolbar from './components/Toolbar' ;
9
- import { normalizeFont } from '../../config' ;
10
+ import { normalizeFont , colors } from '../../config' ;
10
11
import { fetchFileContent } from './../../api' ;
12
+ import LoadingView from './components/LoadingView' ;
11
13
12
14
const Container = styled . View `
15
+ flex: 1;
16
+ background: #FFFFFF;
13
17
padding-top: 20px;
14
18
` ;
15
19
@@ -20,6 +24,25 @@ const syntaxHighlighterStyle = {
20
24
} ,
21
25
} ;
22
26
27
+ const CodeContainer = styled . View `
28
+ flex: 1;
29
+ padding-vertical: 10;
30
+ padding-horizontal: 10;
31
+ margin-bottom: 10;
32
+ `
33
+
34
+ const ErrorContainer = styled . View `
35
+ flex: 1;
36
+ align-items: center;
37
+ justify-content: center;
38
+ ` ;
39
+
40
+ const ErrorText = styled . Text `
41
+ text-align: center;
42
+ color: ${ colors . greyDark } ;
43
+ font-size: ${ normalizeFont ( 14 ) } ;
44
+ `
45
+
23
46
class GistFileScreen extends React . Component {
24
47
constructor ( props ) {
25
48
super ( props ) ;
@@ -33,6 +56,7 @@ class GistFileScreen extends React.Component {
33
56
componentDidMount ( ) {
34
57
this . setState ( {
35
58
isLoading : true ,
59
+ error : ''
36
60
} )
37
61
38
62
const { navigation, accessToken } = this . props ;
@@ -42,7 +66,7 @@ class GistFileScreen extends React.Component {
42
66
. then ( response => {
43
67
this . fileContent = response ;
44
68
this . setState ( {
45
- isLoading : true ,
69
+ isLoading : false ,
46
70
} ) ;
47
71
} )
48
72
. catch ( err => {
@@ -57,24 +81,35 @@ class GistFileScreen extends React.Component {
57
81
const { navigation } = this . props ;
58
82
const fileName = navigation . state . params . fileData . filename ;
59
83
const fileType = fileName . split ( '.' ) . pop ( ) ;
84
+ const { isLoading, error } = this . state ;
60
85
61
86
return (
62
87
< Container >
63
88
< Toolbar
64
89
toolbarContent = { fileName }
65
90
onBackPress = { ( ) => navigation . goBack ( ) }
66
91
/>
67
- < ScrollView >
68
- < SyntaxHighlighter
69
- language = { fileType }
70
- CodeTag = { Text }
71
- codeTagProps = { { style : { paddingRight : 15 , paddingBottom : 0 } } }
72
- style = { syntaxHighlighterStyle }
73
- fontSize = { normalizeFont ( 12 ) }
74
- >
75
- { this . fileContent }
76
- </ SyntaxHighlighter >
77
- </ ScrollView >
92
+ { isLoading && < LoadingView animating = { true } center /> }
93
+ { ! isLoading && ! isEmpty ( error ) &&
94
+ < ErrorContainer >
95
+ < ErrorText > { error } </ ErrorText >
96
+ </ ErrorContainer >
97
+ }
98
+ { ! isLoading && ! error &&
99
+ < ScrollView >
100
+ < CodeContainer >
101
+ < SyntaxHighlighter
102
+ language = { fileType }
103
+ CodeTag = { Text }
104
+ codeTagProps = { { style : { paddingRight : 15 , paddingBottom : 0 } } }
105
+ style = { syntaxHighlighterStyle }
106
+ fontSize = { normalizeFont ( 12 ) }
107
+ >
108
+ { this . fileContent }
109
+ </ SyntaxHighlighter >
110
+ </ CodeContainer >
111
+ </ ScrollView >
112
+ }
78
113
</ Container >
79
114
)
80
115
}
0 commit comments