Skip to content

Commit

Permalink
Utils refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bgonp committed Apr 4, 2021
1 parent ee1b594 commit f47e11c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 51 deletions.
11 changes: 4 additions & 7 deletions src/hooks/useRandomNumbers.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { useCallback, useState } from 'react'

import { BOUNDS, MAX_QUANTITY, MIN_QUANTITY } from '@constants/numbers'
import { numberGenerator } from '@utils/numberGenerator'
import { randomNumber } from '@utils/randomNumber'
import { getRandomNumbersFactory } from '@utils/randomNumbers'

const generator = numberGenerator(...BOUNDS)
const quantity = { min: MIN_QUANTITY, max: MAX_QUANTITY }
const getNextNumbers = () => [...Array(randomNumber(quantity))].map(() => generator.next().value)
const getRandomNumbers = getRandomNumbersFactory(MIN_QUANTITY, MAX_QUANTITY, BOUNDS)

const useRandomNumbers = () => {
const [numbers, setNumbers] = useState(getNextNumbers)
const [numbers, setNumbers] = useState(getRandomNumbers)

const nextNumbers = useCallback(() => {
setNumbers(getNextNumbers())
setNumbers(getRandomNumbers())
}, [])

return [numbers, nextNumbers]
Expand Down
26 changes: 25 additions & 1 deletion src/services/storage.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import { firestore } from '@utils/firebase'
import { normalizeAttempts } from '@utils/normalizeAttempts'
import { twoDigits } from '@utils/twoDigits'

const attemptsRef = firestore.collection('attempts')

const parseDate = (fetchedDate) => {
const date = fetchedDate.toDate()
const year = date.getFullYear()
const month = twoDigits(date.getMonth() + 1)
const day = twoDigits(date.getDate())
return `${year}-${month}-${day}`
}

const normalizeAttempts = (snapshot) => {
const attempts = {}
snapshot.forEach(document => {
const data = document.data()
if (!data.end) return

const date = parseDate(data.end)
if (!attempts[date]) attempts[date] = [0, 0]

attempts[date][0]++
if (data.success === true) attempts[date][1]++
})

return attempts
}

export const fetchAttempts = async (uid, initDate, finishDate) => {
const snapshot = await attemptsRef
.where('uid', '==', uid)
Expand Down
25 changes: 0 additions & 25 deletions src/utils/normalizeAttempts.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/utils/numberGenerator.js

This file was deleted.

4 changes: 0 additions & 4 deletions src/utils/randomNumber.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/utils/randomNumbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const getRandomNumber = (min, max) =>
min + Math.floor(Math.random() * (max - min + 1))

const runOneOf = (callbacks) => {
const index = getRandomNumber(0, callbacks.length - 1)
return callbacks[index]()
}

export const getRandomNumbersFactory = (minQuantity, maxQuantity, bounds) => {
const callbacks = bounds.map(([min, max]) => () => getRandomNumber(min, max))

return () => {
const quantity = getRandomNumber(minQuantity, maxQuantity)
return [...Array(quantity)].map(() => runOneOf(callbacks))
}
}
7 changes: 0 additions & 7 deletions src/utils/runOneOf.js

This file was deleted.

1 comment on commit f47e11c

@vercel
Copy link

@vercel vercel bot commented on f47e11c Apr 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.