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

303 lines
7.5 KiB
JavaScript
Raw Normal View History

2017-05-09 21:59:14 +02:00
import React, { Component } from 'react';
2017-05-09 22:46:54 +02:00
import { View, Button, TextInput } from 'react-native';
2017-05-09 21:59:14 +02:00
import { connect } from 'react-redux'
import { Provider } from 'react-redux'
import { createStore } from 'redux';
import { combineReducers } from 'redux';
import { StackNavigator } from 'react-navigation';
import { addNavigationHelpers } from 'react-navigation';
2017-05-09 22:46:54 +02:00
import { Log } from 'src/log.js'
2017-05-10 21:21:09 +02:00
import { Note } from 'src/models/note.js'
2017-05-15 21:46:34 +02:00
import { Folder } from 'src/models/folder.js'
2017-05-18 21:58:01 +02:00
import { BaseModel } from 'src/base-model.js'
2017-05-12 22:17:23 +02:00
import { Database } from 'src/database.js'
import { Registry } from 'src/registry.js'
2017-05-09 22:46:54 +02:00
import { ItemList } from 'src/components/item-list.js'
2017-05-12 22:23:54 +02:00
import { NotesScreen } from 'src/components/screens/notes.js'
import { NoteScreen } from 'src/components/screens/note.js'
2017-05-15 21:10:00 +02:00
import { FolderScreen } from 'src/components/screens/folder.js'
2017-05-15 21:46:34 +02:00
import { FoldersScreen } from 'src/components/screens/folders.js'
2017-05-12 22:31:03 +02:00
import { LoginScreen } from 'src/components/screens/login.js'
2017-05-24 22:11:37 +02:00
import { LoadingScreen } from 'src/components/screens/loading.js'
2017-05-12 22:17:23 +02:00
import { Setting } from 'src/models/setting.js'
2017-05-18 21:58:01 +02:00
import { Synchronizer } from 'src/synchronizer.js'
2017-05-16 22:25:19 +02:00
import { MenuContext } from 'react-native-popup-menu';
2017-05-24 21:27:13 +02:00
import { SideMenu } from 'src/components/side-menu.js';
import { SideMenuContent } from 'src/components/side-menu-content.js';
2017-05-24 22:11:37 +02:00
import { NoteFolderService } from 'src/services/note-folder-service.js';
2017-05-16 22:25:19 +02:00
2017-05-09 22:46:54 +02:00
let defaultState = {
2017-05-12 21:54:06 +02:00
notes: [],
2017-05-15 22:50:14 +02:00
folders: [],
2017-05-09 22:46:54 +02:00
selectedNoteId: null,
2017-05-15 21:10:00 +02:00
selectedFolderId: null,
2017-05-15 22:50:14 +02:00
listMode: 'view',
2017-05-16 23:46:21 +02:00
user: { email: 'laurent@cozic.net', session: null },
2017-05-24 21:27:13 +02:00
showSideMenu: false,
2017-05-09 22:46:54 +02:00
};
const reducer = (state = defaultState, action) => {
2017-05-19 21:12:09 +02:00
Log.info('Reducer action', action.type);
2017-05-09 22:46:54 +02:00
2017-05-10 21:21:09 +02:00
let newState = state;
2017-05-09 22:46:54 +02:00
switch (action.type) {
2017-05-10 20:58:02 +02:00
case 'Navigation/NAVIGATE':
case 'Navigation/BACK':
2017-05-09 22:46:54 +02:00
2017-05-12 21:54:06 +02:00
const r = state.nav.routes;
2017-05-24 22:11:37 +02:00
const currentRoute = r.length ? r[r.length - 1] : null;
const currentRouteName = currentRoute ? currentRoute.routeName : '';
2017-05-12 21:54:06 +02:00
2017-05-24 22:11:37 +02:00
Log.info('Current route name', currentRouteName);
Log.info('New route name', action.routeName);
2017-05-15 22:50:14 +02:00
2017-05-10 21:21:09 +02:00
newState = Object.assign({}, state);
2017-05-15 22:50:14 +02:00
if ('noteId' in action) {
2017-05-15 21:46:34 +02:00
newState.selectedNoteId = action.noteId;
}
2017-05-15 22:50:14 +02:00
if ('folderId' in action) {
2017-05-15 21:46:34 +02:00
newState.selectedFolderId = action.folderId;
2017-05-10 21:21:09 +02:00
}
2017-05-24 22:11:37 +02:00
if (currentRouteName == action.routeName) {
// If the current screen is already the requested screen, don't do anything
} else {
action.params = { listMode: 'view' };
const nextStateNav = AppNavigator.router.getStateForAction(action, currentRouteName != 'Loading' ? state.nav : null);
if (nextStateNav) {
newState.nav = nextStateNav;
}
}
2017-05-10 21:21:09 +02:00
break;
2017-05-09 22:46:54 +02:00
// Replace all the notes with the provided array
case 'NOTES_UPDATE_ALL':
2017-05-09 22:46:54 +02:00
2017-05-10 21:21:09 +02:00
newState = Object.assign({}, state);
2017-05-11 22:14:01 +02:00
newState.notes = action.notes;
break;
// Insert the note into the note list if it's new, or
2017-05-12 21:54:06 +02:00
// update it within the note array if it already exists.
case 'NOTES_UPDATE_ONE':
let newNotes = state.notes.splice(0);
2017-05-15 21:10:00 +02:00
var found = false;
for (let i = 0; i < newNotes.length; i++) {
let n = newNotes[i];
if (n.id == action.note.id) {
newNotes[i] = action.note;
found = true;
break;
}
}
2017-05-10 21:21:09 +02:00
if (!found) newNotes.push(action.note);
2017-05-11 22:14:01 +02:00
newState = Object.assign({}, state);
newState.notes = newNotes;
2017-05-11 22:14:01 +02:00
break;
2017-05-09 22:46:54 +02:00
2017-05-15 21:46:34 +02:00
case 'FOLDERS_UPDATE_ALL':
newState = Object.assign({}, state);
newState.folders = action.folders;
break;
2017-05-15 21:10:00 +02:00
case 'FOLDERS_UPDATE_ONE':
2017-05-16 22:25:19 +02:00
var newFolders = state.folders.splice(0);
2017-05-15 21:10:00 +02:00
var found = false;
for (let i = 0; i < newFolders.length; i++) {
let n = newFolders[i];
if (n.id == action.folder.id) {
newFolders[i] = action.folder;
found = true;
break;
}
}
if (!found) newFolders.push(action.folder);
newState = Object.assign({}, state);
newState.folders = newFolders;
break;
2017-05-16 22:25:19 +02:00
case 'FOLDER_DELETE':
var newFolders = [];
for (let i = 0; i < state.folders.length; i++) {
let f = state.folders[i];
if (f.id == action.folderId) continue;
newFolders.push(f);
}
newState = Object.assign({}, state);
newState.folders = newFolders;
break;
2017-05-16 23:46:21 +02:00
case 'USER_SET':
newState = Object.assign({}, state);
newState.user = action.user;
break;
2017-05-15 22:50:14 +02:00
case 'SET_LIST_MODE':
newState = Object.assign({}, state);
newState.listMode = action.listMode;
newState.nav = Object.assign({}, state.nav);
break;
2017-05-24 21:27:13 +02:00
case 'SIDE_MENU_TOGGLE':
newState = Object.assign({}, state);
newState.showSideMenu = !newState.showSideMenu
break;
case 'SIDE_MENU_OPEN':
newState = Object.assign({}, state);
newState.showSideMenu = true
break;
case 'SIDE_MENU_CLOSE':
newState = Object.assign({}, state);
newState.showSideMenu = false
break;
2017-05-09 22:46:54 +02:00
}
2017-05-24 22:11:37 +02:00
// Log.info('newState.selectedFolderId', newState.selectedFolderId);
2017-05-10 21:21:09 +02:00
return newState;
2017-05-09 22:46:54 +02:00
}
let store = createStore(reducer);
2017-05-09 21:59:14 +02:00
const AppNavigator = StackNavigator({
2017-05-24 22:11:37 +02:00
Notes: { screen: NotesScreen },
Note: { screen: NoteScreen },
Folder: { screen: FolderScreen },
Folders: { screen: FoldersScreen },
Login: { screen: LoginScreen },
Loading: { screen: LoadingScreen },
2017-05-09 21:59:14 +02:00
});
2017-05-10 20:58:02 +02:00
class AppComponent extends React.Component {
2017-05-11 22:14:01 +02:00
componentDidMount() {
2017-05-12 22:17:23 +02:00
let db = new Database();
2017-05-19 21:12:09 +02:00
//db.setDebugEnabled(Registry.debugMode());
2017-05-22 22:22:50 +02:00
db.setDebugEnabled(false);
2017-05-20 00:16:50 +02:00
2017-05-18 21:58:01 +02:00
BaseModel.dispatch = this.props.dispatch;
2017-05-20 00:16:50 +02:00
BaseModel.db_ = db;
2017-05-24 22:11:37 +02:00
NoteFolderService.dispatch = this.props.dispatch;
2017-05-18 21:58:01 +02:00
2017-05-12 22:17:23 +02:00
db.open().then(() => {
Log.info('Database is ready.');
Registry.setDb(db);
}).then(() => {
Log.info('Loading settings...');
return Setting.load();
}).then(() => {
2017-05-16 23:46:21 +02:00
let user = Setting.object('user');
2017-05-14 23:51:45 +02:00
Log.info('Client ID', Setting.value('clientId'));
2017-05-16 23:46:21 +02:00
Log.info('User', user);
2017-05-18 21:58:01 +02:00
Registry.api().setSession(user.session);
2017-05-16 23:46:21 +02:00
this.props.dispatch({
type: 'USER_SET',
user: user,
});
2017-05-15 21:46:34 +02:00
Log.info('Loading folders...');
2017-05-24 22:11:37 +02:00
return Folder.all().then((folders) => {
2017-05-12 22:17:23 +02:00
this.props.dispatch({
2017-05-15 21:46:34 +02:00
type: 'FOLDERS_UPDATE_ALL',
folders: folders,
2017-05-12 22:17:23 +02:00
});
2017-05-24 22:11:37 +02:00
return folders;
2017-05-12 22:17:23 +02:00
}).catch((error) => {
2017-05-15 21:46:34 +02:00
Log.warn('Cannot load folders', error);
2017-05-11 22:14:01 +02:00
});
2017-05-24 22:11:37 +02:00
}).then((folders) => {
let folder = folders[0];
if (!folder) throw new Error('No default folder is defined');
return NoteFolderService.openNoteList(folder.id);
// this.props.dispatch({
// type: 'Navigation/NAVIGATE',
// routeName: 'Notes',
// folderId: folder.id,
// });
2017-05-18 21:58:01 +02:00
}).then(() => {
2017-05-19 21:12:09 +02:00
let synchronizer = new Synchronizer(db, Registry.api());
2017-05-21 21:55:01 +02:00
Registry.setSynchronizer(synchronizer);
2017-05-19 21:12:09 +02:00
synchronizer.start();
2017-05-11 22:14:01 +02:00
}).catch((error) => {
2017-05-18 21:58:01 +02:00
Log.error('Initialization error:', error);
2017-05-11 22:14:01 +02:00
});
}
2017-05-24 21:27:13 +02:00
sideMenu_change = (isOpen) => {
// Make sure showSideMenu property of state is updated
// when the menu is open/closed.
this.props.dispatch({
type: isOpen ? 'SIDE_MENU_OPEN' : 'SIDE_MENU_CLOSE',
});
}
2017-05-11 22:14:01 +02:00
render() {
2017-05-24 21:27:13 +02:00
const sideMenuContent = <SideMenuContent/>;
2017-05-24 21:09:46 +02:00
2017-05-11 22:14:01 +02:00
return (
2017-05-24 21:27:13 +02:00
<SideMenu menu={sideMenuContent} onChange={this.sideMenu_change}>
2017-05-24 21:09:46 +02:00
<MenuContext style={{ flex: 1 }}>
<AppNavigator navigation={addNavigationHelpers({
dispatch: this.props.dispatch,
state: this.props.nav,
})} />
</MenuContext>
</SideMenu>
2017-05-11 22:14:01 +02:00
);
}
2017-05-10 20:58:02 +02:00
}
2017-05-15 22:50:14 +02:00
defaultState.nav = AppNavigator.router.getStateForAction({
type: 'Navigation/NAVIGATE',
2017-05-24 22:11:37 +02:00
routeName: 'Loading',
2017-05-15 22:50:14 +02:00
params: {listMode: 'view'}
});
2017-05-10 20:58:02 +02:00
const mapStateToProps = (state) => {
return {
nav: state.nav
};
};
const App = connect(mapStateToProps)(AppComponent);
2017-05-09 22:46:54 +02:00
class Root extends React.Component {
render() {
return (
<Provider store={store}>
2017-05-10 20:58:02 +02:00
<App />
2017-05-09 22:46:54 +02:00
</Provider>
);
}
2017-05-09 21:59:14 +02:00
}
export { Root };