Skip to content

Commit

Permalink
Gestion des notifications : mise à jour du DOM (#4107)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti authored Aug 5, 2024
1 parent 2d0b082 commit c32541c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
3 changes: 3 additions & 0 deletions apps/transport/client/stylesheets/espace_producteur.scss
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
margin-right: 5px;
}
}
form.full-width {
max-width: 100%;
}
}

a.filler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ display_error = is_producer and not is_nil(@error) %>
<div :if={!display_error} class="panel mt-24">
<div :if={is_reuser} data-content="platform-wide-notifications">
<h3><%= dgettext("reuser-space", "Global notifications") %></h3>
<table class="table">
<.form :let={f} for={%{}}>
<.form :let={f} for={%{}} class="full-width">
<table class="table">
<%= for reason <- @platform_wide_reasons do %>
<% subscribed = reason in @subscribed_platform_wide_reasons %>
<tr>
Expand Down Expand Up @@ -64,13 +64,13 @@ display_error = is_producer and not is_nil(@error) %>
</td>
</tr>
<% end %>
</.form>
</table>
</table>
</.form>
</div>

<h3 :if={is_reuser}><%= dgettext("reuser-space", "Notifications by dataset") %></h3>
<table class="table" data-content="dataset-notifications">
<.form :let={f} for={%{}}>
<.form :let={f} for={%{}} class="full-width">
<table class="table" data-content="dataset-notifications">
<tr>
<td><strong><%= dgettext("espace-producteurs", "All notifications") %></strong></td>
<td>
Expand Down Expand Up @@ -146,8 +146,8 @@ display_error = is_producer and not is_nil(@error) %>
</tr>
<% end %>
<% end %>
</.form>
</table>
</table>
</.form>
</div>
</div>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ defmodule TransportWeb.Live.NotificationsLiveTest do
@producer_url espace_producteur_path(TransportWeb.Endpoint, :notifications)
@reuser_url reuser_space_path(TransportWeb.Endpoint, :notifications)

@dataset_notifications_path ~s|form table[data-content="dataset-notifications"]|
@platform_wide_path ~s|div[data-content="platform-wide-notifications"] form table|

setup :verify_on_exit!

setup do
Expand Down Expand Up @@ -95,7 +98,7 @@ defmodule TransportWeb.Live.NotificationsLiveTest do

[{"input", switch_attr, []}] =
doc
|> Floki.find(~s|table[data-content="dataset-notifications"] tr td .form__group fieldset .switch input|)
|> Floki.find("#{@dataset_notifications_path} tr td .form__group fieldset .switch input")
|> Floki.find("[value=true]")
|> Floki.find("[checked=checked]")

Expand All @@ -105,7 +108,7 @@ defmodule TransportWeb.Live.NotificationsLiveTest do
[switch_attr["id"], switch_attr["phx-value-dataset-id"], switch_attr["phx-value-subscription-id"]]

# Platform-wide subscriptions are not displayed
assert [] == doc |> Floki.find(~s|div[data-content="platform-wide-notifications"]|)
assert [] == doc |> Floki.find(@platform_wide_path)
end

test "displays existing subscriptions for a reuser", %{conn: conn} do
Expand Down Expand Up @@ -164,7 +167,7 @@ defmodule TransportWeb.Live.NotificationsLiveTest do
[{"input", switch_attr, []}] =
content
|> Floki.parse_document!()
|> Floki.find(~s|table[data-content="dataset-notifications"] tr td .form__group fieldset .switch input|)
|> Floki.find("#{@dataset_notifications_path} tr td .form__group fieldset .switch input")
|> Floki.find("[value=true]")
|> Floki.find("[checked=checked]")

Expand Down Expand Up @@ -201,9 +204,7 @@ defmodule TransportWeb.Live.NotificationsLiveTest do
] ==
content
|> Floki.parse_document!()
|> Floki.find(
~s|div[data-content="platform-wide-notifications"] tr td .form__group fieldset .switch input|
)
|> Floki.find("#{@platform_wide_path} tr td .form__group fieldset .switch input")
end

test "displays an error message if we can’t retrieve user orgs (and thus datasets) through data.gouv.fr", %{
Expand Down Expand Up @@ -279,12 +280,18 @@ defmodule TransportWeb.Live.NotificationsLiveTest do

assert [] = DB.NotificationSubscription |> DB.Repo.all()

render_change(view, :toggle, %{
"dataset-id" => dataset_id,
"subscription-id" => "",
"reason" => "expiration",
"action" => "turn_on"
})
content =
render_change(view, :toggle, %{
"dataset-id" => dataset_id,
"subscription-id" => "",
"reason" => "expiration",
"action" => "turn_on"
})

assert [
%{"name" => "daily_new_comments", "phx-value-action" => "turn_on"},
%{"name" => "new_dataset", "phx-value-action" => "turn_on"}
] == dom_platform_wide_reasons(content)

assert [
%DB.NotificationSubscription{
Expand All @@ -297,12 +304,18 @@ defmodule TransportWeb.Live.NotificationsLiveTest do
}
] = DB.NotificationSubscription |> DB.Repo.all()

render_change(view, :toggle, %{
"dataset-id" => dataset_id,
"subscription-id" => subscription_id,
"reason" => "expiration",
"action" => "turn_off"
})
content =
render_change(view, :toggle, %{
"dataset-id" => dataset_id,
"subscription-id" => subscription_id,
"reason" => "expiration",
"action" => "turn_off"
})

assert [
%{"name" => "daily_new_comments", "phx-value-action" => "turn_on"},
%{"name" => "new_dataset", "phx-value-action" => "turn_on"}
] == dom_platform_wide_reasons(content)

assert [] = DB.NotificationSubscription |> DB.Repo.all()
end
Expand All @@ -315,7 +328,13 @@ defmodule TransportWeb.Live.NotificationsLiveTest do

assert [] = DB.NotificationSubscription |> DB.Repo.all()

render_change(view, :toggle, %{"reason" => "new_dataset", "action" => "turn_on"})
content =
render_change(view, :toggle, %{"reason" => "new_dataset", "action" => "turn_on"})

assert [
%{"name" => "daily_new_comments", "phx-value-action" => "turn_on"},
%{"name" => "new_dataset", "phx-value-action" => "turn_off"}
] == dom_platform_wide_reasons(content)

assert [
%DB.NotificationSubscription{
Expand All @@ -327,7 +346,12 @@ defmodule TransportWeb.Live.NotificationsLiveTest do
}
] = DB.NotificationSubscription |> DB.Repo.all()

render_change(view, :toggle, %{"reason" => "new_dataset", "action" => "turn_off"})
content = render_change(view, :toggle, %{"reason" => "new_dataset", "action" => "turn_off"})

assert [
%{"name" => "daily_new_comments", "phx-value-action" => "turn_on"},
%{"name" => "new_dataset", "phx-value-action" => "turn_on"}
] == dom_platform_wide_reasons(content)

assert [] = DB.NotificationSubscription |> DB.Repo.all()
end
Expand Down Expand Up @@ -511,4 +535,11 @@ defmodule TransportWeb.Live.NotificationsLiveTest do
]
})
end

defp dom_platform_wide_reasons(content) do
content
|> Floki.parse_document!()
|> Floki.find(~s|#{@platform_wide_path} tr td .form__group fieldset .switch input[phx-click="toggle"]|)
|> Enum.map(fn {"input", attributes, []} -> attributes |> Map.new() |> Map.take(["name", "phx-value-action"]) end)
end
end

0 comments on commit c32541c

Please sign in to comment.