Files
freeCodeCamp/common/app/routes/Jobs/components/JobTotal.jsx

307 lines
7.1 KiB
JavaScript
Raw Normal View History

2016-03-02 16:33:44 -08:00
import { CompositeDisposable } from 'rx';
2016-02-05 20:48:59 -08:00
import React, { PropTypes } from 'react';
import { Button, Input, Col, Row, Well } from 'react-bootstrap';
import { connect } from 'react-redux';
2016-03-02 16:33:44 -08:00
import { push } from 'react-router-redux';
2016-02-05 20:48:59 -08:00
import PureComponent from 'react-pure-render/component';
import { createSelector } from 'reselect';
2016-03-02 16:33:44 -08:00
import {
applyPromo,
clearPromo,
updatePromo
} from '../redux/actions';
2016-02-05 20:48:59 -08:00
// real paypal buttons
// will take your money
const paypalIds = {
regular: 'Q8Z82ZLAX3Q8N',
highlighted: 'VC8QPSKCYMZLN'
};
const bindableActions = {
2016-03-02 16:33:44 -08:00
applyPromo,
clearPromo,
push,
updatePromo
2016-02-05 20:48:59 -08:00
};
const mapStateToProps = createSelector(
2016-03-02 16:33:44 -08:00
state => state.jobsApp.newJob,
2016-02-05 20:48:59 -08:00
state => state.jobsApp,
(
{ id, isHighlighted } = {},
{
buttonId,
price = 1000,
discountAmount = 0,
promoCode = '',
promoApplied = false,
promoName = ''
}
) => {
if (!buttonId) {
buttonId = isHighlighted ?
paypalIds.highlighted :
paypalIds.regular;
}
return {
id,
isHighlighted,
price,
discountAmount,
promoName,
promoCode,
promoApplied
};
}
);
export class JobTotal extends PureComponent {
2016-03-02 16:33:44 -08:00
constructor(...args) {
super(...args);
this._subscriptions = new CompositeDisposable();
}
2016-02-05 20:48:59 -08:00
static displayName = 'JobTotal';
static propTypes = {
id: PropTypes.string,
isHighlighted: PropTypes.bool,
buttonId: PropTypes.string,
price: PropTypes.number,
discountAmount: PropTypes.number,
promoName: PropTypes.string,
promoCode: PropTypes.string,
promoApplied: PropTypes.bool
};
2016-03-02 19:45:54 -08:00
componentWillMount() {
2016-03-02 16:33:44 -08:00
if (!this.props.id) {
this.props.push('/jobs');
}
this.props.clearPromo();
2016-02-05 20:48:59 -08:00
}
2016-03-02 16:33:44 -08:00
componentWillUnmount() {
this._subscriptions.dispose();
2016-02-05 20:48:59 -08:00
}
renderDiscount(discountAmount) {
if (!discountAmount) {
return null;
}
return (
<Row>
<Col
md={ 3 }
mdOffset={ 3 }>
<h4>Promo Discount</h4>
</Col>
<Col
md={ 3 }>
<h4>-{ discountAmount }</h4>
</Col>
</Row>
);
}
renderHighlightPrice(isHighlighted) {
if (!isHighlighted) {
return null;
}
return (
<Row>
<Col
md={ 3 }
mdOffset={ 3 }>
<h4>Highlighting</h4>
</Col>
<Col
md={ 3 }>
<h4>+ 250</h4>
</Col>
</Row>
);
}
renderPromo() {
const {
id,
promoApplied,
promoCode,
promoName,
isHighlighted,
2016-03-02 16:33:44 -08:00
applyPromo,
updatePromo
2016-02-05 20:48:59 -08:00
} = this.props;
if (promoApplied) {
return (
<div>
<div className='spacer' />
<Row>
<Col
md={ 3 }
mdOffset={ 3 }>
{ promoName } applied
</Col>
</Row>
</div>
);
}
return (
<div>
<div className='spacer' />
<Row>
<Col
md={ 3 }
mdOffset={ 3 }>
Have a promo code?
</Col>
</Row>
<Row>
<Col
md={ 3 }
mdOffset={ 3 }>
<Input
2016-03-02 16:33:44 -08:00
onChange={ updatePromo }
2016-02-05 20:48:59 -08:00
type='text'
value={ promoCode } />
</Col>
<Col
md={ 3 }>
<Button
block={ true }
onClick={ () => {
2016-03-02 16:33:44 -08:00
const subscription = applyPromo({
2016-02-05 20:48:59 -08:00
id,
code: promoCode,
type: isHighlighted ? 'isHighlighted' : null
2016-03-02 16:33:44 -08:00
}).subscribe();
this._subscriptions.add(subscription);
2016-02-05 20:48:59 -08:00
}}>
Apply Promo Code
</Button>
</Col>
</Row>
</div>
);
}
render() {
const {
id,
isHighlighted,
buttonId,
price,
2016-03-02 16:33:44 -08:00
discountAmount,
push
2016-02-05 20:48:59 -08:00
} = this.props;
return (
<div>
<Row>
<Col
md={ 10 }
mdOffset={ 1 }
sm={ 8 }
smOffset={ 2 }
xs={ 12 }>
<div>
<Row>
<Col
md={ 6 }
mdOffset={ 3 }>
<h2 className='text-center'>
One more step
</h2>
<div className='spacer' />
You're Awesome! just one more step to go.
Clicking on the link below will redirect to paypal.
</Col>
</Row>
<div className='spacer' />
<Well>
<Row>
<Col
md={ 3 }
mdOffset={ 3 }>
<h4>Job Posting</h4>
</Col>
<Col
md={ 6 }>
<h4>+ { price }</h4>
</Col>
</Row>
{ this.renderHighlightPrice(isHighlighted) }
{ this.renderDiscount(discountAmount) }
<Row>
<Col
md={ 3 }
mdOffset={ 3 }>
<h4>Total</h4>
</Col>
<Col
md={ 6 }>
<h4>${
price - discountAmount + (isHighlighted ? 250 : 0)
}</h4>
</Col>
</Row>
</Well>
{ this.renderPromo() }
<div className='spacer' />
<Row>
<Col
md={ 6 }
mdOffset={ 3 }>
<form
action='https://www.paypal.com/cgi-bin/webscr'
method='post'
2016-03-02 16:33:44 -08:00
onClick={ () => setTimeout(push, 0, '/jobs') }
2016-02-05 20:48:59 -08:00
target='_blank'>
<input
name='cmd'
type='hidden'
value='_s-xclick' />
<input
name='hosted_button_id'
type='hidden'
value={ buttonId } />
<input
name='custom'
type='hidden'
value={ '' + id } />
<Button
block={ true }
bsSize='large'
className='signup-btn'
type='submit'>
<i className='fa fa-paypal' />
Continue to PayPal
</Button>
<div className='spacer' />
<img
alt='An array of credit cards'
border='0'
2016-03-16 20:32:12 -06:00
src='//i.imgur.com/Q2SdSZG.png'
2016-02-05 20:48:59 -08:00
style={{
width: '100%'
}} />
</form>
</Col>
</Row>
<div className='spacer' />
</div>
</Col>
</Row>
</div>
);
}
}
export default connect(mapStateToProps, bindableActions)(JobTotal);