Created
May 17, 2020 19:45
-
-
Save clintonyeb/4912202c9174d443e1004b48aa89f1e6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Reducers are used to create state machines. | |
//They take a state and action as input and return final state as output | |
const INIT_STATE = { | |
loading: false, //whether app is currently loading | |
activeSubscription: [], //list of current subscriptions | |
availableCountries:[] //list of available countries | |
} | |
//limiting max number of subscriptions to 4 | |
const maxSubscriptionAtaTime = 4; | |
export default (state = INIT_STATE, action) => { | |
switch (action.type) { | |
case 'INIT_GET_CURRENCY_NAME_REQUEST': | |
return { | |
...state, //this is returning a new state using the object spread operator https://redux.js.org/recipes/using-object-spread-operator | |
loading: true | |
} | |
case 'GET_CURRENCY_PAIR':{ | |
//If currency is part of supported currencypairs update list of active subscriptions and countries | |
const currencySupported = action.payload; | |
const activeSubscription = []; | |
const availableCountries = []; | |
if(Array.isArray(currencySupported)) { | |
currencySupported.forEach((data, i)=>{ | |
availableCountries.push(data.currency_name) | |
if(i < maxSubscriptionAtaTime) | |
activeSubscription.push(data.currency_name); | |
}) | |
} | |
return { | |
...state, | |
availableCountries, | |
activeSubscription, | |
loading: false | |
} | |
} | |
//modify the list of active subscriptions | |
case 'COUNTRY_CHANGE': { | |
const activeSubscription = JSON.parse(JSON.stringify(state.activeSubscription)); // prevent mutation make clone | |
if(action.cardPosition === undefined || action.code === undefined) return state; | |
activeSubscription[action.cardPosition] = action.code; | |
return { | |
...state, | |
activeSubscription | |
} | |
} | |
default: | |
return state | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment