add tracking saved to db

This commit is contained in:
2021-09-14 17:24:08 +02:00
parent 1af37846bd
commit 42e025af12
6 changed files with 36 additions and 19 deletions
@@ -4,6 +4,7 @@ defmodule PokemonCoutureWeb.ClothesTrackerLive do
alias PokemonCouture.Shops
alias PokemonCouture.Shops.Clothes
alias PokemonCouture.Live.Components.ClothesComponent
alias PokemonCouture.Accounts
def create_shop_map(clothes, map) do
case map[clothes.location] do
@@ -14,14 +15,13 @@ defmodule PokemonCoutureWeb.ClothesTrackerLive do
end
end
def mount(_params, _session, socket) do
def mount(_params, session, socket) do
user = Accounts.get_user_by_session_token(session["user_token"])
clothes_map = Enum.reduce(Shops.list_clothes_with_owners(), %{}, &create_shop_map/2)
socket =
socket
|> assign(:light_bulb_status, "off")
|> assign(:on_button_status, "")
|> assign(:off_button_status, "disabled")
|> assign(:active, true)
|> assign(:clothes_map, Enum.reduce(Shops.list_clothes(), %{}, &create_shop_map/2))
|> assign(:clothes_map, clothes_map)
|> assign(:user, user)
{:ok, socket}
end
@@ -12,7 +12,7 @@
</thead>
<tbody>
<%= for clothes <- list_of_clothes do %>
<%= live_component PokemonCoutureWeb.Components.ClothesComponent, id: clothes.id, clothes: clothes %>
<%= live_component PokemonCoutureWeb.Components.ClothesComponent, id: clothes.id, clothes: clothes, user: @user %>
<% end %>
</tbody>
</table>
@@ -1,10 +1,8 @@
defmodule PokemonCoutureWeb.Components.ClothesComponent do
use Phoenix.LiveComponent
alias PokemonCouture.Shops
def mount(socket) do
socket =
socket
|> assign(active: true)
{:ok, socket}
end
@@ -20,13 +18,12 @@ defmodule PokemonCoutureWeb.Components.ClothesComponent do
phx-click="toggle-active"
phx-target="<%= @myself %>"
>
<%= if @active do %>
Got it
<% else %>
<%= if @user in @clothes.users do %>
X
<% else %>
Got it
<% end %>
</button>
<!-- here be acquire buton -->
</td>
</tr>
"""
@@ -34,9 +31,12 @@ defmodule PokemonCoutureWeb.Components.ClothesComponent do
def handle_event("toggle-active", _value, socket) do
socket =
socket
|> assign(:active, not socket.assigns.active)
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)
{:noreply, socket}
end
end