Commit e0e14dff authored by Alexander Turinske's avatar Alexander Turinske

Simplify alert messaging

- decouple messaging from error
- update tests
parent 24f161ed
<script> <script>
import { GlAlert, GlLink, GlSprintf } from '@gitlab/ui'; import { GlAlert, GlLink, GlSprintf } from '@gitlab/ui';
import { ERRORS } from '../constants';
export default { export default {
ERRORS,
components: { components: {
GlAlert, GlAlert,
GlLink, GlLink,
...@@ -16,18 +14,7 @@ export default { ...@@ -16,18 +14,7 @@ export default {
}, },
wikiPagePath: { wikiPagePath: {
type: String, type: String,
required: false, required: true,
default: '',
},
},
computed: {
errorMessage() {
if (this.error === this.$options.ERRORS.PAGE_CHANGE.ERROR) {
return this.$options.ERRORS.PAGE_CHANGE.MESSAGE;
} else if (this.error === this.$options.ERRORS.PAGE_RENAME.ERROR) {
return this.$options.ERRORS.PAGE_RENAME.MESSAGE;
}
return this.error;
}, },
}, },
}; };
...@@ -35,7 +22,7 @@ export default { ...@@ -35,7 +22,7 @@ export default {
<template> <template>
<gl-alert variant="danger" :dismissible="false"> <gl-alert variant="danger" :dismissible="false">
<gl-sprintf :message="errorMessage"> <gl-sprintf :message="error">
<template #wikiLink="{ content }"> <template #wikiLink="{ content }">
<gl-link :href="wikiPagePath" target="_blank">{{ content }}</gl-link> <gl-link :href="wikiPagePath" target="_blank">{{ content }}</gl-link>
</template> </template>
......
import { s__ } from '~/locale';
export const ERRORS = {
PAGE_CHANGE: {
ERROR: 'WikiPage::PageChangedError',
MESSAGE: s__(
'WikiPageConflictMessage|Someone edited the page the same time you did. Please check out %{wikiLinkStart}the page%{wikiLinkEnd} and make sure your changes will not unintentionally remove theirs.',
),
},
PAGE_RENAME: {
ERROR: 'WikiPage::PageRenameError',
MESSAGE: s__('WikiEdit|There is already a page with the same title in that path.'),
},
};
...@@ -116,7 +116,7 @@ module WikiActions ...@@ -116,7 +116,7 @@ module WikiActions
render 'shared/wikis/edit' render 'shared/wikis/edit'
end end
rescue WikiPage::PageChangedError, WikiPage::PageRenameError => e rescue WikiPage::PageChangedError, WikiPage::PageRenameError => e
@error = e @error = e.message
render 'shared/wikis/edit' render 'shared/wikis/edit'
end end
# rubocop:enable Gitlab/ModuleWithInstanceVariables # rubocop:enable Gitlab/ModuleWithInstanceVariables
......
...@@ -205,14 +205,15 @@ class WikiPage ...@@ -205,14 +205,15 @@ class WikiPage
last_commit_sha = attrs.delete(:last_commit_sha) last_commit_sha = attrs.delete(:last_commit_sha)
if last_commit_sha && last_commit_sha != self.last_commit_sha if last_commit_sha && last_commit_sha != self.last_commit_sha
raise PageChangedError raise PageChangedError, s_(
'WikiPageConflictMessage|Someone edited the page the same time you did. Please check out %{wikiLinkStart}the page%{wikiLinkEnd} and make sure your changes will not unintentionally remove theirs.')
end end
update_attributes(attrs) update_attributes(attrs)
if title.present? && title_changed? && wiki.find_page(title).present? if title.present? && title_changed? && wiki.find_page(title).present?
attributes[:title] = page.title attributes[:title] = page.title
raise PageRenameError raise PageRenameError, s_('WikiEdit|There is already a page with the same title in that path.')
end end
save do save do
......
- wiki_page_title @page, @page.persisted? ? _('Edit') : _('New') - wiki_page_title @page, @page.persisted? ? _('Edit') : _('New')
- add_page_specific_style 'page_bundles/wiki' - add_page_specific_style 'page_bundles/wiki'
- errorMessage = @error || ''
- if @error - if @error
#js-wiki-error{ data: { error: errorMessage, wiki_page_path: wiki_page_path(@wiki, @page) } } #js-wiki-error{ data: { error: @error, wiki_page_path: wiki_page_path(@wiki, @page) } }
.wiki-page-header.top-area.has-sidebar-toggle.flex-column.flex-lg-row .wiki-page-header.top-area.has-sidebar-toggle.flex-column.flex-lg-row
= wiki_sidebar_toggle_button = wiki_sidebar_toggle_button
......
import { GlAlert, GlLink, GlSprintf } from '@gitlab/ui'; import { GlAlert, GlLink, GlSprintf } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import WikiAlert from '~/pages/shared/wikis/components/wiki_alert.vue'; import WikiAlert from '~/pages/shared/wikis/components/wiki_alert.vue';
import { ERRORS } from '~/pages/shared/wikis/constants';
describe('WikiAlert', () => { describe('WikiAlert', () => {
let wrapper; let wrapper;
const ERROR = 'There is already a page with the same title in that path.';
const ERROR_WITH_LINK =
'Someone edited the page the same time you did. Please check out %{wikiLinkStart}the page%{wikiLinkEnd} and make sure your changes will not unintentionally remove theirs.';
const PATH = '/test';
function createWrapper(propsData = {}, stubs = {}) { function createWrapper(propsData = {}, stubs = {}) {
wrapper = shallowMount(WikiAlert, { wrapper = shallowMount(WikiAlert, {
propsData, propsData: { wikiPagePath: PATH, ...propsData },
stubs, stubs,
}); });
} }
...@@ -24,32 +27,15 @@ describe('WikiAlert', () => { ...@@ -24,32 +27,15 @@ describe('WikiAlert', () => {
describe('Wiki Alert', () => { describe('Wiki Alert', () => {
it('does show an alert when there is an error', () => { it('does show an alert when there is an error', () => {
createWrapper({ error: ERRORS.PAGE_RENAME.ERROR }); createWrapper({ error: ERROR });
expect(findGlAlert().exists()).toBe(true); expect(findGlAlert().exists()).toBe(true);
});
it('does show the page change message text', () => {
createWrapper({ error: ERRORS.PAGE_CHANGE.ERROR });
expect(findGlSprintf().exists()).toBe(true); expect(findGlSprintf().exists()).toBe(true);
const text = findGlSprintf(); expect(findGlSprintf().attributes('message')).toBe(ERROR);
expect(text.attributes('message')).toBe(ERRORS.PAGE_CHANGE.MESSAGE);
});
it('does show the page rename message text', () => {
createWrapper({ error: ERRORS.PAGE_RENAME.ERROR });
expect(findGlSprintf().attributes('message')).toBe(ERRORS.PAGE_RENAME.MESSAGE);
});
it('does show the error message', () => {
const error = 'test message';
createWrapper({ error });
expect(findGlSprintf().attributes('message')).toBe(error);
}); });
it('does show the link to the help path', () => { it('does show the link to the help path', () => {
const wikiPagePath = '/help'; createWrapper({ error: ERROR_WITH_LINK }, { GlAlert, GlSprintf });
createWrapper({ error: ERRORS.PAGE_CHANGE.ERROR, wikiPagePath }, { GlAlert, GlSprintf }); expect(findGlLink().attributes('href')).toBe(PATH);
expect(findGlLink().attributes('href')).toBe(wikiPagePath);
}); });
}); });
}); });
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