From f42397d19a735de2e5ae38725270614826ec05da Mon Sep 17 00:00:00 2001 From: Jose Ivan Vargas Date: Tue, 27 Feb 2018 16:11:09 -0600 Subject: Add persistent flash messages --- app/assets/javascripts/lib/utils/flash_queue.js | 46 ++++++++++++++++++++++ app/assets/javascripts/main.js | 2 + .../shared/components/promote_milestone_modal.vue | 6 ++- .../labels/components/promote_label_modal.vue | 6 ++- 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/lib/utils/flash_queue.js (limited to 'app/assets') diff --git a/app/assets/javascripts/lib/utils/flash_queue.js b/app/assets/javascripts/lib/utils/flash_queue.js new file mode 100644 index 00000000000..f77afddbae3 --- /dev/null +++ b/app/assets/javascripts/lib/utils/flash_queue.js @@ -0,0 +1,46 @@ +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)); + } +} diff --git a/app/assets/javascripts/main.js b/app/assets/javascripts/main.js index 659dc9eaa1f..f76e2ae47a0 100644 --- a/app/assets/javascripts/main.js +++ b/app/assets/javascripts/main.js @@ -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(); }); diff --git a/app/assets/javascripts/pages/milestones/shared/components/promote_milestone_modal.vue b/app/assets/javascripts/pages/milestones/shared/components/promote_milestone_modal.vue index eb49a5780bd..af85c3915df 100644 --- a/app/assets/javascripts/pages/milestones/shared/components/promote_milestone_modal.vue +++ b/app/assets/javascripts/pages/milestones/shared/components/promote_milestone_modal.vue @@ -1,6 +1,7 @@