2017-05-12 22:23:54 +02:00
|
|
|
import React, { Component } from 'react';
|
2017-07-10 23:34:26 +02:00
|
|
|
import { View, Button, TextInput, WebView } from 'react-native';
|
2017-05-12 22:23:54 +02:00
|
|
|
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 { _ } from 'lib/locale.js';
|
2017-07-10 23:34:26 +02:00
|
|
|
import marked from 'lib/marked.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-07-10 23:34:26 +02:00
|
|
|
this.state = {
|
|
|
|
note: Note.new(),
|
|
|
|
mode: 'view',
|
|
|
|
}
|
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);
|
|
|
|
this.setState({ note: note });
|
2017-05-22 22:22:50 +02:00
|
|
|
} else {
|
|
|
|
Note.load(this.props.noteId).then((note) => {
|
|
|
|
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-07-05 23:29:00 +02:00
|
|
|
async saveNoteButton_press() {
|
2017-06-21 00:16:41 +02:00
|
|
|
let isNew = !this.state.note.id;
|
2017-07-05 23:29:00 +02:00
|
|
|
let note = await Note.save(this.state.note);
|
|
|
|
this.setState({ note: note });
|
|
|
|
if (isNew) Note.updateGeolocation(note.id);
|
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-07-10 23:34:26 +02:00
|
|
|
let bodyComponent = null;
|
|
|
|
if (this.state.mode == 'view') {
|
|
|
|
const source = {
|
|
|
|
html: note ? marked(note.body, { gfm: true, breaks: true }) : '',
|
|
|
|
};
|
|
|
|
|
|
|
|
bodyComponent = (
|
|
|
|
<View style={{flex:1}}>
|
|
|
|
<WebView source={source}/>
|
|
|
|
<Button title="Edit note" onPress={() => { this.setState({ mode: 'edit' }); }}/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
bodyComponent = <TextInput style={{flex: 1, textAlignVertical: 'top', fontFamily: 'monospace'}} multiline={true} value={note.body} onChangeText={(text) => this.body_changeText(text)} />
|
|
|
|
}
|
|
|
|
|
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-07-10 23:34:26 +02:00
|
|
|
{ bodyComponent }
|
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 };
|