From 2983c70729ac99890bc7ed9f64cd783eb1b2dc56 Mon Sep 17 00:00:00 2001 From: Calvin Mclean Date: Mon, 13 Aug 2018 13:52:33 -0700 Subject: [PATCH] Handle failed attempts to create a token If the Atmosphere API fails to create a token, the modal would still act as if it was trying to create the token. Now, it will go back to the initial state of the modal (while still showing the entered name), and will present a notification with an error message. --- .../static/js/actions/APITokenActions.js | 11 ++++++++-- .../modals/api_token/APITokenCreate.jsx | 21 +++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/troposphere/static/js/actions/APITokenActions.js b/troposphere/static/js/actions/APITokenActions.js index 5eb52478c..83eb94bf5 100644 --- a/troposphere/static/js/actions/APITokenActions.js +++ b/troposphere/static/js/actions/APITokenActions.js @@ -1,9 +1,10 @@ import APITokenConstants from "constants/APITokenConstants"; import APIToken from "models/APIToken"; +import NotificationController from "controllers/NotificationController"; import Utils from "./Utils"; export default { - create: ({name, atmo_user}, callback) => { + create: ({name, atmo_user}, successCallback, failCallback) => { if (!name) throw new Error("Missing Token name"); if (!atmo_user) throw new Error("Missing Token author"); let apiToken = new APIToken({ @@ -18,10 +19,16 @@ export default { .save() .done(() => { Utils.dispatch(APITokenConstants.UPDATE_TOKEN, {apiToken}); - callback(apiToken); + successCallback(apiToken); }) .fail(() => { Utils.dispatch(APITokenConstants.REMOVE_TOKEN, {apiToken}); + NotificationController.error( + "Error creating token.", + "Your login might be expired. If you continue to see this error " + + "after logging in again, contact support." + ); + failCallback(apiToken); }); return apiToken; }, diff --git a/troposphere/static/js/components/modals/api_token/APITokenCreate.jsx b/troposphere/static/js/components/modals/api_token/APITokenCreate.jsx index 8af96ac02..2fa10ca13 100644 --- a/troposphere/static/js/components/modals/api_token/APITokenCreate.jsx +++ b/troposphere/static/js/components/modals/api_token/APITokenCreate.jsx @@ -37,12 +37,21 @@ export default React.createClass({ this.setState({ isSubmitting: true }); - actions.APITokenActions.create(attributes, (response) => { - this.setState({ - isSubmitting: false, - successView: true, - hash: response.changed.token - }); + actions.APITokenActions.create(attributes, this.successCallback, this.failCallback); + }, + + successCallback(response) { + this.setState({ + isSubmitting: false, + successView: true, + hash: response.changed.token + }); + }, + + failCallback(response) { + this.setState({ + isSubmitting: false, + successView: false }); },