pokemon-couture/lib/pokemon_couture_web/live/clothes_tracker_live.ex

50 lines
1.6 KiB
Elixir
Raw Normal View History

2021-09-08 16:30:37 +02:00
defmodule PokemonCoutureWeb.ClothesTrackerLive do
use PokemonCoutureWeb, :live_view
2021-09-13 14:36:14 +02:00
alias PokemonCouture.Shops
2021-09-14 17:24:08 +02:00
alias PokemonCouture.Accounts
2021-09-24 17:42:14 +02:00
alias PokemonCouture.Shops.Clothes
2021-09-13 14:36:14 +02:00
def create_shop_map(clothes, map) do
case map[clothes.location] do
nil ->
2021-09-24 16:44:36 +02:00
Map.put(map, clothes.location, %{clothes.type => [clothes]})
map_of_clothes when is_map(map_of_clothes) ->
map_of_clothes = case map_of_clothes[clothes.type] do
nil ->
Map.put(map_of_clothes, clothes.type, [clothes])
list_of_clothes when is_list(list_of_clothes) ->
Map.put(map_of_clothes, clothes.type, [clothes | list_of_clothes])
end
Map.put(map, clothes.location, map_of_clothes)
2021-09-13 14:36:14 +02:00
end
end
2021-09-29 15:00:49 +02:00
def assign_clothespiece_to_map(clothes, map) do
case map[clothes.name] do
nil ->
Map.put(map, clothes.name, [clothes])
list_of_clothes when is_list(list_of_clothes) ->
Map.put(map, clothes.name, [clothes | list_of_clothes])
end
end
def inner_map_creator({type, list}, acc) do
clothes_map = Enum.reduce(list, %{}, &assign_clothespiece_to_map/2)
Map.put(acc, type, clothes_map)
2021-09-24 17:42:14 +02:00
end
def mount(_params, %{"user_token" => user_token} = _session, socket) do
user = Accounts.get_user_by_session_token(user_token)
2021-09-14 17:24:08 +02:00
clothes_map = Enum.reduce(Shops.list_clothes_with_owners(), %{}, &create_shop_map/2)
2021-09-24 17:42:14 +02:00
clothes_map = for {shop, map} <- clothes_map
2021-09-29 15:00:49 +02:00
do {shop, Enum.reduce(map, %{}, &inner_map_creator/2)}
2021-09-24 17:42:14 +02:00
end
2021-09-08 16:49:52 +02:00
socket =
socket
2021-09-14 17:24:08 +02:00
|> assign(:clothes_map, clothes_map)
|> assign(:user, user)
2021-09-08 16:30:37 +02:00
{:ok, socket}
end
end