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
This commit is contained in:
Graham McIntire 2026-02-04 18:02:13 -06:00
parent aa0e7f3d05
commit c70b385db1
No known key found for this signature in database

View file

@ -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