diff --git a/lib/memex/accounts/invites.ex b/lib/memex/accounts/invites.ex index 2bc74cf..75ad2f5 100644 --- a/lib/memex/accounts/invites.ex +++ b/lib/memex/accounts/invites.ex @@ -109,6 +109,19 @@ defmodule Memex.Accounts.Invites do ) end + @spec get_use_counts([Invite.t()], User.t()) :: %{Invite.id() => non_neg_integer()} + def get_use_counts(invites, %User{role: :admin}) do + invite_ids = invites |> Enum.map(fn %{id: invite_id} -> invite_id end) + + Repo.all( + from u in User, + where: u.invite_id in ^invite_ids, + group_by: u.invite_id, + select: {u.invite_id, count(u.id)} + ) + |> Map.new() + end + @spec decrement_invite_changeset(Invite.t()) :: Invite.changeset() defp decrement_invite_changeset(%Invite{uses_left: nil} = invite) do invite |> Invite.update_changeset(%{}) diff --git a/lib/memex_web/components/core_components.ex b/lib/memex_web/components/core_components.ex index ecc9bff..f089650 100644 --- a/lib/memex_web/components/core_components.ex +++ b/lib/memex_web/components/core_components.ex @@ -4,7 +4,7 @@ defmodule MemexWeb.CoreComponents do """ use Phoenix.Component import MemexWeb.{Gettext, ViewHelpers} - alias Memex.{Accounts, Accounts.Invite, Accounts.Invites, Accounts.User} + alias Memex.{Accounts, Accounts.Invite, Accounts.User} alias Memex.Contexts.Context alias Memex.Notes.Note alias Memex.Pipelines.Steps.Step @@ -77,64 +77,12 @@ defmodule MemexWeb.CoreComponents do def user_card(assigns) attr :invite, Invite, required: true + attr :use_count, :integer, default: nil attr :current_user, User, required: true slot(:inner_block) slot(:code_actions) - def invite_card(%{invite: invite, current_user: current_user} = assigns) do - assigns = assigns |> assign(:use_count, Invites.get_use_count(invite, current_user)) - - ~H""" -
-

- <%= @invite.name %> -

- - <%= if @invite.disabled_at |> is_nil() do %> -

- <%= if @invite.uses_left do %> - <%= gettext( - "uses left: %{uses_left_count}", - uses_left_count: @invite.uses_left - ) %> - <% else %> - <%= gettext("uses left: unlimited") %> - <% end %> -

- <% else %> -

- <%= gettext("invite disabled") %> -

- <% end %> - - <.qr_code - content={Routes.user_registration_url(Endpoint, :new, invite: @invite.token)} - filename={@invite.name} - /> - -

- <%= gettext("uses: %{uses_count}", uses_count: @use_count) %> -

- -
- <%= Routes.user_registration_url(Endpoint, :new, invite: @invite.token) %> - <%= if @code_actions, do: render_slot(@code_actions) %> -
- -
- <%= render_slot(@inner_block) %> -
-
- """ - end + def invite_card(assigns) attr :id, :string, required: true attr :datetime, :any, required: true, doc: "A `DateTime` struct or nil" diff --git a/lib/memex_web/components/core_components/invite_card.html.heex b/lib/memex_web/components/core_components/invite_card.html.heex new file mode 100644 index 0000000..da0374a --- /dev/null +++ b/lib/memex_web/components/core_components/invite_card.html.heex @@ -0,0 +1,48 @@ +
+

+ <%= @invite.name %> +

+ + <%= if @invite.disabled_at |> is_nil() do %> +

+ <%= if @invite.uses_left do %> + <%= gettext( + "uses left: %{uses_left_count}", + uses_left_count: @invite.uses_left + ) %> + <% else %> + <%= gettext("uses left: unlimited") %> + <% end %> +

+ <% else %> +

+ <%= gettext("invite disabled") %> +

+ <% end %> + + <.qr_code + content={Routes.user_registration_url(Endpoint, :new, invite: @invite.token)} + filename={@invite.name} + /> + +

+ <%= gettext("uses: %{uses_count}", uses_count: @use_count) %> +

+ +
+ <%= Routes.user_registration_url(Endpoint, :new, invite: @invite.token) %> + <%= if @code_actions, do: render_slot(@code_actions) %> +
+ +
+ <%= render_slot(@inner_block) %> +
+
diff --git a/lib/memex_web/live/invite_live/index.ex b/lib/memex_web/live/invite_live/index.ex index 66fdecd..784e14e 100644 --- a/lib/memex_web/live/invite_live/index.ex +++ b/lib/memex_web/live/invite_live/index.ex @@ -148,7 +148,9 @@ defmodule MemexWeb.InviteLive.Index do |> Map.get(:admin, []) |> Enum.reject(fn %{id: user_id} -> user_id == current_user.id end) + use_counts = invites |> Invites.get_use_counts(current_user) users = all_users |> Map.get(:user, []) - socket |> assign(invites: invites, admins: admins, users: users) + + socket |> assign(invites: invites, use_counts: use_counts, admins: admins, users: users) end end diff --git a/lib/memex_web/live/invite_live/index.html.heex b/lib/memex_web/live/invite_live/index.html.heex index 88de97f..bbe0c18 100644 --- a/lib/memex_web/live/invite_live/index.html.heex +++ b/lib/memex_web/live/invite_live/index.html.heex @@ -14,7 +14,12 @@ <% end %>
- <.invite_card :for={invite <- @invites} invite={invite} current_user={@current_user}> + <.invite_card + :for={invite <- @invites} + invite={invite} + current_user={@current_user} + use_count={Map.get(@use_counts, invite.id)} + > <:code_actions>