shift73k/lib/shift73k_web/live/live_helpers.ex

95 lines
2.6 KiB
Elixir
Raw Normal View History

2021-03-05 19:23:32 -05:00
defmodule Shift73kWeb.LiveHelpers do
2020-09-14 10:17:47 -04:00
import Phoenix.LiveView
import Phoenix.LiveView.Helpers
2021-03-05 19:23:32 -05:00
alias Shift73k.Accounts
alias Shift73k.Accounts.User
alias Shift73kWeb.UserAuth
2021-03-15 15:46:59 -04:00
alias Shift73k.Shifts.Templates.ShiftTemplate
2020-09-14 12:22:25 -04:00
@doc """
Performs the {:noreply, socket} for a given socket.
This helps make the noreply pipeable
"""
def live_noreply(socket), do: {:noreply, socket}
@doc """
Performs the {:ok, socket} for a given socket.
This helps make the ok reply pipeable
"""
def live_okreply(socket), do: {:ok, socket}
2020-09-14 12:22:25 -04:00
@doc """
2021-03-05 19:23:32 -05:00
Renders a component inside the `Shift73kWeb.ModalComponent` component.
2020-09-14 12:22:25 -04:00
The rendered modal receives a `:return_to` option to properly update
the URL when the modal is closed.
## Examples
2021-03-05 19:23:32 -05:00
<%= live_modal @socket, Shift73kWeb.PropertyLive.FormComponent,
2020-09-14 12:22:25 -04:00
id: @property.id || :new,
action: @live_action,
property: @property,
return_to: Routes.property_index_path(@socket, :index) %>
"""
def live_modal(socket, component, opts) do
modal_opts = [id: :modal, component: component, opts: opts]
2021-03-03 16:52:26 -05:00
# dirty little workaround for elixir complaining about socket being unused
_socket = socket
2021-03-05 19:23:32 -05:00
live_component(socket, Shift73kWeb.ModalComponent, modal_opts)
2020-09-14 12:22:25 -04:00
end
2020-09-14 10:17:47 -04:00
@doc """
Loads default assigns for liveviews
"""
def assign_defaults(socket, session) do
2021-03-05 19:23:32 -05:00
Shift73kWeb.Endpoint.subscribe(UserAuth.pubsub_topic())
assign_current_user(socket, session)
2020-09-14 10:17:47 -04:00
end
# For liveviews, ensures current_user is in socket assigns.
def assign_current_user(socket, session) do
2020-09-14 10:17:47 -04:00
with user_token when not is_nil(user_token) <- session["user_token"],
%User{} = user <- Accounts.get_user_by_session_token(user_token) do
assign(socket, :current_user, user)
else
_ -> socket
end
2020-09-14 10:17:47 -04:00
end
@doc """
Copies current flash into new put_flash invocations.
To be used before a push_patch.
"""
def copy_flash(%{assigns: %{flash: flash}} = socket) do
Enum.reduce(flash, socket, fn {k, v}, acc ->
put_flash(acc, String.to_existing_atom(k), v)
end)
end
2021-03-15 15:46:59 -04:00
def format_shift_time(time) do
time
|> Calendar.strftime("%-I:%M%P")
|> String.trim_trailing("m")
end
2021-03-15 15:46:59 -04:00
def format_shift_length(%ShiftTemplate{} = shift_template) do
2021-03-15 15:46:59 -04:00
shift_template
|> ShiftTemplate.shift_length()
|> format_shift_length()
end
def format_shift_length(minutes) when is_integer(minutes) do
h = Integer.floor_div(minutes, 60)
m = rem(minutes, 60)
cond do
h > 0 && m > 0 -> "#{h}h #{m}m"
h > 0 -> "#{h}h"
m > 0 -> "#{m}m"
true -> "0m"
end
2021-03-15 15:46:59 -04:00
end
2020-09-14 10:17:47 -04:00
end