Skip to content

Commit

Permalink
chore: suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ruioliveira02 committed Nov 18, 2024
1 parent 68226e7 commit c2ebb6b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
12 changes: 12 additions & 0 deletions lib/safira_web/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 15 additions & 18 deletions lib/safira_web/live/backoffice/event_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule SafiraWeb.Backoffice.EventLive.FormComponent do

alias Safira.Event

alias SafiraWeb.Helpers

@impl true
def render(assigns) do
~H"""
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion priv/repo/seeds/constants.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit c2ebb6b

Please sign in to comment.