Background
I have Phoenix LiveView application called web_interface
. This application has a layout file under the normal predefined path: web_interface/lib/web_interface/components/layouts/app.html.heex
.
I want to show the navigation bar of my application only if the following conditions apply:
- A User is logged in
- No operations are currently in progress
Code
To achieve this I have the follow code:
<header>
<%= if Storage.User.has_user?() and not Storage.OperationProgress.in_progress?() do %>
<nav class="bg-gray-800">
<div class="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
<!-- buttons -->
<a
href={~p"/activate"}
class={
if Storage.Button.button_selected?(:activate) do
"bg-gray-900"
else
"hover:bg-gray-700"
end
}
> Activate Btutton </a>
</div>
</nav>
<% end %>
</header>
<main class="h-full mx-auto">
<.flash_group flash={@flash} />
<%= @inner_content %>
</main>
Basically I have a Storage
module that uses ETS to store data, and when the page layout is loaded, I check for my conditions.
Problem
The problem here is that even the button Activate Btutton
changes color correctly (depending on its state, if it is selected or not), the nav
bar does not.
To be more precise, if a user is not logged in, the nav bar does not show (as expected).
However, if an operation is in progress, the nav bar should hide, but it still appears.
What I tried
The normal reaction would be to assume that Storage.OperationProgress.in_progress?()
is returning an incorrect value. This is not the case, as when I start an operation and manually invoke the function, it returns the correct values.
I also tried moving part of the condition into the nav bar’s class:
<nav class={
if not Storage.OperationProgress.in_progress?() do
"hidden"
else
"bg-gray-800"
end
}>
However this also did not work.
At this point I assumed tha app.html.heex
is only run once, when rendering. However this also proved to be false, as the check for the button color does work properly:
if Storage.Button.button_selected?(:activate) do
"bg-gray-900"
else
"hover:bg-gray-700"
end
So at this point I either miss-interpreted something and I am missing something.
Can someone help me trouble shoot?