joplin/ReactNativeClient/lib/components/note-list.js

114 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-05-15 19:10:00 +00:00
import React, { Component } from 'react';
import { connect } from 'react-redux'
2017-07-25 17:49:31 +00:00
import { ListView, Text, TouchableHighlight, Switch, View, StyleSheet } from 'react-native';
2017-06-24 18:06:28 +00:00
import { Log } from 'lib/log.js';
import { _ } from 'lib/locale.js';
2017-07-25 17:49:31 +00:00
import { Checkbox } from 'lib/components/checkbox.js';
import { NoteItem } from 'lib/components/note-item.js';
import { reg } from 'lib/registry.js';
import { Note } from 'lib/models/note.js';
import { Setting } from 'lib/models/setting.js';
import { time } from 'lib/time-utils.js';
import { globalStyle } from 'lib/components/global-style.js';
2017-05-15 19:10:00 +00:00
2017-07-25 17:49:31 +00:00
const styles = StyleSheet.create({
noItemMessage: {
paddingLeft: globalStyle.marginLeft,
paddingRight: globalStyle.marginRight,
paddingTop: globalStyle.marginTop,
paddingBottom: globalStyle.marginBottom
},
});
class NoteListComponent extends Component {
constructor() {
super();
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => { return r1 !== r2; }
});
this.state = {
dataSource: ds,
items: [],
selectedItemIds: [],
};
}
filterNotes(notes) {
const todoFilter = Setting.value('todoFilter');
if (todoFilter == 'all') return notes;
const now = time.unixMs();
const maxInterval = 1000 * 60 * 60 * 24;
const notRecentTime = now - maxInterval;
let output = [];
for (let i = 0; i < notes.length; i++) {
const note = notes[i];
if (note.is_todo) {
if (todoFilter == 'recent' && note.updated_time < notRecentTime && !!note.todo_completed) continue;
if (todoFilter == 'nonCompleted' && !!note.todo_completed) continue;
}
output.push(note);
}
return output;
}
componentWillMount() {
const newDataSource = this.state.dataSource.cloneWithRows(this.filterNotes(this.props.items));
this.state = { dataSource: newDataSource };
}
componentWillReceiveProps(newProps) {
// https://stackoverflow.com/questions/38186114/react-native-redux-and-listview
this.setState({
dataSource: this.state.dataSource.cloneWithRows(this.filterNotes(newProps.items)),
});
}
async todoCheckbox_change(itemId, checked) {
let note = await Note.load(itemId);
await Note.save({ id: note.id, todo_completed: checked ? time.unixMs() : 0 });
reg.scheduleSync();
}
2017-05-15 19:46:34 +00:00
2017-06-06 20:01:43 +00:00
listView_itemPress(noteId) {
2017-05-15 19:46:34 +00:00
this.props.dispatch({
type: 'Navigation/NAVIGATE',
routeName: 'Note',
noteId: noteId,
});
}
2017-07-25 17:49:31 +00:00
listView_itemLongPress(itemId) {}
render() {
// `enableEmptySections` is to fix this warning: https://github.com/FaridSafi/react-native-gifted-listview/issues/39
if (this.state.dataSource.getRowCount()) {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(note) => {
return <NoteItem
note={note}
onPress={(note) => this.listView_itemPress(note.id) }
onCheckboxChange={(note, checked) => this.todoCheckbox_change(note.id, checked) }
/> }}
enableEmptySections={true}
/>
);
} else {
const noItemMessage = _('There are currently no notes. Create one by clicking on the (+) button.');
return <Text style={styles.noItemMessage} >{noItemMessage}</Text>;
}
}
2017-05-15 19:46:34 +00:00
}
2017-05-15 19:10:00 +00:00
const NoteList = connect(
(state) => {
return { items: state.notes };
}
)(NoteListComponent)
export { NoteList };