Skip to content

Commit

Permalink
Merge 3c7206f into b236b3a
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Lee committed Aug 21, 2019
2 parents b236b3a + 3c7206f commit b551c20
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
15 changes: 13 additions & 2 deletions src/components/ItemTimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import React, { useState, useEffect } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import moment from 'moment'
import { setTimerRunning } from '../redux/actions/world'
import {
setTimerRunning,
clearItems,
refreshItems
} from '../redux/actions/world'

const ItemTimer = ({ handleTimerStarted, handleTimerStopped, world }) => {
const { isTimerRunning } = world
Expand Down Expand Up @@ -42,7 +46,14 @@ const mapStateToProps = ({ world }) => ({ world })

const mapDispatchToProps = dispatch => ({
handleTimerStarted: () => dispatch(setTimerRunning(true)),
handleTimerStopped: () => dispatch(setTimerRunning(false))
handleTimerStopped: () => {
// * Clear all items from planets
dispatch(clearItems())
// * Put new items on planets
dispatch(refreshItems())
// * Tell Redux the timer is no longer running
dispatch(setTimerRunning(false))
}
})

export default connect(
Expand Down
6 changes: 6 additions & 0 deletions src/redux/actions/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
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'

// * ACTION GENERATORS
export const setPlanets = planets => ({
Expand All @@ -21,6 +23,10 @@ export const removeItem = item => ({
}
})

export const clearItems = () => ({ type: CLEAR_ITEMS })

export const refreshItems = () => ({ type: REFRESH_ITEMS })

// * PROMISES
// const thing = () => {
// return new Promise((resolve, reject) => {
Expand Down
18 changes: 18 additions & 0 deletions src/redux/reducers/world.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import { generateItems } from '../../util'

const worldDefaultState = {
isTimerRunning: false,
planets: []
}

export default (state = worldDefaultState, action) => {
switch (action.type) {
case 'CLEAR_ITEMS':
const newPlanets = [...state.planets]

newPlanets.forEach(planet => {
planet.items = []
})

return { ...state, planets: newPlanets }
case 'REFRESH_ITEMS':
const additionalPlanets = [...state.planets]

additionalPlanets.forEach(planet => {
planet.items = generateItems()
})

return { ...state, planets: additionalPlanets }
case 'SET_PLANETS':
const { planets } = action.payload

Expand Down
25 changes: 15 additions & 10 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ const getPlanetName = () => {
return planet
}

export const generateItems = () => {
const items = []

for (let i = 0; i < 5; i++) {
const item = Object.assign(
{},
itemList[Math.floor(Math.random() * itemList.length)],
{ id: uuidv4() }
)
items.push(item)
}
return items
}

export const generatePlanets = () => {
const planets = []

for (let i = 0; i < 3; i++) {
const isHomePlanet = i === 0
const items = []
const items = generateItems()
const location = Math.floor(Math.random() * 100 + 1)
const name = getPlanetName()

for (let j = 0; j < 5; j++) {
const item = Object.assign(
{},
itemList[Math.floor(Math.random() * itemList.length)],
{ id: uuidv4() }
)
items.push(item)
}

planets.push({ isHomePlanet, items, location, name })
}

Expand Down

0 comments on commit b551c20

Please sign in to comment.