diff --git a/assets/js/app.js b/assets/js/app.js index 62e62df3..6d274010 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -36,6 +36,7 @@ import "../node_modules/bootstrap-icons/icons/people.svg"; // users management // calendar/event icons import "../node_modules/bootstrap-icons/icons/calendar2.svg"; import "../node_modules/bootstrap-icons/icons/calendar2-plus.svg"; +import "../node_modules/bootstrap-icons/icons/calendar2-date.svg"; import "../node_modules/bootstrap-icons/icons/calendar2-event.svg"; import "../node_modules/bootstrap-icons/icons/calendar2-range.svg"; import "../node_modules/bootstrap-icons/icons/clock-history.svg"; // shift template @@ -53,6 +54,7 @@ import "../node_modules/bootstrap-icons/icons/binoculars-fill.svg"; import "../node_modules/bootstrap-icons/icons/eraser.svg"; import "../node_modules/bootstrap-icons/icons/save.svg"; import "../node_modules/bootstrap-icons/icons/asterisk.svg"; +import "../node_modules/bootstrap-icons/icons/card-list.svg"; // webpack automatically bundles all modules in your // entry points. Those entry points can be configured diff --git a/lib/shift73k/ecto_enums.ex b/lib/shift73k/ecto_enums.ex index e00ed388..18e08674 100644 --- a/lib/shift73k/ecto_enums.ex +++ b/lib/shift73k/ecto_enums.ex @@ -1,7 +1,8 @@ defmodule Shift73k.EctoEnums do import EctoEnum - @weekdays [:mon, :tue, :wed, :thu, :fri, :sat, :sun] |> Enum.with_index(1) + @weekdays [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday] + |> Enum.with_index(1) defenum(WeekdayEnum, @weekdays) end diff --git a/lib/shift73k/shifts.ex b/lib/shift73k/shifts.ex index 8f2aefaa..dcb55b5c 100644 --- a/lib/shift73k/shifts.ex +++ b/lib/shift73k/shifts.ex @@ -21,26 +21,26 @@ defmodule Shift73k.Shifts do Repo.all(Shift) end - def list_shifts_by_user_between_dates(user_id, start_date, end_date) do + defp query_shifts_by_user(user_id) do from(s in Shift) - |> select([s], %{ - date: s.date, - subject: s.subject, - time_start: s.time_start, - time_end: s.time_end - }) - |> where([s], s.user_id == ^user_id and s.date >= ^start_date and s.date <= ^end_date) + |> where([s], s.user_id == ^user_id) + end + + def list_shifts_by_user_in_date_range(user_id, %Date.Range{} = date_range) do + query_shifts_by_user(user_id) + |> where([s], s.date >= ^date_range.first) + |> where([s], s.date <= ^date_range.last) |> order_by([s], [s.date, s.time_start]) |> Repo.all() end - defp query_shifts_by_user_on_list_of_dates(user_id, date_list) do - from(s in Shift) - |> where([s], s.user_id == ^user_id and s.date in ^date_list) + defp query_shifts_by_user_from_list_of_dates(user_id, date_list) do + query_shifts_by_user(user_id) + |> where([s], s.date in ^date_list) end - def list_shifts_by_user_on_list_of_dates(user_id, date_list) do - query_shifts_by_user_on_list_of_dates(user_id, date_list) + def list_shifts_by_user_from_list_of_dates(user_id, date_list) do + query_shifts_by_user_from_list_of_dates(user_id, date_list) |> Repo.all() end @@ -112,8 +112,8 @@ defmodule Shift73k.Shifts do Repo.delete(shift) end - def delete_shifts_by_user_on_list_of_dates(user_id, date_list) do - query_shifts_by_user_on_list_of_dates(user_id, date_list) + def delete_shifts_by_user_from_list_of_dates(user_id, date_list) do + query_shifts_by_user_from_list_of_dates(user_id, date_list) |> Repo.delete_all() end diff --git a/lib/shift73k_web/live/shift_assign_live/delete_component.ex b/lib/shift73k_web/live/shift_assign_live/delete_component.ex index 8dc279cb..69f40f97 100644 --- a/lib/shift73k_web/live/shift_assign_live/delete_component.ex +++ b/lib/shift73k_web/live/shift_assign_live/delete_component.ex @@ -29,7 +29,7 @@ defmodule Shift73kWeb.ShiftAssignLive.DeleteComponent do def handle_event("confirm-delete-days-shifts", _params, socket) do user = socket.assigns.current_user date_list = socket.assigns.date_list - {n, _} = Shifts.delete_shifts_by_user_on_list_of_dates(user.id, date_list) + {n, _} = Shifts.delete_shifts_by_user_from_list_of_dates(user.id, date_list) s = (n > 1 && "s") || "" flash = {:info, "Successfully deleted #{n} assigned shift#{s}"} send(self(), {:put_flash_message, flash}) diff --git a/lib/shift73k_web/live/shift_assign_live/index.ex b/lib/shift73k_web/live/shift_assign_live/index.ex index 91640d52..38c2db9c 100644 --- a/lib/shift73k_web/live/shift_assign_live/index.ex +++ b/lib/shift73k_web/live/shift_assign_live/index.ex @@ -27,6 +27,8 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do @impl true def handle_params(_params, _url, socket) do + user = socket.assigns.current_user + socket |> init_shift_templates() |> init_shift_template() @@ -34,9 +36,9 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do |> assign_shift_length() |> assign_shift_template_changeset() |> assign_modal_close_handlers() + |> assign(:day_names, day_names(user.week_start_at)) |> init_today(Timex.today()) - |> init_calendar() - |> assign_known_shifts() + |> update_calendar() |> live_noreply() end @@ -52,20 +54,12 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do assign(socket, :shift_length, format_shift_length(shift)) end - defp assign_known_shifts(socket) do - user = socket.assigns.current_user - first = socket.assigns.day_first - last = socket.assigns.day_last - known_shifts = Shifts.list_shifts_by_user_between_dates(user.id, first, last) + defp assign_known_shifts(%{assigns: %{current_user: user}} = socket) do + date_range = socket.assigns.date_range + known_shifts = Shifts.list_shifts_by_user_in_date_range(user.id, date_range) assign(socket, :known_shifts, known_shifts) end - defp init_calendar(%{assigns: %{current_user: user}} = socket) do - days = day_names(user.week_start_at) - {first, last, rows} = week_rows(socket.assigns.cursor_date, user.week_start_at) - assign(socket, day_names: days, week_rows: rows, day_first: first, day_last: last) - end - defp init_today(socket, today) do assign(socket, current_date: today, cursor_date: today) end @@ -88,7 +82,7 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do defp init_shift_templates(%{assigns: %{current_user: user}} = socket) do shift_templates = Templates.list_shift_templates_by_user_id(user.id) - |> Enum.map(fn t -> shift_template_option(t, user.fave_shift_template_id) end) + |> Stream.map(fn t -> shift_template_option(t, user.fave_shift_template_id) end) |> Enum.concat([@custom_shift_opt]) assign(socket, :shift_templates, shift_templates) @@ -123,23 +117,31 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do |> Enum.map(&Timex.day_shortname/1) end - defp week_rows(cursor_date, week_start_at) do - first = - cursor_date - |> Timex.beginning_of_month() - |> Timex.beginning_of_week(week_start_at) - + defp date_range(cursor_date, week_start_at) do last = cursor_date |> Timex.end_of_month() |> Timex.end_of_week(week_start_at) - week_rows = - Interval.new(from: first, until: last, right_open: false) - |> Enum.map(&NaiveDateTime.to_date(&1)) - |> Enum.chunk_every(7) + cursor_date + |> Timex.beginning_of_month() + |> Timex.beginning_of_week(week_start_at) + |> Date.range(last) + end - {first, last, week_rows} + defp assign_date_range(%{assigns: %{current_user: user}} = socket) do + date_range = date_range(socket.assigns.cursor_date, user.week_start_at) + assign(socket, :date_range, date_range) + end + + defp week_rows(%Date.Range{} = date_range) do + Interval.new(from: date_range.first, until: date_range.last, right_open: false) + |> Stream.map(&NaiveDateTime.to_date(&1)) + |> Enum.chunk_every(7) + end + + defp assign_week_rows(%{assigns: %{date_range: date_range}} = socket) do + assign(socket, :week_rows, week_rows(date_range)) end def day_color(day, current_date, cursor_date, selected_days) do @@ -180,17 +182,11 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do end end - defp set_month(socket, new_cursor_date) do - {first, last, rows} = week_rows(new_cursor_date, socket.assigns.current_user.week_start_at) - - assigns = [ - cursor_date: new_cursor_date, - week_rows: rows, - day_first: first, - day_last: last - ] - - assign(socket, assigns) |> assign_known_shifts() + defp update_calendar(socket) do + socket + |> assign_date_range() + |> assign_week_rows() + |> assign_known_shifts() end @impl true @@ -236,7 +232,10 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do Timex.shift(socket.assigns.cursor_date, months: months) end - {:noreply, set_month(socket, new_cursor)} + socket + |> assign(:cursor_date, new_cursor) + |> update_calendar() + |> live_noreply() end @impl true @@ -260,9 +259,9 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do @impl true def handle_event("select-day", %{"day" => day}, socket) do selected_days = - case day_index = Enum.find_index(socket.assigns.selected_days, fn d -> d == day end) do - nil -> [day | socket.assigns.selected_days] - _ -> List.delete_at(socket.assigns.selected_days, day_index) + case Enum.member?(socket.assigns.selected_days, day) do + false -> [day | socket.assigns.selected_days] + true -> Enum.reject(socket.assigns.selected_days, fn d -> d == day end) end {:noreply, assign(socket, :selected_days, selected_days)} diff --git a/lib/shift73k_web/live/shift_assign_live/index.html.leex b/lib/shift73k_web/live/shift_assign_live/index.html.leex index a8160bfa..81b7e550 100644 --- a/lib/shift73k_web/live/shift_assign_live/index.html.leex +++ b/lib/shift73k_web/live/shift_assign_live/index.html.leex @@ -10,7 +10,7 @@
Subject | -Location | -Description | -Time zone | -Date | -Time zone | -Time start | -Time end | -- |
---|---|---|---|---|---|---|---|---|
<%= shift.subject %> | -<%= shift.location %> | -<%= shift.description %> | -<%= shift.time_zone %> | -<%= shift.date %> | -<%= shift.time_zone %> | -<%= shift.time_start %> | -<%= shift.time_end %> | +- <%= live_redirect "Show", to: Routes.shift_show_path(@socket, :show, shift) %> - <%= live_patch "Edit", to: Routes.shift_index_path(@socket, :edit, shift) %> - <%= link "Delete", to: "#", phx_click: "delete", phx_value_id: shift.id, data: [confirm: "Are you sure?"] %> - | -