From c70b385db1d7e2f0ad864ea4782989c4df9578e4 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Wed, 4 Feb 2026 18:02:13 -0600 Subject: [PATCH] refactor: consolidate reorder handlers in device_live Extract common pattern from perform_site_reorder/4 and perform_device_reorder/4 into a single perform_reorder/4 helper function. Both reorder handlers now delegate to the shared helper, reducing duplication and making the reload/regroup/flash pattern consistent. - Reduces code from 40 lines to 28 lines - Maintains identical behavior (all tests pass) - Uses static gettext strings to satisfy compile-time requirements --- lib/towerops_web/live/device_live/index.ex | 44 +++++++++++----------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/towerops_web/live/device_live/index.ex b/lib/towerops_web/live/device_live/index.ex index a9db2daf..91870281 100644 --- a/lib/towerops_web/live/device_live/index.ex +++ b/lib/towerops_web/live/device_live/index.ex @@ -167,44 +167,44 @@ defmodule ToweropsWeb.DeviceLive.Index do end defp perform_site_reorder(socket, site_id, new_position, organization_id) do - # Handle both integer and string input position = if is_integer(new_position), do: new_position, else: String.to_integer(new_position) - - case Sites.reorder_site(site_id, position) do - {:ok, _site} -> - # Reload devices with updated order - devices = Devices.list_organization_devices(organization_id) - grouped_devices = group_devices_by_site(devices) - - {:noreply, - socket - |> assign(:device, devices) - |> assign(:grouped_devices, grouped_devices) - |> put_flash(:info, t_equipment("Site order updated"))} - - {:error, _changeset} -> - {:noreply, put_flash(socket, :error, t_equipment("Failed to reorder site"))} - end + result = Sites.reorder_site(site_id, position) + perform_reorder(socket, result, "site", organization_id) end defp perform_device_reorder(socket, device_id, new_position, organization_id) do - # Handle both integer and string input position = if is_integer(new_position), do: new_position, else: String.to_integer(new_position) + result = Devices.reorder_device(device_id, position) + perform_reorder(socket, result, "device", organization_id) + end - case Devices.reorder_device(device_id, position) do - {:ok, _device} -> + defp perform_reorder(socket, reorder_result, resource_name, organization_id) do + case reorder_result do + {:ok, _} -> # Reload devices with updated order devices = Devices.list_organization_devices(organization_id) grouped_devices = group_devices_by_site(devices) + success_message = + case resource_name do + "site" -> t_equipment("Site order updated") + "device" -> t_equipment("Device order updated") + end + {:noreply, socket |> assign(:device, devices) |> assign(:grouped_devices, grouped_devices) - |> put_flash(:info, t_equipment("Device order updated"))} + |> put_flash(:info, success_message)} {:error, _changeset} -> - {:noreply, put_flash(socket, :error, t_equipment("Failed to reorder device"))} + error_message = + case resource_name do + "site" -> t_equipment("Failed to reorder site") + "device" -> t_equipment("Failed to reorder device") + end + + {:noreply, put_flash(socket, :error, error_message)} end end