pokemon-couture/lib/pokemon_couture_web/live/components/clothes_component.ex

43 lines
970 B
Elixir
Raw Normal View History

2021-09-13 14:36:14 +02:00
defmodule PokemonCoutureWeb.Components.ClothesComponent do
use Phoenix.LiveComponent
2021-09-14 17:24:08 +02:00
alias PokemonCouture.Shops
2021-09-13 14:36:14 +02:00
def mount(socket) do
{:ok, socket}
end
def render(assigns) do
~L"""
<tr>
<td><%= @clothes.name %></td>
<td><%= @clothes.color %></td>
<td><%= @clothes.game %></td>
<td>
<button
phx-click="toggle-active"
phx-target="<%= @myself %>"
>
2021-09-14 17:24:08 +02:00
<%= if @user in @clothes.users do %>
2021-09-13 14:36:14 +02:00
X
2021-09-14 17:24:08 +02:00
<% else %>
Got it
2021-09-13 14:36:14 +02:00
<% end %>
</button>
</td>
</tr>
"""
end
def handle_event("toggle-active", _value, socket) do
2021-09-14 17:24:08 +02:00
new_clothes = if socket.assigns.user in socket.assigns.clothes.users do
Shops.remove_owner(socket.assigns.clothes, socket.assigns.user)
else
Shops.add_owner(socket.assigns.clothes, socket.assigns.user)
end
socket = assign(socket, :clothes, new_clothes)
2021-09-13 14:36:14 +02:00
{:noreply, socket}
end
end