From d9e9469f4d6affdbe39109259be14fdc9e8859f6 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 16 Feb 2016 16:41:33 +0100 Subject: [PATCH] Added support to show error message of object was not able to create. --- .../_application_controller_form.coffee | 13 +++++++++++++ .../app/controllers/getting_started.coffee | 8 ++++---- .../javascripts/app/controllers/signup.coffee | 7 +++++-- .../javascripts/app/lib/app_post/ajax.coffee | 1 + .../javascripts/app/lib/spine/ajax.coffee | 18 ++++++++++++++++-- .../app/models/_application_model.coffee | 15 ++++++++++++++- app/controllers/application_controller.rb | 19 ++++++++++++++----- app/controllers/users_controller.rb | 6 +++--- 8 files changed, 70 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller_form.coffee b/app/assets/javascripts/app/controllers/_application_controller_form.coffee index e33e18f45..c5b64db52 100644 --- a/app/assets/javascripts/app/controllers/_application_controller_form.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller_form.coffee @@ -12,6 +12,10 @@ class App.ControllerForm extends App.Controller # set empty class attributes if needed if !@form @form = @formGen() + + # add alert placeholder + @form.prepend('') + if !@model @model = {} if !@attributes @@ -27,9 +31,18 @@ class App.ControllerForm extends App.Controller @form.find('textarea').trigger('change') @form.find('select').trigger('change') + # remove alert on input + @form.on('input', @hideAlert) + @finishForm = true @form + showAlert: (message) => + @form.find('.alert').removeClass('hide').html( App.i18n.translateContent( message ) ) + + hideAlert: => + @form.find('.alert').addClass('hide').html() + html: => @form.html() diff --git a/app/assets/javascripts/app/controllers/getting_started.coffee b/app/assets/javascripts/app/controllers/getting_started.coffee index 3b05f7af4..42ee9acb3 100644 --- a/app/assets/javascripts/app/controllers/getting_started.coffee +++ b/app/assets/javascripts/app/controllers/getting_started.coffee @@ -228,11 +228,11 @@ class Admin extends App.ControllerContent ) @Config.set('system_init_done', true) - fail: (data) => + fail: (settings, details) => @formEnable(e) App.Event.trigger 'notify', { type: 'error' - msg: App.i18n.translateContent( 'Can\'t create user!' ) + msg: App.i18n.translateContent( details.error_human || 'Can\'t create user!' ) timeout: 2500 } ) @@ -984,11 +984,11 @@ class Agent extends App.ControllerContent # rerender page @render() - fail: (data) => + fail: (settings, details) => @formEnable(e) App.Event.trigger 'notify', { type: 'error' - msg: App.i18n.translateContent( 'Can\'t create user!' ) + msg: App.i18n.translateContent( details.error_human || 'Can\'t create user!' ) timeout: 2500 } ) diff --git a/app/assets/javascripts/app/controllers/signup.coffee b/app/assets/javascripts/app/controllers/signup.coffee index 24105b24d..4400949f0 100644 --- a/app/assets/javascripts/app/controllers/signup.coffee +++ b/app/assets/javascripts/app/controllers/signup.coffee @@ -24,7 +24,7 @@ class Index extends App.ControllerContent @html App.view('signup')() - new App.ControllerForm( + @form = new App.ControllerForm( el: @el.find('form') model: App.User screen: 'signup' @@ -34,7 +34,7 @@ class Index extends App.ControllerContent cancel: -> @navigate '#login' - submit: (e) -> + submit: (e) => e.preventDefault() @formDisable(e) @params = @formParam(e.target) @@ -67,6 +67,9 @@ class Index extends App.ControllerContent success: @success error: @error ) + fail: (settings, details) => + @formEnable(e) + @form.showAlert(details.error_human || details.error || 'Unable to update object!') ) success: (data, status, xhr) => diff --git a/app/assets/javascripts/app/lib/app_post/ajax.coffee b/app/assets/javascripts/app/lib/app_post/ajax.coffee index 9f2cf72c8..8bfb5e5db 100644 --- a/app/assets/javascripts/app/lib/app_post/ajax.coffee +++ b/app/assets/javascripts/app/lib/app_post/ajax.coffee @@ -73,6 +73,7 @@ class _ajaxSingleton # do not show any error message with code 401/404 (handled by controllers) return if status is 401 return if status is 404 + return if status is 422 # do not show any error message with code 502 return if status is 502 diff --git a/app/assets/javascripts/app/lib/spine/ajax.coffee b/app/assets/javascripts/app/lib/spine/ajax.coffee index ed2639878..1370780db 100644 --- a/app/assets/javascripts/app/lib/spine/ajax.coffee +++ b/app/assets/javascripts/app/lib/spine/ajax.coffee @@ -166,7 +166,14 @@ class Collection extends Base failResponse: (options) => (xhr, statusText, error, settings) => @model.trigger('ajaxError', null, xhr, statusText, error, settings) - options.fail?.call(@model, settings) + # add errors to calllback + @record.trigger('ajaxError', @record, xhr, statusText, error, settings) + #options.fail?.call(@model, settings) + detailsRaw = xhr.responseText + if !_.isEmpty(detailsRaw) + details = JSON.parse(detailsRaw) + options.fail?.call(@record, settings, details) + # /add errors to calllback class Singleton extends Base constructor: (@record) -> @@ -230,8 +237,15 @@ class Singleton extends Base switch settings.type when 'POST' then @createFailed() when 'DELETE' then @destroyFailed() + # add errors to calllback @record.trigger('ajaxError', @record, xhr, statusText, error, settings) - options.fail?.call(@record, settings) + #options.fail?.call(@record, settings) + detailsRaw = xhr.responseText + if !_.isEmpty(detailsRaw) + details = JSON.parse(detailsRaw) + options.fail?.call(@record, settings, details) + @record.trigger('remove', @record) + # /add errors to calllback createFailed: -> @record.remove(clear: true) diff --git a/app/assets/javascripts/app/models/_application_model.coffee b/app/assets/javascripts/app/models/_application_model.coffee index 39a284abc..d1d182880 100644 --- a/app/assets/javascripts/app/models/_application_model.coffee +++ b/app/assets/javascripts/app/models/_application_model.coffee @@ -307,7 +307,7 @@ class App.Model extends Spine.Model # subscribe and render data / fetch new data if triggered @bind( - 'refresh change' + 'refresh change remove' (items) => App.Log.debug('Model', "local collection refresh/change #{@className}", items) for key, callback of @SUBSCRIPTION_COLLECTION @@ -376,6 +376,19 @@ class App.Model extends Spine.Model item = App[ @className ]._fillUp( item ) callback(item, 'change') ) + @bind( + 'remove' + (items) => + + # check if result is array or singel item + if !_.isArray(items) + items = [items] + App.Log.debug('Model', "local remove #{@className}", items) + for item in items + for key, callback of App[ @className ].SUBSCRIPTION_ITEM[ item.id ] + item = App[ @className ]._fillUp( item ) + callback(item, 'remove') + ) @changeTable = {} @bind( diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6f4cc4170..665abdbed 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -313,7 +313,7 @@ class ApplicationController < ActionController::Base rescue => e logger.error e.message logger.error e.backtrace.inspect - render json: { error: e.message }, status: :unprocessable_entity + render json: model_match_error(e.message), status: :unprocessable_entity end def model_create_render_item (generic_object) @@ -335,7 +335,7 @@ class ApplicationController < ActionController::Base rescue => e logger.error e.message logger.error e.backtrace.inspect - render json: { error: e.message }, status: :unprocessable_entity + render json: model_match_error(e.message), status: :unprocessable_entity end def model_update_render_item (generic_object) @@ -349,7 +349,7 @@ class ApplicationController < ActionController::Base rescue => e logger.error e.message logger.error e.backtrace.inspect - render json: { error: e.message }, status: :unprocessable_entity + render json: model_match_error(e.message), status: :unprocessable_entity end def model_destory_render_item () @@ -369,7 +369,7 @@ class ApplicationController < ActionController::Base rescue => e logger.error e.message logger.error e.backtrace.inspect - render json: { error: e.message }, status: :unprocessable_entity + render json: model_match_error(e.message), status: :unprocessable_entity end def model_show_render_item (generic_object) @@ -382,11 +382,20 @@ class ApplicationController < ActionController::Base rescue => e logger.error e.message logger.error e.backtrace.inspect - render json: { error: e.message }, status: :unprocessable_entity + render json: model_match_error(e.message), status: :unprocessable_entity end def model_index_render_result (generic_objects) render json: generic_objects, status: :ok end + def model_match_error (error) + data = { + error: error + } + if error =~ /(already exists|duplicate key)/i + data[:error_human] = 'Object already exists!' + end + data + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0a82973ba..f23a357b7 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -77,7 +77,7 @@ class UsersController < ApplicationController # check if feature is enabled if !Setting.get('user_create_account') - render json: { error: 'Feature not enabled!' }, status: :unprocessable_entity + render json: { error_human: 'Feature not enabled!' }, status: :unprocessable_entity return end @@ -117,7 +117,7 @@ class UsersController < ApplicationController if user.email exists = User.where(email: user.email.downcase).first if exists - render json: { error: 'User already exists!' }, status: :unprocessable_entity + render json: { error_human: 'User already exists!' }, status: :unprocessable_entity return end end @@ -180,7 +180,7 @@ class UsersController < ApplicationController user_new = User.find(user.id) render json: user_new, status: :created rescue => e - render json: { error: e.message }, status: :unprocessable_entity + render json: model_match_error(e.message), status: :unprocessable_entity end end