2016-02-19 02:00:11 +08:00
|
|
|
/** @babel */
|
|
|
|
import NylasStore from 'nylas-store'
|
2016-02-26 04:33:50 +08:00
|
|
|
import {NylasAPI, Actions, Message, DatabaseStore} from 'nylas-exports'
|
2016-02-19 02:00:11 +08:00
|
|
|
import SendLaterActions from './send-later-actions'
|
|
|
|
import {PLUGIN_ID, PLUGIN_NAME} from './send-later-constants'
|
|
|
|
|
|
|
|
|
|
|
|
class SendLaterStore extends NylasStore {
|
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
constructor(pluginId = PLUGIN_ID, pluginName = PLUGIN_NAME) {
|
2016-02-19 02:00:11 +08:00
|
|
|
super()
|
2016-02-24 05:52:08 +08:00
|
|
|
this.pluginId = pluginId;
|
2016-03-04 04:37:20 +08:00
|
|
|
this.pluginName = pluginName;
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
activate() {
|
|
|
|
this.unsubscribers = [
|
|
|
|
SendLaterActions.sendLater.listen(this.onSendLater),
|
|
|
|
SendLaterActions.cancelSendLater.listen(this.onCancelSendLater),
|
2016-02-24 05:52:08 +08:00
|
|
|
];
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setMetadata = (draftClientId, metadata)=> {
|
2016-03-04 04:37:20 +08:00
|
|
|
return DatabaseStore.modelify(Message, [draftClientId])
|
|
|
|
.then((messages)=> {
|
2016-02-25 03:19:03 +08:00
|
|
|
const {accountId} = messages[0];
|
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
return NylasAPI.authPlugin(this.pluginId, this.pluginName, accountId)
|
2016-02-24 05:52:08 +08:00
|
|
|
.then(()=> {
|
|
|
|
Actions.setMetadata(messages, this.pluginId, metadata);
|
|
|
|
})
|
|
|
|
.catch((error)=> {
|
|
|
|
NylasEnv.reportError(error);
|
2016-02-24 07:40:44 +08:00
|
|
|
NylasEnv.showErrorDialog(`Sorry, we were unable to schedule this message. ${error.message}`);
|
2016-02-25 03:19:03 +08:00
|
|
|
});
|
|
|
|
});
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
recordAction(sendLaterDate, dateLabel) {
|
2016-02-26 04:33:50 +08:00
|
|
|
try {
|
|
|
|
if (sendLaterDate) {
|
2016-02-26 06:01:00 +08:00
|
|
|
const min = Math.round(((new Date(sendLaterDate)).valueOf() - Date.now()) / 1000 / 60);
|
2016-02-26 04:33:50 +08:00
|
|
|
Actions.recordUserEvent("Send Later", {
|
2016-02-26 06:01:00 +08:00
|
|
|
sendLaterTime: min,
|
2016-03-04 04:37:20 +08:00
|
|
|
optionLabel: dateLabel,
|
2016-02-26 04:33:50 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Actions.recordUserEvent("Send Later Cancel");
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
onSendLater = (draftClientId, sendLaterDate, dateLabel)=> {
|
|
|
|
this.recordAction(sendLaterDate, dateLabel)
|
2016-02-25 03:19:03 +08:00
|
|
|
this.setMetadata(draftClientId, {sendLaterDate});
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
onCancelSendLater = (draftClientId)=> {
|
2016-02-26 04:33:50 +08:00
|
|
|
this.recordAction(null)
|
2016-02-25 03:19:03 +08:00
|
|
|
this.setMetadata(draftClientId, {sendLaterDate: null});
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
deactivate = ()=> {
|
2016-02-25 03:19:03 +08:00
|
|
|
this.unsubscribers.forEach(unsub => unsub());
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
export default SendLaterStore
|