Skip to content

Commit

Permalink
🔀 Merge pull request #51 from alexlee-dev/feature/realtime-item-timer
Browse files Browse the repository at this point in the history
✨ ItemTimer uses real time.
  • Loading branch information
Alex Lee committed Aug 22, 2019
2 parents 6c58d81 + 217deb7 commit 3d6914c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/components/ItemTimer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import React, { useState, useEffect } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import moment from 'moment'
import {
setTimerRunning,
clearItems,
refreshItems
} from '../redux/actions/world'
import { createDuration } from '../util'

const ItemTimer = ({ handleTimerStarted, handleTimerStopped, world }) => {
const { isTimerRunning } = world

let duration = moment.duration({ minutes: 10, seconds: 0 })
let duration = createDuration()

const [timeLeft, setTimeLeft] = useState(
`${duration.minutes()} minutes ${duration.seconds()} seconds`
)
Expand Down
14 changes: 14 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { itemList, planets } from './constants'
import uuidv4 from 'uuid/v4'
import moment from 'moment'

const getPlanetName = () => {
const planet = planets[Math.floor(Math.random() * planets.length)]
Expand Down Expand Up @@ -71,3 +72,16 @@ export const saveState = state => {
console.error(error)
}
}

export const createDuration = () => {
const deadline = moment().minutes(60)
const now = moment()

const minutesLeft = deadline
.clone()
.subtract(now.minutes(), 'minutes')
.minutes()
const secondsLeft = 60 - now.seconds()

return moment.duration({ minutes: minutesLeft, seconds: secondsLeft })
}

0 comments on commit 3d6914c

Please sign in to comment.