diff --git a/lib/pinchflat/settings/settings.ex b/lib/pinchflat/settings/settings.ex index 4b133c8..ab8d10d 100644 --- a/lib/pinchflat/settings/settings.ex +++ b/lib/pinchflat/settings/settings.ex @@ -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 diff --git a/lib/pinchflat_web/controllers/settings/setting_controller.ex b/lib/pinchflat_web/controllers/settings/setting_controller.ex index 461fc78..7ed85ed 100644 --- a/lib/pinchflat_web/controllers/settings/setting_controller.ex +++ b/lib/pinchflat_web/controllers/settings/setting_controller.ex @@ -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 diff --git a/lib/pinchflat_web/controllers/settings/setting_html.ex b/lib/pinchflat_web/controllers/settings/setting_html.ex index 084e01b..0bfc46e 100644 --- a/lib/pinchflat_web/controllers/settings/setting_html.ex +++ b/lib/pinchflat_web/controllers/settings/setting_html.ex @@ -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 diff --git a/lib/pinchflat_web/controllers/settings/setting_html/edit.html.heex b/lib/pinchflat_web/controllers/settings/setting_html/edit.html.heex deleted file mode 100644 index 143f3de..0000000 --- a/lib/pinchflat_web/controllers/settings/setting_html/edit.html.heex +++ /dev/null @@ -1 +0,0 @@ -

TEST

diff --git a/lib/pinchflat_web/controllers/settings/setting_html/setting_form.html.heex b/lib/pinchflat_web/controllers/settings/setting_html/setting_form.html.heex new file mode 100644 index 0000000..0079180 --- /dev/null +++ b/lib/pinchflat_web/controllers/settings/setting_html/setting_form.html.heex @@ -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. + + +

+ TODO TEXT +

+ + <.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 + diff --git a/lib/pinchflat_web/controllers/settings/setting_html/show.html.heex b/lib/pinchflat_web/controllers/settings/setting_html/show.html.heex new file mode 100644 index 0000000..db90e00 --- /dev/null +++ b/lib/pinchflat_web/controllers/settings/setting_html/show.html.heex @@ -0,0 +1,12 @@ +
+
+

+ Settings +

+
+
+
+
+ <.setting_form changeset={@changeset} action={~p"/settings"} /> +
+
diff --git a/lib/pinchflat_web/router.ex b/lib/pinchflat_web/router.ex index 2ed457f..04edebe 100644 --- a/lib/pinchflat_web/router.ex +++ b/lib/pinchflat_web/router.ex @@ -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