Skip to content

Commit

Permalink
fix: preserve sort in recordings (#304)
Browse files Browse the repository at this point in the history
* Preserve sort params after recording preview (or any other actions)

* Reset pagination when current page is greater than total pages

* set default page value to 0

* remove unused alias and variables in tests

* reset pagination after filter
  • Loading branch information
SidAli-Belho authored Dec 25, 2023
1 parent 0ce9336 commit 9fa8516
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion apps/ex_nvr/test/ex_nvr/devices_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ExNVR.DevicesTest do
use ExNVR.DataCase

alias ExNVR.{Devices, Recordings}
alias ExNVR.Devices
alias ExNVR.Model.{Device, Recording, Run}

import ExNVR.DevicesFixtures
Expand Down
22 changes: 12 additions & 10 deletions apps/ex_nvr_web/lib/ex_nvr_web/live/recordings_list_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,18 @@ defmodule ExNVRWeb.RecordingListLive do
devices: Devices.list(),
popup_open: false,
filter_params: params,
pagination_params: %{}
pagination_params: %{},
sort_params: %{}
)}
end

@impl true
def handle_params(params, _uri, socket) do
sort_params = Map.take(params, ["order_by", "order_directions"])

case Recordings.list(params) do
{:ok, {recordings, meta}} ->
{:noreply, assign(socket, meta: meta, recordings: recordings)}
{:noreply, assign(socket, meta: meta, recordings: recordings, sort_params: sort_params)}

{:error, meta} ->
{:noreply, assign(socket, meta: meta)}
Expand All @@ -163,28 +166,27 @@ defmodule ExNVRWeb.RecordingListLive do
socket.assigns.filter_params,
socket.assigns.pagination_params
)
|> Map.merge(socket.assigns.sort_params)

{:noreply, push_navigate(socket, to: Routes.recording_list_path(socket, :list, params))}
end

@impl true
def handle_event("filter-recordings", filter_params, socket) do
params =
Map.merge(
filter_params,
socket.assigns.pagination_params
)

{:noreply,
socket
|> assign(:filter_params, filter_params)
|> push_patch(to: Routes.recording_list_path(socket, :list, params))}
|> assign(:pagination_params, %{})
|> push_patch(to: Routes.recording_list_path(socket, :list, filter_params))}
end

@impl true
def handle_event("paginate", pagination_params, socket) do
pagination_params = Map.merge(socket.assigns.pagination_params, pagination_params)
params = Map.merge(socket.assigns.filter_params, pagination_params)

params =
Map.merge(socket.assigns.filter_params, pagination_params)
|> Map.merge(socket.assigns.sort_params)

{:noreply,
socket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ defmodule ExNVRWeb.API.DeviceControllerTest do

use ExNVRWeb.ConnCase

import ExNVR.{AccountsFixtures, DevicesFixtures, RecordingsFixtures}
import ExNVR.{AccountsFixtures, DevicesFixtures}

alias ExNVR.{Devices, Recordings}
alias ExNVR.Devices

@moduletag :tmp_dir

Expand Down Expand Up @@ -150,21 +150,19 @@ defmodule ExNVRWeb.API.DeviceControllerTest do
end

test "Delete device", %{conn: conn, device: device} do
response =
conn
|> delete(~p"/api/devices/#{device.id}")
|> response(204)
conn
|> delete(~p"/api/devices/#{device.id}")
|> response(204)

refute Devices.get(device.id)
end

test "delete a device by unauthorized user", %{conn: conn, device: device} do
user_conn = log_in_user_with_access_token(conn, user_fixture(%{role: :user}))

response =
user_conn
|> delete(~p"/api/devices/#{device.id}")
|> response(403)
user_conn
|> delete(~p"/api/devices/#{device.id}")
|> response(403)
end
end
end

0 comments on commit 9fa8516

Please sign in to comment.