diff --git a/common/app/routes/Jobs/components/GoToPayPal.jsx b/common/app/routes/Jobs/components/GoToPayPal.jsx
index 591fd375bb..5f0453cb92 100644
--- a/common/app/routes/Jobs/components/GoToPayPal.jsx
+++ b/common/app/routes/Jobs/components/GoToPayPal.jsx
@@ -1,5 +1,5 @@
import React, { PropTypes } from 'react';
-import { Button, Col, Panel, Row, Well } from 'react-bootstrap';
+import { Button, Input, Col, Panel, Row, Well } from 'react-bootstrap';
import { contain } from 'thundercats-react';
const paypalIds = {
@@ -12,14 +12,26 @@ const paypalIds = {
export default contain(
{
store: 'JobsStore',
- actions: 'JobActions',
+ actions: 'jobActions',
map({
job: { id, isHighlighted } = {},
buttonId = paypalIds.regular,
price = 200,
- discountAmount = 0
+ discountAmount = 0,
+ promoCode = '',
+ promoApplied = false,
+ promoName
}) {
- return { id, isHighlighted, buttonId, price, discountAmount };
+ return {
+ id,
+ isHighlighted,
+ buttonId,
+ price,
+ discountAmount,
+ promoName,
+ promoCode,
+ promoApplied
+ };
}
},
React.createClass({
@@ -30,7 +42,11 @@ export default contain(
isHighlighted: PropTypes.bool,
buttonId: PropTypes.string,
price: PropTypes.number,
- discountAmount: PropTypes.number
+ discountAmount: PropTypes.number,
+ promoName: PropTypes.string,
+ promoCode: PropTypes.string,
+ promoApplied: PropTypes.bool,
+ jobActions: PropTypes.object
},
renderDiscount(discountAmount) {
@@ -42,7 +58,7 @@ export default contain(
- Discount
+ Promo Discount
@@ -71,8 +87,75 @@ export default contain(
);
},
+ renderPromo() {
+ const {
+ promoApplied,
+ promoCode,
+ promoName,
+ isHighlighted,
+ jobActions
+ } = this.props;
+ if (promoApplied) {
+ return (
+
+
+
+
+ { promoName } applied
+
+
+
+ );
+ }
+ return (
+
+
+
+
+ Have a promo code?
+
+
+
+
+
+
+
+
+
+
+
+ );
+ },
+
render() {
- const { id, isHighlighted, buttonId, price, discountAmount } = this.props;
+ const {
+ id,
+ isHighlighted,
+ buttonId,
+ price,
+ discountAmount
+ } = this.props;
+
return (
@@ -124,6 +207,7 @@ export default contain(
+ { this.renderPromo() }
{
+ const body = { code: code.replace(/[^\d\w\s]/, '') };
+ if (type) {
+ body.type = type;
+ }
+ postJSON$('/api/promos/getButton', body)
+ .pluck('response')
+ .subscribe(
+ ({ promo }) => {
+ if (promo && promo.buttonId) {
+ jobActions.applyPromo(promo);
+ }
+ jobActions.setError(new Error('no promo found'));
+ },
+ jobActions.setError
+ );
+ });
+
return jobActions;
- });
+ });
diff --git a/common/app/routes/Jobs/flux/Store.js b/common/app/routes/Jobs/flux/Store.js
index 15aa55b770..90e7e26598 100644
--- a/common/app/routes/Jobs/flux/Store.js
+++ b/common/app/routes/Jobs/flux/Store.js
@@ -20,7 +20,9 @@ export default Store({
closeModal,
handleForm,
setForm,
- setFollowersCount
+ setFollowersCount,
+ setPromoCode,
+ applyPromo
} = cat.getActions('JobActions');
const register = createRegistrar(jobsStore);
register(setter(setJobs));
@@ -28,6 +30,8 @@ export default Store({
register(setter(openModal));
register(setter(closeModal));
register(setter(setForm));
+ register(setter(setPromoCode));
+ register(setter(applyPromo));
register(setter(setFollowersCount));
register(transformer(findJob));
diff --git a/common/models/promo.js b/common/models/promo.js
index 617635f0c6..1901e47d2a 100644
--- a/common/models/promo.js
+++ b/common/models/promo.js
@@ -1,8 +1,7 @@
import { isAlphanumeric } from 'validator';
export default function promo(Promo) {
- Promo.getButton = function getButton(code, type = null) {
-
+ Promo.getButton = function getButton(code, type = 'isNot') {
if (
!isAlphanumeric(code) &&
type &&
@@ -15,8 +14,7 @@ export default function promo(Promo) {
const query = {
where: {
- code,
- type
+ and: [{ code }, { type }]
}
};
diff --git a/common/utils/ajax-stream.js b/common/utils/ajax-stream.js
index 63b55e76ce..3bdcc2abdc 100644
--- a/common/utils/ajax-stream.js
+++ b/common/utils/ajax-stream.js
@@ -255,6 +255,7 @@ export function postJSON$(url, body) {
url,
body: JSON.stringify(body),
method: 'POST',
+ responseType: 'json',
headers: { 'Content-Type': 'application/json' }
});
}
@@ -277,10 +278,7 @@ export function get$(url) {
* @returns {Observable} The observable sequence which contains the parsed JSON
*/
export function getJSON$(url) {
- if (!root.JSON && typeof root.JSON.parse !== 'function') {
- throw new TypeError('JSON is not supported in your runtime.');
- }
- return ajax$({url: url, responseType: 'json'}).map(function(x) {
+ return ajax$({ url: url, responseType: 'json' }).map(function(x) {
return x.response;
});
}