1 | import React from 'react'
|
2 | import PropTypes from 'prop-types'
|
3 | import styled from 'styled-components'
|
4 |
|
5 | import CouponCodeInput from './couponCodeInput'
|
6 | import RemoveCouponCode from './removeCouponCode'
|
7 |
|
8 | const RemoveContainer = styled.div`
|
9 | display: flex;
|
10 | align-items: center;
|
11 | justify-content: space-between;
|
12 | max-width: 300px;
|
13 | margin: 0 auto;
|
14 | `
|
15 |
|
16 | const RemoveWrapper = styled.div`
|
17 | display: flex;
|
18 | justify-content: space-between;
|
19 | margin: 10px 0;
|
20 | `
|
21 |
|
22 | const PromoLabel = styled.div`
|
23 | color: ${props => props.theme.colors.navy};
|
24 | font-family: ${props => props.theme.fonts.primaryFont};
|
25 | font-size: 14px;
|
26 | display: inline-block;
|
27 | max-width: 30rem;
|
28 | margin: 0 auto;
|
29 | `
|
30 |
|
31 | class BaseCouponCodeWrapper extends React.Component {
|
32 | constructor (props) {
|
33 | super(props)
|
34 |
|
35 | this.state = { couponCode: '' }
|
36 | }
|
37 |
|
38 | handleCouponCodeChange = (e) => {
|
39 | this.setState({ couponCode: e.target.value })
|
40 | }
|
41 |
|
42 | componentDidMount () {
|
43 | const { appliedPromotion, applyPromotion } = this.props
|
44 | if (appliedPromotion && !applyPromotion.expired) {
|
45 | this.setState({
|
46 | couponCode: appliedPromotion.value
|
47 | })
|
48 | applyPromotion(appliedPromotion.value)
|
49 | }
|
50 | }
|
51 |
|
52 | render () {
|
53 | const {
|
54 | loading,
|
55 | promotion,
|
56 | applyPromotion,
|
57 | removePromotion,
|
58 | errorMessage,
|
59 | promoHasBeenApplied,
|
60 | showToggle,
|
61 | className,
|
62 | } = this.props
|
63 | const { couponCode } = this.state
|
64 |
|
65 | return (
|
66 | <div className={className}>
|
67 | {!promoHasBeenApplied
|
68 | ? <div>
|
69 | <CouponCodeInput
|
70 | couponCode={couponCode}
|
71 | onChange={this.handleCouponCodeChange}
|
72 | applyPromotion={applyPromotion}
|
73 | removePromotion={removePromotion}
|
74 | loading={loading}
|
75 | errorMessage={errorMessage}
|
76 | showToggle={showToggle}
|
77 | />
|
78 | </div>
|
79 | : <RemoveContainer>
|
80 | <RemoveWrapper>
|
81 | <PromoLabel>Promotion Applied</PromoLabel>
|
82 | </RemoveWrapper>
|
83 | <RemoveCouponCode removePromotion={removePromotion} promoHasBeenApplied={promoHasBeenApplied}
|
84 | loading={loading} promotion={promotion.promotion_code_id} />
|
85 | </RemoveContainer>
|
86 | }
|
87 | </div>
|
88 | )
|
89 | }
|
90 | }
|
91 |
|
92 | BaseCouponCodeWrapper.propTypes = {
|
93 | promotion: PropTypes.object,
|
94 | loading: PropTypes.bool,
|
95 | promoHasBeenApplied: PropTypes.bool,
|
96 | errorMessage: PropTypes.string,
|
97 | applyPromotion: PropTypes.func,
|
98 | removePromotion: PropTypes.func,
|
99 | appliedPromotion: PropTypes.object,
|
100 | showToggle: PropTypes.bool,
|
101 | showBorder: PropTypes.bool
|
102 | }
|
103 |
|
104 | const CouponCodeWrapper = styled(BaseCouponCodeWrapper)`
|
105 | width: 100%;
|
106 | padding-bottom: ${props => props.errorMessage ? '30px' : '20px'};
|
107 | border-bottom: ${props => props.showBorder ? '1px solid #d5d5d5' : 'none'};
|
108 | `
|
109 |
|
110 | export default CouponCodeWrapper
|
111 |
|
\ | No newline at end of file |