Allow users a free daily market

This commit is contained in:
Ian Philips 2022-04-28 09:49:42 -06:00
parent 53c79f41a3
commit 9b0d96cfa1
4 changed files with 73 additions and 10 deletions

View File

@ -1,7 +1,7 @@
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { query, where } from 'firebase/firestore'
import { chargeUser, getUser } from './utils'
import {
Binary,
@ -109,7 +109,16 @@ export const createContract = functions
tags ?? []
)
if (ante) await chargeUser(creator.id, ante)
// uses utc time on server:
const today = new Date().setHours(0, 0, 0, 0)
const userContractsCreatedTodaySnapshot = await firestore
.collection(`contracts`)
.where('creatorId', '==', userId)
.where('createdTime', '>=', today)
.get()
const isFree = userContractsCreatedTodaySnapshot.size === 0
if (!isFree && ante) await chargeUser(creator.id, ante)
await contractRef.create(contract)

View File

@ -19,6 +19,13 @@ import { firebaseLogin, firebaseLogout } from '../../lib/firebase/users'
import { ManifoldLogo } from './manifold-logo'
import { MenuButton } from './menu'
import { getNavigationOptions, ProfileSummary } from './profile-menu'
import { CreatorContractsList } from '../contract/contracts-list'
import {
Contract,
listContracts,
userHasCreatedContractToday,
} from '../../lib/firebase/contracts'
import { useState } from 'react'
const navigation = [
{ name: 'Home', href: '/home', icon: HomeIcon },
@ -96,6 +103,11 @@ export default function Sidebar() {
const user = useUser()
let folds = useFollowedFolds(user) || []
folds = _.sortBy(folds, 'followCount').reverse()
const [deservesDailyFreeMarket, setDeservesDailyFreeMarket] = useState(false)
user &&
userHasCreatedContractToday(user.id).then((result) => {
setDeservesDailyFreeMarket(result)
})
const navigationOptions = user === null ? signedOutNavigation : navigation
const mobileNavigationOptions =
@ -159,10 +171,24 @@ export default function Sidebar() {
/>
</div>
{/*// check their markets for if any have a created time of today, if they haven't made a market today, let them know they get a free market*/}
{deservesDailyFreeMarket ? (
<div className=" text-primary mt-4 text-center">
Use your daily free market!
</div>
) : (
<div />
)}
{/*// check their markets for if any have a created time of today, if they haven't made a market today, give let them know they get a free market*/}
{user && (
<Link href={'/create'}>
<button className="btn btn-primary btn-md mt-4">Create Market</button>
</Link>
<div className={'aligncenter flex justify-center'}>
<Link href={'/create'}>
<button className="btn btn-primary btn-md mt-4">
Create Market
</button>
</Link>
</div>
)}
</nav>
)

View File

@ -121,6 +121,19 @@ export async function listAllContracts(): Promise<Contract[]> {
return snapshot.docs.map((doc) => doc.data() as Contract)
}
export async function userHasCreatedContractToday(
userId: string
): Promise<boolean> {
// uses utc time like the server
const todayAtMidnight = dayjs.utc().startOf('day').valueOf()
return listContracts(userId).then((contracts) => {
const todayContracts = contracts.filter((contract) => {
return contract.createdTime > todayAtMidnight
})
return todayContracts.length === 0
})
}
export function listenForContracts(
setContracts: (contracts: Contract[]) => void
) {

View File

@ -6,7 +6,11 @@ import Textarea from 'react-expanding-textarea'
import { Spacer } from '../components/layout/spacer'
import { useUser } from '../hooks/use-user'
import { Contract, contractPath } from '../lib/firebase/contracts'
import {
Contract,
contractPath,
userHasCreatedContractToday,
} from '../lib/firebase/contracts'
import { createContract } from '../lib/firebase/api-call'
import { FIXED_ANTE, MINIMUM_ANTE } from '../../common/antes'
import { InfoTooltip } from '../components/info-tooltip'
@ -70,6 +74,13 @@ export function NewContract(props: { question: string; tag?: string }) {
const tags = parseWordsAsTags(tagText)
const [ante, setAnte] = useState(FIXED_ANTE)
const [deservesDailyFreeMarket, setDeservesDailyFreeMarket] = useState(false)
creator &&
userHasCreatedContractToday(creator.id).then((result) => {
setDeservesDailyFreeMarket(result)
})
// useEffect(() => {
// if (ante === null && creator) {
// const initialAnte = creator.balance < 100 ? MINIMUM_ANTE : 100
@ -241,10 +252,14 @@ export function NewContract(props: { question: string; tag?: string }) {
text={`Cost to create your market. This amount is used to subsidize trading.`}
/>
</label>
<div className="label-text text-neutral pl-1">{formatMoney(ante)}</div>
{ante > balance && (
{deservesDailyFreeMarket ? (
<div className="label-text text-primary pl-1">FREE</div>
) : (
<div className="label-text text-neutral pl-1">
{formatMoney(ante)}
</div>
)}
{!deservesDailyFreeMarket && ante > balance && (
<div className="mb-2 mt-2 mr-auto self-center whitespace-nowrap text-xs font-medium tracking-wide">
<span className="mr-2 text-red-500">Insufficient balance</span>
<button