From 74649474974c578e8ea8c40dee51909966553dbe Mon Sep 17 00:00:00 2001 From: shibao Date: Mon, 4 Jul 2022 21:22:59 -0400 Subject: [PATCH] use initial ammo group form component select state --- .../live/ammo_group_live/form_component.ex | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/cannery_web/live/ammo_group_live/form_component.ex b/lib/cannery_web/live/ammo_group_live/form_component.ex index 1dbbc95..df7c433 100644 --- a/lib/cannery_web/live/ammo_group_live/form_component.ex +++ b/lib/cannery_web/live/ammo_group_live/form_component.ex @@ -22,14 +22,23 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do @spec update(Socket.t()) :: {:ok, Socket.t()} def update(%{assigns: %{current_user: current_user}} = socket) do - socket = + %{assigns: %{ammo_types: ammo_types, containers: containers}} = socket = socket |> assign(:ammo_group_create_limit, @ammo_group_create_limit) - |> assign_changeset(%{}) |> assign(:ammo_types, Ammo.list_ammo_types(current_user)) |> assign_new(:containers, fn -> Containers.list_containers(current_user) end) - {:ok, socket} + params = + if ammo_types |> List.first() |> is_nil(), + do: %{}, + else: %{} |> Map.put("ammo_type_id", ammo_types |> List.first() |> Map.get(:id)) + + params = + if containers |> List.first() |> is_nil(), + do: params, + else: params |> Map.put("container_id", containers |> List.first() |> Map.get(:id)) + + {:ok, socket |> assign_changeset(params)} end @impl true