Created
October 29, 2016 23:41
-
-
Save AloofBuddha/c6f392565d59880029c35bbb79de2dce to your computer and use it in GitHub Desktop.
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
const initialState = { | |
activeIndex: null, | |
flights: [] | |
} | |
export default function reducer (state = initialState, action){ | |
switch(action.type) { | |
// lets create first. | |
// This means we create a flight then add it to a *copy* of the list. | |
case CREATE_NEW_FLIGHT: | |
const newFlight = { | |
departure: action.departure, | |
arrival: action.arrival | |
} | |
return { | |
...state, // spread out the original state | |
// update just the flights object. It's a list so we spread it out then add one | |
flights: [...state.flights, newFlight] | |
} | |
case SET_ACTIVE: | |
return { | |
...state, | |
// the setting active | |
activeIndex: action.activeIndex; | |
} | |
case UPDATE_ACTIVE_FLIGHT: | |
const idx = action.flightNumber; | |
const updatedFlight = { | |
fromLocation: action.from.code, | |
fromLat: action.from.lat, | |
fromLng: -action.from.lng, | |
fromCity: action.from.city, | |
fromValue: action.from.value, | |
toLocation: action.destination.code, | |
toLat: action.destination.lat, | |
toLng: -action.destination.lng, | |
toCity: action.destination.city, | |
toValue: action.destination.value | |
} | |
return { | |
...state, | |
flights: state.flights.map(flight => (idx === state.activeIndex) ? updatedFlight : flight) | |
} | |
} | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment