Last active
October 26, 2017 07:02
-
-
Save 197291/913ce51176ed82e4e6b660c1f667668c to your computer and use it in GitHub Desktop.
Reducers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const plansName = [ | |
{ | |
id: 'standard', | |
nameId: 'billing.plan.name.standard' | |
}, | |
{ | |
id: 'plus', | |
nameId: 'billing.plan.name.plus' | |
}, | |
]; | |
const initialState = { | |
loadingPlans: false, | |
loadingCreate: false, | |
loadingCancel: false, | |
loadingUpgrade: false, | |
errorPlans: null, | |
errorCreate: null, | |
errorCancel: null, | |
errorUpgrade: null, | |
viewIndex: 0, | |
planId: '', | |
planItemId: '', | |
plans: [], | |
etagPlans: '', | |
errorToken: '', | |
isShowCancelConfirm: false, | |
}; | |
export default function reducer(state = initialState, action = {}) { | |
switch (action.type) { | |
case CHANGE_VIEW: | |
return { | |
...state, | |
viewIndex: action.index | |
}; | |
case CHANGE_PLAN: | |
const destinationPlan = state.plans.find((it) => it.id === action.planId); | |
return { | |
...state, | |
planId: action.planId, | |
planItemId: destinationPlan.items[0].id | |
}; | |
case CHANGE_PLAN_ITEM: | |
return { | |
...state, | |
planItemId: action.planItemId, | |
}; | |
case LOAD_PLANS: | |
return { | |
...state, | |
loadingPlans: true, | |
errorPlans: null | |
}; | |
case LOAD_PLANS_SUCCESS: | |
return { | |
...state, | |
loadingPlans: false, | |
plans: action.result.body.reduce((memo, it) => { | |
const planIdInList = getPlanId(it.id); | |
let plan = memo.find((itt) => itt.id === planIdInList); | |
if (!plan) { | |
memo.push({ | |
id: planIdInList, | |
nameId: plansName.find((itt) => itt.id === planIdInList).nameId, | |
items: [] | |
}); | |
plan = memo.find((itt) => itt.id === planIdInList); | |
} | |
plan.items.push({ | |
name: it.name, | |
id: it.id, | |
amount: it.amount, | |
interval: it.interval, | |
perUserPerMonth: getPricePerUserPerMonth(it), | |
}); | |
return memo; | |
}, []), | |
etagPlans: action.result.headers ? action.result.headers.etag : '' | |
}; | |
case LOAD_PLANS_FAIL: | |
return { | |
...state, | |
loadingPlans: false, | |
errorPlans: action.error | |
}; | |
case START_CREATE_TOKEN: | |
return { | |
...state, | |
errorToken: action.errorMassage | |
}; | |
case ERROR_CREATE_TOKEN: | |
return { | |
...state, | |
errorToken: action.errorMassage | |
}; | |
case CREATE: | |
return { | |
...state, | |
loadingCreate: true, | |
errorCreate: null | |
}; | |
case CREATE_SUCCESS: | |
return { | |
...state, | |
loadingCreate: false | |
}; | |
case CREATE_FAIL: | |
return { | |
...state, | |
loadingCreate: true, | |
errorCreate: action.error | |
}; | |
case CANCEL: | |
return { | |
...state, | |
loadingCancel: true, | |
errorCancel: null | |
}; | |
case CANCEL_SUCCESS: | |
return { | |
...state, | |
loadingCancel: false | |
}; | |
case CANCEL_FAIL: | |
return { | |
...state, | |
loadingCancel: true, | |
errorCancel: action.error | |
}; | |
case UPGRADE: | |
return { | |
...state, | |
loadingUpgrade: true, | |
errorUpgrade: null | |
}; | |
case UPGRADE_SUCCESS: | |
return { | |
...state, | |
loadingUpgrade: false | |
}; | |
case UPGRADE_FAIL: | |
return { | |
...state, | |
loadingUpgrade: false, | |
errorUpgrade: action.error | |
}; | |
case CLEANUP: | |
return { | |
...state, | |
planId: '', | |
planItemId: '', | |
viewIndex: 0 | |
}; | |
case SHOW_CANCEL_CONFIRM: | |
return { | |
...state, | |
isShowCancelConfirm: true | |
}; | |
case CLOSE_CANCEL_CONFIRM: | |
return { | |
...state, | |
isShowCancelConfirm: false | |
}; | |
default: | |
return state; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment