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)=> {
|
|
|
|
return (
|
|
|
|
DatabaseStore.modelify(Message, [draftClientId])
|
|
|
|
.then((messages)=> {
|
2016-02-24 05:52:08 +08:00
|
|
|
const {accountId} = messages[0];
|
|
|
|
return NylasAPI.authPlugin(this.pluginId, PLUGIN_NAME, accountId);
|
2016-02-19 02:00:11 +08:00
|
|
|
})
|
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-24 05:52:08 +08:00
|
|
|
})
|
|
|
|
);
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
onSendLater = (draftClientId, sendLaterDate)=> {
|
|
|
|
this.setMetadata(draftClientId, {sendLaterDate})
|
|
|
|
};
|
|
|
|
|
|
|
|
onCancelSendLater = (draftClientId)=> {
|
|
|
|
this.setMetadata(draftClientId, {sendLaterDate: null})
|
|
|
|
};
|
|
|
|
|
|
|
|
deactivate = ()=> {
|
|
|
|
this.unsubscribers.forEach(unsub => unsub())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default new SendLaterStore()
|