From 5e4f9132fab5a2b3168d4268658c8fc6e2661227 Mon Sep 17 00:00:00 2001 From: George Schneeloch Date: Thu, 16 May 2019 10:14:58 -0400 Subject: [PATCH] Fix coupon apply button bug --- static/js/containers/pages/CheckoutPage.js | 34 ++++++++++++++----- .../js/containers/pages/CheckoutPage_test.js | 15 ++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/static/js/containers/pages/CheckoutPage.js b/static/js/containers/pages/CheckoutPage.js index cbd3aceaf2..0fc9d9cfa5 100644 --- a/static/js/containers/pages/CheckoutPage.js +++ b/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" @@ -117,7 +117,7 @@ export class CheckoutPage extends React.Component { }) } - 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({ @@ -128,9 +128,31 @@ export class CheckoutPage extends React.Component { }) }) + 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 { updateBasket } = this.props - const { couponCode } = this.state + const couponCode = this.getCouponCode() e.preventDefault() @@ -244,11 +266,7 @@ export class CheckoutPage extends React.Component {