1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Load note into component

This commit is contained in:
Laurent Cozic 2017-05-10 19:21:09 +00:00
parent 9529e401de
commit 1e77383b7e
4 changed files with 90 additions and 20 deletions

View File

@ -0,0 +1,31 @@
// import React, { Component } from 'react';
// import { connect } from 'react-redux'
// import { Button } from 'react-native';
// import { _ } from 'src/locale.js';
// class CreateNoteButtonComponent extends Component {
// render() {
// return <Button onPress={this.props.onPress} title={_("Create note")} />
// }
// }
// const CreateNoteButton = connect(
// (state) => {
// return {
// selectedNoteId: selectedNoteId,
// };
// },
// (dispatch) => {
// return {
// onPress: function() {
// dispatch({
// type: 'VIEW_NOTE'
// });
// }
// }
// }
// )(CreateNoteButtonComponent)
// export { CreateNoteButton };

View File

@ -49,8 +49,9 @@ const ItemList = connect(
return {
onItemClick: (noteId) => {
dispatch({
type: 'VIEW_NOTE',
id: noteId,
type: 'Navigation/NAVIGATE',
routeName: 'Note',
noteId: noteId,
});
}
}

View File

@ -0,0 +1,14 @@
import { BaseModel } from 'src/base-model.js';
class Note extends BaseModel {
static noteById(notes, id) {
for (let i = 0; i < notes.length; i++) {
if (notes[i].id == id) return notes[i];
}
return null;
}
}
export { Note };

View File

@ -7,6 +7,7 @@ import { combineReducers } from 'redux';
import { StackNavigator } from 'react-navigation';
import { addNavigationHelpers } from 'react-navigation';
import { Log } from 'src/log.js'
import { Note } from 'src/models/note.js'
import { ItemList } from 'src/components/item-list.js'
let defaultState = {
@ -23,30 +24,39 @@ let defaultState = {
const reducer = (state = defaultState, action) => {
Log.info('Reducer action', action);
let newState = state;
switch (action.type) {
case 'Navigation/NAVIGATE':
case 'Navigation/BACK':
const nextStateNav = AppNavigator.router.getStateForAction(action, state.nav);
if (!nextStateNav) return state;
let newState = Object.assign({}, state);
newState.nav = nextStateNav;
return newState;
const nextStateNav = AppNavigator.router.getStateForAction(action, state.nav);
newState = Object.assign({}, state);
if (nextStateNav) {
newState.nav = nextStateNav;
}
if (action.noteId) {
newState.selectedNoteId = action.noteId;
}
break;
case 'VIEW_NOTE':
// TODO
return state;
newState = Object.assign({}, state);
break;
}
return state;
return newState;
}
let store = createStore(reducer);
class NotesScreen extends React.Component {
class NotesScreenComponent extends React.Component {
static navigationOptions = {
title: 'Notes',
};
@ -66,26 +76,40 @@ class NotesScreen extends React.Component {
}
}
class NoteScreen extends React.Component {
const NotesScreen = connect(
(state) => {
return {};
},
(dispatch) => {
return {};
}
)(NotesScreenComponent)
class NoteScreenComponent extends React.Component {
static navigationOptions = {
title: 'Note',
};
render() {
const { navigate } = this.props.navigation;
let note = this.props.note;
// <Button title="Save note" />
return (
<View style={{flex: 1}}>
<TextInput style={{flex: 1, textAlignVertical: 'top'}} multiline={true} />
<Button
title="Save note"
onPress={() =>
navigate('Notes')
}
/>
<TextInput style={{flex: 1, textAlignVertical: 'top'}} multiline={true} value={note ? note.body : ''} />
</View>
);
}
}
const NoteScreen = connect(
(state) => {
let selectedNote = state.selectedNoteId ? Note.noteById(state.notes, state.selectedNoteId) : null;
return { note: selectedNote };
},
(dispatch) => {
return {};
}
)(NoteScreenComponent)
const AppNavigator = StackNavigator({
Notes: {screen: NotesScreen},
Note: {screen: NoteScreen},