forked from shibao/cannery
harden tags context
This commit is contained in:
@ -29,9 +29,7 @@ defmodule CanneryWeb.LiveHelpers do
|
||||
|
||||
def assign_defaults(socket, %{"user_token" => user_token} = _session) do
|
||||
socket
|
||||
|> assign_new(:current_user, fn ->
|
||||
Accounts.get_user_by_session_token(user_token)
|
||||
end)
|
||||
|> assign_new(:current_user, fn -> Accounts.get_user_by_session_token(user_token) end)
|
||||
end
|
||||
|
||||
def assign_defaults(socket, _session) do
|
||||
|
@ -4,29 +4,21 @@ defmodule CanneryWeb.TagLive.FormComponent do
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_component
|
||||
|
||||
alias Cannery.Tags
|
||||
alias Ecto.Changeset
|
||||
|
||||
@impl true
|
||||
def update(%{tag: tag} = assigns, socket) do
|
||||
changeset = Tags.change_tag(tag)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, changeset)}
|
||||
{:ok, socket |> assign(assigns) |> assign(:changeset, Tags.change_tag(tag))}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"tag" => tag_params}, socket) do
|
||||
tag_params = tag_params |> Map.put("user_id", socket.assigns.current_user.id)
|
||||
changeset = socket.assigns.tag |> Tags.change_tag(tag_params)
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
def handle_event("validate", %{"tag" => tag_params}, %{assigns: %{tag: tag}} = socket) do
|
||||
{:noreply, socket |> assign(:changeset, tag |> Tags.change_tag(tag_params))}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"tag" => tag_params}, socket) do
|
||||
save_tag(socket, socket.assigns.action, tag_params)
|
||||
def handle_event("save", %{"tag" => tag_params}, %{assigns: %{action: action}} = socket) do
|
||||
save_tag(socket, action, tag_params)
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -76,32 +68,39 @@ defmodule CanneryWeb.TagLive.FormComponent do
|
||||
"""
|
||||
end
|
||||
|
||||
defp save_tag(socket, :edit, tag_params) do
|
||||
case Tags.update_tag(socket.assigns.tag, tag_params) do
|
||||
{:ok, _tag} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, dgettext("prompts", "Tag updated successfully"))
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
defp save_tag(
|
||||
%{assigns: %{tag: tag, current_user: current_user, return_to: return_to}} = socket,
|
||||
:edit,
|
||||
tag_params
|
||||
) do
|
||||
socket =
|
||||
case Tags.update_tag(tag, tag_params, current_user) do
|
||||
{:ok, %{name: tag_name}} ->
|
||||
prompt = dgettext("prompts", "%{name} updated successfully", name: tag_name)
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
end
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
socket |> assign(:changeset, changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp save_tag(socket, :new, tag_params) do
|
||||
tag_params
|
||||
|> Map.put("user_id", socket.assigns.current_user.id)
|
||||
|> Tags.create_tag()
|
||||
|> case do
|
||||
{:ok, _tag} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, dgettext("prompts", "Tag created successfully"))
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
defp save_tag(
|
||||
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
|
||||
:new,
|
||||
tag_params
|
||||
) do
|
||||
socket =
|
||||
case Tags.create_tag(tag_params, current_user) do
|
||||
{:ok, %{name: tag_name}} ->
|
||||
prompt = dgettext("prompts", "%{name} created successfully", name: tag_name)
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(changeset: changeset)}
|
||||
end
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
socket |> assign(changeset: changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
@ -14,14 +14,14 @@ defmodule CanneryWeb.TagLive.Index do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, socket) do
|
||||
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
|
||||
{:noreply, apply_action(socket, 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 Tag"))
|
||||
|> assign(:tag, Tags.get_tag!(id))
|
||||
|> assign(:tag, Tags.get_tag!(id, current_user))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
@ -31,21 +31,19 @@ defmodule CanneryWeb.TagLive.Index do
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, gettext("Listing Tags"))
|
||||
|> assign(:tag, nil)
|
||||
socket |> assign(:page_title, gettext("Listing Tags")) |> assign(:tag, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
tag = Tags.get_tag!(id)
|
||||
{:ok, _} = Tags.delete_tag(tag)
|
||||
socket = socket |> put_flash(:info, dgettext("prompts", "Tag deleted succesfully"))
|
||||
{:noreply, socket |> display_tags()}
|
||||
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
|
||||
%{name: tag_name} = Tags.get_tag!(id, current_user) |> Tags.delete_tag!(current_user)
|
||||
|
||||
prompt = dgettext("prompts", "%{name} deleted succesfully", name: tag_name)
|
||||
|
||||
{:noreply, socket |> put_flash(:info, prompt) |> display_tags()}
|
||||
end
|
||||
|
||||
defp display_tags(socket) do
|
||||
tags = Tags.list_tags(socket.assigns.current_user)
|
||||
socket |> assign(tags: tags)
|
||||
defp display_tags(%{assigns: %{current_user: current_user}} = socket) do
|
||||
socket |> assign(tags: Tags.list_tags(current_user))
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user