2022-05-09 13:04:36 +00:00
|
|
|
import { listContracts } from 'web/lib/firebase/contracts'
|
2022-04-28 23:01:50 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { User } from 'common/user'
|
2022-04-28 23:01:50 +00:00
|
|
|
|
2022-05-17 16:05:51 +00:00
|
|
|
let sessionCreatedContractToday = true
|
2022-05-17 15:32:10 +00:00
|
|
|
|
2022-04-28 23:01:50 +00:00
|
|
|
export const useHasCreatedContractToday = (user: User | null | undefined) => {
|
2022-05-17 15:32:10 +00:00
|
|
|
const [hasCreatedContractToday, setHasCreatedContractToday] = useState(
|
|
|
|
sessionCreatedContractToday
|
|
|
|
)
|
2022-04-28 23:01:50 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// Uses utc time like the server.
|
2022-04-29 01:31:43 +00:00
|
|
|
const utcTimeString = new Date().toISOString()
|
|
|
|
const todayAtMidnight = new Date(utcTimeString).setUTCHours(0, 0, 0, 0)
|
2022-04-28 23:01:50 +00:00
|
|
|
|
|
|
|
async function listUserContractsForToday() {
|
|
|
|
if (!user) return
|
|
|
|
|
|
|
|
const contracts = await listContracts(user.id)
|
|
|
|
const todayContracts = contracts.filter(
|
|
|
|
(contract) => contract.createdTime > todayAtMidnight
|
|
|
|
)
|
2022-05-17 15:32:10 +00:00
|
|
|
|
|
|
|
sessionCreatedContractToday = todayContracts.length > 0
|
|
|
|
setHasCreatedContractToday(sessionCreatedContractToday)
|
2022-04-28 23:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listUserContractsForToday()
|
|
|
|
}, [user])
|
|
|
|
|
|
|
|
return hasCreatedContractToday
|
|
|
|
}
|