harden invites context
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-02-11 00:34:29 -05:00
parent fc75948f4c
commit 71397d6b29
9 changed files with 227 additions and 185 deletions

View File

@ -4,8 +4,8 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
"""
use CanneryWeb, :live_component
alias Cannery.{Ammo, Accounts.User, Containers, Containers.Container}
alias Cannery.Ammo.{AmmoType, AmmoGroup}
alias Cannery.Ammo.{AmmoGroup, AmmoType}
alias Cannery.{Accounts.User, Ammo, Containers, Containers.Container}
alias Ecto.Changeset
alias Phoenix.LiveView.Socket

View File

@ -14,22 +14,20 @@ defmodule CanneryWeb.InviteLive.FormComponent do
Socket.t()
) :: {:ok, Socket.t()}
def update(%{invite: invite} = assigns, socket) do
changeset = Invites.change_invite(invite)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
{:ok, socket |> assign(assigns) |> assign(:changeset, Invites.change_invite(invite))}
end
@impl true
def handle_event("validate", %{"invite" => invite_params}, socket) do
changeset = socket.assigns.invite |> Invites.change_invite(invite_params)
{:noreply, assign(socket, :changeset, changeset)}
def handle_event(
"validate",
%{"invite" => invite_params},
%{assigns: %{invite: invite}} = socket
) do
{:noreply, socket |> assign(:changeset, invite |> Invites.change_invite(invite_params))}
end
def handle_event("save", %{"invite" => invite_params}, socket) do
save_invite(socket, socket.assigns.action, invite_params)
def handle_event("save", %{"invite" => invite_params}, %{assigns: %{action: action}} = socket) do
save_invite(socket, action, invite_params)
end
@impl true
@ -71,29 +69,39 @@ defmodule CanneryWeb.InviteLive.FormComponent do
"""
end
defp save_invite(socket, :edit, invite_params) do
case Invites.update_invite(socket.assigns.invite, invite_params) do
{:ok, _invite} ->
{:noreply,
socket
|> put_flash(:info, dgettext("prompts", "Invite updated successfully"))
|> push_redirect(to: socket.assigns.return_to)}
defp save_invite(
%{assigns: %{current_user: current_user, invite: invite, return_to: return_to}} = socket,
:edit,
invite_params
) do
socket =
case invite |> Invites.update_invite(invite_params, current_user) do
{:ok, %{name: invite_name}} ->
prompt = dgettext("prompts", "%{name} updated successfully", name: invite_name)
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
{:error, %Changeset{} = changeset} ->
socket |> assign(:changeset, changeset)
end
{:noreply, socket}
end
defp save_invite(socket, :new, invite_params) do
case Invites.create_invite(socket.assigns.current_user, invite_params) do
{:ok, _invite} ->
{:noreply,
socket
|> put_flash(:info, dgettext("prompts", "Invite created successfully"))
|> push_redirect(to: socket.assigns.return_to)}
defp save_invite(
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
:new,
invite_params
) do
socket =
case current_user |> Invites.create_invite(invite_params) do
{:ok, %{name: invite_name}} ->
prompt = dgettext("prompts", "%{name} created successfully", name: invite_name)
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
{:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset)
end
{:noreply, socket}
end
end

View File

@ -14,53 +14,87 @@ defmodule CanneryWeb.InviteLive.Index do
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, socket |> apply_action(socket.assigns.live_action, params)}
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, socket |> apply_action(live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
socket
|> assign(page_title: gettext("Edit Invite"), invite: Invites.get_invite!(id))
|> assign(page_title: gettext("Edit Invite"), invite: Invites.get_invite!(id, current_user))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(page_title: gettext("New Invite"), invite: %Invite{})
socket |> assign(page_title: gettext("New Invite"), invite: %Invite{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(page_title: gettext("Listing Invites"), invite: nil)
socket |> assign(page_title: gettext("Listing Invites"), invite: nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
invite = Invites.get_invite!(id)
{:ok, _} = Invites.delete_invite(invite)
{:noreply, socket |> display_invites()}
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
%{name: invite_name} =
id |> Invites.get_invite!(current_user) |> Invites.delete_invite!(current_user)
prompt = dgettext("prompts", "%{name} deleted succesfully", name: invite_name)
{:noreply, socket |> put_flash(:info, prompt) |> display_invites()}
end
def handle_event("set_unlimited", %{"id" => id}, socket) do
id |> Invites.get_invite!() |> Invites.update_invite(%{"uses_left" => nil})
{:noreply, socket |> display_invites()}
def handle_event(
"set_unlimited",
%{"id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
socket =
Invites.get_invite!(id, current_user)
|> Invites.update_invite(%{"uses_left" => nil}, current_user)
|> case do
{:ok, %{name: invite_name}} ->
prompt = dgettext("prompts", "%{name} updated succesfully", name: invite_name)
socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
{:noreply, socket}
end
def handle_event("enable", %{"id" => id}, socket) do
attrs = %{"uses_left" => nil, "disabled_at" => nil}
id |> Invites.get_invite!() |> Invites.update_invite(attrs)
{:noreply, socket |> display_invites()}
def handle_event("enable", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
socket =
Invites.get_invite!(id, current_user)
|> Invites.update_invite(%{"uses_left" => nil, "disabled_at" => nil}, current_user)
|> case do
{:ok, %{name: invite_name}} ->
prompt = dgettext("prompts", "%{name} enabled succesfully", name: invite_name)
socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
{:noreply, socket}
end
def handle_event("disable", %{"id" => id}, socket) do
def handle_event("disable", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
attrs = %{"uses_left" => 0, "disabled_at" => now}
id |> Invites.get_invite!() |> Invites.update_invite(attrs)
{:noreply, socket |> display_invites()}
socket =
Invites.get_invite!(id, current_user)
|> Invites.update_invite(%{"uses_left" => 0, "disabled_at" => now}, current_user)
|> case do
{:ok, %{name: invite_name}} ->
prompt = dgettext("prompts", "%{name} disabled succesfully", name: invite_name)
socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
{:noreply, socket}
end
# redisplays invites to socket
defp display_invites(socket) do
invites = Invites.list_invites()
socket |> assign(:invites, invites)
defp display_invites(%{assigns: %{current_user: current_user}} = socket) do
socket |> assign(:invites, Invites.list_invites(current_user))
end
end

View File

@ -69,7 +69,7 @@
<%= if invite.disabled_at |> is_nil() and not(invite.uses_left |> is_nil()) do %>
<a href="#" class="btn btn-primary"
phx-click="set_unlimited" phx-value-id="<%= invite.id %>"
data-confirm={dgettext("prompts", "Are you sure you want to make this invite unlimited?")}>
data-confirm={dgettext("prompts", "Are you sure you want to make %{name} unlimited?", name: invite.name)}>
<%= gettext("Set Unlimited") %>
</a>
<% end %>