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

42 lines
986 B
Elixir
Raw Permalink 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 render(assigns) do
~L"""
<tr>
<td><%= @clothes.color %></td>
<td><%= @clothes.game %></td>
<td>
<button
2021-10-05 13:35:43 +02:00
<%= if @user in @clothes.users do %>
class="button button-outline"
<% end %>
2021-09-13 14:36:14 +02:00
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