From c2ebb6bc93e4b3ebfb7a62ad2431184185f1637f Mon Sep 17 00:00:00 2001 From: Rui Oliveira Date: Mon, 18 Nov 2024 13:53:59 +0000 Subject: [PATCH] chore: suggestions --- lib/safira_web/helpers.ex | 12 +++++++ .../backoffice/event_live/form_component.ex | 33 +++++++++---------- priv/repo/seeds/constants.exs | 2 +- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/safira_web/helpers.ex b/lib/safira_web/helpers.ex index 7971c6bd..10375be6 100644 --- a/lib/safira_web/helpers.ex +++ b/lib/safira_web/helpers.ex @@ -161,4 +161,16 @@ defmodule SafiraWeb.Helpers do |> QRCodeEx.encode() |> QRCodeEx.svg(background_color: "#FFFFFF", color: "#04041C", width: 200) end + + defp parse_date(date_str) do + {:ok, date, _} = DateTime.from_iso8601("#{date_str}:00Z") + date + end + + defp string_to_bool(str) do + case String.downcase(str) do + "true" -> true + _ -> false + end + end end diff --git a/lib/safira_web/live/backoffice/event_live/form_component.ex b/lib/safira_web/live/backoffice/event_live/form_component.ex index c734d513..9d870077 100644 --- a/lib/safira_web/live/backoffice/event_live/form_component.ex +++ b/lib/safira_web/live/backoffice/event_live/form_component.ex @@ -5,6 +5,8 @@ defmodule SafiraWeb.Backoffice.EventLive.FormComponent do alias Safira.Event + alias SafiraWeb.Helpers + @impl true def render(assigns) do ~H""" @@ -61,24 +63,19 @@ defmodule SafiraWeb.Backoffice.EventLive.FormComponent do end def handle_event("save", params, socket) do - Event.change_registrations_open(string_to_bool(params["registrations_open"])) - Event.change_event_start_time(parse_date(params["start_time"])) - - {:noreply, - socket - |> put_flash(:info, "Event settings updated successfully") - |> push_patch(to: socket.assigns.patch)} - end - - defp parse_date(date_str) do - {:ok, date, _} = DateTime.from_iso8601("#{date_str}:00Z") - date - end - - defp string_to_bool(str) do - case String.downcase(str) do - "true" -> true - _ -> false + with {:ok, _registrations_open} <- + Event.change_registrations_open(Helpers.string_to_bool(params["registrations_open"])), + {:ok, _start_time} <- + Event.change_event_start_time(Helpers.parse_date(params["start_time"])) do + {:noreply, + socket + |> put_flash(:info, "Event settings updated successfully") + |> push_patch(to: socket.assigns.patch)} + else + {:error, _reason} -> + {:noreply, + socket + |> put_flash(:error, "Failed to save event settings")} end end end diff --git a/priv/repo/seeds/constants.exs b/priv/repo/seeds/constants.exs index 159a40ac..71530076 100644 --- a/priv/repo/seeds/constants.exs +++ b/priv/repo/seeds/constants.exs @@ -3,7 +3,7 @@ defmodule Safira.Repo.Seeds.Constants do def run do Constants.set("registrations_open", "true") - Constants.set("start_time", "2025-09-29T17:57:00Z") + Constants.set("start_time", "2024-09-29T17:57:00Z") end end