1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00
joplin/ReactNativeClient/app/root.js

321 lines
8.0 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-06-24 20:06:28 +02:00
import { Log } from 'lib/log.js'
import { Note } from 'lib/models/note.js'
import { Folder } from 'lib/models/folder.js'
import { BaseModel } from 'lib/base-model.js'
import { Database } from 'lib/database.js'
2017-07-04 21:12:30 +02:00
import { ItemList } from 'app/components/item-list.js'
import { NotesScreen } from 'app/components/screens/notes.js'
import { NoteScreen } from 'app/components/screens/note.js'
import { FolderScreen } from 'app/components/screens/folder.js'
import { FoldersScreen } from 'app/components/screens/folders.js'
import { LoginScreen } from 'app/components/screens/login.js'
import { LoadingScreen } from 'app/components/screens/loading.js'
2017-06-24 20:06:28 +02:00
import { Setting } from 'lib/models/setting.js'
import { Synchronizer } from 'lib/synchronizer.js'
2017-05-16 22:25:19 +02:00
import { MenuContext } from 'react-native-popup-menu';
2017-07-04 21:12:30 +02:00
import { SideMenu } from 'app/components/side-menu.js';
import { SideMenuContent } from 'app/components/side-menu-content.js';
//import { NoteFolderService } from 'lib/services/note-folder-service.js';
2017-06-24 20:06:28 +02:00
import { DatabaseDriverReactNative } from 'lib/database-driver-react-native';
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-24 22:51:50 +02:00
selectedItemType: 'note',
2017-05-15 21:10:00 +02:00
selectedFolderId: null,
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:51:50 +02:00
if ('itemType' in action) {
newState.selectedItemType = action.itemType;
}
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 {
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-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-06-11 23:11:14 +02:00
let db = new Database(new DatabaseDriverReactNative());
2017-06-25 17:17:40 +02:00
//db.setDebugMode(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-07-04 21:12:30 +02:00
//NoteFolderService.dispatch = this.props.dispatch;
2017-05-18 21:58:01 +02:00
2017-06-11 23:11:14 +02:00
db.open({ name: '/storage/emulated/0/Download/joplin-42.sqlite' }).then(() => {
2017-05-12 22:17:23 +02:00
Log.info('Database is ready.');
}).then(() => {
Log.info('Loading settings...');
return Setting.load();
}).then(() => {
2017-05-16 23:46:21 +02:00
let user = Setting.object('user');
2017-06-11 23:11:14 +02:00
if (!user || !user.session) {
user = {
email: 'laurent@cozic.net',
session: "02d0e9ca42cbbc2d35efb1bc790b9eec",
}
Setting.setObject('user', user);
this.props.dispatch({
type: 'USER_SET',
user: user,
});
}
Setting.setValue('sync.lastRevId', '123456');
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-06-24 20:11:19 +02:00
// this.props.dispatch({
// type: 'USER_SET',
// user: user,
// });
2017-05-16 23:46:21 +02:00
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');
2017-07-04 21:12:30 +02:00
//return NoteFolderService.openNoteList(folder.id);
2017-05-24 22:11:37 +02:00
// this.props.dispatch({
// type: 'Navigation/NAVIGATE',
// routeName: 'Notes',
// folderId: folder.id,
// });
2017-05-18 21:58:01 +02:00
}).then(() => {
2017-06-11 23:11:14 +02:00
var Dropbox = require('dropbox');
var dropboxApi = new Dropbox({ accessToken: '' });
// dbx.filesListFolder({path: '/Joplin/Laurent.4e847cc'})
// .then(function(response) {
// //console.log('DROPBOX RESPONSE', response);
// console.log('DROPBOX RESPONSE', response.entries.length, response.has_more);
// })
// .catch(function(error) {
// console.log('DROPBOX ERROR', error);
// });
// return this.api_;
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
});
}
sideMenu_change(isOpen) {
2017-05-24 21:27:13 +02:00
// 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 (
<SideMenu menu={sideMenuContent} onChange={(isOpen) => this.sideMenu_change(isOpen)}>
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-24 22:18:09 +02:00
params: {}
2017-05-15 22:50:14 +02:00
});
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 };