/
ViewEditBook.js
210 lines (197 loc) · 6.91 KB
/
ViewEditBook.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React from 'react';
import { Alert, TextInput, Dimensions, Image, StyleSheet, Text, View, Button } from 'react-native';
import { openDatabase } from 'react-native-sqlite-storage';
import DatePicker from 'react-native-datepicker'
import { MillisToDisplayDate, DisplayDateToMillis } from '../util/DateUtil';
const styles = StyleSheet.create({
main: {
backgroundColor: '#e8eaed',
flex: 1.8,
},
container: {
minHeight: Dimensions.get('window').height / 3,
backgroundColor: '#ffffff',
flex: 0.5,
margin: 5,
},
touchable: {
minHeight: Dimensions.get('window').height / 3,
flex: 0.5,
},
flatlistContainer: {
justifyContent: 'space-between',
padding: 10,
marginBottom: 40,
},
editBookTitle: {
fontSize: 17,
color: '#000000',
margin: 10,
},
author: {
fontSize: 16,
color: '#333333',
margin: 10,
},
dates: {
textAlign: 'left',
color: '#333333',
marginLeft: 10,
},
addButton: {
flex: 0.1,
textAlign: 'center',
},
sortFilter: {
textAlign: 'center',
margin: 10,
},
});
var db = openDatabase({ name: 'BookDatabase.db' });
export default class App extends React.Component {
static navigationOptions = {
title: 'Edit Book',
//Sets Header text of Status Bar
headerStyle: {
backgroundColor: '#92e8e5',
//Sets Header color
},
headerTintColor: '#fff',
//Sets Header text color
headerTitleStyle: {
fontWeight: 'bold',
//Sets Header text style
},
};
constructor(props) {
super(props);
this.state = {
book_id: this.props.navigation.getParam('book_id'),
book_title: this.props.navigation.getParam('book_title'),
book_author: this.props.navigation.getParam('book_author'),
book_image_url: this.props.navigation.getParam('book_image_url'),
date_started_millis: this.props.navigation.getParam('date_started_millis'),
date_ended_millis: this.props.navigation.getParam('date_ended_millis'),
date_started_display: MillisToDisplayDate(this.props.navigation.getParam('date_started_millis')),
date_ended_display: MillisToDisplayDate(this.props.navigation.getParam('date_ended_millis')),
}
}
render() {
return (<View>
<Image source={{ uri: this.state.book_image_url }}
style={{ width: 65, height: 100 }} />
<TextInput
style={styles.editBookTitle}
onChangeText={book_title => this.setState({ book_title })}
defaultValue={this.state.book_title}
selectTextOnFocus={true}
/>
<TextInput
style={styles.author}
onChangeText={(book_author) => this.setState({ book_author })}
defaultValue={this.state.book_author}
selectTextOnFocus={true}
/>
<DatePicker
style={{ width: 200 }}
date={this.state.date_started_display}
mode="date"
placeholder="select date"
format="MM-DD-YYYY"
minDate="01-01-1970"
maxDate="01-01-2170"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateIcon: {
position: 'absolute',
left: 0,
top: 4,
marginLeft: 0
},
dateInput: {
marginLeft: 36
}
}}
onDateChange={(date_started_display) => { this.setState({ date_started_display: date_started_display }) }}
/>
<DatePicker
style={{ width: 200 }}
date={this.state.date_ended_display}
mode="date"
placeholder="select date"
format="MM-DD-YYYY"
minDate="01-01-1970"
maxDate="01-01-2170"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateIcon: {
position: 'absolute',
left: 0,
top: 4,
marginLeft: 0
},
dateInput: {
marginLeft: 36
}
}}
onDateChange={(date_ended_display) => { this.setState({ date_ended_display: date_ended_display }) }}
/>
<Button
style={styles.addButton}
title="save new"
onPress={() => this.updateBook()}
/>
<Text>
Delete id: {this.state.book_id}?
</Text>
<Button
style={styles.addButton}
title="delete"
onPress={() => this.onPressDeleteButton()}
/>
</View>
);
}
onPressDeleteButton() {
db.transaction(tx => {
tx.executeSql(
'DELETE FROM table_books where book_id=?',
[this.state.book_id],
(tx, results) => {
console.log('Results', results.rowsAffected);
if (results.rowsAffected > 0) {
//Alert.alert('Success');
} else {
alert('Please insert a valid User Id');
}
}
);
});
this.props.navigation.goBack();
}
updateBook() {
startDateMillis = DisplayDateToMillis(this.state.date_started_display)
endDateMillis = DisplayDateToMillis(this.state.date_ended_display)
console.log("start date: " + startDateMillis)
console.log("end date: " + endDateMillis)
db.transaction((tx) => {
tx.executeSql(
'UPDATE table_books set book_title=?, book_author=?, book_image_url=?, date_started_millis=?, date_ended_millis=?, read_category=? where book_id=?',
[this.state.book_title, this.state.book_author, this.state.book_image_url, startDateMillis, endDateMillis, this.state.read_category, this.state.book_id],
(tx, results) => {
console.log('Results', results.rowsAffected);
if (results.rowsAffected > 0) {
Alert.alert('Updated');
console.log("success");
} else {
alert('Updation Failed');
console.log("failure");
}
}
);
});
this.props.navigation.goBack();
}
}