Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Ability to refresh items on planets. #25

Merged
merged 1 commit into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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