Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/bike_brigade_web/helpers/campaign_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
defmodule BikeBrigadeWeb.CampaignHelpers do
alias BikeBrigade.Delivery.Task
alias BikeBrigade.Riders.Rider
alias BikeBrigade.Delivery
alias BikeBrigade.Utils

use BikeBrigadeWeb, :live_component

alias BikeBrigade.LocalizedDateTime

Expand Down Expand Up @@ -128,4 +132,50 @@ defmodule BikeBrigadeWeb.CampaignHelpers do
defp print_item(task_item) do
"#{task_item.count} #{Inflex.inflect(task_item.item.name, task_item.count)}"
end

attr :filled_tasks, :integer, required: true
attr :total_tasks, :integer, required: true
attr :campaign, :any, required: true

@doc """
Renders possible states of campaign "Fullness" in styled text.
"""
def tasks_filled_text(assigns) do
{class, copy} =
cond do
assigns.filled_tasks == nil ->
{"text-gray-600", "N/A"}

campaign_in_past(assigns.campaign) ->
{"text-gray-600", "Campaign over"}

assigns.total_tasks - assigns.filled_tasks == 0 ->
{"text-gray-600", "Fully Assigned"}

true ->
{"text-red-400", "#{assigns.total_tasks - assigns.filled_tasks} Available"}
end

assigns =
assigns
|> assign(:class, class)
|> assign(:copy, copy)

~H"""
<p class="flex flex-col items-center mt-0 text-sm text-gray-700 md:flex-row">
<Icons.maki_bicycle_share class="flex-shrink-0 mb-2 mr-1.5 h-8 w-8 md:h-5 md:w-5 md:mb-0 text-gray-500" />
<span class="flex space-x-2 font-bold md:font-normal">
<span class={@class}><%= @copy %></span>
</span>
</p>
"""
end

# Use this to determine if we need to refetch data to update the liveview.
# ex: dispatcher changes riders/tasks, or another rider signs up -> refetch.
def entity_in_campaigns?(campaigns, entity_campaign_id) do
campaigns
|> Enum.flat_map(fn {_date, campaigns} -> campaigns end)
|> Enum.any?(fn c -> c.id == entity_campaign_id end)
end
end
37 changes: 36 additions & 1 deletion lib/bike_brigade_web/live/campaign_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ defmodule BikeBrigadeWeb.CampaignLive.Index do
alias BikeBrigade.Delivery
alias BikeBrigade.Delivery.Campaign
alias BikeBrigade.Messaging.SmsMessage
alias BikeBrigadeWeb.Components.CampaignComponents

import BikeBrigadeWeb.CampaignHelpers

@impl true
def mount(_params, _session, socket) do
if connected?(socket) do
Delivery.subscribe()
end

current_week =
LocalizedDateTime.today()
|> Date.beginning_of_week()
Expand All @@ -21,7 +26,8 @@ defmodule BikeBrigadeWeb.CampaignLive.Index do
|> assign(:page, :campaigns)
|> assign(:page_title, "Campaigns")
|> assign(:current_week, current_week)
|> assign(:campaigns, fetch_campaigns(current_week))}
|> assign(:campaigns, fetch_campaigns(current_week))
|> assign(:campaign_task_counts, Delivery.get_total_tasks_and_open_tasks(current_week))}
end

@impl true
Expand All @@ -37,6 +43,27 @@ defmodule BikeBrigadeWeb.CampaignLive.Index do
{:noreply, assign(socket, :campaigns, fetch_campaigns(socket.assigns.current_week))}
end

@broadcasted_infos [
:task_created,
:task_deleted,
:task_updated,
:campaign_rider_created,
:campaign_rider_deleted
]

@impl Phoenix.LiveView
def handle_info({event, entity}, socket) when event in @broadcasted_infos do
if entity_in_campaigns?(socket.assigns.campaigns, entity.campaign_id) do
{:noreply, refetch_and_assign_data(socket)}
else
{:noreply, socket}
end
end

Comment on lines +46 to +62
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this!

two feedbacks:

  1. I think this should go after the handle_events
  2. It needs a
@impl Phoenix.LiveView
  @doc "silently ignore other kinds of messages"
  def handle_info(_, socket), do: {:noreply, socket}

after it to silently ignore messages we don't care about (or else the LV crashes :()

@impl Phoenix.LiveView
@doc "silently ignore other kinds of messages"
def handle_info(_, socket), do: {:noreply, socket}

defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Campaign")
Expand Down Expand Up @@ -123,4 +150,12 @@ defmodule BikeBrigadeWeb.CampaignLive.Index do
def message_info(assigns) do
~H""
end

defp refetch_and_assign_data(socket) do
week = socket.assigns.current_week

socket
|> assign(:campaign_task_counts, Delivery.get_total_tasks_and_open_tasks(week))
|> assign(:campaigns, fetch_campaigns(week))
end
end
9 changes: 7 additions & 2 deletions lib/bike_brigade_web/live/campaign_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,15 @@
/>
<%= pickup_window(c) %>
</p>

<p class="flex items-center mt-2 text-sm text-gray-700 sm:mt-0 sm:ml-6">
<Icons.maki_bicycle_share class="flex-shrink-0 mr-1.5 h-5 w-5 text-gray-500" />
<%= c.stats.signed_up_rider_count %>
<.tasks_filled_text
filled_tasks={@campaign_task_counts[c.id][:filled_tasks]}
total_tasks={@campaign_task_counts[c.id][:total_tasks]}
campaign={c}
/>
</p>

<p class="flex items-center mt-2 text-sm text-gray-700 sm:mt-0 sm:ml-6">
<Heroicons.shopping_bag
mini
Expand Down
45 changes: 1 addition & 44 deletions lib/bike_brigade_web/live/campaign_signup_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Index do

@impl Phoenix.LiveView
def handle_info({event, entity}, socket) when event in @broadcasted_infos do
if entity_in_campaigns?(socket, entity.campaign_id) do
if entity_in_campaigns?(socket.assigns.campaigns, entity.campaign_id) do
{:noreply, refetch_and_assign_data(socket)}
else
{:noreply, socket}
Expand Down Expand Up @@ -145,49 +145,6 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Index do
|> assign(:campaigns, fetch_campaigns(campaign_filter))
end

# Use this to determine if we need to refetch data to update the liveview.
# ex: dispatcher changes riders/tasks, or another rider signs up -> refetch.
defp entity_in_campaigns?(socket, entity_campaign_id) do
socket.assigns.campaigns
|> Enum.flat_map(fn {_date, campaigns} -> campaigns end)
|> Enum.any?(fn c -> c.id == entity_campaign_id end)
end

attr :filled_tasks, :integer, required: true
attr :total_tasks, :integer, required: true
attr :campaign, :any, required: true

defp tasks_filled_text(assigns) do
{class, copy} =
cond do
assigns.filled_tasks == nil ->
{"text-gray-600", "N/A"}

campaign_in_past(assigns.campaign) ->
{"text-gray-600", "Campaign over"}

assigns.total_tasks - assigns.filled_tasks == 0 ->
{"text-gray-600", "Fully Assigned"}

true ->
{"text-red-400", "#{assigns.total_tasks - assigns.filled_tasks} Available"}
end

assigns =
assigns
|> assign(:class, class)
|> assign(:copy, copy)

~H"""
<p class="flex flex-col items-center mt-0 text-sm text-gray-700 md:flex-row">
<Icons.maki_bicycle_share class="flex-shrink-0 mb-2 mr-1.5 h-8 w-8 md:h-5 md:w-5 md:mb-0 text-gray-500" />
<span class="flex space-x-2 font-bold md:font-normal">
<span class={@class}><%= @copy %></span>
</span>
</p>
"""
end

attr :campaign, :any, required: true
attr :rider_id, :integer, required: true
attr :campaign_task_counts, :any, required: true
Expand Down