From e863349531064321bb6c63a772a1f16a50131397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Tue, 1 Mar 2016 12:55:27 +0100 Subject: [PATCH] Refactor to comply with PR feedback --- .../ascribe_buttons/s3_download_button.js | 55 +++++++++---------- js/stores/notification_store.js | 4 -- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/js/components/ascribe_buttons/s3_download_button.js b/js/components/ascribe_buttons/s3_download_button.js index 72b22115..9f7c355a 100644 --- a/js/components/ascribe_buttons/s3_download_button.js +++ b/js/components/ascribe_buttons/s3_download_button.js @@ -41,42 +41,41 @@ const S3DownloadButton = React.createClass({ window.clearInterval(this.state.signatureExpiryTime); }, - componentDidUpdate() { - const { signatureExpiryTimerId, downloadUrl } = this.state; - - if(!signatureExpiryTimerId && downloadUrl) { - /** - * The signed url, however can expire, which is why - * we need to renew it when it expires. - */ - const { downloadUrl } = this.state; - const expires = parseInt(queryParamsToArgs(downloadUrl.split('?')[1]).expires, 10); - const now = new Date().getTime() / 1000; - - /** - * Amazon uses seconds as their signature unix timestamp - * while `setInterval` uses milliseconds. Therefore we need to - * multiply with 1000. - */ - const interval = (expires - now) * 1000; - - this.setState({ - // Substract 5s to make sure there is a big enough window to sign again before expiration - signatureExpiryTimerId: window.setInterval(this.signUrl, interval - 5000) - }); - } - }, - transformS3UrlToS3Key(url) { return url.replace(`https://${AppConstants.cloudfrontDomain}/`, ''); }, - signUrl(next) { + signUrl() { const { url, title, artistName } = this.props; S3Fetcher .signUrl(this.transformS3UrlToS3Key(url), title, artistName) - .then(({ signed_url: downloadUrl }) => this.setState({ downloadUrl }, next)) + .then(({ signed_url: downloadUrl }) => { + const { signatureExpiryTimerId } = this.state; + let newState = { downloadUrl }; + + if(!signatureExpiryTimerId) { + /** + * The signed url, however can expire, which is why + * we need to renew it when it expires. + */ + const expires = parseInt(queryParamsToArgs(downloadUrl.split('?')[1]).expires, 10); + const now = new Date().getTime() / 1000; + + /** + * Amazon uses seconds as their signature unix timestamp + * while `setInterval` uses milliseconds. Therefore we need to + * multiply with 1000. + */ + const interval = (expires - now) * 1000; + + Object.assign(newState, { + // Substract 5s to make sure there is a big enough window to sign again before expiration + signatureExpiryTimerId: window.setInterval(this.signUrl, interval - 5000) + }); + } + this.setState(newState); + }) .catch(console.logGlobal); }, diff --git a/js/stores/notification_store.js b/js/stores/notification_store.js index 7a9add9c..515a3730 100644 --- a/js/stores/notification_store.js +++ b/js/stores/notification_store.js @@ -34,10 +34,6 @@ class NotificationStore { this.editionListNotifications = res.notifications; } - onFlushPieceListNotifications() { - this.editionListNotifications = []; - } - onUpdateEditionNotifications(res) { this.editionNotifications = res.notification; }