2017-05-12 22:23:54 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { View, Button, TextInput } from 'react-native';
|
|
|
|
import { connect } from 'react-redux'
|
2017-06-24 20:06:28 +02:00
|
|
|
import { Log } from 'lib/log.js'
|
|
|
|
import { Note } from 'lib/models/note.js'
|
|
|
|
import { ScreenHeader } from 'lib/components/screen-header.js';
|
|
|
|
import { Checkbox } from 'lib/components/checkbox.js'
|
|
|
|
import { NoteFolderService } from 'lib/services/note-folder-service.js';
|
|
|
|
import { _ } from 'lib/locale.js';
|
2017-05-12 22:23:54 +02:00
|
|
|
|
|
|
|
class NoteScreenComponent extends React.Component {
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
static navigationOptions(options) {
|
2017-05-16 21:57:09 +02:00
|
|
|
return { header: null };
|
|
|
|
}
|
2017-05-12 22:23:54 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2017-05-20 00:16:50 +02:00
|
|
|
this.state = { note: Note.new() }
|
2017-05-23 22:36:19 +02:00
|
|
|
this.originalNote = null;
|
2017-05-12 22:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
2017-05-22 22:22:50 +02:00
|
|
|
if (!this.props.noteId) {
|
2017-05-24 22:51:50 +02:00
|
|
|
let note = this.props.itemType == 'todo' ? Note.newTodo(this.props.folderId) : Note.new(this.props.folderId);
|
|
|
|
Log.info(note);
|
|
|
|
this.setState({ note: note });
|
2017-05-22 22:22:50 +02:00
|
|
|
} else {
|
|
|
|
Note.load(this.props.noteId).then((note) => {
|
2017-05-23 22:36:19 +02:00
|
|
|
this.originalNote = Object.assign({}, note);
|
2017-05-22 22:22:50 +02:00
|
|
|
this.setState({ note: note });
|
|
|
|
});
|
|
|
|
}
|
2017-05-12 22:23:54 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
noteComponent_change(propName, propValue) {
|
2017-05-12 22:23:54 +02:00
|
|
|
this.setState((prevState, props) => {
|
|
|
|
let note = Object.assign({}, prevState.note);
|
|
|
|
note[propName] = propValue;
|
|
|
|
return { note: note }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
title_changeText(text) {
|
2017-05-12 22:23:54 +02:00
|
|
|
this.noteComponent_change('title', text);
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
body_changeText(text) {
|
2017-05-12 22:23:54 +02:00
|
|
|
this.noteComponent_change('body', text);
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
saveNoteButton_press() {
|
2017-06-21 00:16:41 +02:00
|
|
|
|
|
|
|
console.warn('CHANGE NOT TESTED');
|
|
|
|
|
|
|
|
let isNew = !this.state.note.id;
|
|
|
|
let toSave = BaseModel.diffObjects(this.originalNote, this.state.note);
|
|
|
|
toSave.id = this.state.note.id;
|
|
|
|
Note.save(toSave).then((note) => {
|
2017-05-23 22:36:19 +02:00
|
|
|
this.originalNote = Object.assign({}, note);
|
|
|
|
this.setState({ note: note });
|
2017-06-21 00:16:41 +02:00
|
|
|
if (isNew) return Note.updateGeolocation(note.id);
|
2017-05-23 21:01:37 +02:00
|
|
|
});
|
2017-06-21 00:16:41 +02:00
|
|
|
|
|
|
|
// NoteFolderService.save('note', this.state.note, this.originalNote).then((note) => {
|
|
|
|
// this.originalNote = Object.assign({}, note);
|
|
|
|
// this.setState({ note: note });
|
|
|
|
// });
|
2017-05-12 22:23:54 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
deleteNote_onPress(noteId) {
|
2017-06-04 17:01:52 +02:00
|
|
|
Log.info('DELETE', noteId);
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
attachFile_onPress(noteId) {
|
2017-06-04 17:01:52 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
menuOptions() {
|
2017-06-04 17:01:52 +02:00
|
|
|
return [
|
|
|
|
{ title: _('Attach file'), onPress: () => { this.attachFile_onPress(this.state.note.id); } },
|
|
|
|
{ title: _('Delete note'), onPress: () => { this.deleteNote_onPress(this.state.note.id); } },
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-05-12 22:23:54 +02:00
|
|
|
render() {
|
2017-05-24 22:51:50 +02:00
|
|
|
const note = this.state.note;
|
|
|
|
const isTodo = !!Number(note.is_todo);
|
|
|
|
let todoComponents = null;
|
|
|
|
|
|
|
|
if (note.is_todo) {
|
|
|
|
todoComponents = (
|
|
|
|
<View>
|
|
|
|
<Button title="test" onPress={this.saveNoteButton_press} />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-12 22:23:54 +02:00
|
|
|
return (
|
|
|
|
<View style={{flex: 1}}>
|
2017-06-04 17:01:52 +02:00
|
|
|
<ScreenHeader navState={this.props.navigation.state} menuOptions={this.menuOptions()} />
|
2017-05-24 22:51:50 +02:00
|
|
|
<View style={{ flexDirection: 'row' }}>
|
2017-06-06 22:01:43 +02:00
|
|
|
{ isTodo && <Checkbox checked={!!Number(note.todo_completed)} /> }<TextInput style={{flex:1}} value={note.title} onChangeText={(text) => this.title_changeText(text)} />
|
2017-05-24 22:51:50 +02:00
|
|
|
</View>
|
2017-06-06 22:01:43 +02:00
|
|
|
<TextInput style={{flex: 1, textAlignVertical: 'top'}} multiline={true} value={note.body} onChangeText={(text) => this.body_changeText(text)} />
|
2017-05-24 22:51:50 +02:00
|
|
|
{ todoComponents }
|
2017-06-06 22:01:43 +02:00
|
|
|
<Button title="Save note" onPress={() => this.saveNoteButton_press()} />
|
2017-05-12 22:23:54 +02:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const NoteScreen = connect(
|
|
|
|
(state) => {
|
|
|
|
return {
|
2017-05-22 22:22:50 +02:00
|
|
|
noteId: state.selectedNoteId,
|
|
|
|
folderId: state.selectedFolderId,
|
2017-05-24 22:51:50 +02:00
|
|
|
itemType: state.selectedItemType,
|
2017-05-12 22:23:54 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
)(NoteScreenComponent)
|
|
|
|
|
|
|
|
export { NoteScreen };
|