diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index ae629a1..e4bce3a 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -161,10 +161,11 @@ respond_to do |format| if @plan.update_attributes(attrs) format.html { redirect_to @plan, :editing => false, notice: success_message(_('plan'), _('saved')) } - format.json { head :no_content } + format.json {render json: {code: 1, msg: success_message(_('plan'), _('saved'))}} else flash[:alert] = failed_update_error(@plan, _('plan')) format.html { render action: "edit" } + format.json {render json: {code: 0, msg: failed_update_error(@plan, _('plan'))}} end end end @@ -318,9 +319,9 @@ authorize plan plan.visibility = (params[:is_test] === "1" ? :is_test : :privately_visible) if plan.save - render json: {msg: (plan.is_test? ? _('Your project is now a test.') : _('Your project is no longer a test.') )} + render json: {code: 1, msg: (plan.is_test? ? _('Your project is now a test.') : _('Your project is no longer a test.') )} else - render status: :bad_request, json: {msg: _("Unable to change the plan's test status")} + render status: :bad_request, json: {code: 0, msg: _("Unable to change the plan's test status")} end end diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index 43e0c55..041a0c3 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -50,9 +50,7 @@ UserMailer.permissions_change_notification(@role, current_user).deliver_now render json: {code: 1, msg: "Successfully changed the permissions for #{@role.user.email}. They have been notified via email."} else -# flash[:alert] = failed_create_error(@role, _('role')) - #format.html{ render action: "edit" } - render json: {code: 1, msg: flash[:alert]} + render json: {code: 0, msg: flash[:alert]} end end diff --git a/app/views/plans/_share_form.html.erb b/app/views/plans/_share_form.html.erb index a62fb16..b9bb8d7 100644 --- a/app/views/plans/_share_form.html.erb +++ b/app/views/plans/_share_form.html.erb @@ -1,7 +1,7 @@

<%= _('Set plan visibility') %>

<%= _('Public or organisational visibility is intended for finished plans. You must answer at least one question to enable these options.') %>

-<%= form_for @plan, html: {method: :put} do |f| %> +<%= form_for @plan, html: {method: :put, id: "set_visibility"} do |f| %> >
<%= f.label :visibility, raw("#{f.radio_button :visibility, :privately_visible} #{_('Private: restricted to me and my collaborators')}") %> @@ -38,7 +38,7 @@ <% elsif !role.creator? && role.user == current_user %> <%= display_role(role) %> <% elsif !role.creator? && role.user != current_user %> - <%= form_for role, url: {controller: :roles, action: :update, id: role.id }, html: {method: :put} do |f| %> + <%= form_for role, url: {controller: :roles, action: :update, id: role.id }, html: {method: :put, class: 'change_plan_role'} do |f| %>
<%= f.hidden_field :id %> <%= f.select :access_level, {"#{_('Co-owner')}": 3, "#{_('Editor')}": 2, "#{_('Read only')}": 1}, {}, {id: "#{role.id}-can-edit", class: "toggle-existing-user-access has-tooltip", 'data-toggle': "tooltip", 'title': _('Editors can contribute to plans. Co-owners have additional rights to edit plan details and control access.') } %> diff --git a/lib/assets/javascripts/utils/notificationHelper.js b/lib/assets/javascripts/utils/notificationHelper.js new file mode 100644 index 0000000..bb40b9b --- /dev/null +++ b/lib/assets/javascripts/utils/notificationHelper.js @@ -0,0 +1,28 @@ +import { isString, isObject } from './isType'; +/* + Helpers that will display the specified message in in the notification + area at the top of the page +*/ +export const renderNotice = (msg) => { + const notificationArea = $('#notification-area'); + + if (isString(msg) && isObject(notificationArea)) { + notificationArea.removeClass('alert-warning').addClass('alert-info'); + notificationArea.find('i, span').remove(); + notificationArea.append(` + ${msg}`); + notificationArea.removeClass('hide'); + } +}; + +export const renderAlert = (msg) => { + const notificationArea = $('#notification-area'); + + if (isString(msg) && isObject(notificationArea)) { + notificationArea.removeClass('alert-info').addClass('alert-warning'); + notificationArea.find('i, span').remove(); + notificationArea.append(` + ${msg}`); + notificationArea.removeClass('hide'); + } +}; diff --git a/lib/assets/javascripts/views/plans/index.js b/lib/assets/javascripts/views/plans/index.js index 9124c6b..8ce3de7 100644 --- a/lib/assets/javascripts/views/plans/index.js +++ b/lib/assets/javascripts/views/plans/index.js @@ -1,4 +1,9 @@ -import { PLAN_VISIBILITY_WHEN_TEST, PLAN_VISIBILITY_WHEN_NOT_TEST, PLAN_VISIBILITY_WHEN_NOT_TEST_TOOLTIP } from '../../constants'; +import * as notifier from '../../utils/notificationHelper'; +import { + PLAN_VISIBILITY_WHEN_TEST, + PLAN_VISIBILITY_WHEN_NOT_TEST, + PLAN_VISIBILITY_WHEN_NOT_TEST_TOOLTIP, +} from '../../constants'; $(() => { const checkboxHandler = (el) => { @@ -9,8 +14,11 @@ url: $(form).attr('action'), data: $(form).serializeArray(), }).done((data) => { - $('#notification-area').removeClass('hide').find('span').last() - .html(data.msg); + if (data.code === 1 && data.msg && data.msg !== '') { + notifier.renderNotice(data.msg); + } else { + notifier.renderAlert(data.msg); + } if ($(el).is(':checked')) { $(form).parent().siblings('.plan-visibility').html(PLAN_VISIBILITY_WHEN_TEST) diff --git a/lib/assets/javascripts/views/plans/share.js b/lib/assets/javascripts/views/plans/share.js index e85491e..0d1aece 100644 --- a/lib/assets/javascripts/views/plans/share.js +++ b/lib/assets/javascripts/views/plans/share.js @@ -1,6 +1,34 @@ +import * as notifier from '../../utils/notificationHelper'; import ariatiseForm from '../../utils/ariatiseForm'; $(() => { // Invite Collaborators form on the Share page ariatiseForm({ selector: '#new_role' }); + + const xhrRequest = (el) => { + const form = $(el).closest('form'); + + $.ajax({ + method: $(form).attr('method'), + url: $(form).attr('action'), + data: $(form).serializeArray(), + dataType: 'json', + }).done((data) => { + if (data.code === 1 && data.msg && data.msg !== '') { + notifier.renderNotice(data.msg); + } else { + notifier.renderAlert(data.msg); + } + }, () => { + // TODO adequate error handling for network error + }); + }; + + $('#set_visibility [name="plan[visibility]"]').click((e) => { + xhrRequest(e.currentTarget); + }); + + $('.change_plan_role select').change((e) => { + xhrRequest(e.currentTarget); + }); });