forked from seanpdoyle/select-your-own-seat
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Seat Selection to Shopping Cart
For comparison [see this commit][commit] Mostly similar to the above. A few things to call out is the introduction of a `ButtonTo` component and the usage of `form_props`. 1. [form_props] is a forked of `form_with`, but made to output html props for react components instead of HTML. It allows us to use Rails forms in React. 2. Rail has the equivalent `button_to` helper which builds a form that looks like a link when the action is POST, PUT, DELETE. We can easily replicate this with `form_props` and a custom component `ButtonTo`. [commit]: seanpdoyle@9ea344f [form_props]: https://github.com/thoughtbot/form_props
- Loading branch information
Showing
21 changed files
with
240 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react' | ||
|
||
export default ({props, extras, inputs, children, ...rest}) => { | ||
const btnSubmit = (inputs && inputs.submit) ? | ||
<button {...inputs.submit} {...rest}>{inputs.submit.text}</button> : | ||
<button type="submit" {...rest}>{ children }</button>; | ||
|
||
return ( | ||
<form {...props}> | ||
{Object.values(extras).map((hiddenProps) => (<input {...hiddenProps} key={hiddenProps.name}/>))} | ||
{btnSubmit} | ||
</form> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from 'react' | ||
import SVG from 'react-inlinesvg'; | ||
import closeSvg from '../assets/images/icons/x-circle.svg' | ||
|
||
export default class extends React.Component { | ||
render () { | ||
const cartItems = this.props.cart.map(({rowNumber, price, removeSvg, id}) => ( | ||
<tr key={id}> | ||
<td> {rowNumber} </td> | ||
<td className="syos-table__cell--numerals"> {price} </td> | ||
<td className="syos-u-text-align-right"> | ||
<button className="syos-button syos-button--transparent"> | ||
<SVG src={ closeSvg } className="syos-icon" title="Remove"/> | ||
</button> | ||
</td> | ||
</tr> | ||
)) | ||
|
||
return ( | ||
<div id="cart-summary"> | ||
<h2 className="syos-u-margin-bottom-2"> | ||
Your seat selections | ||
</h2> | ||
|
||
<p className="syos-u-font-size-small syos-u-margin-bottom-2"> | ||
Seats are not reserved until added to the cart. | ||
</p> | ||
|
||
<table className="syos-table"> | ||
<thead> | ||
<tr> | ||
<th> | ||
Seat | ||
</th> | ||
|
||
<th className="syos-table__cell--numerals"> | ||
Price | ||
</th> | ||
|
||
<th className="visually-hidden"> | ||
Remove | ||
</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
{ cartItems } | ||
</tbody> | ||
</table> | ||
</div> | ||
) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
class ApplicationController < ActionController::Base | ||
before_action do | ||
cart_token = cookies[:cart_token] | ||
|
||
Current.cart ||= Cart.create_or_find_by(token: cart_token) | ||
|
||
cookies[:cart_token] ||= Current.cart.token | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class SelectionsController < ApplicationController | ||
def create | ||
seat = Seat.find(params[:seat_id]) | ||
|
||
Current.cart.seat_selections.create_or_find_by(seat: seat) | ||
|
||
redirect_to venue_floor_seats_url(seat.venue, seat.floor) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Cart < ApplicationRecord | ||
has_secure_token | ||
|
||
has_many :seat_selections | ||
has_many :seats, through: :seat_selections | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Current < ActiveSupport::CurrentAttributes | ||
attribute :cart | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class SeatSelection < ApplicationRecord | ||
belongs_to :seat | ||
belongs_to :cart | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
json.array! Current.cart.seats do |seat| | ||
json.id seat.id | ||
json.row_number seat.row_number | ||
json.price number_to_currency(seat.section.price / 100.0) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateCarts < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :carts do |t| | ||
t.string :token, null: false | ||
|
||
t.timestamps | ||
end | ||
add_index :carts, :token, unique: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateSeatSelections < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :seat_selections do |t| | ||
t.references :seat, null: false, foreign_key: true | ||
t.references :cart, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :seat_selections, [:seat_id, :cart_id], unique: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require "test_helper" | ||
|
||
class SeatsControllerTest < ActionDispatch::IntegrationTest | ||
test "#index when a Cart exists, does not create a new one" do | ||
venue = create(:benedum_center) | ||
floor = create(:orchestra, venue: venue) | ||
cart = create(:cart) | ||
|
||
cookies[:cart_token] = cart.token | ||
get venue_floor_seats_path(venue, floor) | ||
|
||
assert_equal Cart.count, 1 | ||
assert_equal cookies[:cart_token], cart.token | ||
end | ||
|
||
test "#index when a Cart does not exist, creates a new one" do | ||
venue = create(:benedum_center) | ||
floor = create(:orchestra, venue: venue) | ||
|
||
get venue_floor_seats_path(venue, floor) | ||
|
||
assert_equal Cart.last.token, cookies[:cart_token] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "test_helper" | ||
|
||
class SelectionsControllerTest < ActionDispatch::IntegrationTest | ||
test "#create when a Seat is not selected" do | ||
seat = create(:seat) | ||
|
||
post seat_selections_path(seat) | ||
|
||
assert_equal SeatSelection.pluck(:seat_id), [seat.id] | ||
end | ||
|
||
test "#create when a Seat is already selected" do | ||
seat_selection = create(:seat_selection) | ||
cart = seat_selection.cart | ||
seat = seat_selection.seat | ||
cookies[:cart_token] = cart.token | ||
|
||
post seat_selections_path(seat) | ||
|
||
assert_equal cart.seats.ids, [seat.id] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require "application_system_test_case" | ||
|
||
class VisitorSelectsSeatTest < ApplicationSystemTestCase | ||
test "visiting the seat page" do | ||
venue = create(:benedum_center) | ||
floor = create(:orchestra, venue: venue) | ||
section = create(:section, floor: floor, price: 10_00) | ||
seat = create(:seat, row: "AA", number: "101", section: section) | ||
|
||
visit("/venues/benedum_center/floors/orchestra/seats/AA-101") | ||
click_on("Select") | ||
|
||
within("#cart-summary") do | ||
assert_text "$10.00" | ||
end | ||
end | ||
end |