mirror of
https://github.com/ash-project/ash_phoenix.git
synced 2024-09-17 05:42:49 +12:00
docs: Use AshPhoenix.Form.submit/2 instead of calling context functions (#220)
* Use AshPhoenix.Form.submit/2 instead of calling context functions * Fix name
This commit is contained in:
parent
d7a766742c
commit
5c5ee5a545
1 changed files with 16 additions and 8 deletions
|
@ -444,20 +444,28 @@ defmodule MyAshPhoenixAppWeb.PostsLive do
|
|||
{:noreply, assign(socket, posts: posts, post_selector: post_selector(posts))}
|
||||
end
|
||||
|
||||
def handle_event("create_post", %{"form" => %{"title" => title}}, socket) do
|
||||
Blog.create_post(%{title: title})
|
||||
posts = Blog.list_posts!()
|
||||
def handle_event("create_post", %{"form" => form_params}, socket) do
|
||||
case AshPhoenix.Form.submit(socket.assigns.create_form, params: form_params) do
|
||||
{:ok, _post} ->
|
||||
posts = Blog.list_posts!()
|
||||
|
||||
{:noreply, assign(socket, posts: posts, post_selector: post_selector(posts))}
|
||||
{:noreply, assign(socket, posts: posts, post_selector: post_selector(posts))}
|
||||
|
||||
{:error, create_form} ->
|
||||
{:noreply, assign(socket, create_form: create_form)}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("update_post", %{"form" => form_params}, socket) do
|
||||
%{"post_id" => post_id, "content" => content} = form_params
|
||||
case AshPhoenix.Form.submit(socket.assigns.update_form, params: form_params) do
|
||||
{:ok, _post} ->
|
||||
posts = Blog.list_posts!()
|
||||
|
||||
post_id |> Blog.get_post!() |> Blog.update_post!(%{content: content})
|
||||
posts = Blog.list_posts!()
|
||||
{:noreply, assign(socket, posts: posts, post_selector: post_selector(posts))}
|
||||
|
||||
{:noreply, assign(socket, posts: posts, post_selector: post_selector(posts))}
|
||||
{:error, update_form} ->
|
||||
{:noreply, assign(socket, update_form: update_form)}
|
||||
end
|
||||
end
|
||||
|
||||
defp post_selector(posts) do
|
||||
|
|
Loading…
Reference in a new issue