Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prompt if the rider is unassigning a delivery today #375

Merged
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
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 @@ -224,7 +225,12 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Show do
</span>

<.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 @@ -235,7 +241,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 @@ -264,16 +270,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 @@
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 @@
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
Loading