2021-03-11 17:16:11 -05:00
|
|
|
defmodule Shift73kWeb.ShiftLive.Index do
|
|
|
|
use Shift73kWeb, :live_view
|
|
|
|
|
|
|
|
alias Shift73k.Shifts
|
|
|
|
alias Shift73k.Shifts.Shift
|
2021-03-20 12:45:50 -04:00
|
|
|
alias Shift73kWeb.Roles
|
2021-03-11 17:16:11 -05:00
|
|
|
|
|
|
|
@impl true
|
2021-03-20 12:45:50 -04:00
|
|
|
def mount(_params, session, socket) do
|
|
|
|
socket
|
|
|
|
|> assign_defaults(session)
|
|
|
|
|> live_okreply()
|
2021-03-11 17:16:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_params(params, _url, socket) do
|
2021-03-20 12:45:50 -04:00
|
|
|
current_user = socket.assigns.current_user
|
|
|
|
live_action = socket.assigns.live_action
|
|
|
|
shift = shift_from_params(params)
|
|
|
|
|
|
|
|
if Roles.can?(current_user, shift, live_action) do
|
|
|
|
socket
|
2021-03-21 10:47:53 -04:00
|
|
|
|> init_today(Date.utc_today())
|
|
|
|
|> update_agenda()
|
2021-03-20 12:45:50 -04:00
|
|
|
|> assign(:delete_shift, nil)
|
|
|
|
|> apply_action(socket.assigns.live_action, params)
|
|
|
|
|> live_noreply()
|
|
|
|
else
|
|
|
|
socket
|
|
|
|
|> put_flash(:error, "Unauthorised")
|
|
|
|
|> redirect(to: "/")
|
|
|
|
|> live_noreply()
|
|
|
|
end
|
2021-03-11 17:16:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :index, _params) do
|
|
|
|
socket
|
2021-03-25 20:31:32 -04:00
|
|
|
|> assign(:page_title, "My Shifts")
|
2021-03-11 17:16:11 -05:00
|
|
|
|> assign(:shift, nil)
|
|
|
|
end
|
|
|
|
|
2021-03-20 12:45:50 -04:00
|
|
|
defp shift_from_params(params)
|
|
|
|
|
|
|
|
defp shift_from_params(%{"id" => id}),
|
|
|
|
do: Shifts.get_shift!(id)
|
|
|
|
|
|
|
|
defp shift_from_params(_params), do: %Shift{}
|
|
|
|
|
|
|
|
defp init_today(socket, today) do
|
2021-03-21 10:47:53 -04:00
|
|
|
assign(socket, current_date: today, cursor_date: cursor_date(today))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp cursor_date(%Date{} = date) do
|
|
|
|
date |> Date.beginning_of_month() |> Date.add(4)
|
2021-03-20 12:45:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defp assign_date_range(%{assigns: %{cursor_date: cursor_date}} = socket) do
|
|
|
|
assign(socket, date_range: date_range(cursor_date))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp date_range(cursor_date) do
|
|
|
|
cursor_date
|
2021-03-21 10:47:53 -04:00
|
|
|
|> Date.beginning_of_month()
|
|
|
|
|> Date.range(Date.end_of_month(cursor_date))
|
2021-03-20 12:45:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defp assign_known_shifts(socket) do
|
|
|
|
user = socket.assigns.current_user
|
|
|
|
shifts = Shifts.list_shifts_by_user_in_date_range(user.id, socket.assigns.date_range)
|
|
|
|
assign(socket, :shifts, shifts)
|
|
|
|
end
|
|
|
|
|
2021-03-21 10:47:53 -04:00
|
|
|
defp update_agenda(socket) do
|
|
|
|
socket
|
|
|
|
|> assign_date_range()
|
|
|
|
|> assign_known_shifts()
|
|
|
|
end
|
|
|
|
|
2021-03-11 17:16:11 -05:00
|
|
|
@impl true
|
|
|
|
def handle_event("delete", %{"id" => id}, socket) do
|
|
|
|
shift = Shifts.get_shift!(id)
|
|
|
|
{:ok, _} = Shifts.delete_shift(shift)
|
|
|
|
|
2021-03-20 12:45:50 -04:00
|
|
|
{:noreply, assign_known_shifts(socket)}
|
2021-03-11 17:16:11 -05:00
|
|
|
end
|
|
|
|
|
2021-03-20 12:45:50 -04:00
|
|
|
@impl true
|
2021-03-21 10:47:53 -04:00
|
|
|
def handle_event("month-nav", %{"month" => nav}, socket) do
|
2021-03-20 12:45:50 -04:00
|
|
|
new_cursor =
|
|
|
|
cond do
|
2021-03-21 10:47:53 -04:00
|
|
|
nav == "now" ->
|
|
|
|
Date.utc_today()
|
2021-03-20 12:45:50 -04:00
|
|
|
|
|
|
|
true ->
|
2021-03-21 10:47:53 -04:00
|
|
|
socket.assigns.cursor_date
|
|
|
|
|> Date.add((nav == "prev" && -30) || 30)
|
|
|
|
|> cursor_date()
|
2021-03-20 12:45:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
socket
|
|
|
|
|> assign(:cursor_date, new_cursor)
|
2021-03-21 10:47:53 -04:00
|
|
|
|> update_agenda()
|
2021-03-20 12:45:50 -04:00
|
|
|
|> live_noreply()
|
2021-03-11 17:16:11 -05:00
|
|
|
end
|
|
|
|
end
|