Files
freeCodeCamp/common/app/routes/Jobs/redux/reducer.js

86 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-02-25 18:30:10 -08:00
import { handleActions } from 'redux-actions';
import types from './types';
const replaceMethod = ''.replace;
function replace(str) {
if (!str) { return ''; }
return replaceMethod.call(str, /[^\d\w\s]/, '');
}
const initialState = {
2016-03-02 16:33:44 -08:00
// used by NewJob form
initialValues: {},
2016-02-25 18:30:10 -08:00
currentJob: '',
newJob: {},
jobs: {
entities: {},
results: []
}
};
export default handleActions(
{
[types.findJob]: (state, { payload: id }) => {
const currentJob = state.jobs.entities[id];
return {
...state,
currentJob: currentJob && currentJob.id ?
currentJob.id :
state.currentJob
};
},
2016-02-28 17:00:31 -08:00
[types.fetchJobsCompleted]: (state, { payload: { jobs, currentJob } }) => ({
2016-02-25 18:30:10 -08:00
...state,
currentJob,
jobs
}),
2016-03-02 16:33:44 -08:00
[types.updatePromo]: (state, { payload }) => ({
2016-02-25 18:30:10 -08:00
...state,
promoCode: replace(payload)
}),
2016-03-02 16:33:44 -08:00
[types.saveCompleted]: (state, { payload: newJob }) => {
return {
...state,
newJob
};
},
[types.loadSavedFormCompleted]: (state, { payload: initialValues }) => ({
...state,
initialValues
}),
[types.applyPromoCompleted]: (state, { payload: promo }) => {
2016-02-25 18:30:10 -08:00
const {
fullPrice: price,
buttonId,
discountAmount,
code: promoCode,
name: promoName
} = promo;
return {
...state,
price,
buttonId,
discountAmount,
promoCode,
promoApplied: true,
promoName
};
},
[types.clearPromo]: state => ({
/* eslint-disable no-undefined */
...state,
price: undefined,
buttonId: undefined,
discountAmount: undefined,
promoCode: undefined,
promoApplied: false,
promoName: undefined
/* eslint-enable no-undefined */
})
},
initialState
);