Skip to content

Commit

Permalink
Refactor the caller
Browse files Browse the repository at this point in the history
  • Loading branch information
mveytsman authored and teesloane committed Jul 4, 2024
1 parent c6d721e commit 6fc8119
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/bike_brigade_web/live/campaign_signup_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,30 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Index do
LocalizedDateTime.today()
|> Date.beginning_of_week()

campaigns = fetch_campaigns(current_week)
campaign_filter = {:current_week, current_week}

campaigns = fetch_campaigns(campaign_filter)

{:ok,
socket
|> assign(:page, :campaigns_signup)
|> assign(:page_title, "Delivery Sign Up")
|> assign(:current_week, current_week)
|> assign(:campaign_filter, campaign_filter)
|> assign(:campaign_task_counts, Delivery.get_total_tasks_and_open_tasks(current_week))
|> assign(:showing_urgent_campaigns, false)
|> assign(:campaigns, campaigns)}
end

@impl true
def handle_params(%{"campaign_ids" => campaign_ids}, _url, socket) do
campaigns = fetch_campaigns(socket.assigns.current_week, campaign_ids: campaign_ids)
campaign_filter = {:campaign_ids, campaign_ids}
campaigns = fetch_campaigns(campaign_filter)

{:noreply,
socket
|> assign(:campaigns, campaigns)
|> assign(:campaign_filter, campaign_filter)
|> assign(:showing_urgent_campaigns, true)}
end

Expand Down Expand Up @@ -76,10 +81,12 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Index do
case params do
%{"current_week" => week} ->
week = Date.from_iso8601!(week)
campaign_filter = {:current_week, week}

assign(socket,
current_week: week,
campaigns: fetch_campaigns(week),
campaign_filter: campaign_filter,
campaigns: fetch_campaigns(campaign_filter),
campaign_task_counts: Delivery.get_total_tasks_and_open_tasks(week)
)

Expand All @@ -91,12 +98,22 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Index do
|> assign(:campaign, nil)
end

defp fetch_campaigns(current_week, opts \\ []) do
defp fetch_campaigns({:current_week, current_week}) do
Delivery.list_campaigns(
start_date: current_week,
end_date: Date.add(current_week, 6),
preload: [:program, :stats, :latest_message, :scheduled_message],
campaign_ids: opts[:campaign_ids],
public: true
)
|> Enum.reverse()
|> Utils.ordered_group_by(&LocalizedDateTime.to_date(&1.delivery_start))
|> Enum.reverse()
end

defp fetch_campaigns({:campaign_ids, campaign_ids}) do
Delivery.list_campaigns(
campaign_ids: campaign_ids,
preload: [:program, :stats, :latest_message, :scheduled_message],
public: true
)
|> Enum.reverse()
Expand All @@ -108,11 +125,12 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Index do
# changes (add, edit, delete), we refetch all tasks and campaign riders.
# This may eventually become a problem.
defp refetch_and_assign_data(socket) do
campaign_filter = socket.assigns.campaign_filter
week = socket.assigns.current_week

socket
|> assign(:campaign_task_counts, Delivery.get_total_tasks_and_open_tasks(week))
|> assign(:campaigns, fetch_campaigns(week))
|> assign(:campaigns, fetch_campaigns(campaign_filter))
end

# Use this to determine if we need to refetch data to update the liveview.
Expand Down

0 comments on commit 6fc8119

Please sign in to comment.