2020-10-18 22:52:10 +02:00
|
|
|
import { CommandRuntime, CommandDeclaration, CommandContext } from 'lib/services/CommandService';
|
2020-10-09 19:35:46 +02:00
|
|
|
import eventManager from 'lib/eventManager';
|
|
|
|
import { _ } from 'lib/locale';
|
2020-10-18 22:52:10 +02:00
|
|
|
import { stateUtils } from 'lib/reducer';
|
2020-07-03 23:32:39 +02:00
|
|
|
const Note = require('lib/models/Note');
|
|
|
|
const { time } = require('lib/time-utils');
|
|
|
|
|
|
|
|
export const declaration:CommandDeclaration = {
|
|
|
|
name: 'editAlarm',
|
|
|
|
label: () => _('Set alarm'),
|
2020-09-15 15:01:07 +02:00
|
|
|
iconName: 'icon-alarm',
|
2020-07-03 23:32:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const runtime = (comp:any):CommandRuntime => {
|
|
|
|
return {
|
2020-10-18 22:52:10 +02:00
|
|
|
execute: async (context:CommandContext, noteId:string = null) => {
|
|
|
|
noteId = noteId || stateUtils.selectedNoteId(context.state);
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
const note = await Note.load(noteId);
|
|
|
|
|
|
|
|
const defaultDate = new Date(Date.now() + 2 * 3600 * 1000);
|
|
|
|
defaultDate.setMinutes(0);
|
|
|
|
defaultDate.setSeconds(0);
|
|
|
|
|
|
|
|
comp.setState({
|
|
|
|
promptOptions: {
|
|
|
|
label: _('Set alarm:'),
|
|
|
|
inputType: 'datetime',
|
|
|
|
buttons: ['ok', 'cancel', 'clear'],
|
|
|
|
value: note.todo_due ? new Date(note.todo_due) : defaultDate,
|
|
|
|
onClose: async (answer:any, buttonType:string) => {
|
|
|
|
let newNote = null;
|
|
|
|
|
|
|
|
if (buttonType === 'clear') {
|
|
|
|
newNote = {
|
|
|
|
id: note.id,
|
|
|
|
todo_due: 0,
|
|
|
|
};
|
|
|
|
} else if (answer !== null) {
|
|
|
|
newNote = {
|
|
|
|
id: note.id,
|
|
|
|
todo_due: answer.getTime(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newNote) {
|
|
|
|
await Note.save(newNote);
|
|
|
|
eventManager.emit('alarmChange', { noteId: note.id, note: newNote });
|
|
|
|
}
|
|
|
|
|
|
|
|
comp.setState({ promptOptions: null });
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-10-18 22:52:10 +02:00
|
|
|
enabledCondition: 'oneNoteSelected && noteIsTodo && !noteTodoCompleted',
|
|
|
|
|
|
|
|
mapStateToTitle: (state:any) => {
|
|
|
|
const note = stateUtils.selectedNote(state);
|
|
|
|
return note && note.todo_due ? time.formatMsToLocal(note.todo_due) : null;
|
2020-07-03 23:32:39 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|