fix credo
This commit is contained in:
		| @@ -106,33 +106,30 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do | ||||
|   defp get_rows_for_containers(containers, assigns, columns) do | ||||
|     containers | ||||
|     |> Enum.map(fn container -> | ||||
|       assigns = assigns |> Map.put(:container, container) | ||||
|  | ||||
|       columns | ||||
|       |> Enum.into(%{}, fn %{key: key} -> | ||||
|         value = | ||||
|           case key do | ||||
|             "actions" -> | ||||
|               ~H""" | ||||
|               <div class="px-4 py-2 space-x-4 flex justify-center items-center"> | ||||
|                 <button | ||||
|                   type="button" | ||||
|                   class="btn btn-primary" | ||||
|                   phx-click="move" | ||||
|                   phx-target={@myself} | ||||
|                   phx-value-container_id={container.id} | ||||
|                 > | ||||
|                   <%= dgettext("actions", "Select") %> | ||||
|                 </button> | ||||
|               </div> | ||||
|               """ | ||||
|  | ||||
|             key -> | ||||
|               container |> Map.get(key |> String.to_existing_atom()) | ||||
|           end | ||||
|  | ||||
|         {key, value} | ||||
|       end) | ||||
|       |> Enum.into(%{}, fn %{key: key} -> {key, get_row_value_by_key(key, container, assigns)} end) | ||||
|     end) | ||||
|   end | ||||
|  | ||||
|   @spec get_row_value_by_key(String.t(), Container.t(), map()) :: any() | ||||
|   defp get_row_value_by_key("actions", container, assigns) do | ||||
|     assigns = assigns |> Map.put(:container, container) | ||||
|  | ||||
|     ~H""" | ||||
|     <div class="px-4 py-2 space-x-4 flex justify-center items-center"> | ||||
|       <button | ||||
|         type="button" | ||||
|         class="btn btn-primary" | ||||
|         phx-click="move" | ||||
|         phx-target={@myself} | ||||
|         phx-value-container_id={container.id} | ||||
|       > | ||||
|         <%= dgettext("actions", "Select") %> | ||||
|       </button> | ||||
|     </div> | ||||
|     """ | ||||
|   end | ||||
|  | ||||
|   defp get_row_value_by_key(key, container, _assigns), | ||||
|     do: container |> Map.get(key |> String.to_existing_atom()) | ||||
| end | ||||
|   | ||||
| @@ -5,15 +5,19 @@ | ||||
|         <%= for %{key: key, label: label} = column <- @columns do %> | ||||
|           <%= if column |> Map.get(:sortable, true) do %> | ||||
|             <th class={"p-2 #{column[:class]}"}> | ||||
|               <span class="cursor-pointer" | ||||
|               <span | ||||
|                 class="cursor-pointer" | ||||
|                 phx-click="sort_by" | ||||
|                 phx-value-sort-key={key} | ||||
|                 phx-target={@myself}> | ||||
|                 phx-target={@myself} | ||||
|               > | ||||
|                 <span class="underline"><%= label %></span> | ||||
|                 <%= if @last_sort_key == key do %> | ||||
|                   <%= case @sort_mode do %> | ||||
|                     <% :asc -> %> <i class="fas fa-sm fa-chevron-down"></i> | ||||
|                     <% :desc -> %> <i class="fas fa-sm fa-chevron-up"></i> | ||||
|                     <% :asc -> %> | ||||
|                       <i class="fas fa-sm fa-chevron-down"></i> | ||||
|                     <% :desc -> %> | ||||
|                       <i class="fas fa-sm fa-chevron-up"></i> | ||||
|                   <% end %> | ||||
|                 <% else %> | ||||
|                   <i class="fas fa-sm fa-chevron-up opacity-0"></i> | ||||
| @@ -34,8 +38,10 @@ | ||||
|           <%= for %{key: key} = value <- @columns do %> | ||||
|             <td class={"p-2 #{value[:class]}"}> | ||||
|               <%= case values |> Map.get(key) do %> | ||||
|                 <%= {_custom_sort_value, value} -> %> <%= value %> | ||||
|                 <% value -> %> <%= value %> | ||||
|                 <% {_custom_sort_value, value} -> %> | ||||
|                   <%= value %> | ||||
|                 <% value -> %> | ||||
|                   <%= value %> | ||||
|               <% end %> | ||||
|             </td> | ||||
|           <% end %> | ||||
|   | ||||
| @@ -88,98 +88,99 @@ defmodule CanneryWeb.AmmoGroupLive.Index do | ||||
|  | ||||
|     rows = | ||||
|       ammo_groups | ||||
|       |> Enum.map(fn ammo_group -> | ||||
|         assigns = %{ammo_group: ammo_group} | ||||
|  | ||||
|         columns | ||||
|         |> Enum.into(%{}, fn %{key: key} -> | ||||
|           value = | ||||
|             case key do | ||||
|               "ammo_type" -> | ||||
|                 {ammo_group.ammo_type.name, | ||||
|                  live_patch(ammo_group.ammo_type.name, | ||||
|                    to: Routes.ammo_type_show_path(Endpoint, :show, ammo_group.ammo_type), | ||||
|                    class: "link" | ||||
|                  )} | ||||
|  | ||||
|               "price_paid" -> | ||||
|                 if ammo_group.price_paid do | ||||
|                   gettext("$%{amount}", | ||||
|                     amount: ammo_group.price_paid |> :erlang.float_to_binary(decimals: 2) | ||||
|                   ) | ||||
|                 else | ||||
|                   {"a", nil} | ||||
|                 end | ||||
|  | ||||
|               "remaining" -> | ||||
|                 "#{ammo_group |> Ammo.get_percentage_remaining()}%" | ||||
|  | ||||
|               "range" -> | ||||
|                 {ammo_group.staged, | ||||
|                  ~H""" | ||||
|                  <button | ||||
|                    type="button" | ||||
|                    class="btn btn-primary" | ||||
|                    phx-click="toggle_staged" | ||||
|                    phx-value-ammo_group_id={ammo_group.id} | ||||
|                  > | ||||
|                    <%= if ammo_group.staged, do: gettext("Unstage"), else: gettext("Stage") %> | ||||
|                  </button> | ||||
|  | ||||
|                  <%= live_patch(dgettext("actions", "Record shots"), | ||||
|                    to: Routes.ammo_group_index_path(Endpoint, :add_shot_group, ammo_group), | ||||
|                    class: "btn btn-primary" | ||||
|                  ) %> | ||||
|                  """} | ||||
|  | ||||
|               "container" -> | ||||
|                 if ammo_group.container do | ||||
|                   {ammo_group.container.name, | ||||
|                    live_patch(ammo_group.container.name, | ||||
|                      to: Routes.ammo_group_index_path(Endpoint, :move, ammo_group), | ||||
|                      class: "btn btn-primary" | ||||
|                    )} | ||||
|                 else | ||||
|                   {nil, nil} | ||||
|                 end | ||||
|  | ||||
|               "actions" -> | ||||
|                 ~H""" | ||||
|                 <div class="py-2 px-4 h-full space-x-4 flex justify-center items-center"> | ||||
|                   <%= live_redirect to: Routes.ammo_group_show_path(Endpoint, :show, ammo_group), | ||||
|                                 class: "text-primary-600 link", | ||||
|                                 data: [qa: "view-#{ammo_group.id}"] do %> | ||||
|                     <i class="fa-fw fa-lg fas fa-eye"></i> | ||||
|                   <% end %> | ||||
|  | ||||
|                   <%= live_patch to: Routes.ammo_group_index_path(Endpoint, :edit, ammo_group), | ||||
|                               class: "text-primary-600 link", | ||||
|                               data: [qa: "edit-#{ammo_group.id}"] do %> | ||||
|                     <i class="fa-fw fa-lg fas fa-edit"></i> | ||||
|                   <% end %> | ||||
|  | ||||
|                   <%= link to: "#", | ||||
|                         class: "text-primary-600 link", | ||||
|                         phx_click: "delete", | ||||
|                         phx_value_id: ammo_group.id, | ||||
|                         data: [ | ||||
|                           confirm: dgettext("prompts", "Are you sure you want to delete this ammo?"), | ||||
|                           qa: "delete-#{ammo_group.id}" | ||||
|                         ] do %> | ||||
|                     <i class="fa-fw fa-lg fas fa-trash"></i> | ||||
|                   <% end %> | ||||
|                 </div> | ||||
|                 """ | ||||
|  | ||||
|               _ -> | ||||
|                 ammo_group |> Map.get(key |> String.to_existing_atom()) | ||||
|             end | ||||
|  | ||||
|           {key, value} | ||||
|         end) | ||||
|       end) | ||||
|       |> Enum.map(fn ammo_group -> ammo_group |> get_row_data_for_ammo_group(columns) end) | ||||
|  | ||||
|     socket | ||||
|     |> assign(ammo_groups: ammo_groups, containers: containers, columns: columns, rows: rows) | ||||
|   end | ||||
|  | ||||
|   @spec get_row_data_for_ammo_group(AmmoGroup.t(), [map()]) :: [map()] | ||||
|   defp get_row_data_for_ammo_group(ammo_group, columns) do | ||||
|     ammo_group = ammo_group |> Repo.preload([:ammo_type, :container]) | ||||
|  | ||||
|     columns | ||||
|     |> Enum.into(%{}, fn %{key: key} -> {key, get_value_for_key(key, ammo_group)} end) | ||||
|   end | ||||
|  | ||||
|   @spec get_value_for_key(String.t(), AmmoGroup.t()) :: any() | ||||
|   defp get_value_for_key("ammo_type", %{ammo_type: ammo_type}) do | ||||
|     {ammo_type.name, | ||||
|      live_patch(ammo_type.name, | ||||
|        to: Routes.ammo_type_show_path(Endpoint, :show, ammo_type), | ||||
|        class: "link" | ||||
|      )} | ||||
|   end | ||||
|  | ||||
|   defp get_value_for_key("price_paid", %{price_paid: nil}), do: {"a", nil} | ||||
|  | ||||
|   defp get_value_for_key("price_paid", %{price_paid: price_paid}), | ||||
|     do: gettext("$%{amount}", amount: price_paid |> :erlang.float_to_binary(decimals: 2)) | ||||
|  | ||||
|   defp get_value_for_key("range", %{staged: staged} = ammo_group) do | ||||
|     assigns = %{ammo_group: ammo_group} | ||||
|  | ||||
|     {staged, | ||||
|      ~H""" | ||||
|      <button | ||||
|        type="button" | ||||
|        class="btn btn-primary" | ||||
|        phx-click="toggle_staged" | ||||
|        phx-value-ammo_group_id={ammo_group.id} | ||||
|      > | ||||
|        <%= if ammo_group.staged, do: gettext("Unstage"), else: gettext("Stage") %> | ||||
|      </button> | ||||
|  | ||||
|      <%= live_patch(dgettext("actions", "Record shots"), | ||||
|        to: Routes.ammo_group_index_path(Endpoint, :add_shot_group, ammo_group), | ||||
|        class: "btn btn-primary" | ||||
|      ) %> | ||||
|      """} | ||||
|   end | ||||
|  | ||||
|   defp get_value_for_key("remaining", ammo_group), | ||||
|     do: "#{ammo_group |> Ammo.get_percentage_remaining()}%" | ||||
|  | ||||
|   defp get_value_for_key("actions", ammo_group) do | ||||
|     assigns = %{ammo_group: ammo_group} | ||||
|  | ||||
|     ~H""" | ||||
|     <div class="py-2 px-4 h-full space-x-4 flex justify-center items-center"> | ||||
|       <%= live_redirect to: Routes.ammo_group_show_path(Endpoint, :show, ammo_group), | ||||
|                     class: "text-primary-600 link", | ||||
|                     data: [qa: "view-#{ammo_group.id}"] do %> | ||||
|         <i class="fa-fw fa-lg fas fa-eye"></i> | ||||
|       <% end %> | ||||
|  | ||||
|       <%= live_patch to: Routes.ammo_group_index_path(Endpoint, :edit, ammo_group), | ||||
|                   class: "text-primary-600 link", | ||||
|                   data: [qa: "edit-#{ammo_group.id}"] do %> | ||||
|         <i class="fa-fw fa-lg fas fa-edit"></i> | ||||
|       <% end %> | ||||
|  | ||||
|       <%= link to: "#", | ||||
|             class: "text-primary-600 link", | ||||
|             phx_click: "delete", | ||||
|             phx_value_id: ammo_group.id, | ||||
|             data: [ | ||||
|               confirm: dgettext("prompts", "Are you sure you want to delete this ammo?"), | ||||
|               qa: "delete-#{ammo_group.id}" | ||||
|             ] do %> | ||||
|         <i class="fa-fw fa-lg fas fa-trash"></i> | ||||
|       <% end %> | ||||
|     </div> | ||||
|     """ | ||||
|   end | ||||
|  | ||||
|   defp get_value_for_key("container", %{container: nil}), do: {nil, nil} | ||||
|  | ||||
|   defp get_value_for_key("container", %{container: %{name: container_name}} = ammo_group) do | ||||
|     {container_name, | ||||
|      live_patch(container_name, | ||||
|        to: Routes.ammo_group_index_path(Endpoint, :move, ammo_group), | ||||
|        class: "btn btn-primary" | ||||
|      )} | ||||
|   end | ||||
|  | ||||
|   defp get_value_for_key(key, ammo_group), | ||||
|     do: ammo_group |> Map.get(key |> String.to_existing_atom()) | ||||
| end | ||||
|   | ||||
| @@ -47,7 +47,7 @@ | ||||
|  | ||||
|     <.live_component | ||||
|       module={CanneryWeb.Components.TableComponent} | ||||
|       id="ammo_groups_index" | ||||
|       id="ammo_groups_index_table" | ||||
|       action={@live_action} | ||||
|       columns={@columns} | ||||
|       rows={@rows} | ||||
| @@ -93,5 +93,6 @@ | ||||
|         current_user={@current_user} | ||||
|       /> | ||||
|     </.modal> | ||||
|   <% true -> %> <%= nil %> | ||||
|   <% true -> %> | ||||
|     <%= nil %> | ||||
| <% end %> | ||||
|   | ||||
| @@ -113,7 +113,7 @@ | ||||
|  | ||||
|     <.live_component | ||||
|       module={CanneryWeb.Components.TableComponent} | ||||
|       id="shot_groups_table" | ||||
|       id="ammo_group_shot_groups_table" | ||||
|       columns={@columns} | ||||
|       rows={@rows} | ||||
|     /> | ||||
|   | ||||
| @@ -21,7 +21,7 @@ | ||||
|  | ||||
|     <.live_component | ||||
|       module={CanneryWeb.Components.TableComponent} | ||||
|       id="ammo_types_index" | ||||
|       id="ammo_types_index_table" | ||||
|       action={@live_action} | ||||
|       columns={@columns} | ||||
|       rows={@rows} | ||||
|   | ||||
| @@ -88,54 +88,58 @@ defmodule CanneryWeb.RangeLive.Index do | ||||
|  | ||||
|     rows = | ||||
|       shot_groups | ||||
|       |> Enum.map(fn %{date: date} = shot_group -> | ||||
|         assigns = %{shot_group: shot_group} | ||||
|  | ||||
|         columns | ||||
|         |> Enum.into(%{}, fn %{key: key} -> | ||||
|           value = | ||||
|             case key do | ||||
|               "name" -> | ||||
|                 {shot_group.ammo_group.ammo_type.name, | ||||
|                  live_patch(shot_group.ammo_group.ammo_type.name, | ||||
|                    to: Routes.ammo_group_show_path(Endpoint, :show, shot_group.ammo_group), | ||||
|                    class: "link" | ||||
|                  )} | ||||
|  | ||||
|               "date" -> | ||||
|                 date |> display_date() | ||||
|  | ||||
|               "actions" -> | ||||
|                 ~H""" | ||||
|                 <div class="px-4 py-2 space-x-4 flex justify-center items-center"> | ||||
|                   <%= live_patch to: Routes.range_index_path(Endpoint, :edit, shot_group), | ||||
|                              class: "text-primary-600 link", | ||||
|                              data: [qa: "edit-#{shot_group.id}"] do %> | ||||
|                     <i class="fa-fw fa-lg fas fa-edit"></i> | ||||
|                   <% end %> | ||||
|  | ||||
|                   <%= link to: "#", | ||||
|                        class: "text-primary-600 link", | ||||
|                        phx_click: "delete", | ||||
|                        phx_value_id: shot_group.id, | ||||
|                        data: [ | ||||
|                          confirm: dgettext("prompts", "Are you sure you want to delete this shot record?"), | ||||
|                          qa: "delete-#{shot_group.id}" | ||||
|                        ] do %> | ||||
|                     <i class="fa-fw fa-lg fas fa-trash"></i> | ||||
|                   <% end %> | ||||
|                 </div> | ||||
|                 """ | ||||
|  | ||||
|               value -> | ||||
|                 shot_group |> Map.get(key |> String.to_existing_atom()) | ||||
|             end | ||||
|  | ||||
|           {key, value} | ||||
|         end) | ||||
|       end) | ||||
|       |> Enum.map(fn shot_group -> shot_group |> get_row_data_for_shot_group(columns) end) | ||||
|  | ||||
|     socket | ||||
|     |> assign(ammo_groups: ammo_groups, columns: columns, rows: rows, shot_groups: shot_groups) | ||||
|   end | ||||
|  | ||||
|   @spec get_row_data_for_shot_group(ShotGroup.t(), [map()]) :: [map()] | ||||
|   defp get_row_data_for_shot_group(%{date: date} = shot_group, columns) do | ||||
|     shot_group = shot_group |> Repo.preload(ammo_group: :ammo_type) | ||||
|     assigns = %{shot_group: shot_group} | ||||
|  | ||||
|     columns | ||||
|     |> Enum.into(%{}, fn %{key: key} -> | ||||
|       value = | ||||
|         case key do | ||||
|           "name" -> | ||||
|             {shot_group.ammo_group.ammo_type.name, | ||||
|              live_patch(shot_group.ammo_group.ammo_type.name, | ||||
|                to: Routes.ammo_group_show_path(Endpoint, :show, shot_group.ammo_group), | ||||
|                class: "link" | ||||
|              )} | ||||
|  | ||||
|           "date" -> | ||||
|             date |> display_date() | ||||
|  | ||||
|           "actions" -> | ||||
|             ~H""" | ||||
|             <div class="px-4 py-2 space-x-4 flex justify-center items-center"> | ||||
|               <%= live_patch to: Routes.range_index_path(Endpoint, :edit, shot_group), | ||||
|                           class: "text-primary-600 link", | ||||
|                           data: [qa: "edit-#{shot_group.id}"] do %> | ||||
|                 <i class="fa-fw fa-lg fas fa-edit"></i> | ||||
|               <% end %> | ||||
|  | ||||
|               <%= link to: "#", | ||||
|                     class: "text-primary-600 link", | ||||
|                     phx_click: "delete", | ||||
|                     phx_value_id: shot_group.id, | ||||
|                     data: [ | ||||
|                       confirm: dgettext("prompts", "Are you sure you want to delete this shot record?"), | ||||
|                       qa: "delete-#{shot_group.id}" | ||||
|                     ] do %> | ||||
|                 <i class="fa-fw fa-lg fas fa-trash"></i> | ||||
|               <% end %> | ||||
|             </div> | ||||
|             """ | ||||
|  | ||||
|           key -> | ||||
|             shot_group |> Map.get(key |> String.to_existing_atom()) | ||||
|         end | ||||
|  | ||||
|       {key, value} | ||||
|     end) | ||||
|   end | ||||
| end | ||||
|   | ||||
| @@ -55,7 +55,7 @@ | ||||
|  | ||||
|     <.live_component | ||||
|       module={CanneryWeb.Components.TableComponent} | ||||
|       id="shot_groups_table" | ||||
|       id="shot_groups_index_table" | ||||
|       columns={@columns} | ||||
|       rows={@rows} | ||||
|     /> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user