[WIP] added placeholder UI and logic for settings form
This commit is contained in:
parent
24b3bfb038
commit
f84727b97e
7 changed files with 76 additions and 15 deletions
|
|
@ -20,6 +20,18 @@ defmodule Pinchflat.Settings do
|
||||||
|> Repo.one()
|
|> Repo.one()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Updates the setting record.
|
||||||
|
|
||||||
|
Returns {:ok, %Setting{}} | {:error, %Ecto.Changeset{}}
|
||||||
|
"""
|
||||||
|
# TODO: test
|
||||||
|
def update_setting(%Setting{} = setting, attrs) do
|
||||||
|
setting
|
||||||
|
|> Setting.changeset(attrs)
|
||||||
|
|> Repo.update()
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Updates a setting, returning the new value.
|
Updates a setting, returning the new value.
|
||||||
Is setup to take a keyword list argument so you
|
Is setup to take a keyword list argument so you
|
||||||
|
|
@ -29,8 +41,7 @@ defmodule Pinchflat.Settings do
|
||||||
"""
|
"""
|
||||||
def set([{attr, value}]) do
|
def set([{attr, value}]) do
|
||||||
record()
|
record()
|
||||||
|> Setting.changeset(%{attr => value})
|
|> update_setting(%{attr => value})
|
||||||
|> Repo.update()
|
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, %{^attr => _}} -> {:ok, value}
|
{:ok, %{^attr => _}} -> {:ok, value}
|
||||||
{:ok, _} -> {:error, :invalid_key}
|
{:ok, _} -> {:error, :invalid_key}
|
||||||
|
|
@ -61,4 +72,12 @@ defmodule Pinchflat.Settings do
|
||||||
{:error, _} -> raise "Setting `#{name}` not found"
|
{:error, _} -> raise "Setting `#{name}` not found"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns `%Ecto.Changeset{}`
|
||||||
|
"""
|
||||||
|
# TODO: test
|
||||||
|
def change_setting(%Setting{} = setting, attrs \\ %{}) do
|
||||||
|
Setting.changeset(setting, attrs)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,30 @@
|
||||||
defmodule PinchflatWeb.Settings.SettingController do
|
defmodule PinchflatWeb.Settings.SettingController do
|
||||||
use PinchflatWeb, :controller
|
use PinchflatWeb, :controller
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
# import Ecto.Query, warn: false
|
||||||
|
|
||||||
alias Pinchflat.Repo
|
# alias Pinchflat.Repo
|
||||||
|
alias Pinchflat.Settings
|
||||||
|
|
||||||
def edit(conn, _params) do
|
# TODO: test
|
||||||
render(conn, "edit.html")
|
def show(conn, _params) do
|
||||||
|
setting = Settings.record()
|
||||||
|
changeset = Settings.change_setting(setting)
|
||||||
|
|
||||||
|
render(conn, "show.html", changeset: changeset)
|
||||||
|
end
|
||||||
|
|
||||||
|
def update(conn, %{"setting" => setting_params}) do
|
||||||
|
setting = Settings.record()
|
||||||
|
|
||||||
|
case Settings.update_setting(setting, setting_params) do
|
||||||
|
{:ok, setting} ->
|
||||||
|
conn
|
||||||
|
|> put_flash(:info, "Settings updated successfully.")
|
||||||
|
|> redirect(to: ~p"/settings")
|
||||||
|
|
||||||
|
{:error, %Ecto.Changeset{} = changeset} ->
|
||||||
|
render(conn, "show.html", changeset: changeset)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,11 @@ defmodule PinchflatWeb.Settings.SettingHTML do
|
||||||
|
|
||||||
embed_templates "setting_html/*"
|
embed_templates "setting_html/*"
|
||||||
|
|
||||||
# @doc """
|
@doc """
|
||||||
# Renders a setting form.
|
Renders a setting form.
|
||||||
# """
|
"""
|
||||||
# attr :changeset, Ecto.Changeset, required: true
|
attr :changeset, Ecto.Changeset, required: true
|
||||||
# attr :action, :string, required: true
|
attr :action, :string, required: true
|
||||||
|
|
||||||
# def setting_form(assigns)
|
def setting_form(assigns)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
<h1>TEST</h1>
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
<.simple_form :let={f} for={@changeset} action={@action}>
|
||||||
|
<.error :if={@changeset.action}>
|
||||||
|
Oops, something went wrong! Please check the errors below.
|
||||||
|
</.error>
|
||||||
|
|
||||||
|
<h3 class="mt-8 text-2xl text-black dark:text-white">
|
||||||
|
TODO TEXT
|
||||||
|
</h3>
|
||||||
|
|
||||||
|
<.input field={f[:pro_enabled]} type="toggle" label="Pro Enabled" help="Just for testing" />
|
||||||
|
|
||||||
|
<.button class="my-10 sm:mb-7.5 w-full sm:w-auto" rounding="rounded-lg">Save Settings</.button>
|
||||||
|
</.simple_form>
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
<div class="mb-6 flex gap-3 flex-row items-center justify-between">
|
||||||
|
<div class="flex gap-3 items-center">
|
||||||
|
<h2 class="text-title-md2 font-bold text-black dark:text-white ml-4">
|
||||||
|
Settings
|
||||||
|
</h2>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="rounded-sm border border-stroke bg-white px-5 py-5 shadow-default dark:border-strokedark dark:bg-boxdark sm:px-7.5">
|
||||||
|
<div class="max-w-full overflow-x-auto">
|
||||||
|
<.setting_form changeset={@changeset} action={~p"/settings"} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
@ -27,11 +27,10 @@ defmodule PinchflatWeb.Router do
|
||||||
pipe_through :browser
|
pipe_through :browser
|
||||||
|
|
||||||
get "/", Pages.PageController, :home
|
get "/", Pages.PageController, :home
|
||||||
get "/settings", Settings.SettingController, :edit
|
|
||||||
resources "/settings", Settings.SettingController, only: [:update], singleton: true
|
|
||||||
|
|
||||||
resources "/media_profiles", MediaProfiles.MediaProfileController
|
resources "/media_profiles", MediaProfiles.MediaProfileController
|
||||||
resources "/search", Searches.SearchController, only: [:show], singleton: true
|
resources "/search", Searches.SearchController, only: [:show], singleton: true
|
||||||
|
resources "/settings", Settings.SettingController, only: [:show, :update], singleton: true
|
||||||
|
|
||||||
resources "/sources", Sources.SourceController do
|
resources "/sources", Sources.SourceController do
|
||||||
post "/force_download", Sources.SourceController, :force_download
|
post "/force_download", Sources.SourceController, :force_download
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue