joplin/ReactNativeClient/lib/components/screens/notes.js

165 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-05-12 20:23:54 +00:00
import React, { Component } from 'react';
2017-05-15 19:10:00 +00:00
import { View, Button, Picker } from 'react-native';
2017-05-12 20:23:54 +00:00
import { connect } from 'react-redux'
2017-07-25 18:09:01 +00:00
import { reg } from 'lib/registry.js';
2017-06-24 18:06:28 +00:00
import { Log } from 'lib/log.js'
import { NoteList } from 'lib/components/note-list.js'
import { Folder } from 'lib/models/folder.js'
2017-07-25 18:36:52 +00:00
import { Tag } from 'lib/models/tag.js'
2017-07-25 18:09:01 +00:00
import { Note } from 'lib/models/note.js'
2017-06-24 18:06:28 +00:00
import { ScreenHeader } from 'lib/components/screen-header.js';
2017-05-16 20:25:19 +00:00
import { MenuOption, Text } from 'react-native-popup-menu';
2017-06-24 18:06:28 +00:00
import { _ } from 'lib/locale.js';
import { ActionButton } from 'lib/components/action-button.js';
2017-07-12 23:01:15 +00:00
import { dialogs } from 'lib/dialogs.js';
2017-07-12 22:32:08 +00:00
import DialogBox from 'react-native-dialogbox';
2017-07-14 18:49:14 +00:00
import { BaseScreenComponent } from 'lib/components/base-screen.js';
2017-05-12 20:23:54 +00:00
2017-07-14 18:49:14 +00:00
class NotesScreenComponent extends BaseScreenComponent {
2017-05-12 20:23:54 +00:00
2017-06-06 20:01:43 +00:00
static navigationOptions(options) {
2017-05-16 19:57:09 +00:00
return { header: null };
}
2017-05-12 20:23:54 +00:00
2017-07-25 18:09:01 +00:00
async componentDidMount() {
await this.refreshNotes();
}
async componentWillReceiveProps(newProps) {
if (newProps.notesOrder.orderBy != this.props.notesOrder.orderBy ||
newProps.notesOrder.orderByDir != this.props.notesOrder.orderByDir ||
2017-07-25 18:36:52 +00:00
newProps.selectedFolderId != this.props.selectedFolderId ||
newProps.selectedTagId != this.props.selectedTagId ||
newProps.notesParentType != this.props.notesParentType) {
2017-07-25 18:09:01 +00:00
await this.refreshNotes(newProps);
}
}
async refreshNotes(props = null) {
if (props === null) props = this.props;
let options = {
orderBy: props.notesOrder.orderBy,
orderByDir: props.notesOrder.orderByDir,
};
2017-07-25 18:36:52 +00:00
const parent = this.parentItem(props);
2017-07-25 18:09:01 +00:00
const source = JSON.stringify({
options: options,
2017-07-25 18:36:52 +00:00
parentId: parent.id,
2017-07-25 18:09:01 +00:00
});
2017-07-25 18:36:52 +00:00
if (source == props.notesSource) {
console.info('NO SOURCE CHAGNE');
console.info(source);
console.info(props.notesSource);
return;
}
2017-07-25 18:09:01 +00:00
2017-07-25 18:36:52 +00:00
let notes = [];
if (props.notesParentType == 'Folder') {
notes = await Note.previews(props.selectedFolderId, options);
} else {
notes = await Tag.notes(props.selectedTagId); // TODO: should also return previews
}
2017-07-25 18:09:01 +00:00
this.props.dispatch({
type: 'NOTES_UPDATE_ALL',
notes: notes,
notesSource: source,
});
}
2017-06-06 20:01:43 +00:00
deleteFolder_onPress(folderId) {
2017-07-12 23:01:15 +00:00
dialogs.confirm(this, _('Delete notebook?')).then((ok) => {
if (!ok) return;
2017-07-12 22:32:08 +00:00
2017-07-12 23:01:15 +00:00
Folder.delete(folderId).then(() => {
2017-07-25 18:09:01 +00:00
this.props.dispatch({
type: 'NAV_GO',
2017-07-25 18:09:01 +00:00
routeName: 'Welcome',
});
2017-07-12 23:01:15 +00:00
}).catch((error) => {
alert(error.message);
2017-05-16 20:25:19 +00:00
});
});
}
2017-06-06 20:01:43 +00:00
editFolder_onPress(folderId) {
2017-05-16 20:25:19 +00:00
this.props.dispatch({
type: 'NAV_GO',
2017-05-16 20:25:19 +00:00
routeName: 'Folder',
folderId: folderId,
});
}
2017-06-06 20:01:43 +00:00
menuOptions() {
2017-07-25 18:36:52 +00:00
if (this.props.notesParentType == 'Folder') {
if (this.props.selectedFolderId == Folder.conflictFolderId()) return [];
return [
{ title: _('Delete notebook'), onPress: () => { this.deleteFolder_onPress(this.props.selectedFolderId); } },
{ title: _('Edit notebook'), onPress: () => { this.editFolder_onPress(this.props.selectedFolderId); } },
];
} else {
return []; // TODO
}
}
parentItem(props = null) {
if (!props) props = this.props;
let output = null;
if (props.notesParentType == 'Folder') {
output = Folder.byId(props.folders, props.selectedFolderId);
} else if (props.notesParentType == 'Tag') {
output = Tag.byId(props.tags, props.selectedTagId);
} else {
throw new Error('Invalid parent type: ' + props.notesParentType);
}
return output;
2017-05-16 20:25:19 +00:00
}
2017-05-12 20:23:54 +00:00
render() {
2017-07-25 18:36:52 +00:00
const parent = this.parentItem();
2017-07-15 22:47:11 +00:00
2017-07-25 18:36:52 +00:00
if (!parent) {
2017-07-25 18:09:01 +00:00
return (
<View style={this.styles().screen}>
<ScreenHeader title={title} menuOptions={this.menuOptions()} />
</View>
)
2017-07-15 22:47:11 +00:00
}
2017-07-25 18:36:52 +00:00
let title = parent ? parent.title : null;
const addFolderNoteButtons = this.props.selectedFolderId && this.props.selectedFolderId != Folder.conflictFolderId();
2017-05-16 20:25:19 +00:00
2017-05-12 20:23:54 +00:00
const { navigate } = this.props.navigation;
return (
2017-07-14 18:49:14 +00:00
<View style={this.styles().screen}>
2017-07-24 21:58:14 +00:00
<ScreenHeader title={title} menuOptions={this.menuOptions()} />
2017-07-25 17:49:31 +00:00
<NoteList style={{flex: 1}}/>
2017-07-15 15:54:19 +00:00
<ActionButton addFolderNoteButtons={addFolderNoteButtons} parentFolderId={this.props.selectedFolderId}></ActionButton>
2017-07-12 23:01:15 +00:00
<DialogBox ref={dialogbox => { this.dialogbox = dialogbox }}/>
2017-05-12 20:23:54 +00:00
</View>
);
}
}
const NotesScreen = connect(
(state) => {
2017-05-15 19:10:00 +00:00
return {
2017-05-16 20:25:19 +00:00
folders: state.folders,
2017-07-25 18:36:52 +00:00
tags: state.tags,
2017-05-16 20:25:19 +00:00
selectedFolderId: state.selectedFolderId,
2017-07-25 18:36:52 +00:00
selectedTagId: state.selectedTagId,
notesParentType: state.notesParentType,
2017-07-25 18:09:01 +00:00
notes: state.notes,
notesOrder: state.notesOrder,
notesSource: state.notesSource,
2017-05-15 19:10:00 +00:00
};
2017-05-12 20:23:54 +00:00
}
)(NotesScreenComponent)
export { NotesScreen };