2016-02-19 02:00:11 +08:00
|
|
|
/** @babel */
|
|
|
|
import NylasStore from 'nylas-store'
|
patch(save): Only save drafts when necessary, avoid sync-engine issues
Summary:
Previously, we have saved drafts back to the user's provider through the sync engine. There are a handful of very serious edge case issues we're working to solve that are creating a bad user experience. (#933, #1175, #1504, #1237)
For now, we're going to change the behavior of N1 to mitagate these issues.
- If you create a draft in N1, we will not sync it to other mail clients while you're working on it.
- If you enable send later, we'll start syncing the draft to the server as before.
- If you created the draft in another client, we'll sync the draft to the server as before.
Fix specs
Test Plan: Run specs
Reviewers: evan, juan
Reviewed By: juan
Differential Revision: https://phab.nylas.com/D2706
2016-03-11 03:03:38 +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
|
|
|
}
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
setMetadata = (draftClientId, metadata) => {
|
2016-03-04 04:37:20 +08:00
|
|
|
return DatabaseStore.modelify(Message, [draftClientId])
|
2016-03-17 10:27:12 +08:00
|
|
|
.then((messages) => {
|
|
|
|
const message = messages[0];
|
2016-02-25 03:19:03 +08:00
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
return NylasAPI.authPlugin(this.pluginId, this.pluginName, message.accountId)
|
|
|
|
.then(() => {
|
|
|
|
Actions.setMetadata(message, this.pluginId, metadata);
|
|
|
|
Actions.ensureDraftSynced(message.clientId);
|
2016-02-24 05:52:08 +08:00
|
|
|
})
|
2016-03-17 10:27:12 +08:00
|
|
|
.catch((error) => {
|
2016-02-24 05:52:08 +08:00
|
|
|
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-17 10:27:12 +08:00
|
|
|
onSendLater = (draftClientId, sendLaterDate, dateLabel) => {
|
2016-03-04 04:37:20 +08:00
|
|
|
this.recordAction(sendLaterDate, dateLabel)
|
2016-03-17 10:27:12 +08:00
|
|
|
this.setMetadata(draftClientId, {sendLaterDate}).then(() => {
|
2016-03-24 08:46:24 +08:00
|
|
|
Actions.closePopover();
|
2016-03-17 10:27:12 +08:00
|
|
|
});
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
2016-03-17 10:27:12 +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
|
|
|
};
|
|
|
|
|
2016-03-17 10:27:12 +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
|