From 946ca36abc6f98f27be2b92d0580a7e03695772a Mon Sep 17 00:00:00 2001 From: Max Veytsman Date: Mon, 17 Jun 2024 17:43:39 -0400 Subject: [PATCH] Add prompt if the rider is unassigning a delivery today (#375) --- .../live/campaign_signup_live/show.ex | 18 ++++++++++--- .../live/campaign_signup_live_test.exs | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/bike_brigade_web/live/campaign_signup_live/show.ex b/lib/bike_brigade_web/live/campaign_signup_live/show.ex index 7ebb9fc1..0e9df0fd 100644 --- a/lib/bike_brigade_web/live/campaign_signup_live/show.ex +++ b/lib/bike_brigade_web/live/campaign_signup_live/show.ex @@ -1,4 +1,5 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Show do + alias BikeBrigade.LocalizedDateTime use BikeBrigadeWeb, :live_view import BikeBrigadeWeb.CampaignHelpers @@ -228,7 +229,12 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Show do You <.button - :if={task_eligigle_for_unassign(@task, @campaign, @current_rider_id)} + :if={task_eligigle_for_unassign?(@task, @campaign, @current_rider_id)} + data-confirm={ + if campaign_today?(@campaign), + do: + "This delivery starts today. If you need to unassign yourself, please also text dispatch to let us know!" + } phx-click={JS.push("unassign_task", value: %{task_id: @task.id})} id={"#{@id}-unassign-task-#{@task.id}"} color={:red} @@ -239,7 +245,7 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Show do <% end %> - <%= if task_eligible_for_signup(@task, @campaign) do %> + <%= if task_eligible_for_signup?(@task, @campaign) do %> <.button phx-click={ JS.push("signup_rider", value: %{task_id: @task.id, rider_id: @current_rider_id}) @@ -268,16 +274,20 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Show do """ end - defp task_eligible_for_signup(task, campaign) do + defp task_eligible_for_signup?(task, campaign) do # campaign not in past, assigned rider not nil. task.assigned_rider == nil && !campaign_in_past(campaign) end # determine if a rider is eligible to "unassign" themselves - defp task_eligigle_for_unassign(task, campaign, current_rider_id) do + defp task_eligigle_for_unassign?(task, campaign, current_rider_id) do task.assigned_rider.id == current_rider_id && !campaign_in_past(campaign) end + defp campaign_today?(campaign) do + LocalizedDateTime.to_date(campaign.delivery_start) == LocalizedDateTime.today() + end + def initials(name) do name |> String.split(~r/[\s+|-]/, trim: true) diff --git a/test/bike_brigade_web/live/campaign_signup_live_test.exs b/test/bike_brigade_web/live/campaign_signup_live_test.exs index 9758abe1..8457474e 100644 --- a/test/bike_brigade_web/live/campaign_signup_live_test.exs +++ b/test/bike_brigade_web/live/campaign_signup_live_test.exs @@ -192,6 +192,25 @@ defmodule BikeBrigadeWeb.CampaignSignupLiveTest do assert live |> has_element?("#signup-btn-mobile-task-over-#{task.id}") end + test "Rider sees message about texting dispatch if unassigning from a campaign that's today", ctx do + + {:ok, live, html} = live(ctx.conn, ~p"/campaigns/signup/#{ctx.campaign.id}/") + assert html =~ "Sign up" + html = live |> element("#signup-btn-desktop-sign-up-task-#{ctx.task.id}") |> render_click() + assert html =~ "Unassign me" + assert html =~ "This delivery starts today. If you need to unassign yourself, please also text dispatch to let us know!" + + + campaign = make_campaign_in_future(ctx.program.id) + task = fixture(:task, %{campaign: campaign, rider: nil}) + + {:ok, live, html} = live(ctx.conn, ~p"/campaigns/signup/#{campaign.id}/") + + html = live |> element("#signup-btn-desktop-sign-up-task-#{task.id}") |> render_click() + assert html =~ "Unassign me" + refute html =~ "This delivery starts today. If you need to unassign yourself, please also text dispatch to let us know!" + end + test "we see pertinent task information", ctx do {:ok, live, html} = live(ctx.conn, ~p"/campaigns/signup/#{ctx.campaign.id}/") @@ -251,4 +270,12 @@ defmodule BikeBrigadeWeb.CampaignSignupLiveTest do delivery_end: LocalizedDateTime.now() |> DateTime.add(-7, :day) |> DateTime.add(60, :second) }) end + + defp make_campaign_in_future(program_id) do + fixture(:campaign, %{ + program_id: program_id, + delivery_start: LocalizedDateTime.now() |> DateTime.add(7, :day), + delivery_end: LocalizedDateTime.now() |> DateTime.add(7, :day) |> DateTime.add(60, :second) + }) + end end