shift73k/lib/shift73k_web/live/shift_template_live/index.ex

111 lines
3.2 KiB
Elixir
Raw Normal View History

defmodule Shift73kWeb.ShiftTemplateLive.Index do
use Shift73kWeb, :live_view
use Timex
alias Shift73k.Accounts
2021-03-11 16:57:20 -05:00
alias Shift73k.Shifts.Templates
alias Shift73k.Shifts.Templates.ShiftTemplate
alias Shift73kWeb.Roles
@impl true
def mount(_params, session, socket) do
socket
|> assign_defaults(session)
|> live_okreply()
end
@impl true
def handle_params(params, _url, socket) do
current_user = socket.assigns.current_user
live_action = socket.assigns.live_action
shift_template = shift_template_from_params(params)
if Roles.can?(current_user, shift_template, live_action) do
socket
|> assign_shift_templates()
|> assign(:modal_return_to, Routes.shift_template_index_path(socket, :index))
|> assign(:delete_shift_template, nil)
|> apply_action(socket.assigns.live_action, params)
|> live_noreply()
else
socket
|> put_flash(:error, "Unauthorised")
|> redirect(to: "/")
|> live_noreply()
end
end
2021-03-11 14:05:22 -05:00
defp apply_action(socket, :clone, %{"id" => id}) do
socket
|> assign(:page_title, "Clone Shift Template")
2021-03-11 16:57:20 -05:00
|> assign(:shift_template, Templates.get_shift_template!(id))
2021-03-11 14:05:22 -05:00
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
2021-03-10 09:27:24 -05:00
|> assign(:page_title, "Edit Shift Template")
2021-03-11 16:57:20 -05:00
|> assign(:shift_template, Templates.get_shift_template!(id))
end
defp apply_action(socket, :new, _params) do
socket
2021-03-10 09:27:24 -05:00
|> assign(:page_title, "New Shift Template")
|> assign(:shift_template, %ShiftTemplate{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Shift templates")
|> assign(:shift_template, nil)
end
defp assign_shift_templates(socket) do
%User{id: uid} = socket.assigns.current_user
2021-03-11 16:57:20 -05:00
user_shifts = Templates.list_shift_templates_by_user_id(uid)
assign(socket, :shift_templates, user_shifts)
end
defp shift_template_from_params(params)
defp shift_template_from_params(%{"id" => id}),
2021-03-11 16:57:20 -05:00
do: Templates.get_shift_template!(id)
defp shift_template_from_params(_params), do: %ShiftTemplate{}
@impl true
def handle_event("delete-modal", %{"id" => id}, socket) do
2021-03-11 16:57:20 -05:00
{:noreply, assign(socket, :delete_shift_template, Templates.get_shift_template!(id))}
end
def handle_event("set-user-fave-shift-template", %{"id" => shift_template_id}, socket) do
user_id = socket.assigns.current_user.id
Accounts.set_user_fave_shift_template(user_id, shift_template_id)
socket
|> assign(:current_user, Accounts.get_user!(user_id))
|> assign_shift_templates()
|> live_noreply()
end
def handle_event("unset-user-fave-shift-template", _params, socket) do
user_id = socket.assigns.current_user.id
Accounts.unset_user_fave_shift_template(user_id)
socket
|> assign(:current_user, Accounts.get_user!(user_id))
|> assign_shift_templates()
|> live_noreply()
end
@impl true
def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do
socket |> copy_flash() |> push_patch(to: to) |> live_noreply()
end
@impl true
def handle_info({:put_flash_message, {flash_type, msg}}, socket) do
socket |> put_flash(flash_type, msg) |> live_noreply()
end
end