Skip to content

Commit

Permalink
Merge branch 'main' into mv/login_cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
mveytsman authored Jun 19, 2024
2 parents 66dee79 + b9fdf62 commit bf6f084
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![CI](https://github.com/mveytsman/bike-brigade/workflows/CI/badge.svg)](https://github.com/mveytsman/bike-brigade/actions?query=workflow%3ACI)
# BikeBrigade

**[🎬 Project Board](https://github.com/orgs/bikebrigade/projects/4/views/1)**
**[🎬 Project Board](https://github.com/orgs/bikebrigade/projects/14/)**

## Prerequisities
1. [nix](https://nixos.org/download.html) with [flakes enabled](https://nixos.wiki/wiki/Flakes)
Expand Down
36 changes: 24 additions & 12 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 @@ -74,13 +75,13 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Show do
|> redirect(to: ~p"/campaigns/signup/#{socket.assigns.campaign}")
end

defp split_first_name(full_name) do
case String.split(full_name, " ") do
defp first_name_and_last_initial(full_name) do
case String.split(full_name, " ", parts: 2) do
[first_name, last_name] when is_binary(first_name) and is_binary(last_name) ->
first_name
"#{first_name} #{String.first(last_name)}"

_ ->
full_name
[first_name] ->
first_name
end
end

Expand Down Expand Up @@ -189,7 +190,10 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Show do
<div class="flex items-center">
<details>
<summary class="cursor-pointer" title={task_item.item.description}>
<span :if={task_item.count > 1} class="mr-1"> <%= task_item.count %></span><%= Inflex.inflect(task_item.item.name, task_item.count) %>
<span :if={task_item.count > 1} class="mr-1"><%= task_item.count %></span><%= Inflex.inflect(
task_item.item.name,
task_item.count
) %>
</summary>
<%= task_item.item.description %>
</details>
Expand Down Expand Up @@ -218,15 +222,19 @@ defmodule BikeBrigadeWeb.CampaignSignupLive.Show do
<div class="flex flex-col space-y-2 md:items-center md:space-y-0 md:flex-row md:space-x-2">
<%= if @task.assigned_rider do %>
<div :if={@task.assigned_rider.id != @current_rider_id}>
<%= split_first_name(@task.assigned_rider.name) %>
<%= first_name_and_last_initial(@task.assigned_rider.name) %>
</div>
<div :if={@task.assigned_rider.id == @current_rider_id}>
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 @@ -237,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 @@ -266,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 bf6f084

Please sign in to comment.