Only save latlong if saving new note

pull/41/head
Laurent Cozic 2017-05-23 19:01:37 +00:00
parent 5e187e4f1a
commit fde66df646
2 changed files with 21 additions and 24 deletions

View File

@ -43,7 +43,11 @@ class NoteScreenComponent extends React.Component {
}
saveNoteButton_press = () => {
Note.save(this.state.note);
let isNew = !this.state.note.id;
Note.save(this.state.note).then((note) => {
Log.info('NOTE', note);
if (isNew) Note.updateGeolocation(note.id);
});
}
render() {

View File

@ -36,38 +36,31 @@ class Note extends BaseModel {
});
}
static byFolderId() {
static updateGeolocation(noteId) {
Log.info('Updating lat/long of note ' + noteId);
let geoData = null;
return Geolocation.currentPosition().then((data) => {
Log.info('Got lat/long');
geoData = data;
return Note.load(noteId);
}).then((note) => {
if (!note) return; // Race condition note - has been deleted in the meantime
note.longitude = geoData.coords.longitude;
note.latitude = geoData.coords.latitude;
note.altitude = geoData.coords.altitude;
return Note.save(note);
}).catch((error) => {
Log.info('Cannot get location:', error);
});
}
static save(o, options = null) {
if (!options) options = {};
if (!('updateLatLong' in options)) options.updateLatLong = true;
return super.save(o, options).then((note) => {
this.dispatch({
type: 'NOTES_UPDATE_ONE',
note: note,
});
if (options.updateLatLong && !note.latitude && !note.longitude) {
Log.info('Updating lat/long of note...');
let geoData = null;
Geolocation.currentPosition().then((data) => {
Log.info('Got lat/long');
geoData = data;
return Note.load(note.id);
}).then((note) => {
if (!note) return; // Has been deleted in the meantime
note.longitude = geoData.coords.longitude;
note.latitude = geoData.coords.latitude;
note.altitude = geoData.coords.altitude;
Note.save(note, { updateLatLong: false });
}).catch((error) => {
Log.info('Cannot get location:', error);
});
}
return note;
});
}