Skip to content

Commit

Permalink
Fix coupon apply button bug (#296)
Browse files Browse the repository at this point in the history
* Fix coupon apply button bug

* After coupon is submitted, use coupon value from basket
  • Loading branch information
George Schneeloch committed May 20, 2019
1 parent 5a37736 commit ee99523
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
49 changes: 36 additions & 13 deletions static/js/containers/pages/CheckoutPage.js
@@ -1,6 +1,6 @@
// @flow
import React from "react"
import * as R from "ramda"
import { curry } from "ramda"
import { connect } from "react-redux"
import { connectRequest, mutateAsync } from "redux-query"
import { compose } from "redux"
Expand Down Expand Up @@ -70,6 +70,14 @@ export class CheckoutPage extends React.Component<Props, State> {
this.setState({ errors: response.body.errors })
throw new Error("Received error from request")
}

// clear state so the state from the basket is used
this.setState({
couponCode: null,
selectedRuns: null,
errors: null
})

return response
}

Expand Down Expand Up @@ -117,7 +125,7 @@ export class CheckoutPage extends React.Component<Props, State> {
})
}

updateSelectedRun = R.curry((courseId: number, event: any) => {
updateSelectedRun = curry((courseId: number, event: any) => {
const { selectedRuns } = this.state
const runId = parseInt(event.target.value)
this.setState({
Expand All @@ -128,13 +136,34 @@ export class CheckoutPage extends React.Component<Props, State> {
})
})

submitCoupon = async (e: Event) => {
const { updateBasket } = this.props
getCouponCode = (): string => {
const { basket } = this.props
const { couponCode } = this.state

if (couponCode !== null) {
return couponCode
}
if (!basket) {
return ""
}

const item = basket.items[0]
if (!item) {
return ""
}

const coupon = basket.coupons.find(coupon =>
coupon.targets.includes(item.id)
)
return (coupon && coupon.code) || ""
}

submitCoupon = async (e: Event) => {
const couponCode = this.getCouponCode()

e.preventDefault()

const response = await updateBasket({
await this.updateBasket({
coupons: couponCode
? [
{
Expand All @@ -143,8 +172,6 @@ export class CheckoutPage extends React.Component<Props, State> {
]
: []
})
const errors = response.status !== 200 ? response.body.errors : null
this.setState({ errors })
}

// $FlowFixMe
Expand Down Expand Up @@ -200,7 +227,7 @@ export class CheckoutPage extends React.Component<Props, State> {

render() {
const { basket } = this.props
const { couponCode, errors } = this.state
const { errors } = this.state

if (!basket) {
return null
Expand Down Expand Up @@ -244,11 +271,7 @@ export class CheckoutPage extends React.Component<Props, State> {
<input
type="text"
className={errors ? "error-border" : ""}
value={
(couponCode !== null
? couponCode
: coupon && coupon.code) || ""
}
value={this.getCouponCode()}
onChange={this.updateCouponCode}
/>
<button
Expand Down
48 changes: 45 additions & 3 deletions static/js/containers/pages/CheckoutPage_test.js
Expand Up @@ -153,6 +153,46 @@ describe("CheckoutPage", () => {
})
})

it("submits the coupon code currently in the basket if there is none in the state", async () => {
const { inner } = await renderPage()
const code = basket.coupons[0].code
await inner.find(".apply-button").prop("onClick")({
preventDefault: helper.sandbox.stub()
})
sinon.assert.calledWith(helper.handleRequestStub, "/api/basket/", "PATCH", {
body: { coupons: [{ code: code }] },
credentials: undefined,
headers: {
"X-CSRFTOKEN": null
}
})
})

it("clears the state after submitting the basket", async () => {
const { inner } = await renderPage()
const couponCode = "coupon code"
inner.setState({
errors: "errors",
selectedRuns: ["runs"],
couponCode: couponCode
})
await inner.find(".apply-button").prop("onClick")({
preventDefault: helper.sandbox.stub()
})
sinon.assert.calledWith(helper.handleRequestStub, "/api/basket/", "PATCH", {
body: { coupons: [{ code: couponCode }] },
credentials: undefined,
headers: {
"X-CSRFTOKEN": null
}
})
assert.deepEqual(inner.state(), {
couponCode: null,
errors: null,
selectedRuns: null
})
})

it("tries to submit the coupon code but receives an error message", async () => {
const { inner } = await renderPage()
const errors = "Unknown error"
Expand All @@ -162,9 +202,11 @@ describe("CheckoutPage", () => {
errors
}
})
await inner.find("form").prop("onSubmit")({
preventDefault: helper.sandbox.stub()
})
await assertRaises(async () => {
await inner.find("form").prop("onSubmit")({
preventDefault: helper.sandbox.stub()
})
}, "Received error from request")

assert.equal(inner.state().errors, errors)
assert.equal(
Expand Down

0 comments on commit ee99523

Please sign in to comment.