2022-04-06 14:55:12 -04:00
|
|
|
defmodule AggieditWeb.PostLive.FormComponent do
|
|
|
|
use AggieditWeb, :live_component
|
|
|
|
|
|
|
|
alias Aggiedit.Rooms
|
2022-04-06 20:17:27 -04:00
|
|
|
alias Aggiedit.Rooms.Post
|
2022-04-07 17:52:32 -04:00
|
|
|
alias Aggiedit.Uploads
|
2022-04-06 20:17:27 -04:00
|
|
|
alias Aggiedit.Uploads.Upload
|
|
|
|
alias Aggiedit.Repo
|
2022-04-06 14:55:12 -04:00
|
|
|
|
|
|
|
@impl true
|
2022-04-07 17:52:32 -04:00
|
|
|
def update(%{current_user: current_user, post: post} = assigns, socket) do
|
2022-04-06 14:55:12 -04:00
|
|
|
changeset = Rooms.change_post(post)
|
|
|
|
|
|
|
|
{:ok,
|
|
|
|
socket
|
|
|
|
|> assign(assigns)
|
2022-04-13 16:17:28 -04:00
|
|
|
|> assign(%{changeset: changeset, current_user: current_user, uploaded_files: []})
|
2022-04-06 20:17:27 -04:00
|
|
|
|> allow_upload(:upload, accept: ~w(.jpg .jpeg .png .gif), max_entries: 1)
|
|
|
|
}
|
2022-04-06 14:55:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("validate", %{"post" => post_params}, socket) do
|
|
|
|
changeset =
|
|
|
|
socket.assigns.post
|
|
|
|
|> Rooms.change_post(post_params)
|
|
|
|
|> Map.put(:action, :validate)
|
|
|
|
|
|
|
|
{:noreply, assign(socket, :changeset, changeset)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("save", %{"post" => post_params}, socket) do
|
2022-04-06 20:17:27 -04:00
|
|
|
socket
|
|
|
|
|> save_post(socket.assigns.action, post_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp save_upload(socket, %Post{} = post) do
|
|
|
|
consume_uploaded_entries(socket, :upload, fn data, upload ->
|
|
|
|
[extension | _] = MIME.extensions(upload.client_type)
|
2022-04-08 15:01:24 -04:00
|
|
|
filename = "#{upload.uuid}.#{extension}"
|
2022-04-06 20:17:27 -04:00
|
|
|
|
|
|
|
dest = Path.join("priv/static/uploads", filename)
|
|
|
|
File.cp!(data.path, dest)
|
|
|
|
|
2022-04-07 17:52:32 -04:00
|
|
|
{:ok, upload} = Uploads.create_upload(%{
|
|
|
|
file: filename,
|
|
|
|
size: upload.client_size,
|
|
|
|
mime: upload.client_type,
|
|
|
|
}, socket.assigns.current_user)
|
|
|
|
|
|
|
|
post
|
|
|
|
|> Repo.preload(:upload)
|
2022-04-06 20:17:27 -04:00
|
|
|
|> Post.change_upload(upload)
|
2022-04-07 17:52:32 -04:00
|
|
|
|> Repo.update()
|
2022-04-06 20:17:27 -04:00
|
|
|
|
|
|
|
{:ok, upload}
|
|
|
|
end)
|
|
|
|
{:ok, post}
|
2022-04-06 14:55:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defp save_post(socket, :edit, post_params) do
|
2022-04-06 20:17:27 -04:00
|
|
|
case Rooms.update_post(socket.assigns.post, post_params, &save_upload(socket, &1)) do
|
2022-04-06 14:55:12 -04:00
|
|
|
{:ok, _post} ->
|
|
|
|
{:noreply,
|
|
|
|
socket
|
|
|
|
|> put_flash(:info, "Post updated successfully")
|
|
|
|
|> push_redirect(to: socket.assigns.return_to)}
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
{:noreply, assign(socket, :changeset, changeset)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp save_post(socket, :new, post_params) do
|
2022-04-07 17:52:32 -04:00
|
|
|
case Rooms.create_post(post_params, socket.assigns.current_user, &save_upload(socket, &1)) do
|
2022-04-06 14:55:12 -04:00
|
|
|
{:ok, _post} ->
|
|
|
|
{:noreply,
|
|
|
|
socket
|
|
|
|
|> put_flash(:info, "Post created successfully")
|
|
|
|
|> push_redirect(to: socket.assigns.return_to)}
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
{:noreply, assign(socket, changeset: changeset)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|