2016-02-19 02:00:11 +08:00
|
|
|
/** @babel */
|
|
|
|
import NylasStore from 'nylas-store'
|
|
|
|
import {NylasAPI, Actions, Message, Rx, DatabaseStore} from 'nylas-exports'
|
|
|
|
import SendLaterActions from './send-later-actions'
|
|
|
|
import {PLUGIN_ID, PLUGIN_NAME} from './send-later-constants'
|
|
|
|
|
|
|
|
|
|
|
|
class SendLaterStore extends NylasStore {
|
|
|
|
|
|
|
|
constructor(pluginId = PLUGIN_ID) {
|
|
|
|
super()
|
2016-02-24 05:52:08 +08:00
|
|
|
this.pluginId = pluginId;
|
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
|
|
|
}
|
|
|
|
|
2016-02-24 05:52:08 +08:00
|
|
|
getScheduledDateForMessage = (message)=> {
|
2016-02-24 07:40:44 +08:00
|
|
|
if (!message) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-02-24 05:52:08 +08:00
|
|
|
const metadata = message.metadataForPluginId(this.pluginId) || {};
|
|
|
|
return metadata.sendLaterDate || null;
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
setMetadata = (draftClientId, metadata)=> {
|
2016-02-25 03:19:03 +08:00
|
|
|
DatabaseStore.modelify(Message, [draftClientId]).then((messages)=> {
|
|
|
|
const {accountId} = messages[0];
|
|
|
|
|
|
|
|
NylasAPI.authPlugin(this.pluginId, PLUGIN_NAME, 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
|
|
|
};
|
|
|
|
|
|
|
|
onSendLater = (draftClientId, sendLaterDate)=> {
|
2016-02-25 03:19:03 +08:00
|
|
|
this.setMetadata(draftClientId, {sendLaterDate});
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
onCancelSendLater = (draftClientId)=> {
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default new SendLaterStore()
|