This repository was archived by the owner on Dec 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDocsApp.js
More file actions
182 lines (149 loc) · 3.71 KB
/
DocsApp.js
File metadata and controls
182 lines (149 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text
} from 'react-native';
import PouchDB from 'pouchdb-react-native';
import _ from 'lodash';
import DocForm from './DocForm';
import Docs from './Docs';
const localDB = new PouchDB('docs');
const remoteDB = new PouchDB('http://localhost:5984/docs');
const syncStates = ['change', 'paused', 'active', 'denied', 'complete', 'error'];
class DocsApp extends Component {
constructor(props) {
super(props);
this.defaultImgId = Date.now();
this.imgId = this.defaultImgId;
this.state = {
docs: [],
syncStatus: ''
};
}
addDoc(newDoc) {
if (!_.find(this.state.docs, '_id', newDoc._id)) {
this.setState({
docs: this.state.docs.concat(newDoc)
});
}
}
removeDoc(oldDoc) {
this.setState({
docs: this.state.docs.filter(doc => doc._id !== oldDoc._id)
});
}
componentDidMount() {
localDB.allDocs({include_docs: true})
.then(results => {
this.setState({
docs: results.rows.map(row => row.doc)
});
}).catch(err => console.log.bind(console, '[Fetch all]'));
const sync = localDB.sync(remoteDB, {
live: true,
retry: true
});
syncStates.forEach(state => {
sync.on(state, setCurrentState.bind(this, state));
function setCurrentState(state) {
console.log('[Sync:' + state + ']');
this.setState({
syncStatus: state
});
}
});
localDB.changes({
live: true,
include_docs: true
}).on('change', this.handleChange.bind(this))
.on('complete', console.log.bind(console, '[Change:Complete]'))
.on('error', console.log.bind(console, '[Change:Error]'))
}
onDocSubmit(doc) {
var imageUrl = 'http://unsplash.it/200?random&t=' + this.imgId;
localDB.put({_id: doc, content: doc, imageUrl: imageUrl})
.catch(console.log.bind(console, 'Error inserting'));
this.imgId++;
if (this.imgId == this.defaultImgId + 3) {
this.imgId = this.defaultImgId;
}
}
onDocRemove(oldDoc) {
localDB.remove(oldDoc)
.catch(console.log.bind(console, 'Error removing'));
}
handleChange(change) {
console.log('[Change:Change]', change);
var doc = change.doc;
if (!doc) {
return;
}
if (doc._deleted) {
this.removeDoc(doc);
} else {
this.addDoc(doc);
}
}
render() {
return (
<View style={styles.container}>
<View style={styles.containerForm}>
<View style={styles.containerStatus}>
<Text style={styles.statusText}>{this.state.syncStatus}</Text>
</View>
<DocForm
onDocSubmit={this.onDocSubmit.bind(this)}
/>
</View>
<View style={styles.separator} />
<View style={styles.containerList}>
<Docs
docs={this.state.docs}
onDocRemove={this.onDocRemove.bind(this)}
/>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
//Status bar
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: '#EEEEEE',
},
//containerForm
containerForm: {
paddingLeft: 10,
paddingRight: 10,
paddingBottom: 10,
marginTop: 40,
backgroundColor: '#EEEEEE',
},
//containerStatus
containerStatus: {
backgroundColor: 'red',
height: 10,
marginBottom: 20,
borderRadius: 20,
},
//Status Text
statusText: {
color: 'white',
flexDirection: 'row',
textAlign: 'center',
},
//containerList
containerList: {
paddingLeft: 10,
paddingRight: 10,
},
//Separator - Add form/List
separator: {
height: 0,
backgroundColor: 'aliceblue',
}
});
export default DocsApp;