Created
May 20, 2020 09:14
-
-
Save Maartz/4c7931865cd947eff09ad0b1a91bd57a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule LiveViewStudioWeb.LightLive do | |
use LiveViewStudioWeb, :live_view | |
def mount(_params, _session, socket) do | |
socket = assign(socket, :brightness, 10) | |
{:ok, socket} | |
end | |
def render(assigns) do | |
~L""" | |
<h1>Front Porch Light</h1> | |
<div id="light"> | |
<div class="meter"> | |
<span style="width: <%= @brightness %>%"> | |
<%= @brightness %>% | |
</div> | |
<button phx-click="off"> | |
<img src="images/light-off.svg"/> | |
</button> | |
<button phx-click="down"> | |
<img src="images/down.svg"/> | |
</button> | |
<button phx-click="up"> | |
<img src="images/up.svg"/> | |
</button> | |
<button phx-click="on"> | |
<img src="images/light-on.svg"/> | |
</button> | |
</div> | |
""" | |
end | |
def handle_event("on", _metadata, socket) do | |
socket = assign(socket, :brightness, 100) | |
{:noreply, socket} | |
end | |
def handle_event("off", _metadata, socket) do | |
socket = assign(socket, :brightness, 0) | |
{:noreply, socket} | |
end | |
def handle_event("up", _metadata, socket) do | |
socket = update(socket, :brightness, &min(&1 + 10, 100)) | |
{:noreply, socket} | |
end | |
def handle_event("down", _metadata, socket) do | |
socket = update(socket, :brightness, &max(&1 - 10, 0)) | |
{:noreply, socket} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment