Allow users a free daily market
This commit is contained in:
parent
53c79f41a3
commit
9b0d96cfa1
|
@ -1,7 +1,7 @@
|
||||||
import * as functions from 'firebase-functions'
|
import * as functions from 'firebase-functions'
|
||||||
import * as admin from 'firebase-admin'
|
import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
import { query, where } from 'firebase/firestore'
|
||||||
import { chargeUser, getUser } from './utils'
|
import { chargeUser, getUser } from './utils'
|
||||||
import {
|
import {
|
||||||
Binary,
|
Binary,
|
||||||
|
@ -109,7 +109,16 @@ export const createContract = functions
|
||||||
tags ?? []
|
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)
|
await contractRef.create(contract)
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,13 @@ import { firebaseLogin, firebaseLogout } from '../../lib/firebase/users'
|
||||||
import { ManifoldLogo } from './manifold-logo'
|
import { ManifoldLogo } from './manifold-logo'
|
||||||
import { MenuButton } from './menu'
|
import { MenuButton } from './menu'
|
||||||
import { getNavigationOptions, ProfileSummary } from './profile-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 = [
|
const navigation = [
|
||||||
{ name: 'Home', href: '/home', icon: HomeIcon },
|
{ name: 'Home', href: '/home', icon: HomeIcon },
|
||||||
|
@ -96,6 +103,11 @@ export default function Sidebar() {
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
let folds = useFollowedFolds(user) || []
|
let folds = useFollowedFolds(user) || []
|
||||||
folds = _.sortBy(folds, 'followCount').reverse()
|
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 navigationOptions = user === null ? signedOutNavigation : navigation
|
||||||
const mobileNavigationOptions =
|
const mobileNavigationOptions =
|
||||||
|
@ -159,10 +171,24 @@ export default function Sidebar() {
|
||||||
/>
|
/>
|
||||||
</div>
|
</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 && (
|
{user && (
|
||||||
|
<div className={'aligncenter flex justify-center'}>
|
||||||
<Link href={'/create'}>
|
<Link href={'/create'}>
|
||||||
<button className="btn btn-primary btn-md mt-4">Create Market</button>
|
<button className="btn btn-primary btn-md mt-4">
|
||||||
|
Create Market
|
||||||
|
</button>
|
||||||
</Link>
|
</Link>
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
</nav>
|
</nav>
|
||||||
)
|
)
|
||||||
|
|
|
@ -121,6 +121,19 @@ export async function listAllContracts(): Promise<Contract[]> {
|
||||||
return snapshot.docs.map((doc) => doc.data() as 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(
|
export function listenForContracts(
|
||||||
setContracts: (contracts: Contract[]) => void
|
setContracts: (contracts: Contract[]) => void
|
||||||
) {
|
) {
|
||||||
|
|
|
@ -6,7 +6,11 @@ import Textarea from 'react-expanding-textarea'
|
||||||
|
|
||||||
import { Spacer } from '../components/layout/spacer'
|
import { Spacer } from '../components/layout/spacer'
|
||||||
import { useUser } from '../hooks/use-user'
|
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 { createContract } from '../lib/firebase/api-call'
|
||||||
import { FIXED_ANTE, MINIMUM_ANTE } from '../../common/antes'
|
import { FIXED_ANTE, MINIMUM_ANTE } from '../../common/antes'
|
||||||
import { InfoTooltip } from '../components/info-tooltip'
|
import { InfoTooltip } from '../components/info-tooltip'
|
||||||
|
@ -70,6 +74,13 @@ export function NewContract(props: { question: string; tag?: string }) {
|
||||||
const tags = parseWordsAsTags(tagText)
|
const tags = parseWordsAsTags(tagText)
|
||||||
|
|
||||||
const [ante, setAnte] = useState(FIXED_ANTE)
|
const [ante, setAnte] = useState(FIXED_ANTE)
|
||||||
|
|
||||||
|
const [deservesDailyFreeMarket, setDeservesDailyFreeMarket] = useState(false)
|
||||||
|
creator &&
|
||||||
|
userHasCreatedContractToday(creator.id).then((result) => {
|
||||||
|
setDeservesDailyFreeMarket(result)
|
||||||
|
})
|
||||||
|
|
||||||
// useEffect(() => {
|
// useEffect(() => {
|
||||||
// if (ante === null && creator) {
|
// if (ante === null && creator) {
|
||||||
// const initialAnte = creator.balance < 100 ? MINIMUM_ANTE : 100
|
// 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.`}
|
text={`Cost to create your market. This amount is used to subsidize trading.`}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
|
{deservesDailyFreeMarket ? (
|
||||||
<div className="label-text text-neutral pl-1">{formatMoney(ante)}</div>
|
<div className="label-text text-primary pl-1">FREE</div>
|
||||||
|
) : (
|
||||||
{ante > balance && (
|
<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">
|
<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>
|
<span className="mr-2 text-red-500">Insufficient balance</span>
|
||||||
<button
|
<button
|
||||||
|
|
Loading…
Reference in New Issue
Block a user