Files
freeCodeCamp/common/app/routes/Jobs/flux/Actions.js

144 lines
3.5 KiB
JavaScript
Raw Normal View History

import { Actions } from 'thundercats';
2015-09-24 20:28:04 -07:00
import store from 'store';
import { getDefaults } from '../utils';
2015-07-25 15:42:03 -07:00
import debugFactory from 'debug';
const debug = debugFactory('freecc:jobs:actions');
const assign = Object.assign;
export default Actions({
2015-07-25 15:42:03 -07:00
setJobs: null,
// 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 ?
assign({}, oldState, { currentJob: foundJob }) :
null;
};
},
setError: null,
2015-08-29 01:00:52 -07:00
getJob: null,
getJobs(params) {
return { params };
2015-09-14 13:06:27 -07:00
},
openModal() {
return { showModal: true };
},
closeModal() {
return { showModal: false };
},
handleForm({ name, value, validator = () => {} }) {
if (!name) {
// operation noop
return { replace: null };
}
if (!validator(value)) {
return {
transform(oldState) {
const { form } = oldState;
const newState = assign({}, oldState);
newState.form = assign(
{},
form,
2015-09-24 20:28:04 -07:00
{
[name]: {
value,
valid: false,
pristine: false,
bsStyle: value ? 'error' : null
}
}
);
return newState;
}
};
}
return {
transform(oldState) {
const { form } = oldState;
const newState = assign({}, oldState);
newState.form = assign(
{},
form,
2015-09-24 20:28:04 -07:00
{
[name]: {
value,
valid: true,
pristine: false,
bsStyle: value ? 'success' : null
}
}
);
return newState;
}
};
2015-09-24 20:28:04 -07:00
},
saveForm: null,
getSavedForm: null,
setForm(job) {
const form = Object.keys(job).reduce((accu, prop) => {
console.log('form', accu);
return Object.assign(
accu,
{ [prop]: getDefaults(typeof prop, job[prop]) }
);
}, {});
return { form };
}
})
2015-07-25 15:42:03 -07:00
.refs({ displayName: 'JobActions' })
.init(({ instance: jobActions, args: [services] }) => {
jobActions.getJobs.subscribe(() => {
services.read('jobs', null, null, (err, jobs) => {
2015-07-25 15:42:03 -07:00
if (err) {
debug('job services experienced an issue', err);
return jobActions.setError({ err });
2015-07-25 15:42:03 -07:00
}
jobActions.setJobs({ jobs });
});
});
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 });
}
if (job) {
jobActions.setJobs({ currentJob: job });
}
jobActions.setJobs({});
});
});
2015-09-24 20:28:04 -07:00
jobActions.saveForm.subscribe((form) => {
store.set('newJob', form);
});
jobActions.getSavedForm.subscribe(() => {
const job = store.get('newJob');
jobActions.setForm(job);
});
2015-07-25 15:42:03 -07:00
return jobActions;
});