Commit 98a82f75 authored by Phuengton Chummuel's avatar Phuengton Chummuel

apply redux to reminder list

parent 3f2f06d2
......@@ -24,12 +24,7 @@ class AppointmentListScreen extends Component {
constructor(props) {
super(props)
this.state = {
appointmentList: [],
}
}
componentWillMount() {
this._queryDbAppointment()
};
}
render() {
......@@ -54,27 +49,6 @@ class AppointmentListScreen extends Component {
/>
<Content>
<AppointmentList />
{/* <View style={{ flex: 1, backgroundColor: 'white', height: 60 }}>
<Text style={headerStyle}>Appointment List</Text>
</View>
<View>
<List dataArray={this.state.appointmentList}
renderRow={(item) =>
<ListItem style={appointmentListStyle}>
<View style={{ flex: 1 }}>
<Text>{"Doctor: " + item.doctorName}</Text>
<Text>{"Date: " + moment(item.date).format("DD MMM YYYY")}</Text>
<Text>{"Time: " + moment(item.date).format("HH:mm")}</Text>
<Text>{"Place: " + item.place}</Text>
</View>
<TouchableOpacity onPress={() => this._deleteAppointment(item)}>
<Icon name='more' />
</TouchableOpacity>
</ListItem>
}>
</List>
</View> */}
</Content>
</Container>
)
......@@ -84,33 +58,6 @@ class AppointmentListScreen extends Component {
this.props.navigation.navigate("AddAppointment")
}
_queryDbAppointment() {
const tmpAppointment = realm.objects("Appointments")
this.setState({ appointmentList: tmpAppointment })
}
_deleteAppointment(data) {
// ToastAndroid.show("Clicked", ToastAndroid.SHORT)
Alert.alert(
'Confirm',
'Want to delete this appointment?',
[
{ text: 'CANCLE', onPress: () => this.setState({ addItemVisible: true }) },
{
text: 'OK', onPress: () => {
const tmpAppointment = realm.objects("Appointments").filtered('id == ' + data.id)
cancleNotification(data.id)
realm.write(() => {
realm.delete(tmpAppointment)
})
}
},
],
{ cancelable: false }
)
}
}
const styles = {
......
......@@ -18,7 +18,7 @@ import {
Grid
} from 'react-native-easy-grid'
import AppHeader from './../components/Header';
import ReminderList from './../components/ReminderList';
import ReminderBoxList from './../components/ReminderBoxList';
import { formatDate } from './../Utils';
import realm from './../Database';
......@@ -68,7 +68,7 @@ class HomeScreen extends Component {
<Col>
<Row>
<TouchableOpacity style={timePeriodLeftStyle} onPress={() => this._timePeriodPress(this.state.morningReminder, 1)}>
<ReminderList
<ReminderBoxList
periodText={"Morning"}
items={this.state.morningReminder}
/>
......@@ -76,7 +76,7 @@ class HomeScreen extends Component {
</Row>
<Row>
<TouchableOpacity style={timePeriodLeftStyle} onPress={() => this._timePeriodPress(this.state.afternoonReminder, 2)}>
<ReminderList
<ReminderBoxList
periodText={"Afternoon"}
items={this.state.afternoonReminder}
/>
......@@ -86,7 +86,7 @@ class HomeScreen extends Component {
<Col>
<Row>
<TouchableOpacity style={timePeriodRightStyle} onPress={() => this._timePeriodPress(this.state.eveningReminder, 3)}>
<ReminderList
<ReminderBoxList
periodText={"Evening"}
items={this.state.eveningReminder}
/>
......@@ -94,7 +94,7 @@ class HomeScreen extends Component {
</Row>
<Row>
<TouchableOpacity style={timePeriodRightStyle} onPress={() => this._timePeriodPress(this.state.nightReminder, 4)}>
<ReminderList
<ReminderBoxList
periodText={"Night"}
items={this.state.nightReminder}
/>
......
......@@ -5,11 +5,9 @@ import {
Content,
View,
Button,
Icon,
Text,
List,
ListItem
Icon
} from 'native-base';
import ReminderList from './../components/ReminderList';
import { cancleNotification } from './../Utils';
import AppHeader from './../components/Header';
import realm from './../Database/';
......@@ -20,14 +18,9 @@ class MedicationListScreen extends Component {
constructor(props) {
super(props)
this.state = {
reminderList: []
}
}
componentWillMount() {
this._queryMedicineList()
}
render() {
const { reminderListStyle, headerStyle } = styles
......@@ -50,65 +43,17 @@ class MedicationListScreen extends Component {
}
/>
<Content>
<View style={{ flex: 1, backgroundColor: 'white', height: 60 }}>
<Text style={headerStyle}>Medication List</Text>
</View>
<View>
<List dataArray={this.state.reminderList}
renderRow={(item) =>
<ListItem style={reminderListStyle}>
<View style={{ flex: 1 }}>
<Text>{item.medName}</Text>
<Text>{"Take: " + item.dose}</Text>
<Text>{'Time: ' + moment(item.reminderTime).format('HH:mm')}</Text>
</View>
<TouchableOpacity onPress={() => this._deleteMedicine(item)}>
<Icon name='more' />
</TouchableOpacity>
</ListItem>
}>
</List>
</View>
<ReminderList />
</Content>
</Container>
)
}
_queryMedicineList() {
var tmpReminder = realm.objects("Reminder")
this.setState({ reminderList: tmpReminder })
}
_addReminder() {
this.props.navigation.navigate("AddMedication")
}
_deleteMedicine(data) {
Alert.alert(
'Confirm',
'Want to delete this medicine?',
[
{ text: 'CANCLE', onPress: () => this.setState({ addItemVisible: true }) },
{
text: 'OK', onPress: () => {
const tmpMedicine = realm.objects("Reminder").filtered('id == ' + data.id)
cancleNotification(data.id)
realm.write(() => {
realm.delete(tmpMedicine)
})
}
},
],
{ cancelable: false }
)
}
}
const styles = {
reminderListStyle: {
marginLeft: 0,
......@@ -124,4 +69,6 @@ const styles = {
}
}
export default MedicationListScreen;
\ No newline at end of file
import React, { Component } from 'react';
import { DeviceEventEmitter, ToastAndroid, Alert } from 'react-native';
import { formatDate, scheduleLocalNotification } from './../Utils';
import { formatDate, scheduleLocalNotification, generateID } from './../Utils';
import { Grid, Col } from 'react-native-easy-grid';
import { SinglePickerMaterialDialog } from 'react-native-material-dialog';
import PushNotification from 'react-native-push-notification';
......@@ -24,8 +24,10 @@ import {
import DatePicker from 'react-native-datepicker';
import AppHeader from './../components/Header';
import MedForm from '../components/MedForm';
import moment from 'moment'
import realm from './../Database'
import { connect } from 'react-redux';
import { reminderCreate } from './../actions';
import moment from 'moment';
import realm from './../Database';
PushNotification.configure({
// (required) Called when a remote or local notification is opened or received
......@@ -96,7 +98,52 @@ class MedicationsScreen extends Component {
this.setState({ morningTime, afternoonTime, eveningTime, nightTime })
// ToastAndroid.show(morningTime + afternoonTime + eveningTime + nightTime, ToastAndroid.SHORT)
}
_addReminder() {
let reminder = ""
if (this.state.morning === true) {
var time = this.state.morningTime.split(':')
reminder = this._setNotification(time[0], time[1], 1)
this.props.reminderCreate(reminder)
}
if (this.state.afternoon === true) {
var time = this.state.afternoonTime.split(':')
reminder = this._setNotification(time[0], time[1], 2)
this.props.reminderCreate(reminder)
}
if (this.state.evening === true) {
var time = this.state.eveningTime.split(':')
reminder = this._setNotification(time[0], time[1], 3)
this.props.reminderCreate(reminder)
}
if (this.state.night === true) {
var time = this.state.nightTime.split(':')
reminder = this._setNotification(time[0], time[1], 4)
this.props.reminderCreate(reminder)
}
this.props.navigation.goBack(null)
}
_setNotification(hour, minute, period) {
var id = generateID()
var reminderDate = moment().hour(hour).minute(minute).second(0).toDate()
var beforeMeal = this.state.beforeMeal ? "\tก่อนอาหาร" : "\tหลังอาหาร";
// var msg = "แจ้งเตือนกินยา\n" + "เวลา: " + hour + ":" + minute + "\nยา: " + this.state.text + beforeMeal;
// scheduleLocalNotification(msg, reminderDate, id)
return {
id: id,
medName: this.state.text,
reminderTime: reminderDate,
meal: beforeMeal.trim(),
period: period,
dose: this.state.dose
}
}
......@@ -189,19 +236,7 @@ class MedicationsScreen extends Component {
</MedForm>
<MedForm titleText={"Schedule"}>
<Text style={timeStyle}>Start date : {formatDate(new Date(Date.now()))}</Text>
{/* <DatePicker
style={timeStyle}
date={new Date(this.state.date).toISOString().split('T')[0]}
mode="date"
placeholder="placeholder"
format="YYYY-MM-DD"
minDate={this.state.minDate}
confirmBtnText="Confirm"
cancelBtnText="Cancel"
onDateChange={(date) => { this.setState({ date: date }); }}
/> */}
</MedForm>
<MedForm titleText={"Dose"}>
......@@ -232,7 +267,7 @@ class MedicationsScreen extends Component {
</Button>
</Col>
<Col>
<Button full style={btnAddNoti} onPress={() => this._onAddNotificationPressed()}>
<Button full style={btnAddNoti} onPress={() => this._addReminder()}>
<Text style={{ color: "#1686C4" }}>Add</Text>
</Button>
</Col>
......@@ -259,75 +294,6 @@ class MedicationsScreen extends Component {
}
}
_onAddNotificationPressed() {
if (this.state.text && this.state.morning || this.state.afternoon || this.state.evening || this.state.night) {
if (this.state.morning === true) {
var time = this.state.morningTime.split(':')
this._setNotification(time[0], time[1], 1)
}
if (this.state.afternoon === true) {
var time = this.state.afternoonTime.split(':')
this._setNotification(time[0], time[1], 2)
}
if (this.state.evening === true) {
var time = this.state.eveningTime.split(':')
this._setNotification(time[0], time[1], 3)
}
if (this.state.night === true) {
var time = this.state.nightTime.split(':')
this._setNotification(time[0], time[1], 4)
}
Alert.alert(
'Successful',
'',
[
// { text: 'OK', onPress: () => ToastAndroid.show("Successful", ToastAndroid.SHORT) },
{ text: 'OK', onPress: () => { } },
],
{ cancelable: false }
)
} else {
Alert.alert(
'Please enter medine name / Select reminder time',
'',
[
// { text: 'OK', onPress: () => ToastAndroid.show("Please enter medine name / Select reminder time", ToastAndroid.SHORT) },
{ text: 'OK', onPress: () => { } },
],
{ cancelable: false }
)
}
}
_setNotification(hour, minute, period) {
var id = Math.random() * 10000
var reminderDate = moment().hour(hour).minute(minute).second(0).toDate()
var beforeMeal = this.state.beforeMeal ? "\tก่อนอาหาร" : "\tหลังอาหาร";
var msg = "แจ้งเตือนกินยา\n" + "เวลา: " + hour + ":" + minute + "\nยา: " + this.state.text + beforeMeal;
scheduleLocalNotification(msg, reminderDate, id)
realm.write(() => {
realm.create('Reminder', {
id: id,
medName: this.state.text,
reminderTime: reminderDate,
meal: beforeMeal.trim(),
period: period,
dose: this.state.dose,
timeStamp: moment().toDate()
})
})
}
_onResetPressed() {
realm.write(() => {
......@@ -377,4 +343,4 @@ const styles = {
}
}
export default MedicationsScreen;
\ No newline at end of file
export default connect(null, { reminderCreate })(MedicationsScreen);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment