-
Notifications
You must be signed in to change notification settings - Fork 4
/
RecordScreen.js
195 lines (178 loc) · 5.01 KB
/
RecordScreen.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
import React from 'react';
import { Button, View, ScrollView, StyleSheet, Alert } from 'react-native';
import t from 'tcomb-form-native';
import sync from "./sync"
var Form = t.form.Form;
class RecordScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
formType: t.struct({}),
formOptions: { fields: {} },
formValue: {},
buttonDisabled: true
};
}
async componentDidMount() {
try {
let params = this.props.navigation.state.params;
let syncTable = params.syncTable;
let item = params.item;
let types = {};
let options = {};
let formValue = {};
for (let column of syncTable.columnsPkRegLob) {
let name = column.columnName;
let type = column.deviceColDef.type;
let nullable = column.nullable;
let label = name + " (" + type + ")";
let editable = true;
// Realm supports the following basic types: bool, int, float, double, string, data, and date.
// tcomb: Boolean, String, Number, Date
if (type == "bool") {
types[name] = t.Boolean;
} else if (type == "int" || type == "float" || type == "double") {
types[name] = t.Number;
} else if (type == "date") {
types[name] = t.Date;
} else if (type == "string") {
types[name] = t.String;
} else {
types[name] = t.String;
editable = false;
}
if (nullable) {
types[name] = t.maybe(types[name]);
}
options[name] = {
label: label,
editable: editable,
autoCorrect: false,
autoCapitalize: "none"
}
if (editable) {
formValue[name] = item[name];
}
}
this.setState({
formType: t.struct(types),
formOptions: { fields: options },
formValue
});
} catch (error) {
Alert.alert("Error", "" + error);
}
}
onFormChange(value) {
this.setState({
formValue: value,
buttonDisabled: false
});
}
onPressDelete() {
Alert.alert(
"Confirmation",
"Are you sure to delete the record?",
[
{ text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel' },
{
text: 'OK',
onPress: () => {
console.log('OK Pressed');
let params = this.props.navigation.state.params;
let realm = params.realm;
let item = params.item;
realm.write(() => {
realm.delete(item);
});
this.props.navigation.goBack();
params.refresh();// force update parent
sync.sync();
}
},
],
{ cancelable: false }
);
}
async onPressSave() {
// value will be null if validation didn't pass. this.state.formValue may not be null
const value = this.formRef.getValue();
this.setState({
buttonDisabled: true
});
if (value) {
let params = this.props.navigation.state.params;
let realm = params.realm;
let syncTable = params.syncTable;
try {
realm.write(() => {
let itemToSave = {};
for (let column of syncTable.columnsPkRegLob) {
let name = column.columnName;
itemToSave[name] = value[name];
}
realm.create(syncTable.name, itemToSave, true);
});
Alert.alert("Success", "Record saved.", [{
text: "OK",
onPress: ()=>{
this.props.navigation.goBack();
params.refresh();// force update parent
sync.sync();
}
}]);
} catch (error) {
Alert.alert("Error", "" + error);
}
} else {
Alert.alert("Error", "Correct the form errors and try again.");
}
}
render() {
return (
<ScrollView style={{
flex: 1
}}>
<View style={styles.container}>
<Form
ref={(formRef) => this.formRef = formRef}
type={this.state.formType}
options={this.state.formOptions}
value={this.state.formValue}
onChange={(value) => this.onFormChange(value)}
/>
<View style={{
flexDirection: 'row',
flex: 1,
justifyContent: 'space-between',
alignItems: 'flex-start'
}}>
<View style={{ flex: 1 }}>
<Button
title="Delete"
onPress={() => this.onPressDelete()}
color="red"
/>
</View>
<View style={{ flex: 1 }}>
<Button
disabled={this.state.buttonDisabled}
title="Save"
onPress={() => this.onPressSave()}
/>
</View>
</View>
</View>
</ScrollView>
);
}
}
let styles = StyleSheet.create({
container: {
justifyContent: 'center',
marginTop: 0,
padding: 20,
backgroundColor: '#ffffff',
}
});
export default RecordScreen;