Commit f42397d1 authored by Jose Ivan Vargas's avatar Jose Ivan Vargas

Add persistent flash messages

parent 84e01b3e
import _ from 'underscore';
import createFlash from '~/flash';
import AccessorUtilities from '~/lib/utils/accessor';
const FLASH_QUEUE_KEY = 'flash-key';
export function popFlashMessage() {
const page = $('body').attr('data-page');
let savedFlashMessages;
let returnVal = false;
if (!page) {
return returnVal;
}
if (AccessorUtilities.isLocalStorageAccessSafe()) {
savedFlashMessages = JSON.parse(window.localStorage.getItem(FLASH_QUEUE_KEY));
const queuedMessage = _.findWhere(savedFlashMessages, { bodyData: page });
if (queuedMessage) {
const queuedMessageIndex = _.findIndex(savedFlashMessages, { bodyData: page });
createFlash(queuedMessage.message, queuedMessage.type);
savedFlashMessages.splice(queuedMessageIndex, 1);
window.localStorage.setItem(FLASH_QUEUE_KEY, JSON.stringify(savedFlashMessages));
}
returnVal = true;
}
return returnVal;
}
export function saveFlashMessage(bodyData, message, type) {
let savedFlashMessages;
if (AccessorUtilities.isLocalStorageAccessSafe()) {
savedFlashMessages = JSON.parse(window.localStorage.getItem(FLASH_QUEUE_KEY));
if (!savedFlashMessages) {
savedFlashMessages = [];
}
savedFlashMessages.push({
bodyData,
message,
type,
});
window.localStorage.setItem(FLASH_QUEUE_KEY, JSON.stringify(savedFlashMessages));
}
}
...@@ -33,6 +33,7 @@ import './milestone_select'; ...@@ -33,6 +33,7 @@ import './milestone_select';
import './projects_dropdown'; import './projects_dropdown';
import './render_gfm'; import './render_gfm';
import initBreadcrumbs from './breadcrumb'; import initBreadcrumbs from './breadcrumb';
import { popFlashMessage } from './lib/utils/flash_queue';
import initDispatcher from './dispatcher'; import initDispatcher from './dispatcher';
...@@ -262,4 +263,5 @@ document.addEventListener('DOMContentLoaded', () => { ...@@ -262,4 +263,5 @@ document.addEventListener('DOMContentLoaded', () => {
} }
initDispatcher(); initDispatcher();
popFlashMessage();
}); });
<script> <script>
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import createFlash from '~/flash'; import createFlash from '~/flash';
import { saveFlashMessage } from '~/lib/utils/flash_queue';
import GlModal from '~/vue_shared/components/gl_modal.vue'; import GlModal from '~/vue_shared/components/gl_modal.vue';
import { redirectTo } from '~/lib/utils/url_utility'; import { redirectTo } from '~/lib/utils/url_utility';
import { s__, sprintf } from '~/locale'; import { s__, sprintf } from '~/locale';
...@@ -36,7 +37,10 @@ ...@@ -36,7 +37,10 @@
return axios.post(this.url) return axios.post(this.url)
.then((response) => { .then((response) => {
eventHub.$emit('promoteMilestoneModal.requestFinished', { milestoneUrl: this.url, successful: true }); eventHub.$emit('promoteMilestoneModal.requestFinished', { milestoneUrl: this.url, successful: true });
redirectTo(response.request.responseURL); const responseURL = new URL(response.request.responseURL);
const bodyData = responseURL.searchParams.get('body_data');
saveFlashMessage(bodyData, `${this.milestoneTitle} promoted to group milestone`, 'notice');
redirectTo(`${responseURL.protocol}//${responseURL.host}${responseURL.pathname}`);
}) })
.catch((error) => { .catch((error) => {
eventHub.$emit('promoteMilestoneModal.requestFinished', { milestoneUrl: this.url, successful: false }); eventHub.$emit('promoteMilestoneModal.requestFinished', { milestoneUrl: this.url, successful: false });
......
<script> <script>
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import { saveFlashMessage } from '~/lib/utils/flash_queue';
import createFlash from '~/flash'; import createFlash from '~/flash';
import GlModal from '~/vue_shared/components/gl_modal.vue'; import GlModal from '~/vue_shared/components/gl_modal.vue';
import { redirectTo } from '~/lib/utils/url_utility'; import { redirectTo } from '~/lib/utils/url_utility';
...@@ -50,7 +51,10 @@ ...@@ -50,7 +51,10 @@
return axios.post(this.url) return axios.post(this.url)
.then((response) => { .then((response) => {
eventHub.$emit('promoteLabelModal.requestFinished', { labelUrl: this.url, successful: true }); eventHub.$emit('promoteLabelModal.requestFinished', { labelUrl: this.url, successful: true });
redirectTo(response.request.responseURL); const responseURL = new URL(response.request.responseURL);
const bodyData = responseURL.searchParams.get('body_data');
saveFlashMessage(bodyData, `${this.labelTitle} promoted to group label`, 'notice');
redirectTo(`${responseURL.protocol}//${responseURL.host}${responseURL.pathname}`);
}) })
.catch((error) => { .catch((error) => {
eventHub.$emit('promoteLabelModal.requestFinished', { labelUrl: this.url, successful: false }); eventHub.$emit('promoteLabelModal.requestFinished', { labelUrl: this.url, successful: false });
......
class Projects::LabelsController < Projects::ApplicationController class Projects::LabelsController < Projects::ApplicationController
include ToggleSubscriptionAction include ToggleSubscriptionAction
include FlashHelper
before_action :check_issuables_available! before_action :check_issuables_available!
before_action :label, only: [:edit, :update, :destroy, :promote] before_action :label, only: [:edit, :update, :destroy, :promote]
...@@ -114,7 +115,7 @@ class Projects::LabelsController < Projects::ApplicationController ...@@ -114,7 +115,7 @@ class Projects::LabelsController < Projects::ApplicationController
respond_to do |format| respond_to do |format|
format.html do format.html do
redirect_to(project_labels_path(@project), status: 303) redirect_to(project_labels_path(@project, body_data: get_body_data_page(project_labels_path(@project))), status: 303)
end end
format.js format.js
end end
......
class Projects::MilestonesController < Projects::ApplicationController class Projects::MilestonesController < Projects::ApplicationController
include MilestoneActions include MilestoneActions
include FlashHelper
before_action :check_issuables_available! before_action :check_issuables_available!
before_action :milestone, only: [:edit, :update, :destroy, :show, :merge_requests, :participants, :labels, :promote] before_action :milestone, only: [:edit, :update, :destroy, :show, :merge_requests, :participants, :labels, :promote]
...@@ -70,8 +71,8 @@ class Projects::MilestonesController < Projects::ApplicationController ...@@ -70,8 +71,8 @@ class Projects::MilestonesController < Projects::ApplicationController
end end
def promote def promote
promoted_milestone = Milestones::PromoteService.new(project, current_user).execute(milestone) Milestones::PromoteService.new(project, current_user).execute(milestone)
redirect_to group_milestone_path(project.group, promoted_milestone.iid), status: 303 redirect_to project_milestones_path(project, body_data: get_body_data_page(project_milestones_path(project))), status: 303
rescue Milestones::PromoteService::PromoteMilestoneError => error rescue Milestones::PromoteService::PromoteMilestoneError => error
redirect_to milestone, alert: error.message redirect_to milestone, alert: error.message
end end
......
module FlashHelper
def get_body_data_page(path)
return unless path.empty? == false
path_controller = Rails.application.routes.recognize_path(path)
[path_controller[:controller].split('/'), path_controller[:action]].compact.join(':')
end
end
...@@ -133,7 +133,7 @@ describe Projects::LabelsController do ...@@ -133,7 +133,7 @@ describe Projects::LabelsController do
it 'gives access' do it 'gives access' do
post :promote, namespace_id: project.namespace.to_param, project_id: project, id: label_1.to_param post :promote, namespace_id: project.namespace.to_param, project_id: project, id: label_1.to_param
expect(response).to redirect_to(namespace_project_labels_path) expect(response.location).to include(namespace_project_labels_path)
end end
it 'promotes the label' do it 'promotes the label' do
......
...@@ -98,9 +98,7 @@ describe Projects::MilestonesController do ...@@ -98,9 +98,7 @@ describe Projects::MilestonesController do
it 'shows group milestone' do it 'shows group milestone' do
post :promote, namespace_id: project.namespace.id, project_id: project.id, id: milestone.iid post :promote, namespace_id: project.namespace.id, project_id: project.id, id: milestone.iid
group_milestone = assigns(:milestone) expect(response.location).to include(project_milestones_path(project))
expect(response).to redirect_to(group_milestone_path(project.group, group_milestone.iid))
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment