allow updating contract description

This commit is contained in:
mantikoros 2022-01-07 13:27:59 -06:00
parent e3800de2ad
commit 1b69a34270
3 changed files with 15 additions and 3 deletions

View File

@ -10,6 +10,7 @@ service cloud.firestore {
match /contracts/{contractId} { match /contracts/{contractId} {
allow read; allow read;
allow update: if resource.data.creatorId == request.auth.uid && request.resource.data.keys().hasOnly(["description"]);
allow delete: if resource.data.creatorId == request.auth.uid; allow delete: if resource.data.creatorId == request.auth.uid;
} }

View File

@ -16,7 +16,7 @@ import { Comment, mapCommentsByBetId } from '../lib/firebase/comments'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime' import relativeTime from 'dayjs/plugin/relativeTime'
import { OutcomeLabel } from './outcome-label' import { OutcomeLabel } from './outcome-label'
import { Contract, setContract } from '../lib/firebase/contracts' import { Contract, updateContract } from '../lib/firebase/contracts'
import { useUser } from '../hooks/use-user' import { useUser } from '../hooks/use-user'
import { Linkify } from './linkify' import { Linkify } from './linkify'
import { Row } from './layout/row' import { Row } from './layout/row'
@ -136,8 +136,10 @@ export function ContractDescription(props: {
async function saveDescription(e: any) { async function saveDescription(e: any) {
e.preventDefault() e.preventDefault()
setEditing(false) setEditing(false)
contract.description = `${contract.description}\n${description}`.trim()
await setContract(contract) const newDescription = `${contract.description}\n${description}`.trim()
await updateContract(contract.id, { description: newDescription })
setDescription(editStatement()) setDescription(editStatement())
} }

View File

@ -11,6 +11,7 @@ import {
onSnapshot, onSnapshot,
orderBy, orderBy,
getDoc, getDoc,
updateDoc,
} from 'firebase/firestore' } from 'firebase/firestore'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { Bet, getRecentBets } from './bets' import { Bet, getRecentBets } from './bets'
@ -71,6 +72,14 @@ export async function setContract(contract: Contract) {
await setDoc(docRef, contract) await setDoc(docRef, contract)
} }
export async function updateContract(
contractId: string,
update: Partial<Contract>
) {
const docRef = doc(db, 'contracts', contractId)
await updateDoc(docRef, update)
}
export async function pushNewContract(contract: Omit<Contract, 'id'>) { export async function pushNewContract(contract: Omit<Contract, 'id'>) {
const newContractRef = doc(contractCollection) const newContractRef = doc(contractCollection)
const fullContract: Contract = { ...contract, id: newContractRef.id } const fullContract: Contract = { ...contract, id: newContractRef.id }