2015-07-25 13:58:26 -07:00
|
|
|
import { Actions } from 'thundercats';
|
2015-09-24 20:28:04 -07:00
|
|
|
import store from 'store';
|
2015-07-25 15:42:03 -07:00
|
|
|
import debugFactory from 'debug';
|
2015-10-19 14:19:04 -07:00
|
|
|
import { jsonp$ } from '../../../../utils/jsonp$';
|
2015-10-26 17:39:38 -07:00
|
|
|
import { postJSON$ } from '../../../../utils/ajax-stream';
|
2015-07-25 15:42:03 -07:00
|
|
|
|
|
|
|
const debug = debugFactory('freecc:jobs:actions');
|
2015-09-21 22:41:12 -07:00
|
|
|
const assign = Object.assign;
|
2015-07-25 13:58:26 -07:00
|
|
|
|
|
|
|
export default Actions({
|
2015-07-25 15:42:03 -07:00
|
|
|
setJobs: null,
|
2015-09-10 16:26:41 -07:00
|
|
|
// findJob assumes that the job is already in the list of jobs
|
|
|
|
findJob(id) {
|
|
|
|
return oldState => {
|
|
|
|
const { currentJob = {}, jobs = [] } = oldState;
|
|
|
|
// currentJob already set
|
|
|
|
// do nothing
|
|
|
|
if (currentJob.id === id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const foundJob = jobs.reduce((newJob, job) => {
|
|
|
|
if (job.id === id) {
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
return newJob;
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
// if no job found this will be null which is a op noop
|
|
|
|
return foundJob ?
|
2015-09-21 22:41:12 -07:00
|
|
|
assign({}, oldState, { currentJob: foundJob }) :
|
2015-09-10 16:26:41 -07:00
|
|
|
null;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
setError: null,
|
2015-08-29 01:00:52 -07:00
|
|
|
getJob: null,
|
2015-10-13 23:05:31 -07:00
|
|
|
saveJobToDb: null,
|
2015-07-25 13:58:26 -07:00
|
|
|
getJobs(params) {
|
|
|
|
return { params };
|
2015-09-14 13:06:27 -07:00
|
|
|
},
|
|
|
|
openModal() {
|
|
|
|
return { showModal: true };
|
|
|
|
},
|
|
|
|
closeModal() {
|
|
|
|
return { showModal: false };
|
2015-09-21 22:41:12 -07:00
|
|
|
},
|
2015-09-25 12:53:29 -07:00
|
|
|
handleForm(value) {
|
2015-09-21 22:41:12 -07:00
|
|
|
return {
|
|
|
|
transform(oldState) {
|
2015-09-22 13:56:55 -07:00
|
|
|
const { form } = oldState;
|
2015-09-21 22:41:12 -07:00
|
|
|
const newState = assign({}, oldState);
|
|
|
|
newState.form = assign(
|
|
|
|
{},
|
2015-09-22 13:56:55 -07:00
|
|
|
form,
|
2015-09-25 12:53:29 -07:00
|
|
|
value
|
2015-09-21 22:41:12 -07:00
|
|
|
);
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
};
|
2015-09-24 20:28:04 -07:00
|
|
|
},
|
|
|
|
saveForm: null,
|
|
|
|
getSavedForm: null,
|
2015-10-19 15:38:48 -07:00
|
|
|
clearSavedForm: null,
|
2015-09-25 12:53:29 -07:00
|
|
|
setForm(form) {
|
2015-09-24 20:28:04 -07:00
|
|
|
return { form };
|
2015-10-19 14:19:04 -07:00
|
|
|
},
|
|
|
|
getFollowers: null,
|
|
|
|
setFollowersCount(numOfFollowers) {
|
|
|
|
return { numOfFollowers };
|
2015-10-26 17:39:38 -07:00
|
|
|
},
|
|
|
|
setPromoCode({ target: { value = '' }} = {}) {
|
|
|
|
return { promoCode: value.replace(/[^\d\w\s]/, '') };
|
|
|
|
},
|
|
|
|
applyCode: null,
|
2015-10-26 18:00:33 -07:00
|
|
|
clearPromo(foo, undef) {
|
|
|
|
return {
|
|
|
|
price: undef,
|
|
|
|
buttonId: undef,
|
|
|
|
discountAmount: undef,
|
|
|
|
promoCode: undef,
|
|
|
|
promoApplied: false,
|
|
|
|
promoName: undef
|
|
|
|
};
|
|
|
|
},
|
2015-10-26 17:39:38 -07:00
|
|
|
applyPromo({
|
|
|
|
fullPrice: price,
|
|
|
|
buttonId,
|
|
|
|
discountAmount,
|
|
|
|
code: promoCode,
|
|
|
|
name: promoName
|
|
|
|
} = {}) {
|
|
|
|
return {
|
|
|
|
price,
|
|
|
|
buttonId,
|
|
|
|
discountAmount,
|
|
|
|
promoCode,
|
|
|
|
promoApplied: true,
|
|
|
|
promoName
|
|
|
|
};
|
2015-07-25 13:58:26 -07:00
|
|
|
}
|
|
|
|
})
|
2015-07-25 15:42:03 -07:00
|
|
|
.refs({ displayName: 'JobActions' })
|
2015-10-13 23:05:31 -07:00
|
|
|
.init(({ instance: jobActions, args: [cat, services] }) => {
|
2015-07-25 15:42:03 -07:00
|
|
|
jobActions.getJobs.subscribe(() => {
|
2015-09-10 16:26:41 -07:00
|
|
|
services.read('jobs', null, null, (err, jobs) => {
|
2015-07-25 15:42:03 -07:00
|
|
|
if (err) {
|
|
|
|
debug('job services experienced an issue', err);
|
2015-09-10 16:26:41 -07:00
|
|
|
return jobActions.setError({ err });
|
2015-07-25 15:42:03 -07:00
|
|
|
}
|
|
|
|
jobActions.setJobs({ jobs });
|
|
|
|
});
|
|
|
|
});
|
2015-09-10 16:26:41 -07:00
|
|
|
|
|
|
|
jobActions.getJob.subscribe(({ id, isPrimed }) => {
|
|
|
|
// job is already set, do nothing.
|
|
|
|
if (isPrimed) {
|
|
|
|
debug('job is primed');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
services.read('jobs', { id }, null, (err, job) => {
|
|
|
|
if (err) {
|
|
|
|
debug('job services experienced an issue', err);
|
|
|
|
return jobActions.setError({ err });
|
|
|
|
}
|
2015-09-11 10:58:24 -07:00
|
|
|
if (job) {
|
|
|
|
jobActions.setJobs({ currentJob: job });
|
|
|
|
}
|
|
|
|
jobActions.setJobs({});
|
2015-09-10 16:26:41 -07:00
|
|
|
});
|
|
|
|
});
|
2015-09-24 20:28:04 -07:00
|
|
|
|
|
|
|
jobActions.saveForm.subscribe((form) => {
|
|
|
|
store.set('newJob', form);
|
|
|
|
});
|
|
|
|
|
|
|
|
jobActions.getSavedForm.subscribe(() => {
|
|
|
|
const job = store.get('newJob');
|
2015-09-25 00:04:38 -07:00
|
|
|
if (job && !Array.isArray(job) && typeof job === 'object') {
|
|
|
|
jobActions.setForm(job);
|
|
|
|
}
|
2015-09-24 20:28:04 -07:00
|
|
|
});
|
2015-10-13 23:05:31 -07:00
|
|
|
|
2015-10-19 15:38:48 -07:00
|
|
|
jobActions.clearSavedForm.subscribe(() => {
|
|
|
|
store.remove('newJob');
|
|
|
|
});
|
|
|
|
|
2015-10-13 23:05:31 -07:00
|
|
|
jobActions.saveJobToDb.subscribe(({ goTo, job }) => {
|
|
|
|
const appActions = cat.getActions('appActions');
|
|
|
|
services.create('jobs', { job }, null, (err, job) => {
|
|
|
|
if (err) {
|
|
|
|
debug('job services experienced an issue', err);
|
|
|
|
return jobActions.setError(err);
|
|
|
|
}
|
|
|
|
jobActions.setJobs({ job });
|
2015-10-27 23:46:42 -07:00
|
|
|
appActions.updateRoute(goTo);
|
2015-10-13 23:05:31 -07:00
|
|
|
});
|
|
|
|
});
|
2015-10-19 14:19:04 -07:00
|
|
|
|
|
|
|
jobActions.getFollowers.subscribe(() => {
|
|
|
|
const url = 'https://cdn.syndication.twimg.com/widgets/followbutton/' +
|
|
|
|
'info.json?lang=en&screen_names=CamperJobs' +
|
|
|
|
'&callback=JSONPCallback';
|
|
|
|
|
|
|
|
jsonp$(url)
|
|
|
|
.map(({ response }) => {
|
|
|
|
return response[0]['followers_count'];
|
|
|
|
})
|
|
|
|
.subscribe(
|
|
|
|
count => jobActions.setFollowersCount(count),
|
|
|
|
err => jobActions.setError(err)
|
|
|
|
);
|
|
|
|
});
|
2015-10-19 15:38:48 -07:00
|
|
|
|
2015-11-23 15:38:05 -08:00
|
|
|
jobActions.applyCode.subscribe(({ id, code = '', type = null}) => {
|
|
|
|
const body = {
|
|
|
|
id,
|
|
|
|
code: code.replace(/[^\d\w\s]/, '')
|
|
|
|
};
|
2015-10-26 17:39:38 -07:00
|
|
|
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
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-07-25 15:42:03 -07:00
|
|
|
return jobActions;
|
2015-10-26 17:39:38 -07:00
|
|
|
});
|