2021-03-06 13:48:13 -05:00
|
|
|
defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do
|
|
|
|
use Shift73kWeb, :live_component
|
|
|
|
|
2021-03-11 16:57:20 -05:00
|
|
|
alias Shift73k.Shifts.Templates
|
|
|
|
alias Shift73k.Shifts.Templates.ShiftTemplate
|
2021-03-06 13:48:13 -05:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def update(%{shift_template: shift_template} = assigns, socket) do
|
2021-03-11 16:57:20 -05:00
|
|
|
changeset = Templates.change_shift_template(shift_template)
|
2021-03-06 13:48:13 -05:00
|
|
|
|
2021-03-11 13:30:30 -05:00
|
|
|
socket
|
|
|
|
|> assign(assigns)
|
|
|
|
|> assign(:changeset, changeset)
|
2021-03-12 09:41:41 -05:00
|
|
|
|> assign_shift_length(shift_template)
|
2021-03-11 13:30:30 -05:00
|
|
|
|> live_okreply()
|
|
|
|
end
|
|
|
|
|
2021-03-12 09:41:41 -05:00
|
|
|
defp assign_shift_length(socket, shift_template) do
|
2021-03-12 12:47:06 -05:00
|
|
|
assign(socket, :shift_length, format_shift_length(shift_template))
|
2021-03-11 13:30:30 -05:00
|
|
|
end
|
|
|
|
|
2021-03-12 09:41:41 -05:00
|
|
|
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"))
|
2021-03-11 13:30:30 -05:00
|
|
|
|> Map.put("user_id", current_user.id)
|
2021-03-06 13:48:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2021-03-12 09:41:41 -05:00
|
|
|
def handle_event("validate", %{"shift_template" => params}, socket) do
|
|
|
|
params = prep_template_params(params, socket.assigns.current_user)
|
2021-03-11 13:30:30 -05:00
|
|
|
|
2021-03-06 13:48:13 -05:00
|
|
|
changeset =
|
|
|
|
socket.assigns.shift_template
|
2021-03-12 09:41:41 -05:00
|
|
|
|> Templates.change_shift_template(params)
|
2021-03-06 13:48:13 -05:00
|
|
|
|> Map.put(:action, :validate)
|
|
|
|
|
2021-03-11 13:30:30 -05:00
|
|
|
socket
|
|
|
|
|> assign(:changeset, changeset)
|
2021-03-12 09:41:41 -05:00
|
|
|
|> assign_shift_length(%ShiftTemplate{
|
|
|
|
time_end: params["time_end"],
|
|
|
|
time_start: params["time_start"]
|
|
|
|
})
|
2021-03-11 13:30:30 -05:00
|
|
|
|> live_noreply()
|
2021-03-06 13:48:13 -05:00
|
|
|
end
|
|
|
|
|
2021-03-12 09:41:41 -05:00
|
|
|
def handle_event("save", %{"shift_template" => params}, socket) do
|
2021-03-11 13:30:30 -05:00
|
|
|
save_shift_template(
|
|
|
|
socket,
|
|
|
|
socket.assigns.action,
|
2021-03-12 09:41:41 -05:00
|
|
|
prep_template_params(params, socket.assigns.current_user)
|
2021-03-11 13:30:30 -05:00
|
|
|
)
|
2021-03-06 13:48:13 -05:00
|
|
|
end
|
|
|
|
|
2021-03-11 13:30:30 -05:00
|
|
|
@impl true
|
|
|
|
def handle_event("cancel", _, socket) do
|
|
|
|
{:noreply, push_event(socket, "modal-please-hide", %{})}
|
|
|
|
end
|
|
|
|
|
2021-03-12 09:41:41 -05:00
|
|
|
defp save_shift_template(socket, :new, params) do
|
|
|
|
case Templates.create_shift_template(params) do
|
2021-03-06 13:48:13 -05:00
|
|
|
{:ok, _shift_template} ->
|
2021-03-11 13:30:30 -05:00
|
|
|
flash = {:info, "Shift template created successfully"}
|
|
|
|
send(self(), {:put_flash_message, flash})
|
|
|
|
|
|
|
|
socket
|
|
|
|
|> push_event("modal-please-hide", %{})
|
|
|
|
|> live_noreply()
|
2021-03-06 13:48:13 -05:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
2021-03-11 13:30:30 -05:00
|
|
|
{:noreply, assign(socket, changeset: changeset)}
|
2021-03-06 13:48:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-12 09:41:41 -05:00
|
|
|
defp save_shift_template(socket, :clone, params) do
|
|
|
|
save_shift_template(socket, :new, params)
|
2021-03-11 14:05:22 -05:00
|
|
|
end
|
|
|
|
|
2021-03-12 09:41:41 -05:00
|
|
|
defp save_shift_template(socket, :edit, params) do
|
|
|
|
case Templates.update_shift_template(socket.assigns.shift_template, params) do
|
2021-03-06 13:48:13 -05:00
|
|
|
{:ok, _shift_template} ->
|
2021-03-11 13:30:30 -05:00
|
|
|
flash = {:info, "Shift template updated successfully"}
|
|
|
|
send(self(), {:put_flash_message, flash})
|
|
|
|
|
|
|
|
socket
|
|
|
|
|> push_event("modal-please-hide", %{})
|
|
|
|
|> live_noreply()
|
2021-03-06 13:48:13 -05:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
2021-03-11 13:30:30 -05:00
|
|
|
{:noreply, assign(socket, :changeset, changeset)}
|
2021-03-06 13:48:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|