Packages
literature
0.1.10
0.5.1
0.5.0
0.4.22
0.4.21
0.4.20
0.4.19
0.4.18
0.4.17
0.4.16
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
retired
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.18
0.2.17
0.2.16
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.32
0.1.30
0.1.29
0.1.28
0.1.27
0.1.26
0.1.25
0.1.24
0.1.23
0.1.22
0.1.21
0.1.20
0.1.19
0.1.18
0.1.17
0.1.16
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
A simple CMS / Blog
Current section
Files
Jump to
Current section
Files
lib/literature/components/forms/author_form_component.ex
defmodule Literature.AuthorFormComponent do
@moduledoc false
use Literature.Web, :live_component
import Literature.FormComponent
@accept ~w(.jpg .jpeg .png)
@impl Phoenix.LiveComponent
def update(%{author: author} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign(:changeset, Literature.change_author(author))
|> allow_upload()
{:ok, socket}
end
defp allow_upload(socket) do
socket
|> allow_upload(:profile_image, accept: @accept, max_entries: 1, auto_upload: true)
|> allow_upload(:cover_image, accept: @accept, max_entries: 1, auto_upload: true)
end
@impl Phoenix.LiveComponent
def render(assigns) do
~H"""
<div>
<.form
let={f}
for={@changeset}
id="author-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save">
<.form_group title="Details">
<.form_field form={f} type="text_input" field={:name} label="Name" required={true} />
<.form_field form={f} type="text_input" field={:slug} label="Slug" required={true} disabled={@action == :new_author} placeholder={if @action == :new_author, do: "(auto-generate) you can change from edit page", else: ""} />
<.form_field form={f} type="image_upload" field={:profile_image} label="Profile Image" uploads={@uploads} />
<.form_field form={f} type="image_upload" field={:cover_image} label="Cover Image" uploads={@uploads} />
<.form_field form={f} type="textarea" field={:bio} label="Bio" />
<.form_field form={f} type="url_input" field={:website} label="Website" />
<.form_field form={f} type="text_input" field={:location} label="Location" />
<.form_field form={f} type="url_input" field={:facebook} label="Facebook" />
<.form_field form={f} type="url_input" field={:twitter} label="Twitter" />
</.form_group>
<.accordion title="Meta Tags">
<.form_field form={f} type="text_input" field={:meta_title} label="Meta Title" />
<.form_field form={f} type="textarea" field={:meta_description} label="Meta Description" />
<.form_field form={f} type="text_input" field={:meta_keywords} label="Meta Keywords" />
</.accordion>
<.button_group>
<.back_button label="Cancel" return_to={@return_to} />
<.submit_button label="Save changes" />
</.button_group>
</.form>
</div>
"""
end
@impl Phoenix.LiveComponent
def handle_event("validate", %{"author" => author_params}, socket) do
changeset =
socket.assigns.author
|> Literature.change_author(author_params)
|> put_validation(socket.assigns.action)
{:noreply, assign(socket, :changeset, changeset)}
end
@impl Phoenix.LiveComponent
def handle_event("save", %{"author" => author_params}, socket) do
save_author(socket, socket.assigns.action, author_params)
end
defp save_author(socket, :edit_author, author_params) do
images = build_uploaded_entries(socket, ~w(profile_image cover_image)a)
author_params = Map.merge(author_params, images)
case Literature.update_author(socket.assigns.author, author_params) do
{:ok, _author} ->
{:noreply,
socket
|> put_flash(:success, "Author updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_author(socket, :new_author, author_params) do
author_params =
author_params
|> put_publication_id(socket)
|> Map.merge(build_uploaded_entries(socket, ~w(profile_image cover_image)a))
case Literature.create_author(author_params) do
{:ok, _author} ->
{:noreply,
socket
|> put_flash(:success, "Author created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
defp put_publication_id(params, %{assigns: %{slug: slug}}) do
[slug: slug]
|> Literature.get_publication!()
|> then(&Map.put(params, "publication_id", &1.id))
end
defp put_validation(changeset, :new_author), do: changeset
defp put_validation(changeset, :edit_author), do: Map.put(changeset, :action, :validate)
end