1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/ReactNativeClient/lib/components/screens/notes.js

174 lines
4.8 KiB
JavaScript
Raw Normal View History

2017-05-12 22:23:54 +02:00
import React, { Component } from 'react';
2017-05-15 21:10:00 +02:00
import { View, Button, Picker } from 'react-native';
2017-05-12 22:23:54 +02:00
import { connect } from 'react-redux'
2017-07-25 20:09:01 +02:00
import { reg } from 'lib/registry.js';
2017-06-24 20:06:28 +02: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 20:36:52 +02:00
import { Tag } from 'lib/models/tag.js'
2017-07-25 20:09:01 +02:00
import { Note } from 'lib/models/note.js'
2017-08-01 19:59:01 +02:00
import { Setting } from 'lib/models/setting.js'
import { themeStyle } from 'lib/components/global-style.js';
2017-06-24 20:06:28 +02:00
import { ScreenHeader } from 'lib/components/screen-header.js';
2017-05-16 22:25:19 +02:00
import { MenuOption, Text } from 'react-native-popup-menu';
2017-06-24 20:06:28 +02:00
import { _ } from 'lib/locale.js';
import { ActionButton } from 'lib/components/action-button.js';
2017-07-13 01:01:15 +02:00
import { dialogs } from 'lib/dialogs.js';
2017-07-13 00:32:08 +02:00
import DialogBox from 'react-native-dialogbox';
2017-07-14 20:49:14 +02:00
import { BaseScreenComponent } from 'lib/components/base-screen.js';
2017-05-12 22:23:54 +02:00
2017-07-14 20:49:14 +02:00
class NotesScreenComponent extends BaseScreenComponent {
2017-05-12 22:23:54 +02:00
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
2017-07-25 20:09:01 +02:00
async componentDidMount() {
await this.refreshNotes();
}
async componentWillReceiveProps(newProps) {
if (newProps.notesOrder !== this.props.notesOrder ||
2017-07-25 20:36:52 +02:00
newProps.selectedFolderId != this.props.selectedFolderId ||
newProps.selectedTagId != this.props.selectedTagId ||
newProps.notesParentType != this.props.notesParentType) {
2017-07-25 20:09:01 +02:00
await this.refreshNotes(newProps);
}
}
async refreshNotes(props = null) {
if (props === null) props = this.props;
let options = {
order: props.notesOrder,
uncompletedTodosOnTop: props.uncompletedTodosOnTop,
2017-07-25 20:09:01 +02:00
};
2017-07-25 20:36:52 +02:00
const parent = this.parentItem(props);
2017-07-31 22:03:12 +02:00
if (!parent) return;
2017-07-25 20:36:52 +02:00
2017-07-25 20:09:01 +02:00
const source = JSON.stringify({
options: options,
2017-07-25 20:36:52 +02:00
parentId: parent.id,
2017-07-25 20:09:01 +02:00
});
2017-07-25 22:24:30 +02:00
if (source == props.notesSource) return;
2017-07-25 20:09:01 +02:00
2017-07-25 20:36:52 +02: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 20:09:01 +02:00
this.props.dispatch({
type: 'NOTES_UPDATE_ALL',
notes: notes,
notesSource: source,
});
}
2017-06-06 22:01:43 +02:00
deleteFolder_onPress(folderId) {
2017-07-13 01:01:15 +02:00
dialogs.confirm(this, _('Delete notebook?')).then((ok) => {
if (!ok) return;
2017-07-13 00:32:08 +02:00
2017-07-13 01:01:15 +02:00
Folder.delete(folderId).then(() => {
2017-07-25 20:09:01 +02:00
this.props.dispatch({
type: 'NAV_GO',
2017-07-25 20:09:01 +02:00
routeName: 'Welcome',
});
2017-07-13 01:01:15 +02:00
}).catch((error) => {
alert(error.message);
2017-05-16 22:25:19 +02:00
});
});
}
2017-06-06 22:01:43 +02:00
editFolder_onPress(folderId) {
2017-05-16 22:25:19 +02:00
this.props.dispatch({
type: 'NAV_GO',
2017-05-16 22:25:19 +02:00
routeName: 'Folder',
folderId: folderId,
});
}
2017-06-06 22:01:43 +02:00
menuOptions() {
2017-07-25 20:36:52 +02: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 {
2017-07-25 22:24:30 +02:00
return []; // For tags - TODO
2017-07-25 20:36:52 +02:00
}
}
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 {
2017-07-31 22:03:12 +02:00
return null;
2017-07-25 20:36:52 +02:00
throw new Error('Invalid parent type: ' + props.notesParentType);
}
return output;
2017-05-16 22:25:19 +02:00
}
2017-05-12 22:23:54 +02:00
render() {
2017-07-25 20:36:52 +02:00
const parent = this.parentItem();
const theme = themeStyle(this.props.theme);
2017-08-01 19:59:01 +02:00
let rootStyle = {
flex: 1,
backgroundColor: theme.backgroundColor,
}
2017-07-31 22:03:12 +02:00
if (!this.props.visible) {
rootStyle.flex = 0.001; // This is a bit of a hack but it seems to work fine - it makes the component invisible but without unmounting it
}
2017-08-02 19:55:03 +02:00
if (!parent) {
return (
<View style={rootStyle}>
<ScreenHeader title={title} menuOptions={this.menuOptions()} />
</View>
)
}
let title = parent ? parent.title : null;
const addFolderNoteButtons = this.props.selectedFolderId && this.props.selectedFolderId != Folder.conflictFolderId();
2017-05-12 22:23:54 +02:00
return (
2017-07-31 22:03:12 +02:00
<View style={rootStyle}>
2017-07-24 23:58:14 +02:00
<ScreenHeader title={title} menuOptions={this.menuOptions()} />
2017-07-25 19:49:31 +02:00
<NoteList style={{flex: 1}}/>
2017-07-15 17:54:19 +02:00
<ActionButton addFolderNoteButtons={addFolderNoteButtons} parentFolderId={this.props.selectedFolderId}></ActionButton>
2017-07-13 01:01:15 +02:00
<DialogBox ref={dialogbox => { this.dialogbox = dialogbox }}/>
2017-05-12 22:23:54 +02:00
</View>
);
}
}
const NotesScreen = connect(
(state) => {
2017-05-15 21:10:00 +02:00
return {
2017-05-16 22:25:19 +02:00
folders: state.folders,
2017-07-25 20:36:52 +02:00
tags: state.tags,
2017-05-16 22:25:19 +02:00
selectedFolderId: state.selectedFolderId,
2017-07-25 20:36:52 +02:00
selectedTagId: state.selectedTagId,
notesParentType: state.notesParentType,
2017-07-25 20:09:01 +02:00
notes: state.notes,
notesOrder: state.notesOrder,
notesSource: state.notesSource,
uncompletedTodosOnTop: state.settings.uncompletedTodosOnTop,
2017-08-01 19:59:01 +02:00
theme: state.settings.theme,
2017-05-15 21:10:00 +02:00
};
2017-05-12 22:23:54 +02:00
}
)(NotesScreenComponent)
export { NotesScreen };