-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_live.ex
60 lines (47 loc) · 1.79 KB
/
example_live.ex
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
defmodule LiveViewFormWeb.ExampleLive do
use Phoenix.LiveView
alias LiveViewFormWeb.Router.Helpers, as: Routes
def render(assigns) do
LiveViewFormWeb.ExampleView.render("form.html", assigns)
end
def mount(_session, socket) do
socket =
socket
|> assign(:current_step, 1)
|> assign(:changeset, LiveViewForm.change_example(%{}))
{:ok, socket}
end
def handle_event("prev-step", _value, socket) do
new_step = max(socket.assigns.current_step - 1, 1)
{:noreply, assign(socket, :current_step, new_step)}
end
def handle_event("next-step", _value, socket) do
current_step = socket.assigns.current_step
changeset = socket.assigns.changeset
step_invalid =
case current_step do
1 -> Enum.any?(Keyword.keys(changeset.errors), fn k -> k in [:title, :description] end)
2 -> Enum.any?(Keyword.keys(changeset.errors), fn k -> k in [:type] end)
_ -> true
end
new_step = if step_invalid, do: current_step, else: current_step + 1
{:noreply, assign(socket, :current_step, new_step)}
end
def handle_event("validate", %{"example" => params}, socket) do
changeset = LiveViewForm.change_example(params) |> Map.put(:action, :insert)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"example" => params}, socket) do
# Pretending to insert stuff if changeset is valid
changeset = LiveViewForm.change_example(params)
case changeset.valid? do
true ->
{:stop,
socket
|> put_flash(:info, "Example inserted => #{inspect(changeset.changes)}")
|> redirect(to: Routes.live_path(LiveViewFormWeb.Endpoint, LiveViewFormWeb.ExampleLive))}
false ->
{:noreply, assign(socket, :changeset, %{changeset | action: :insert})}
end
end
end