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';
import './projects_dropdown';
import './render_gfm';
import initBreadcrumbs from './breadcrumb';
import { popFlashMessage } from './lib/utils/flash_queue';
import initDispatcher from './dispatcher';
......@@ -262,4 +263,5 @@ document.addEventListener('DOMContentLoaded', () => {
}
initDispatcher();
popFlashMessage();
});
<script>
import axios from '~/lib/utils/axios_utils';
import createFlash from '~/flash';
import { saveFlashMessage } from '~/lib/utils/flash_queue';
import GlModal from '~/vue_shared/components/gl_modal.vue';
import { redirectTo } from '~/lib/utils/url_utility';
import { s__, sprintf } from '~/locale';
......@@ -36,7 +37,10 @@
return axios.post(this.url)
.then((response) => {
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) => {
eventHub.$emit('promoteMilestoneModal.requestFinished', { milestoneUrl: this.url, successful: false });
......
<script>
import axios from '~/lib/utils/axios_utils';
import { saveFlashMessage } from '~/lib/utils/flash_queue';
import createFlash from '~/flash';
import GlModal from '~/vue_shared/components/gl_modal.vue';
import { redirectTo } from '~/lib/utils/url_utility';
......@@ -50,7 +51,10 @@
return axios.post(this.url)
.then((response) => {
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) => {
eventHub.$emit('promoteLabelModal.requestFinished', { labelUrl: this.url, successful: false });
......
class Projects::LabelsController < Projects::ApplicationController
include ToggleSubscriptionAction
include FlashHelper
before_action :check_issuables_available!
before_action :label, only: [:edit, :update, :destroy, :promote]
......@@ -114,7 +115,7 @@ class Projects::LabelsController < Projects::ApplicationController
respond_to do |format|
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
format.js
end
......
class Projects::MilestonesController < Projects::ApplicationController
include MilestoneActions
include FlashHelper
before_action :check_issuables_available!
before_action :milestone, only: [:edit, :update, :destroy, :show, :merge_requests, :participants, :labels, :promote]
......@@ -70,8 +71,8 @@ class Projects::MilestonesController < Projects::ApplicationController
end
def promote
promoted_milestone = Milestones::PromoteService.new(project, current_user).execute(milestone)
redirect_to group_milestone_path(project.group, promoted_milestone.iid), status: 303
Milestones::PromoteService.new(project, current_user).execute(milestone)
redirect_to project_milestones_path(project, body_data: get_body_data_page(project_milestones_path(project))), status: 303
rescue Milestones::PromoteService::PromoteMilestoneError => error
redirect_to milestone, alert: error.message
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
it 'gives access' do
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
it 'promotes the label' do
......
......@@ -98,9 +98,7 @@ describe Projects::MilestonesController do
it 'shows group milestone' do
post :promote, namespace_id: project.namespace.id, project_id: project.id, id: milestone.iid
group_milestone = assigns(:milestone)
expect(response).to redirect_to(group_milestone_path(project.group, group_milestone.iid))
expect(response.location).to include(project_milestones_path(project))
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