add tracking saved to db
This commit is contained in:
parent
1af37846bd
commit
42e025af12
|
@ -8,7 +8,7 @@ defmodule PokemonCouture.Accounts.User do
|
||||||
field :password, :string, virtual: true
|
field :password, :string, virtual: true
|
||||||
field :hashed_password, :string
|
field :hashed_password, :string
|
||||||
field :confirmed_at, :naive_datetime
|
field :confirmed_at, :naive_datetime
|
||||||
many_to_many :clothes, PokemonCouture.Shops.Clothes, join_through: "ownerships", unique: :true
|
many_to_many :clothes, PokemonCouture.Shops.Clothes, join_through: "ownerships", unique: :true, on_replace: :delete
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,6 +21,9 @@ defmodule PokemonCouture.Shops do
|
||||||
Repo.all(Clothes)
|
Repo.all(Clothes)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def list_clothes_with_owners do
|
||||||
|
Repo.all(from c in Clothes, preload: [:users])
|
||||||
|
end
|
||||||
@doc """
|
@doc """
|
||||||
Gets a single clothes.
|
Gets a single clothes.
|
||||||
|
|
||||||
|
@ -101,4 +104,18 @@ defmodule PokemonCouture.Shops do
|
||||||
def change_clothes(%Clothes{} = clothes, attrs \\ %{}) do
|
def change_clothes(%Clothes{} = clothes, attrs \\ %{}) do
|
||||||
Clothes.changeset(clothes, attrs)
|
Clothes.changeset(clothes, attrs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_owner(clothes, user) do
|
||||||
|
clothes
|
||||||
|
|> Ecto.Changeset.change()
|
||||||
|
|> Ecto.Changeset.put_assoc(:users, [user | clothes.users])
|
||||||
|
|> Repo.update!()
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_owner(clothes, user) do
|
||||||
|
clothes
|
||||||
|
|> Ecto.Changeset.change()
|
||||||
|
|> Ecto.Changeset.put_assoc(:users, List.delete(clothes.users, user))
|
||||||
|
|> Repo.update!()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ defmodule PokemonCouture.Shops.Clothes do
|
||||||
field :game, :string
|
field :game, :string
|
||||||
field :location, :string
|
field :location, :string
|
||||||
|
|
||||||
many_to_many :users, PokemonCouture.Accounts.User, join_through: "ownerships", unique: :true
|
many_to_many :users, PokemonCouture.Accounts.User, join_through: "ownerships", unique: :true, on_replace: :delete
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,6 +4,7 @@ defmodule PokemonCoutureWeb.ClothesTrackerLive do
|
||||||
alias PokemonCouture.Shops
|
alias PokemonCouture.Shops
|
||||||
alias PokemonCouture.Shops.Clothes
|
alias PokemonCouture.Shops.Clothes
|
||||||
alias PokemonCouture.Live.Components.ClothesComponent
|
alias PokemonCouture.Live.Components.ClothesComponent
|
||||||
|
alias PokemonCouture.Accounts
|
||||||
|
|
||||||
def create_shop_map(clothes, map) do
|
def create_shop_map(clothes, map) do
|
||||||
case map[clothes.location] do
|
case map[clothes.location] do
|
||||||
|
@ -14,14 +15,13 @@ defmodule PokemonCoutureWeb.ClothesTrackerLive do
|
||||||
end
|
end
|
||||||
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 =
|
||||||
socket
|
socket
|
||||||
|> assign(:light_bulb_status, "off")
|
|> assign(:clothes_map, clothes_map)
|
||||||
|> assign(:on_button_status, "")
|
|> assign(:user, user)
|
||||||
|> assign(:off_button_status, "disabled")
|
|
||||||
|> assign(:active, true)
|
|
||||||
|> assign(:clothes_map, Enum.reduce(Shops.list_clothes(), %{}, &create_shop_map/2))
|
|
||||||
{:ok, socket}
|
{:ok, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<%= for clothes <- list_of_clothes do %>
|
<%= 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 %>
|
<% end %>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
defmodule PokemonCoutureWeb.Components.ClothesComponent do
|
defmodule PokemonCoutureWeb.Components.ClothesComponent do
|
||||||
use Phoenix.LiveComponent
|
use Phoenix.LiveComponent
|
||||||
|
|
||||||
|
alias PokemonCouture.Shops
|
||||||
def mount(socket) do
|
def mount(socket) do
|
||||||
socket =
|
|
||||||
socket
|
|
||||||
|> assign(active: true)
|
|
||||||
{:ok, socket}
|
{:ok, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -20,13 +18,12 @@ defmodule PokemonCoutureWeb.Components.ClothesComponent do
|
||||||
phx-click="toggle-active"
|
phx-click="toggle-active"
|
||||||
phx-target="<%= @myself %>"
|
phx-target="<%= @myself %>"
|
||||||
>
|
>
|
||||||
<%= if @active do %>
|
<%= if @user in @clothes.users do %>
|
||||||
Got it
|
|
||||||
<% else %>
|
|
||||||
X
|
X
|
||||||
|
<% else %>
|
||||||
|
Got it
|
||||||
<% end %>
|
<% end %>
|
||||||
</button>
|
</button>
|
||||||
<!-- here be acquire buton -->
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
"""
|
"""
|
||||||
|
@ -34,9 +31,12 @@ defmodule PokemonCoutureWeb.Components.ClothesComponent do
|
||||||
|
|
||||||
|
|
||||||
def handle_event("toggle-active", _value, socket) do
|
def handle_event("toggle-active", _value, socket) do
|
||||||
socket =
|
new_clothes = if socket.assigns.user in socket.assigns.clothes.users do
|
||||||
socket
|
Shops.remove_owner(socket.assigns.clothes, socket.assigns.user)
|
||||||
|> assign(:active, not socket.assigns.active)
|
else
|
||||||
|
Shops.add_owner(socket.assigns.clothes, socket.assigns.user)
|
||||||
|
end
|
||||||
|
socket = assign(socket, :clothes, new_clothes)
|
||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue