2015-07-25 13:58:26 -07:00
|
|
|
import { Actions } from 'thundercats';
|
2015-07-25 15:42:03 -07:00
|
|
|
import debugFactory from 'debug';
|
|
|
|
|
|
|
|
const debug = debugFactory('freecc:jobs:actions');
|
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 ?
|
|
|
|
Object.assign({}, oldState, { currentJob: foundJob }) :
|
|
|
|
null;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
setError: null,
|
2015-08-29 01:00:52 -07:00
|
|
|
getJob: null,
|
2015-07-25 13:58:26 -07:00
|
|
|
getJobs(params) {
|
|
|
|
return { params };
|
|
|
|
}
|
|
|
|
})
|
2015-07-25 15:42:03 -07:00
|
|
|
.refs({ displayName: 'JobActions' })
|
|
|
|
.init(({ instance: jobActions, args: [services] }) => {
|
|
|
|
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-07-25 15:42:03 -07:00
|
|
|
return jobActions;
|
|
|
|
});
|