Skip to content

Commit

Permalink
Add prompt if the rider is unassigning a delivery today (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
mveytsman authored Jun 17, 2024
1 parent e0f91ef commit 946ca36
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/bike_brigade_web/live/campaign_signup_live/show.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule BikeBrigadeWeb.CampaignSignupLive.Show do
alias BikeBrigade.LocalizedDateTime
use BikeBrigadeWeb, :live_view

import BikeBrigadeWeb.CampaignHelpers
Expand Down Expand Up @@ -228,7 +229,12 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Show do
You
</div>
<.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}
Expand All @@ -239,7 +245,7 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Show do
</.button>
<% 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})
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions test/bike_brigade_web/live/campaign_signup_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}/")

Check warning on line 207 in test/bike_brigade_web/live/campaign_signup_live_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

variable "html" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)

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}/")

Expand Down Expand Up @@ -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

0 comments on commit 946ca36

Please sign in to comment.