Commit ef82e4c0 authored by Tom Quirk's avatar Tom Quirk

Address maintainer feedback

- check localStorage is available before persisting/retrieving alert
- remove unnecessary async call
parent d0500d6a
import AccessorUtilities from '~/lib/utils/accessor';
import { ALERT_LOCALSTORAGE_KEY } from './constants'; import { ALERT_LOCALSTORAGE_KEY } from './constants';
/** /**
* Persist alert data to localStorage. * Persist alert data to localStorage.
*/ */
export const persistAlert = ({ title, message, linkUrl, variant } = {}) => { export const persistAlert = ({ title, message, linkUrl, variant } = {}) => {
if (!AccessorUtilities.isLocalStorageAccessSafe()) {
return;
}
const payload = JSON.stringify({ title, message, linkUrl, variant }); const payload = JSON.stringify({ title, message, linkUrl, variant });
localStorage.setItem(ALERT_LOCALSTORAGE_KEY, payload); localStorage.setItem(ALERT_LOCALSTORAGE_KEY, payload);
}; };
...@@ -12,9 +17,13 @@ export const persistAlert = ({ title, message, linkUrl, variant } = {}) => { ...@@ -12,9 +17,13 @@ export const persistAlert = ({ title, message, linkUrl, variant } = {}) => {
* Return alert data from localStorage. * Return alert data from localStorage.
*/ */
export const retrieveAlert = () => { export const retrieveAlert = () => {
const initialAlertJSON = window.localStorage.getItem(ALERT_LOCALSTORAGE_KEY); if (!AccessorUtilities.isLocalStorageAccessSafe()) {
return null;
}
const initialAlertJSON = localStorage.getItem(ALERT_LOCALSTORAGE_KEY);
// immediately clean up // immediately clean up
window.localStorage.removeItem(ALERT_LOCALSTORAGE_KEY); localStorage.removeItem(ALERT_LOCALSTORAGE_KEY);
if (!initialAlertJSON) { if (!initialAlertJSON) {
return null; return null;
......
...@@ -127,7 +127,7 @@ describe('JiraConnectApp', () => { ...@@ -127,7 +127,7 @@ describe('JiraConnectApp', () => {
}); });
describe('when alert is set in localStoage', () => { describe('when alert is set in localStoage', () => {
it('renders alert on mount', async () => { it('renders alert on mount', () => {
persistAlert({ message: 'error message' }); persistAlert({ message: 'error message' });
createComponent(); createComponent();
......
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