From aee8329da8dff869907d965fd8a8a2e3a578bd11 Mon Sep 17 00:00:00 2001 From: alexlee-dev Date: Wed, 21 Aug 2019 15:48:33 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Clean=20up=20Redux.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ItemTimer.js | 2 +- src/redux/actions/ship.js | 24 +++++++++--------- src/redux/actions/world.js | 26 +++++++++---------- src/redux/reducers/ship.js | 18 ++++++------- src/redux/reducers/ui.js | 3 +-- src/redux/reducers/world.js | 50 +++++++++++++++---------------------- 6 files changed, 54 insertions(+), 69 deletions(-) diff --git a/src/components/ItemTimer.js b/src/components/ItemTimer.js index bfe2250..9cb7493 100644 --- a/src/components/ItemTimer.js +++ b/src/components/ItemTimer.js @@ -11,7 +11,7 @@ import { const ItemTimer = ({ handleTimerStarted, handleTimerStopped, world }) => { const { isTimerRunning } = world - let duration = moment.duration({ minutes: 10, seconds: 0 }) + let duration = moment.duration({ minutes: 0, seconds: 5 }) const [timeLeft, setTimeLeft] = useState( `${duration.minutes()} minutes ${duration.seconds()} seconds` ) diff --git a/src/redux/actions/ship.js b/src/redux/actions/ship.js index c2b208a..af6c7dd 100644 --- a/src/redux/actions/ship.js +++ b/src/redux/actions/ship.js @@ -1,17 +1,10 @@ // * ACTION TYPES -const STORE_CARGO = 'STORE_CARGO' const REMOVE_CARGO = 'REMOVE_CARGO' -const SET_SHIP_LOCATION_VALUE = 'SET_SHIP_LOCATION_VALUE' const SET_SHIP_LOCATION_NAME = 'SET_SHIP_LOCATION_NAME' +const SET_SHIP_LOCATION_VALUE = 'SET_SHIP_LOCATION_VALUE' +const STORE_CARGO = 'STORE_CARGO' // * ACTION GENERATORS -export const storeCargo = item => ({ - type: STORE_CARGO, - payload: { - item - } -}) - export const removeCargo = item => ({ type: REMOVE_CARGO, payload: { @@ -19,14 +12,21 @@ export const removeCargo = item => ({ } }) +export const setShipLocationName = name => ({ + type: SET_SHIP_LOCATION_NAME, + payload: { name } +}) + export const setShipLocationValue = value => ({ type: SET_SHIP_LOCATION_VALUE, payload: { value } }) -export const setShipLocationName = name => ({ - type: SET_SHIP_LOCATION_NAME, - payload: { name } +export const storeCargo = item => ({ + type: STORE_CARGO, + payload: { + item + } }) // * PROMISES diff --git a/src/redux/actions/world.js b/src/redux/actions/world.js index 15ae018..dfbaab4 100644 --- a/src/redux/actions/world.js +++ b/src/redux/actions/world.js @@ -1,20 +1,14 @@ // * ACTION TYPES -const SET_TIMER_RUNNING = 'SET_TIMER_RUNNING' -const REMOVE_ITEM = 'REMOVE_ITEM' -const SET_PLANETS = 'SET_PLANETS' const CLEAR_ITEMS = 'CLEAR_ITEMS' const REFRESH_ITEMS = 'REFRESH_ITEMS' +const REMOVE_ITEM = 'REMOVE_ITEM' +const SET_PLANETS = 'SET_PLANETS' +const SET_TIMER_RUNNING = 'SET_TIMER_RUNNING' // * ACTION GENERATORS -export const setPlanets = planets => ({ - type: SET_PLANETS, - payload: { planets } -}) +export const clearItems = () => ({ type: CLEAR_ITEMS }) -export const setTimerRunning = isTimerRunning => ({ - type: SET_TIMER_RUNNING, - payload: { isTimerRunning } -}) +export const refreshItems = () => ({ type: REFRESH_ITEMS }) export const removeItem = item => ({ type: REMOVE_ITEM, @@ -23,9 +17,15 @@ export const removeItem = item => ({ } }) -export const clearItems = () => ({ type: CLEAR_ITEMS }) +export const setPlanets = planets => ({ + type: SET_PLANETS, + payload: { planets } +}) -export const refreshItems = () => ({ type: REFRESH_ITEMS }) +export const setTimerRunning = isTimerRunning => ({ + type: SET_TIMER_RUNNING, + payload: { isTimerRunning } +}) // * PROMISES diff --git a/src/redux/reducers/ship.js b/src/redux/reducers/ship.js index 05b67dd..423af2d 100644 --- a/src/redux/reducers/ship.js +++ b/src/redux/reducers/ship.js @@ -8,27 +8,23 @@ const shipDefaultState = { export default (state = shipDefaultState, action) => { switch (action.type) { - case 'SET_SHIP_LOCATION_VALUE': + case 'REMOVE_CARGO': return { ...state, - location: { ...state.location, value: action.payload.value } + cargo: state.cargo.filter(item => item.id !== action.payload.item.id) } case 'SET_SHIP_LOCATION_NAME': return { ...state, location: { ...state.location, name: action.payload.name } } + case 'SET_SHIP_LOCATION_VALUE': + return { + ...state, + location: { ...state.location, value: action.payload.value } + } case 'STORE_CARGO': return { ...state, cargo: [...state.cargo, action.payload.item] } - case 'REMOVE_CARGO': - const itemIndex = state.cargo.findIndex( - item => item.id === action.payload.item.id - ) - - const newCargo = Array.from(state.cargo) - newCargo.splice(itemIndex, 1) - - return { ...state, cargo: newCargo } default: return state } diff --git a/src/redux/reducers/ui.js b/src/redux/reducers/ui.js index 9ff3790..37416f8 100644 --- a/src/redux/reducers/ui.js +++ b/src/redux/reducers/ui.js @@ -5,8 +5,7 @@ const uiDefaultState = { export default (state = uiDefaultState, action) => { switch (action.type) { case 'SET_VIEW': - const { view } = action.payload - return { ...state, view } + return { ...state, view: action.payload.view } default: return state } diff --git a/src/redux/reducers/world.js b/src/redux/reducers/world.js index 0b2f2f5..baf8179 100644 --- a/src/redux/reducers/world.js +++ b/src/redux/reducers/world.js @@ -8,51 +8,41 @@ const worldDefaultState = { export default (state = worldDefaultState, action) => { switch (action.type) { case 'CLEAR_ITEMS': - const newPlanets = [...state.planets] - - newPlanets.forEach(planet => { - planet.items = [] - }) - - return { ...state, planets: newPlanets } + return { + ...state, + planets: state.planets.map(planet => ({ ...planet, items: [] })) + } case 'REFRESH_ITEMS': - const additionalPlanets = [...state.planets] - - additionalPlanets.forEach(planet => { - planet.items = generateItems() - }) - - return { ...state, planets: additionalPlanets } - case 'SET_PLANETS': - const { planets } = action.payload - - return { ...state, planets } - case 'SET_TIMER_RUNNING': - const { isTimerRunning } = action.payload - - return { ...state, isTimerRunning } + return { + ...state, + planets: state.planets.map(planet => ({ + ...planet, + items: generateItems(state.planets) + })) + } case 'REMOVE_ITEM': const { item } = action.payload - const updatedPlanets = [] - - state.planets.forEach(planet => { + const updatedPlanets = state.planets.map(planet => { const { isHomePlanet, items, location, name } = planet - const planetContainsItem = items.includes(item) - if (planetContainsItem) { - const newPlanetObj = { + + if (planet.items.includes(item)) { + return { isHomePlanet, items: items.filter(currentItem => item !== currentItem), location, name } - updatedPlanets.push(newPlanetObj) } else { - updatedPlanets.push(planet) + return planet } }) return { ...state, planets: updatedPlanets } + case 'SET_PLANETS': + return { ...state, planets: action.payload.planets } + case 'SET_TIMER_RUNNING': + return { ...state, isTimerRunning: action.payload.isTimerRunning } default: return state }