Skip to content

Commit 8b1e256

Browse files
committed
fix: make improvements
1 parent 9a28c74 commit 8b1e256

File tree

9 files changed

+151
-21
lines changed

9 files changed

+151
-21
lines changed

lib/admin/languages.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ defmodule Admin.Languages do
2020
@languages |> Enum.map(&Keyword.new(&1))
2121
end
2222

23+
def all_values do
24+
@languages |> Enum.map(& &1.value)
25+
end
26+
2327
@doc """
2428
Returns a list of languages excluding the ones with the given codes.
2529

lib/admin/mailing_worker.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ defmodule Admin.MailingWorker do
2323
scope = Scope.for_user(user)
2424

2525
with {:ok, notification} <- Notifications.get_notification(scope, notification_id),
26-
{:ok, audience} <- Notifications.get_target_audience(scope, notification.audience) do
26+
included_langs = notification.localized_emails |> Enum.map(& &1.language),
27+
{:ok, audience} <-
28+
Notifications.get_target_audience(
29+
scope,
30+
notification.audience,
31+
if(notification.use_strict_languages, do: [only_langs: included_langs], else: [])
32+
) do
2733
# save number of recipients to the notification
2834
Notifications.update_recipients(notification, %{total_recipients: length(audience)})
2935
# start sending emails

lib/admin/notifications.ex

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ defmodule Admin.Notifications do
103103
Repo.all(from n in Notification, order_by: [desc: :updated_at]) |> Repo.preload([:logs])
104104
end
105105

106+
def list_notifications_by_status(%Scope{} = _scope) do
107+
Repo.all(from n in Notification, order_by: [desc: :sent_at])
108+
|> Repo.preload([:logs, :localized_emails])
109+
end
110+
106111
def list_recently_sent_notifications(%Scope{} = _scope) do
107112
Repo.all(
108113
from n in Notification, where: not is_nil(n.sent_at), order_by: [desc: :sent_at], limit: 10
@@ -301,31 +306,42 @@ defmodule Admin.Notifications do
301306
end
302307
end
303308

304-
def get_target_audience(%Scope{} = _scope, "active") do
309+
def get_target_audience(scope, target_audience, opts \\ [])
310+
311+
def get_target_audience(%Scope{} = _scope, "active", opts) do
305312
audience =
306-
Accounts.get_active_members() |> Enum.map(&%{name: &1.name, email: &1.email, lang: &1.lang})
313+
Accounts.get_active_members()
314+
|> Enum.map(&%{name: &1.name, email: &1.email, lang: &1.lang})
315+
|> filter_audience_with_options(opts)
307316

308317
{:ok, audience}
309318
end
310319

311-
def get_target_audience(%Scope{} = _scope, "french") do
320+
def get_target_audience(%Scope{} = _scope, "french", opts) do
312321
audience =
313322
Accounts.get_members_by_language("fr")
314323
|> Enum.map(&%{name: &1.name, email: &1.email, lang: &1.lang})
324+
|> filter_audience_with_options(opts)
315325

316326
{:ok, audience}
317327
end
318328

319-
def get_target_audience(%Scope{} = _scope, "graasp_team") do
329+
def get_target_audience(%Scope{} = _scope, "graasp_team", opts) do
320330
audience =
321331
Accounts.list_users()
322332
|> Enum.map(&%{name: &1.name, email: &1.email, lang: &1.language})
333+
|> filter_audience_with_options(opts)
323334

324335
{:ok, audience}
325336
end
326337

327-
def get_target_audience(%Scope{} = _scope, target_audience) do
338+
def get_target_audience(%Scope{} = _scope, target_audience, _opts) do
328339
Logger.error("Invalid target audience: #{target_audience}")
329340
{:error, :invalid_target_audience}
330341
end
342+
343+
defp filter_audience_with_options(audience, opts) do
344+
only_langs = Keyword.get(opts, :only_langs, Admin.Languages.all_values()) |> MapSet.new()
345+
audience |> Enum.filter(fn user -> MapSet.member?(only_langs, user.lang) end)
346+
end
331347
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule AdminWeb.MailingComponents do
2+
@moduledoc """
3+
Components for the mailing module.
4+
"""
5+
6+
use Phoenix.Component
7+
8+
attr :id, :string, required: true
9+
attr :mailing, :map, required: true
10+
11+
def sent_mailing(assigns) do
12+
~H"""
13+
<div id={@id} class="card w-full bg-base-100 card-sm shadow-sm">
14+
<div class="card-body w-full flex flex-row items-center">
15+
<div class="flex flex-col">
16+
<h2 class="card-title">{@mailing.name}</h2>
17+
<span>
18+
<%= for lang <- @mailing.localized_emails |> Enum.map(& &1.language) do %>
19+
<div class="chip">
20+
{lang}
21+
</div>
22+
<% end %>
23+
</span>
24+
<p>Target Audience: {@mailing.audience}</p>
25+
</div>
26+
27+
<span class="text-base h-fit">{length(@mailing.logs)} / {@mailing.total_recipients}</span>
28+
</div>
29+
</div>
30+
"""
31+
end
32+
end

lib/admin_web/live/notification_live/form.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ defmodule AdminWeb.NotificationLive.Form do
7575
7676
<footer>
7777
<.button variant="primary">Save Mail</.button>
78-
<.button navigate={~p"/notifications"}>Cancel</.button>
78+
<.button navigate={~p"/notifications/#{@notification}"}>Cancel</.button>
7979
</footer>
8080
</.form>
8181
</Layouts.admin>
@@ -110,9 +110,14 @@ defmodule AdminWeb.NotificationLive.Form do
110110

111111
defp apply_action(socket, :edit, %{"notification_id" => id}) do
112112
notification = Notifications.get_notification!(socket.assigns.current_scope, id)
113+
included_langs = notification.localized_emails |> Enum.map(& &1.language)
113114

114115
{:ok, recipients} =
115-
Notifications.get_target_audience(socket.assigns.current_scope, notification.audience)
116+
Notifications.get_target_audience(
117+
socket.assigns.current_scope,
118+
notification.audience,
119+
if(notification.use_strict_languages, do: [only_langs: included_langs], else: [])
120+
)
116121

117122
socket
118123
|> assign(:page_title, "Edit Mail")

lib/admin_web/live/notification_live/index.ex

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,38 @@ defmodule AdminWeb.NotificationLive.Index do
3030
</div>
3131
<% end %>
3232
33-
<%!-- Idea: represent the mails as cards ? --%>
33+
<h2 class="text-lg font-bold">Drafts</h2>
3434
<.table
35-
id="notifications"
36-
rows={@streams.notifications}
35+
id="wip_notifications"
36+
rows={@streams.wip_notifications}
3737
row_click={fn {_id, notification} -> JS.navigate(~p"/notifications/#{notification}") end}
38+
>
39+
<:col :let={{_id, notification}} label="Name">{notification.name}</:col>
40+
<:col :let={{_id, notification}} label="Audience">{notification.audience}</:col>
41+
<:action :let={{_id, notification}}>
42+
<div class="sr-only">
43+
<.link navigate={~p"/notifications/#{notification}"}>Show</.link>
44+
</div>
45+
</:action>
46+
<:action :let={{id, notification}}>
47+
<.link
48+
class="text-error"
49+
phx-click={JS.push("delete", value: %{id: notification.id}) |> hide("##{id}")}
50+
data-confirm="Are you sure?"
51+
>
52+
Delete
53+
</.link>
54+
</:action>
55+
</.table>
56+
57+
<h2 class="text-lg font-bold">Sent</h2>
58+
59+
<.table
60+
id="notifications"
61+
rows={@streams.sent_notifications}
62+
row_click={
63+
fn {_id, notification} -> JS.navigate(~p"/notifications/#{notification}/archive") end
64+
}
3865
>
3966
<:col :let={{_id, notification}} label="Name">{notification.name}</:col>
4067
<:col :let={{_id, notification}} label="Audience">{notification.audience}</:col>
@@ -71,6 +98,11 @@ defmodule AdminWeb.NotificationLive.Index do
7198
Notifications.subscribe_sending_progress(socket.assigns.current_scope)
7299
end
73100

101+
notifications = Notifications.list_notifications_by_status(socket.assigns.current_scope)
102+
103+
{wip_notifications, sent_notifications} =
104+
notifications |> Enum.split_with(&(&1.sent_at == nil))
105+
74106
{:ok,
75107
socket
76108
|> assign(:page_title, "Mailing")
@@ -79,7 +111,14 @@ defmodule AdminWeb.NotificationLive.Index do
79111
:sent_notifications,
80112
Notifications.list_recently_sent_notifications(socket.assigns.current_scope)
81113
)
82-
|> stream(:notifications, Notifications.list_notifications(socket.assigns.current_scope))}
114+
|> stream(
115+
:wip_notifications,
116+
wip_notifications
117+
)
118+
|> stream(
119+
:sent_notifications,
120+
sent_notifications
121+
)}
83122
end
84123

85124
@impl true

lib/admin_web/live/notification_live/message_live/form.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ defmodule AdminWeb.NotificationMessageLive.Form do
7070
{:ok, socket}
7171
end
7272

73-
def apply_action(socket, :new, _params) do
73+
def apply_action(socket, :new, params) do
7474
localized_email = %LocalizedEmail{}
75+
params |> IO.inspect()
76+
Map.get(params, "language", "") |> IO.inspect()
7577

7678
changeset =
7779
Notifications.change_localized_email(
@@ -83,7 +85,7 @@ defmodule AdminWeb.NotificationMessageLive.Form do
8385
"message" => "",
8486
"button_text" => "",
8587
"button_url" => "",
86-
"language" => ""
88+
"language" => Map.get(params, "language", "")
8789
}
8890
)
8991

lib/admin_web/live/notification_live/show.ex

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ defmodule AdminWeb.NotificationLive.Show do
2121
2222
<.list>
2323
<:item title="Name">{@notification.name}</:item>
24-
<:item title="Target Audience">{@notification.audience}</:item>
24+
<:item title="Target Audience">{@notification.audience} {length(@recipients)}</:item>
2525
<:item title="Default language">
2626
<div class="flex flex-col gap-2">
2727
{@notification.default_language}
28-
28+
<%!--
2929
<label class="label text-sm">
3030
<input
3131
type="checkbox"
3232
phx-click="toggle_strict_languages"
3333
checked={if @notification.use_strict_languages, do: "checked", else: nil}
3434
class="checkbox checkbox-primary"
3535
/> Use only specified languages
36-
</label>
36+
</label> --%>
3737
</div>
3838
</:item>
3939
</.list>
@@ -100,6 +100,11 @@ defmodule AdminWeb.NotificationLive.Show do
100100
<div role="alert" class="alert alert-error alert-soft">
101101
<.icon name="hero-exclamation-circle" class="size-6" />
102102
<span>Missing the email template for the default language.</span>
103+
<.button navigate={
104+
~p"/notifications/#{@notification}/messages/new?language=#{@notification.default_language}"
105+
}>
106+
Add locale
107+
</.button>
103108
</div>
104109
<% end %>
105110
</Layouts.admin>
@@ -112,24 +117,44 @@ defmodule AdminWeb.NotificationLive.Show do
112117
Notifications.subscribe_notifications(socket.assigns.current_scope, id)
113118
end
114119

120+
notification = Notifications.get_notification!(socket.assigns.current_scope, id)
121+
included_langs = notification.localized_emails |> Enum.map(& &1.language)
122+
123+
{:ok, recipients} =
124+
Notifications.get_target_audience(
125+
socket.assigns.current_scope,
126+
notification.audience,
127+
if(notification.use_strict_languages, do: [only_langs: included_langs], else: [])
128+
)
129+
115130
{:ok,
116131
socket
117132
|> assign(:page_title, "Show Mail")
118133
|> assign(
119134
:notification,
120-
Notifications.get_notification!(socket.assigns.current_scope, id)
121-
)}
135+
notification
136+
)
137+
|> assign(:recipients, recipients)}
122138
end
123139

124140
@impl true
125141
def handle_event("toggle_strict_languages", _, socket) do
126-
{:ok, _notification} =
142+
{:ok, notification} =
127143
Notifications.toggle_strict_languages(
128144
socket.assigns.current_scope,
129145
socket.assigns.notification
130146
)
131147

132-
{:noreply, socket}
148+
included_langs = notification.localized_emails |> Enum.map(& &1.language)
149+
150+
{:ok, recipients} =
151+
Notifications.get_target_audience(
152+
socket.assigns.current_scope,
153+
notification.audience,
154+
if(notification.use_strict_languages, do: [only_langs: included_langs], else: [])
155+
)
156+
157+
{:noreply, socket |> assign(:recipients, recipients)}
133158
end
134159

135160
def handle_event("delete", %{"id" => id} = _params, socket) do

lib/admin_web/router.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ defmodule AdminWeb.Router do
115115

116116
scope "/:notification_id" do
117117
live "/", NotificationLive.Show, :show
118+
live "/archive", NotificationLive.Show, :archive
118119
live "/edit", NotificationLive.Form, :edit
119120

120121
live "/messages/new", NotificationMessageLive.Form, :new

0 commit comments

Comments
 (0)