Skip to content

Commit

Permalink
Add alerts cache control
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJanik committed Dec 11, 2023
1 parent be480ad commit 6bf6ea4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/components/common/alert_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="fixed top-20 inset-x-0 m-4 z-50">
<div class="flex flex-col items-end space-y-4">
<% flash.each do |type, msg| %>
<div data-controller="dismissible-alert" class="max-w-sm w-full pointer-events-auto rounded-md <%= type == 'notice' ? "bg-green-50" : "bg-red-50" %> p-4">
<div data-controller="dismissible-alert" data-turbo-temporary-hide class="max-w-sm w-full pointer-events-auto rounded-md <%= type == 'notice' ? "bg-green-50" : "bg-red-50" %> p-4">
<div class="flex">
<div class="flex-shrink-0">
<% if type == 'notice' %>
Expand Down
17 changes: 14 additions & 3 deletions app/javascript/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,34 @@ application.register('tabs', Tabs)
application.register('popover', Popover)
application.register('toggle', Toggle)
application.register('slideover', Slideover)
document.addEventListener('turbo:before-cache', function(event) {
document.addEventListener('turbo:before-cache', function({ target}) {
const setOpenAsFalse = (attribute) => {
const element = event.target.querySelector(`[${attribute}="true"]`)
const element = target.querySelector(`[${attribute}="true"]`)
element?.setAttribute(attribute, "false")
}

const queryAllAndModify = (selector, modify) => {
target.querySelectorAll(selector).forEach(modify)
}

const addHiddenClass = () => {
event.target.querySelectorAll("[data-turbo-temporary-hide]").forEach((elm) => {
queryAllAndModify("[data-turbo-temporary-hide]", (elm) => {
if (!elm.classList.contains('hidden')) {
elm.classList.add('hidden')
}
})
}

const removeAlerts = () => {
queryAllAndModify("[data-controller=\"dismissible-alert\"][data-turbo-temporary-hide]", (elm) => {
elm.remove()
})
}

addHiddenClass()
setOpenAsFalse('data-slideover-open-value')
setOpenAsFalse('data-dropdown-open-value')
removeAlerts()
})

export { application }

0 comments on commit 6bf6ea4

Please sign in to comment.