1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ReactNativeClient/src/components/screens/note.js

112 lines
3.0 KiB
JavaScript
Raw Normal View History

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'
import { Log } from 'src/log.js'
import { Note } from 'src/models/note.js'
2017-05-23 21:58:12 +02:00
import { Registry } from 'src/registry.js'
2017-05-16 21:57:09 +02:00
import { ScreenHeader } from 'src/components/screen-header.js';
2017-05-24 22:51:50 +02:00
import { Checkbox } from 'src/components/checkbox.js'
import { NoteFolderService } from 'src/services/note-folder-service.js';
2017-06-04 17:01:52 +02:00
import { _ } from 'src/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() }
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) => {
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() {
NoteFolderService.save('note', this.state.note, this.originalNote).then((note) => {
this.originalNote = Object.assign({}, note);
this.setState({ note: note });
2017-05-23 21:01:37 +02:00
});
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 };