diff --git a/firestore.rules b/firestore.rules index d7149344..9d10dc61 100644 --- a/firestore.rules +++ b/firestore.rules @@ -10,6 +10,7 @@ service cloud.firestore { match /contracts/{contractId} { 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; } diff --git a/web/components/contract-feed.tsx b/web/components/contract-feed.tsx index 3e028e76..b5cfca3d 100644 --- a/web/components/contract-feed.tsx +++ b/web/components/contract-feed.tsx @@ -16,7 +16,7 @@ import { Comment, mapCommentsByBetId } from '../lib/firebase/comments' import dayjs from 'dayjs' import relativeTime from 'dayjs/plugin/relativeTime' 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 { Linkify } from './linkify' import { Row } from './layout/row' @@ -136,8 +136,10 @@ export function ContractDescription(props: { async function saveDescription(e: any) { e.preventDefault() 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()) } diff --git a/web/lib/firebase/contracts.ts b/web/lib/firebase/contracts.ts index 891831c4..4300a2db 100644 --- a/web/lib/firebase/contracts.ts +++ b/web/lib/firebase/contracts.ts @@ -11,6 +11,7 @@ import { onSnapshot, orderBy, getDoc, + updateDoc, } from 'firebase/firestore' import dayjs from 'dayjs' import { Bet, getRecentBets } from './bets' @@ -71,6 +72,14 @@ export async function setContract(contract: Contract) { await setDoc(docRef, contract) } +export async function updateContract( + contractId: string, + update: Partial +) { + const docRef = doc(db, 'contracts', contractId) + await updateDoc(docRef, update) +} + export async function pushNewContract(contract: Omit) { const newContractRef = doc(contractCollection) const fullContract: Contract = { ...contract, id: newContractRef.id }