-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathform_component.ex
More file actions
95 lines (79 loc) · 2.63 KB
/
form_component.ex
File metadata and controls
95 lines (79 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
defmodule AtomicWeb.ActivityLive.FormComponent do
use AtomicWeb, :live_component
alias Atomic.Activities
import AtomicWeb.Components.{Forms, ImageUploader}
@impl true
def update(%{activity: activity} = assigns, socket) do
changeset = Activities.change_activity(activity)
{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)
|> allow_upload(:image, accept: Uploaders.Post.extension_whitelist(), max_entries: 1)}
end
@impl true
def handle_event("validate", %{"activity" => activity_params}, socket) do
changeset =
socket.assigns.activity
|> Activities.change_activity(activity_params)
|> Map.put(:action, :validate)
{:noreply, assign_form(socket, changeset)}
end
@impl true
def handle_event("save", %{"activity" => activity_params}, socket) do
activity_params =
activity_params
|> Map.put("organization_id", socket.assigns.current_organization.id)
save_activity(socket, socket.assigns.action, activity_params)
end
@impl true
def handle_event("cancel-image", %{"ref" => ref}, socket) do
{:noreply, cancel_upload(socket, :image, ref)}
end
defp save_activity(socket, :new, activity_params) do
case Activities.create_activity_with_post(activity_params, &consume_image_data(socket, &1)) do
{:ok, _activity} ->
{:noreply,
socket
|> put_flash(:info, "Activity created successfully!")
|> push_navigate(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp save_activity(socket, :edit, activity_params) do
case Activities.update_activity(
socket.assigns.activity,
activity_params,
&consume_image_data(socket, &1)
) do
{:ok, _activity} ->
{:noreply,
socket
|> put_flash(:info, "Activity updated successfully!")
|> push_navigate(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp consume_image_data(socket, activity) do
consume_uploaded_entries(socket, :image, fn %{path: path}, entry ->
Activities.update_activity_image(activity, %{
"image" => %Plug.Upload{
content_type: entry.client_type,
filename: entry.client_name,
path: path
}
})
end)
|> case do
[{:ok, activity}] ->
{:ok, activity}
_errors ->
{:ok, activity}
end
end
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
end