Skip to content

Commit 2729a26

Browse files
authored
Add device count, selected count and select all checkbox (#1511)
* Add device count, selected count and select all checkbox * Add tests for device counts
1 parent cb3ceff commit 2729a26

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

lib/nerves_hub_web/live/devices/index.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
7676
|> assign(:target_product, nil)
7777
|> assign(:valid_tags, true)
7878
|> assign(:device_tags, "")
79+
|> assign(:total_entries, 0)
7980
|> subscribe_and_refresh_device_list_timer()
8081
|> ok()
8182
end
@@ -199,6 +200,19 @@ defmodule NervesHubWeb.Live.Devices.Index do
199200
{:noreply, assign(socket, :selected_devices, selected_devices)}
200201
end
201202

203+
def handle_event("select-all", _, socket) do
204+
selected_devices = socket.assigns.selected_devices
205+
206+
selected_devices =
207+
if Enum.count(selected_devices) > 0 do
208+
[]
209+
else
210+
Enum.map(socket.assigns.devices, & &1.id)
211+
end
212+
213+
{:noreply, assign(socket, :selected_devices, selected_devices)}
214+
end
215+
202216
def handle_event("deselect-all", _, socket) do
203217
{:noreply, assign(socket, selected_devices: [])}
204218
end
@@ -378,6 +392,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
378392

379393
socket
380394
|> assign(:devices, page.entries)
395+
|> assign(:total_entries, page.total_entries)
381396
|> assign(:paginate_opts, paginate_opts)
382397
end
383398

lib/nerves_hub_web/live/devices/index.html.heex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
</span>
2828
<span class="button-icon filter"></span>
2929
</button>
30+
<p class="ml-2" style="opacity: 0.7;">
31+
<%= @total_entries %> devices found <span :if={Enum.count(@selected_devices) > 0}>(<%= Enum.count(@selected_devices) %> selected)</span>
32+
</p>
3033
</div>
3134
<div>
3235
<a class="btn btn-outline-light btn-action" aria-label="Add new device" href={Routes.product_path(@socket, :devices_export, @org.name, @product.name)}>
@@ -39,6 +42,7 @@
3942
</.link>
4043
</div>
4144
</div>
45+
4246
<div class="action-row btn-group-toggle" style="justify-content: flex-end; display: flex; gap: 4px;">
4347
<%= for size <- @paginate_opts.page_sizes do %>
4448
<button phx-click="set-paginate-opts" phx-value-page-size={size} class={"btn btn-secondary btn-sm #{if size == @paginate_opts.page_size, do: "active"}"}>
@@ -218,7 +222,9 @@
218222
<table class="table table-sm table-hover">
219223
<thead>
220224
<tr>
221-
<th></th>
225+
<th>
226+
<input class="checkbox" title="Check/uncheck all" {[checked: Enum.count(@selected_devices) == Enum.count(@devices)]} id="toggle-all" name="toggle-all" type="checkbox" phx-click="select-all" />
227+
</th>
222228
<%= devices_table_header("Identifier", "identifier", @current_sort, @sort_direction) %>
223229
<th>Firmware</th>
224230
<th>Platform</th>

test/nerves_hub_web/live/devices/index_test.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ defmodule NervesHubWeb.Live.Devices.IndexTest do
2121
change = render_change(view, "update-filters", %{"device_id" => device.identifier})
2222
assert change =~ device.identifier
2323
refute change =~ device2.identifier
24+
assert change =~ "1 devices found"
2425
end
2526

2627
test "filters devices by wrong identifier", %{conn: conn, fixture: fixture} do
@@ -35,6 +36,7 @@ defmodule NervesHubWeb.Live.Devices.IndexTest do
3536
change = render_change(view, "update-filters", %{"device_id" => "foo"})
3637
refute change =~ device.identifier
3738
refute change =~ device2.identifier
39+
assert change =~ "0 devices found"
3840
end
3941

4042
test "filters devices by prefix identifier", %{conn: conn, fixture: fixture} do
@@ -128,6 +130,39 @@ defmodule NervesHubWeb.Live.Devices.IndexTest do
128130
assert change =~ device2.identifier
129131
refute change =~ device3.identifier
130132
end
133+
134+
test "select device", %{conn: conn, fixture: fixture} do
135+
%{device: _device, firmware: firmware, org: org, product: product} = fixture
136+
137+
device2 = Fixtures.device_fixture(org, product, firmware, %{tags: nil})
138+
_device3 = Fixtures.device_fixture(org, product, firmware, %{tags: ["foo"]})
139+
140+
{:ok, view, html} = live(conn, device_index_path(fixture))
141+
assert html =~ "3 devices found"
142+
refute html =~ "(1 selected)"
143+
144+
change = render_change(view, "select", %{"id" => device2.id})
145+
assert change =~ "3 devices found"
146+
assert change =~ "(1 selected)"
147+
end
148+
149+
test "select/deselect all devices", %{conn: conn, fixture: fixture} do
150+
%{device: _device, firmware: firmware, org: org, product: product} = fixture
151+
152+
_device2 = Fixtures.device_fixture(org, product, firmware, %{tags: nil})
153+
_device3 = Fixtures.device_fixture(org, product, firmware, %{tags: ["foo"]})
154+
155+
{:ok, view, html} = live(conn, device_index_path(fixture))
156+
assert html =~ "3 devices found"
157+
158+
change = render_change(view, "select-all", %{})
159+
assert change =~ "3 devices found"
160+
assert change =~ "(3 selected)"
161+
162+
change = render_change(view, "select-all", %{})
163+
assert change =~ "3 devices found"
164+
refute change =~ "selected)"
165+
end
131166
end
132167

133168
describe "bulk actions" do

0 commit comments

Comments
 (0)