[WIP] added placeholder UI and logic for settings form

This commit is contained in:
Kieran Eglin 2024-04-04 14:45:58 -07:00
parent 24b3bfb038
commit f84727b97e
No known key found for this signature in database
GPG key ID: 193984967FCF432D
7 changed files with 76 additions and 15 deletions

View file

@ -20,6 +20,18 @@ defmodule Pinchflat.Settings do
|> Repo.one()
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 """
Updates a setting, returning the new value.
Is setup to take a keyword list argument so you
@ -29,8 +41,7 @@ defmodule Pinchflat.Settings do
"""
def set([{attr, value}]) do
record()
|> Setting.changeset(%{attr => value})
|> Repo.update()
|> update_setting(%{attr => value})
|> case do
{:ok, %{^attr => _}} -> {:ok, value}
{:ok, _} -> {:error, :invalid_key}
@ -61,4 +72,12 @@ defmodule Pinchflat.Settings do
{:error, _} -> raise "Setting `#{name}` not found"
end
end
@doc """
Returns `%Ecto.Changeset{}`
"""
# TODO: test
def change_setting(%Setting{} = setting, attrs \\ %{}) do
Setting.changeset(setting, attrs)
end
end

View file

@ -1,11 +1,30 @@
defmodule PinchflatWeb.Settings.SettingController do
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
render(conn, "edit.html")
# TODO: test
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

View file

@ -3,11 +3,11 @@ defmodule PinchflatWeb.Settings.SettingHTML do
embed_templates "setting_html/*"
# @doc """
# Renders a setting form.
# """
# attr :changeset, Ecto.Changeset, required: true
# attr :action, :string, required: true
@doc """
Renders a setting form.
"""
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true
# def setting_form(assigns)
def setting_form(assigns)
end

View file

@ -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>

View file

@ -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>

View file

@ -27,11 +27,10 @@ defmodule PinchflatWeb.Router do
pipe_through :browser
get "/", Pages.PageController, :home
get "/settings", Settings.SettingController, :edit
resources "/settings", Settings.SettingController, only: [:update], singleton: true
resources "/media_profiles", MediaProfiles.MediaProfileController
resources "/search", Searches.SearchController, only: [:show], singleton: true
resources "/settings", Settings.SettingController, only: [:show, :update], singleton: true
resources "/sources", Sources.SourceController do
post "/force_download", Sources.SourceController, :force_download