I get the first one fine. An event is triggered on the server which changes a couple of values on hidden inputs on the page and liveview refreshes. I catch an event in JS on the browser, read the hidden values and trigger a timer that counts down seconds and moves a progress bar. At this point I can re-click the same button and it does nothing which is fine at this point. But when the progress bar gets to zero I would like to re-activate the button. There are two buttons one for 8 minutes and one for 30. The other button stays active and if I click it it goes to the server and resets the timer to the new countdown and that button goes inactive and the first one becomes active again. The thing is, if the timer isn’t running I want both timed options to be available.
Here is the sequence of code being run:
Browser:
<span id="bar" style={@bar}>
<%= if (@seconds==0), do: "", else: "#{@seconds} secs" %>
</span>
<button phx-click= "on8"> 8 min</button>
<button phx-click="on30">30 min</button>
<input id="max" type="hidden" value={@max}>
<input id="secs" type="hidden" value={@seconds}>
Server:
def handle_event("on8", _, socket) do
seconds = 8 * 60
bar = "width:100%"
socket = assign(socket, %{bar: bar, seconds: seconds, max: seconds})
{:noreply, socket}
end
Browser:
let liveSocket = new LiveSocket("/live", Socket, {
params: { _csrf_token: csrfToken },
hooks: hooks,
dom: {
onBeforeElUpdated(from, to) {
############# catching the update here to trigger the timer #####################
if (from.id=="max") {
if (to.value>1) {
start_timer();
}
}
#####################################
if (from._x_dataStack) {
window.Alpine.clone(from, to);
}
},
},
});
function start_timer() {
if (! timer_running) {
timer_running = true;
setTimeout(process_timer, 1000);
}
}
function process_timer() {
max = document.getElementById('max').value;
seconds = document.getElementById('secs').value;
seconds = seconds - 1;
if (seconds<0) {
seconds = 0;
}
document.getElementById('secs').value = seconds;
var bar = document.getElementById('bar');
if (seconds==0) {
bar.innerHTML= "";
} else {
bar.innerHTML= seconds + " secs";
}
percent = Math.trunc((seconds * 100.0) / max);
bar.style.width = percent + "%";
if (seconds>0) {
setTimeout(process_timer, 1000);
} else {
timer_running = false;
play_sound();
###################### I WISH THIS WORKED! *****************
eval("Push.reset()");
######################I'd like the 8minute button to be re-activated *****************
}
}
You can see my attempt at trying to reach into the liveview code and call some kind of reset to re-activate the button but it doesn’t work.
I hope this is enough for you to see what I am trying to do.
Surely appreciate any help I can get as am new at Elixir/Phoenix although have been coding for 40+ years.
Brett