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

Update note

This commit is contained in:
Laurent Cozic 2017-05-12 19:54:06 +00:00
parent 38990d1125
commit a0472274a6
5 changed files with 62 additions and 39 deletions

View File

@ -8,18 +8,25 @@ class BaseModel {
throw new Error('Must be overriden');
}
static useUuid() {
return false;
}
static save(o) {
let isNew = !o.id;
if (isNew) o.id = createUuid();
let query = '';
if (isNew) {
let q = Database.insertQuery(this.tableName(), o);
return this.db().insert(q.sql, q.params).then(() => {
return o;
});
if (this.useUuid()) o.id = createUuid();
query = Database.insertQuery(this.tableName(), o);
} else {
Log.error('NOT EIMPLEMETNED');
// TODO: update
let where = { id: o.id };
let temp = Object.assign({}, o);
delete temp.id;
query = Database.updateQuery(this.tableName(), temp, where);
}
return this.db().exec(query.sql, query.params).then(() => { return o; });
}
static setDb(database) {

View File

@ -23,7 +23,6 @@ class ItemListComponent extends Component {
}
render() {
Log.info('RENDER');
let renderRow = (rowData) => {
let onPress = () => {
this.props.onItemClick(rowData.id)
@ -34,10 +33,13 @@ class ItemListComponent extends Component {
</TouchableHighlight>
);
}
// `enableEmptySections` is to fix this warning: https://github.com/FaridSafi/react-native-gifted-listview/issues/39
return (
<ListView
dataSource={this.state.dataSource}
renderRow={renderRow}
enableEmptySections={true}
/>
);
}

View File

@ -151,19 +151,7 @@ class Database {
});
}
insert(sql, params = null) {
this.logQuery(sql, params);
return new Promise((resolve, reject) => {
this.db_.executeSql(sql, params, (r) => {
resolve();
}, (error) => {
reject(error);
});
});
}
del(sql, params = null) {
exec(sql, params = null) {
this.logQuery(sql, params);
return new Promise((resolve, reject) => {
@ -193,6 +181,27 @@ class Database {
};
}
static updateQuery(tableName, data, where) {
let sql = '';
let params = [];
for (let key in data) {
if (!data.hasOwnProperty(key)) continue;
if (sql != '') sql += ', ';
sql += key + '=?';
params.push(data[key]);
}
if (typeof where != 'string') {
params.push(where.id);
where = 'id=?';
}
return {
sql: 'UPDATE `' + tableName + '` SET ' + sql + ' WHERE ' + where,
params: params,
};
}
updateSchema() {
Log.info('Checking for database schema update...');

View File

@ -7,6 +7,10 @@ class Note extends BaseModel {
return 'notes';
}
static useUuid() {
return true;
}
static noteById(notes, id) {
for (let i = 0; i < notes.length; i++) {
if (notes[i].id == id) return notes[i];

View File

@ -12,12 +12,7 @@ import { ItemList } from 'src/components/item-list.js'
let defaultState = {
defaultText: 'bla',
notes: [
{ id: 1, title: "hello", body: "just testing\nmultiple\nlines" },
{ id: 2, title: "hello2", body: "2 just testing\nmultiple\nlines" },
{ id: 3, title: "hello3", body: "3 just testing\nmultiple\nlines" },
{ id: 4, title: "hello4", body: "4 just testing\nmultiple\nlines" },
],
notes: [],
selectedNoteId: null,
};
@ -31,7 +26,14 @@ const reducer = (state = defaultState, action) => {
case 'Navigation/NAVIGATE':
case 'Navigation/BACK':
// If the current screen is already the requested screen, don't do anything
const r = state.nav.routes;
if (r.length && r[r.length - 1].routeName == action.routeName) {
return state
}
const nextStateNav = AppNavigator.router.getStateForAction(action, state.nav);
Log.info('NEXT', nextStateNav);
newState = Object.assign({}, state);
if (nextStateNav) {
newState.nav = nextStateNav;
@ -51,7 +53,7 @@ const reducer = (state = defaultState, action) => {
break;
// Insert the note into the note list if it's new, or
// update it if it already exists.
// update it within the note array if it already exists.
case 'NOTES_UPDATE_ONE':
let newNotes = state.notes.splice(0);
@ -79,20 +81,24 @@ const reducer = (state = defaultState, action) => {
let store = createStore(reducer);
class NotesScreenComponent extends React.Component {
static navigationOptions = {
title: 'Notes',
};
createNoteButton_press = () => {
this.props.dispatch({
type: 'Navigation/NAVIGATE',
routeName: 'Note',
});
}
render() {
const { navigate } = this.props.navigation;
return (
<View style={{flex: 1}}>
<ItemList style={{flex: 1}}/>
<Button
title="Create note"
onPress={() =>
navigate('Note')
}
/>
<Button title="Create note" onPress={this.createNoteButton_press} />
</View>
);
}
@ -101,9 +107,6 @@ class NotesScreenComponent extends React.Component {
const NotesScreen = connect(
(state) => {
return {};
},
(dispatch) => {
return {};
}
)(NotesScreenComponent)
@ -139,8 +142,6 @@ class NoteScreenComponent extends React.Component {
}
saveNoteButton_press = () => {
// TODO: if state changes are asynchronous, how to be sure that, when
// the button is presssed, this.state.note contains the actual note?
Note.save(this.state.note).then((note) => {
this.props.dispatch({
type: 'NOTES_UPDATE_ONE',