Skip to content

Commit

Permalink
Uniform alerts (#3769)
Browse files Browse the repository at this point in the history
Unify alerts to be like Rails flashes instead of modals in an attempt to start to move away from SweetAlert.
  • Loading branch information
johrstrom authored Sep 4, 2024
1 parent c62db0c commit 2e5495b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
33 changes: 33 additions & 0 deletions apps/dashboard/app/javascript/alert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

export function alert(message) {
const div = alertDiv(message);
const main = document.getElementById('main_container');
main.prepend(div);
}

function alertDiv(message) {
const span = document.createElement('span');
span.innerText = message;

const div = document.createElement('div');
div.classList.add('alert', 'alert-danger', 'alert-dismissible');
div.setAttribute('role', 'alert');
div.appendChild(span);
div.appendChild(closeButton());

return div;
}

function closeButton() {
const button = document.createElement('button');
button.classList.add('btn-close');
button.dataset.bsDismiss = 'alert';

const span = document.createElement('span');
span.classList.add('sr-only');
span.innerText = 'Close';

button.appendChild(span);

return button;
}
5 changes: 3 additions & 2 deletions apps/dashboard/app/javascript/files/sweet_alert.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Swal from 'sweetalert2'
import Swal from 'sweetalert2';
import { alert } from '../alert';
import {CONTENTID, EVENTNAME as DATATABLE_EVENTNAME} from './data_table.js';

export {EVENTNAME};
Expand All @@ -16,7 +17,7 @@ let sweetAlert = null;
jQuery(function() {
sweetAlert = new SweetAlert();
$(CONTENTID).on(EVENTNAME.showError, function(e,options) {
sweetAlert.alertError(options.title, options.message);
alert(options.message);
});

$(CONTENTID).on(EVENTNAME.showPrompt, function(e,options) {
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</nav>
</header>

<div class="<%= local_assigns[:layout_container_class] || 'container-md' %> content mt-4" role="main">
<div id="main_container" class="<%= local_assigns[:layout_container_class] || 'container-md' %> content mt-4" role="main">

<%= render "layouts/announcements" %>

Expand Down
20 changes: 20 additions & 0 deletions apps/dashboard/test/system/files_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -655,4 +655,24 @@ def setup

assert_equal(expected_links, null_links)
end

test 'allowlist errors flash' do
with_modified_env({ OOD_ALLOWLIST_PATH: Rails.root.to_s }) do
visit(files_url(Rails.root))

alerts = all('.alert')
assert(alerts.empty?)

find('#goto-btn').click
find('#swal2-input').set('/etc')
find('.swal2-confirm').click

alerts = all('.alert')
refute(alerts.empty?)
assert_equal(1, alerts.size)

alert_text = find('.alert > span').text
assert_equal('/etc does not have an ancestor directory specified in ALLOWLIST_PATH', alert_text)
end
end
end

0 comments on commit 2e5495b

Please sign in to comment.