Skip to content

A helper library that supports converting form to params and params to form

License

Notifications You must be signed in to change notification settings

nallwhy/doumi_phoenix_params

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Doumi.Phoenix.Params

Hex Version Hex Docs License Last Updated

Doumi.Phoenix.Params is a helper library that supports converting form to params and params to form.

도우미(Doumi) means "helper" in Korean.

Usage

defmodule MyAppWeb.TestParams do
  use Ecto.Schema
  use Doumi.Phoenix.Params, as: :test
  import Ecto.Changeset

  @primary_key false
  embedded_schema do
    field :foo, :string
  end

  @required [:foo]
  def changeset(%__MODULE__{} = struct, attrs) do
    struct
    |> cast(attrs, @required)
    |> validate_required(@required)
  end
end

defmodule MyAppWeb.MyLive do
  use MyAppWeb, :live_view
  alias MyAppWeb.TestParams
  alias Doumi.Phoenix.Params

  ...

  @impl true
  def mount(_params, _session, socket) do
    # Without Doumi.Phoenix.Params
    # form =
    #   TestParams.changeset(%TestParams{}, %{})
    #   |> to_form(as: :test)

    # With Doumi.Phoenix.Params
    form = TestParams.to_form(%{}, validate: false)

    socket = socket |> assign(:form, form)

    {:ok, socket}
  end

  @impl true
  def handle_event("validate", %{"test" => test_params}, socket) do
    # Without Doumi.Phoenix.Params
    # form =
    #   TestParams.changeset(%TestParams{}, test_params)
    #   |> to_form(as: :test)
    #   |> Map.put(:action, :validate)

    # With Doumi.Phoenix.Params
    form = TestParams.to_form(test_params)

    socket = socket |> assign(:form, form)

    {:noreply, socket}
  end

  @impl true
  def handle_event("change_foo_from_outside_of_form", %{"foo" => foo}, socket) do
    # Without Doumi.Phoenix.Params
    # test_params =
    #   socket.assigns.form.params
    #   |> Map.put(%{"foo" => foo})
    #
    # form =
    #   TestParams.changeset(%TestParams{}, test_params)
    #   |> to_form(as: :test)
    #   |> Map.put(:action, :validate)

    # With Doumi.Phoenix.Params
    test_params =
      socket.assigns.form
      |> Params.to_params(%{"foo" => foo})

    form = TestParams.to_form(test_params)

    socket = socket |> assign(:form, form)

    {:noreply, socket}
  end

  @impl true
  def handle_event("save", %{"test" => test_params}, socket) do
    # Without Doumi.Phoenix.Params
    # test_params_map =
    #   TestParams.changeset(%TestParams, test_params)
    #   |> Ecto.Changeset.apply_changes()
    #   |> some_how_change_nested_struct_to_map_if_you_use_map_with_atom_key()

    # With Doumi.Phoenix.Params
    test_params_map =
      test_params
      |> Params.to_form()
      |> Params.to_map()

    :ok = TestDomain.create_test(test_params_map)

    {:noreplym, socket}
  end

  ...
end

Here's the working example

Installation

The package can be installed by adding doumi_phoenix_params to your list of dependencies in mix.exs:

def deps do
  [
    {:doumi_phoenix_params, "~> 0.3.0"}
  ]
end

Other Doumi packages

  • Doumi.Phoenix.SVG: A helper library that generates Phoenix function components from SVG files.
  • Doumi.URI.Query: A helper library that encode complicated query of URI.

Copyright and License

Copyright (c) 2023 Jinkyou Son (Json)

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.

About

A helper library that supports converting form to params and params to form

Topics

Resources

License

Stars

Watchers

Forks

Languages