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

Made note form

This commit is contained in:
Laurent Cozic 2017-05-10 19:51:43 +00:00
parent 1e77383b7e
commit 017c4f7338
6 changed files with 112 additions and 35 deletions

View File

@ -1,34 +1,6 @@
import React, { Component } from 'react';
import { AppRegistry, View, Button, TextInput } from 'react-native';
import { connect } from 'react-redux'
import { Provider } from 'react-redux'
import { createStore } from 'redux';
import { combineReducers } from 'redux';
import { WebApi } from 'src/web-api.js'
import { Database } from 'src/database.js'
import { SessionService } from 'src/services/session-service.js';
import { Log } from 'src/log.js'
import { LoginButton } from 'src/components/login-button.js';
import { Root } from 'src/root.js';
//AppRegistry.registerComponent('AwesomeProject', () => AppNavigator);
AppRegistry.registerComponent('AwesomeProject', () => Root);
// let debugMode = true;
// let clientId = 'A7D301DA7D301DA7D301DA7D301DA7D3';
// let db = new Database();
// db.setDebugEnabled(debugMode);
// db.open();
import { main } from 'src/main.js';
main();
// let defaultState = {

View File

@ -1,5 +1,25 @@
import { Log } from 'src/log.js';
import { Database } from 'src/database.js';
class BaseModel {
static tableName() {
throw new Error('Must be overriden');
}
static save(object) {
let sql = Database.insertSql(this.tableName(), object);
Log.info(sql);
}
static setDb(database) {
this.db_ = database;
}
static db() {
return this.db_;
}
}
export { BaseModel };

View File

@ -125,6 +125,20 @@ class Database {
});
}
static insertSql(tableName, data) {
let output = '';
let keySql= '';
let valueSql = '';
for (let key in data) {
if (data.hasOwnProperty(key)) continue;
if (keySql != '') keySql += ', ';
if (valueSql != '') valueSql += ', ';
keySql += key;
valueSql += '?';
}
return 'INSERT INTO ' + tableName + ' (' + keySql + ') VALUES (' + valueSql + ')';
}
updateSchema() {
Log.info('Checking for database schema update...');

View File

@ -0,0 +1,20 @@
import { AppRegistry } from 'react-native';
import { Database } from 'src/database.js'
import { Log } from 'src/log.js'
import { Root } from 'src/root.js';
import { BaseModel } from 'src/base-model.js';
function main() {
let debugMode = true;
let clientId = 'A7D301DA7D301DA7D301DA7D301DA7D3';
let db = new Database();
db.setDebugEnabled(debugMode);
db.open();
BaseModel.setDb(db);
AppRegistry.registerComponent('AwesomeProject', () => Root);
}
export { main }

View File

@ -2,6 +2,10 @@ import { BaseModel } from 'src/base-model.js';
class Note extends BaseModel {
static tableName() {
return 'notes';
}
static noteById(notes, id) {
for (let i = 0; i < notes.length; i++) {
if (notes[i].id == id) return notes[i];
@ -9,6 +13,14 @@ class Note extends BaseModel {
return null;
}
static newNote() {
return {
id: null,
title: '',
body: '',
}
}
}
export { Note };

View File

@ -24,6 +24,8 @@ let defaultState = {
const reducer = (state = defaultState, action) => {
Log.info('Reducer action', action);
Log.info('DB LA', Note.db());
let newState = state;
switch (action.type) {
@ -86,24 +88,61 @@ const NotesScreen = connect(
)(NotesScreenComponent)
class NoteScreenComponent extends React.Component {
static navigationOptions = {
title: 'Note',
};
constructor() {
super();
this.state = { note: Note.newNote() }
}
componentWillMount() {
this.setState({ note: this.props.note });
}
noteComponent_onChange = (propName, propValue) => {
this.setState((prevState, props) => {
let note = Object.assign({}, prevState.note);
note[propName] = propValue;
return { note: note }
});
}
title_onChangeText = (text) => {
this.noteComponent_onChange('title', text);
}
body_onChangeText = (text) => {
this.noteComponent_onChange('body', text);
}
render() {
let note = this.props.note;
// <Button title="Save note" />
let onSaveButtonPress = () => {
return this.props.onSaveButtonPress(this.state.note);
}
return (
<View style={{flex: 1}}>
<TextInput style={{flex: 1, textAlignVertical: 'top'}} multiline={true} value={note ? note.body : ''} />
<TextInput value={this.state.note.title} onChangeText={this.title_onChangeText} />
<TextInput style={{flex: 1, textAlignVertical: 'top'}} multiline={true} value={this.state.note.body} onChangeText={this.body_onChangeText} />
<Button title="Save note" onPress={onSaveButtonPress} />
</View>
);
}
}
const NoteScreen = connect(
(state) => {
let selectedNote = state.selectedNoteId ? Note.noteById(state.notes, state.selectedNoteId) : null;
return { note: selectedNote };
return {
note: state.selectedNoteId ? Note.noteById(state.notes, state.selectedNoteId) : Note.newNote(),
onSaveButtonPress: (note) => {
Log.info(note);
}
};
},
(dispatch) => {
return {};