diff --git a/config/config.exs b/config/config.exs index 37bf20e0..83f8b26b 100644 --- a/config/config.exs +++ b/config/config.exs @@ -10,9 +10,6 @@ use Mix.Config config :shift73k, ecto_repos: [Shift73k.Repo] -# Configure the time zone database -config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase - # Custom application global variables config :shift73k, :app_global_vars, time_zone: "America/New_York", diff --git a/lib/shift73k/shifts/templates/shift_template.ex b/lib/shift73k/shifts/templates/shift_template.ex index 8a53394d..5896ce7d 100644 --- a/lib/shift73k/shifts/templates/shift_template.ex +++ b/lib/shift73k/shifts/templates/shift_template.ex @@ -1,7 +1,10 @@ defmodule Shift73k.Shifts.Templates.ShiftTemplate do + use Timex use Ecto.Schema import Ecto.Changeset + alias Shift73k.Shifts.Templates.ShiftTemplate + @app_vars Application.get_env(:shift73k, :app_global_vars, time_zone: "America/New_York") @time_zone @app_vars[:time_zone] @@ -42,45 +45,42 @@ defmodule Shift73k.Shifts.Templates.ShiftTemplate do |> validate_length(:subject, count: :codepoints, max: 280) |> validate_length(:location, count: :codepoints, max: 280) |> validate_change(:time_end, fn :time_end, time_end -> - shift_length = shift_length(get_time_start(attrs), time_end) + shift_length = shift_length(time_end, time_start_from_attrs(attrs)) cond do shift_length == 0 -> [time_end: "end time cannot equal start time"] - shift_length >= 16 * 3600 -> + shift_length >= 16 * 60 -> [time_end: "you don't want to work 16 or more hours!"] true -> [] end end) - |> validate_inclusion(:time_zone, Tzdata.zone_list()) + |> validate_inclusion(:time_zone, Timex.timezones()) end - defp get_time_start(%{"time_start" => time_start}), do: time_start - defp get_time_start(%{time_start: time_start}), do: time_start - defp get_time_start(_), do: nil + defp time_start_from_attrs(%{"time_start" => time_start}), do: time_start + defp time_start_from_attrs(%{time_start: time_start}), do: time_start + defp time_start_from_attrs(_), do: nil - def shift_length(time_start, time_end) do - cond do - time_end > time_start -> - Time.diff(time_end, time_start) - - time_start > time_end -> - len1 = Time.diff(~T[23:59:59], time_start) + 1 - len2 = Time.diff(time_end, ~T[00:00:00]) - len1 + len2 - - true -> - 0 - end + def shift_length(%ShiftTemplate{time_end: time_end, time_start: time_start}) do + time_end + |> Timex.diff(time_start, :minute) + |> shift_length() end - def shift_length_h_m_tuple(time_start, time_end) do - shift_length_seconds = shift_length(time_start, time_end) - h = shift_length_seconds |> Integer.floor_div(3600) - m = shift_length_seconds |> rem(3600) |> Integer.floor_div(60) + def shift_length(len_min) when is_integer(len_min) and len_min >= 0, do: len_min + def shift_length(len_min) when is_integer(len_min) and len_min < 0, do: 1440 + len_min + + def shift_length(time_end, time_start), + do: shift_length(%ShiftTemplate{time_end: time_end, time_start: time_start}) + + def shift_length_h_m(%ShiftTemplate{time_end: _, time_start: _} = template) do + shift_length_seconds = shift_length(template) + h = shift_length_seconds |> Integer.floor_div(60) + m = shift_length_seconds |> rem(60) {h, m} end end diff --git a/lib/shift73k_web/live/shift_template_live/form_component.ex b/lib/shift73k_web/live/shift_template_live/form_component.ex index 53d9965f..055a7a63 100644 --- a/lib/shift73k_web/live/shift_template_live/form_component.ex +++ b/lib/shift73k_web/live/shift_template_live/form_component.ex @@ -11,49 +11,45 @@ defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do socket |> assign(assigns) |> assign(:changeset, changeset) - |> assign_shift_length(shift_template.time_start, shift_template.time_end) + |> assign_shift_length(shift_template) |> live_okreply() end - defp assign_shift_length(socket, time_start, time_end) do - shift_length = ShiftTemplate.shift_length_h_m_tuple(time_start, time_end) + defp assign_shift_length(socket, shift_template) do + shift_length = ShiftTemplate.shift_length_h_m(shift_template) assign(socket, :shift_length, shift_length) end - defp prep_shift_template_params(shift_template_params, current_user) do - time_start = Time.from_iso8601!("T#{shift_template_params["time_start"]}:00") - time_end = Time.from_iso8601!("T#{shift_template_params["time_end"]}:00") - - shift_template_params - |> Map.put("time_start", time_start) - |> Map.put("time_end", time_end) + defp prep_template_params(params, current_user) do + params + |> Map.put("time_start", Time.from_iso8601!("T#{params["time_start"]}:00")) + |> Map.put("time_end", Time.from_iso8601!("T#{params["time_end"]}:00")) |> Map.put("user_id", current_user.id) end @impl true - def handle_event("validate", %{"shift_template" => shift_template_params}, socket) do - shift_template_params = - prep_shift_template_params(shift_template_params, socket.assigns.current_user) + def handle_event("validate", %{"shift_template" => params}, socket) do + params = prep_template_params(params, socket.assigns.current_user) changeset = socket.assigns.shift_template - |> Templates.change_shift_template(shift_template_params) + |> Templates.change_shift_template(params) |> Map.put(:action, :validate) socket |> assign(:changeset, changeset) - |> assign_shift_length( - shift_template_params["time_start"], - shift_template_params["time_end"] - ) + |> assign_shift_length(%ShiftTemplate{ + time_end: params["time_end"], + time_start: params["time_start"] + }) |> live_noreply() end - def handle_event("save", %{"shift_template" => shift_template_params}, socket) do + def handle_event("save", %{"shift_template" => params}, socket) do save_shift_template( socket, socket.assigns.action, - prep_shift_template_params(shift_template_params, socket.assigns.current_user) + prep_template_params(params, socket.assigns.current_user) ) end @@ -62,8 +58,8 @@ defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do {:noreply, push_event(socket, "modal-please-hide", %{})} end - defp save_shift_template(socket, :new, shift_template_params) do - case Templates.create_shift_template(shift_template_params) do + defp save_shift_template(socket, :new, params) do + case Templates.create_shift_template(params) do {:ok, _shift_template} -> flash = {:info, "Shift template created successfully"} send(self(), {:put_flash_message, flash}) @@ -77,15 +73,12 @@ defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do end end - defp save_shift_template(socket, :clone, shift_template_params) do - save_shift_template(socket, :new, shift_template_params) + defp save_shift_template(socket, :clone, params) do + save_shift_template(socket, :new, params) end - defp save_shift_template(socket, :edit, shift_template_params) do - case Templates.update_shift_template( - socket.assigns.shift_template, - shift_template_params - ) do + defp save_shift_template(socket, :edit, params) do + case Templates.update_shift_template(socket.assigns.shift_template, params) do {:ok, _shift_template} -> flash = {:info, "Shift template updated successfully"} send(self(), {:put_flash_message, flash}) diff --git a/lib/shift73k_web/live/shift_template_live/form_component.html.leex b/lib/shift73k_web/live/shift_template_live/form_component.html.leex index c6286012..71cbd7c5 100644 --- a/lib/shift73k_web/live/shift_template_live/form_component.html.leex +++ b/lib/shift73k_web/live/shift_template_live/form_component.html.leex @@ -86,7 +86,7 @@ list: "tz_list" %> - <%= for tz_name <- Tzdata.zone_list() do %> + <%= for tz_name <- Timex.timezones() do %> <% end %> end diff --git a/lib/shift73k_web/live/shift_template_live/index.ex b/lib/shift73k_web/live/shift_template_live/index.ex index 20fc1e80..07f06de1 100644 --- a/lib/shift73k_web/live/shift_template_live/index.ex +++ b/lib/shift73k_web/live/shift_template_live/index.ex @@ -8,7 +8,6 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do @impl true def mount(_params, session, socket) do - # {:ok, assign(socket, :shift_templates, list_shift_templates())} socket |> assign_defaults(session) |> live_okreply() @@ -97,14 +96,6 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do |> live_noreply() end - # @impl true - # def handle_event("delete", %{"id" => id}, socket) do - # shift_template = Templates.get_shift_template!(id) - # {:ok, _} = Templates.delete_shift_template(shift_template) - - # {:noreply, assign_shift_templates(socket)} - # end - @impl true def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do socket |> copy_flash() |> push_patch(to: to) |> live_noreply() diff --git a/lib/shift73k_web/live/shift_template_live/index.html.leex b/lib/shift73k_web/live/shift_template_live/index.html.leex index 4af1f4f7..501d5f31 100644 --- a/lib/shift73k_web/live/shift_template_live/index.html.leex +++ b/lib/shift73k_web/live/shift_template_live/index.html.leex @@ -33,18 +33,18 @@
- <%= for shift <- @shift_templates do %> + <%= for template <- @shift_templates do %>
Subject:
- <%= shift.subject %> - <%= if shift.id == @current_user.fave_shift_template_id do %> + <%= template.subject %> + <%= if template.id == @current_user.fave_shift_template_id do %> <%= icon_div @socket, "bi-star-fill", [class: "icon baseline text-primary align-self-start ms-2", phx_click: "unset-user-fave-shift-template"] %> <% else %> - <%= icon_div @socket, "bi-star", [class: "icon baseline text-primary align-self-start ms-2", phx_click: "set-user-fave-shift-template", phx_value_id: shift.id] %> + <%= icon_div @socket, "bi-star", [class: "icon baseline text-primary align-self-start ms-2", phx_click: "set-user-fave-shift-template", phx_value_id: template.id] %> <% end %>
@@ -57,15 +57,15 @@ Hours: - <%= shift.time_start |> Calendar.strftime("%I:%M%P") %> + <%= template.time_start |> Calendar.strftime("%I:%M%P") %> — - <%= shift.time_end |> Calendar.strftime("%I:%M%P") %> + <%= template.time_end |> Calendar.strftime("%I:%M%P") %> Shift length: - <% shift_length = ShiftTemplate.shift_length_h_m_tuple(shift.time_start, shift.time_end) %> - (<%= shift_length |> elem(0) %>h <%= shift_length |> elem(1) %>m) + <% shift_len = ShiftTemplate.shift_length_h_m(template) %> + (<%= shift_len |> elem(0) %>h <%= shift_len |> elem(1) %>m) - TZ: <%= shift.time_zone %> + TZ: <%= template.time_zone %> @@ -75,8 +75,8 @@ Location: - <%= if shift.location do %> - <%= shift.location %> + <%= if template.location do %> + <%= template.location %> <% else %> none <% end %> @@ -88,8 +88,8 @@ Description: - <%= if shift.description do %> - <%= text_to_html shift.description %> + <%= if template.description do %> + <%= text_to_html template.description %> <% else %> none <% end %> @@ -98,15 +98,15 @@ - <%= if Roles.can?(@current_user, shift, :edit) do %> - <%= live_patch to: Routes.shift_template_index_path(@socket, :edit, shift), class: "btn btn-primary btn-sm text-nowrap" do %> + <%= if Roles.can?(@current_user, template, :edit) do %> + <%= live_patch to: Routes.shift_template_index_path(@socket, :edit, template), class: "btn btn-primary btn-sm text-nowrap" do %> <%= icon_div @socket, "bi-pencil", [class: "icon baseline", style: "margin-right:0.125rem;"] %> Edit <% end %> <% end %> - <%= if Roles.can?(@current_user, shift, :clone) do %> - <%= live_patch to: Routes.shift_template_index_path(@socket, :clone, shift), class: "btn btn-outline-primary btn-sm text-nowrap" do %> + <%= if Roles.can?(@current_user, template, :clone) do %> + <%= live_patch to: Routes.shift_template_index_path(@socket, :clone, template), class: "btn btn-outline-primary btn-sm text-nowrap" do %> <%= icon_div @socket, "bi-clipboard-plus", [class: "icon baseline", style: "margin-right:0.125rem;"] %> Clone <% end %> @@ -114,8 +114,8 @@ <%#= button "" %> - <%= if Roles.can?(@current_user, shift, :delete) do %> - diff --git a/mix.exs b/mix.exs index 3bc90306..6b83d8a8 100644 --- a/mix.exs +++ b/mix.exs @@ -52,7 +52,7 @@ defmodule Shift73k.MixProject do {:bamboo, "~> 2.0"}, {:bamboo_smtp, "~> 4.0"}, {:scrivener_ecto, "~> 2.0"}, - {:tzdata, "~> 1.1"} + {:timex, "~> 3.6"} ] end diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 5d3cf543..0432d535 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -109,7 +109,7 @@ for user <- Accounts.list_users() do subject: e["subject"], description: e["description"], location: e["location"], - time_zone: Tzdata.zone_list() |> Enum.random(), + time_zone: Timex.timezones() |> Enum.random(), time_start: time_start, time_end: time_end, user_id: user.id,