Allow admins to edit questions

This commit is contained in:
Austin Chen 2022-02-09 10:58:33 -08:00
parent b0a1da62d2
commit bcc011c1fd
4 changed files with 104 additions and 66 deletions

View File

@ -23,6 +23,7 @@ service cloud.firestore {
allow read; allow read;
allow update: if request.resource.data.diff(resource.data).affectedKeys() allow update: if request.resource.data.diff(resource.data).affectedKeys()
.hasOnly(['description', 'tags', 'lowercaseTags']); .hasOnly(['description', 'tags', 'lowercaseTags']);
allow update: if isAdmin();
allow delete: if resource.data.creatorId == request.auth.uid; allow delete: if resource.data.creatorId == request.auth.uid;
} }

View File

@ -43,6 +43,7 @@ import { fromNow } from '../lib/util/time'
import BetRow from './bet-row' import BetRow from './bet-row'
import { parseTags } from '../../common/util/parse' import { parseTags } from '../../common/util/parse'
import { Avatar } from './avatar' import { Avatar } from './avatar'
import { useAdmin } from '../hooks/use-admin'
function FeedComment(props: { function FeedComment(props: {
activityItem: any activityItem: any
@ -154,66 +155,41 @@ function FeedBet(props: { activityItem: any }) {
) )
} }
export function ContractDescription(props: { function EditContract(props: {
contract: Contract text: string
isCreator: boolean onSave: (newText: string) => void
buttonText: string
}) { }) {
const { contract, isCreator } = props const [text, setText] = useState(props.text)
const [editing, setEditing] = useState(false) const [editing, setEditing] = useState(false)
const editStatement = () => `${dayjs().format('MMM D, h:mma')}: ` const onSave = (newText: string) => {
const [description, setDescription] = useState(editStatement())
// Append the new description (after a newline)
async function saveDescription(e: any) {
e.preventDefault()
setEditing(false) setEditing(false)
setText(props.text) // Reset to original text
const newDescription = `${contract.description}\n\n${description}`.trim() props.onSave(newText)
const tags = parseTags(
`${newDescription} ${contract.tags.map((tag) => `#${tag}`).join(' ')}`
)
const lowercaseTags = tags.map((tag) => tag.toLowerCase())
await updateContract(contract.id, {
description: newDescription,
tags,
lowercaseTags,
})
setDescription(editStatement())
} }
if (!isCreator && !contract.description.trim()) return null return editing ? (
<div className="mt-4">
return (
<div className="whitespace-pre-line break-words mt-2 text-gray-700">
<Linkify text={contract.description} />
<br />
{isCreator &&
(editing ? (
<form className="mt-4">
<Textarea <Textarea
className="textarea h-24 textarea-bordered w-full mb-1" className="textarea h-24 textarea-bordered w-full mb-1"
rows={3} rows={3}
value={description} value={text}
onChange={(e) => setDescription(e.target.value || '')} onChange={(e) => setText(e.target.value || '')}
autoFocus autoFocus
onFocus={(e) => onFocus={(e) =>
// Focus starts at end of description. // Focus starts at end of text.
e.target.setSelectionRange( e.target.setSelectionRange(text.length, text.length)
description.length,
description.length
)
} }
onKeyDown={(e) => { onKeyDown={(e) => {
if (e.key === 'Enter' && e.ctrlKey) { if (e.key === 'Enter' && e.ctrlKey) {
saveDescription(e) onSave(text)
} }
}} }}
/> />
<Row className="gap-2"> <Row className="gap-2">
<button <button
className="btn btn-neutral btn-outline btn-sm" className="btn btn-neutral btn-outline btn-sm"
onClick={saveDescription} onClick={() => onSave(text)}
> >
Save Save
</button> </button>
@ -224,17 +200,73 @@ export function ContractDescription(props: {
Cancel Cancel
</button> </button>
</Row> </Row>
</form> </div>
) : ( ) : (
<Row> <Row>
<button <button
className="btn btn-neutral btn-outline btn-sm mt-4" className="btn btn-neutral btn-outline btn-sm mt-4"
onClick={() => setEditing(true)} onClick={() => setEditing(true)}
> >
Add to description {props.buttonText}
</button> </button>
</Row> </Row>
))} )
}
export function ContractDescription(props: {
contract: Contract
isCreator: boolean
}) {
const { contract, isCreator } = props
const descriptionTimestamp = () => `${dayjs().format('MMM D, h:mma')}: `
const isAdmin = useAdmin()
// Append the new description (after a newline)
async function saveDescription(newText: string) {
const newDescription = `${contract.description}\n\n${newText}`.trim()
const tags = parseTags(
`${newDescription} ${contract.tags.map((tag) => `#${tag}`).join(' ')}`
)
const lowercaseTags = tags.map((tag) => tag.toLowerCase())
await updateContract(contract.id, {
description: newDescription,
tags,
lowercaseTags,
})
}
if (!isCreator && !contract.description.trim()) return null
return (
<div className="whitespace-pre-line break-words mt-2 text-gray-700">
<Linkify text={contract.description} />
<br />
{isCreator && (
<EditContract
// Note: Because descriptionTimestamp is called once, later edits use
// a stale timestamp. Ideally this is a function that gets called when
// isEditing is set to true.
text={descriptionTimestamp()}
onSave={saveDescription}
buttonText="Add to description"
/>
)}
{isAdmin && (
<EditContract
text={contract.question}
onSave={(question) => updateContract(contract.id, { question })}
buttonText="ADMIN: Edit question"
/>
)}
{/* {isAdmin && (
<EditContract
text={contract.createdTime.toString()}
onSave={(time) =>
updateContract(contract.id, { createdTime: Number(time) })
}
buttonText="ADMIN: Edit createdTime"
/>
)} */}
</div> </div>
) )
} }

12
web/hooks/use-admin.ts Normal file
View File

@ -0,0 +1,12 @@
import { useUser } from './use-user'
export const useAdmin = () => {
const user = useUser()
const adminIds = [
'igi2zGXsfxYPgB0DJTXVJVmwCOr2', // Austin
'5LZ4LgYuySdL1huCWe7bti02ghx2', // James
'tlmGNz9kjXc2EteizMORes4qvWl2', // Stephen
'IPTOzEqrpkWmEzh6hwvAyY9PqFb2', // Manifold
]
return adminIds.includes(user?.id || '')
}

View File

@ -8,6 +8,7 @@ import { useUser } from '../hooks/use-user'
import Custom404 from './404' import Custom404 from './404'
import { useContracts } from '../hooks/use-contracts' import { useContracts } from '../hooks/use-contracts'
import _ from 'lodash' import _ from 'lodash'
import { useAdmin } from '../hooks/use-admin'
function avatarHtml(avatarUrl: string) { function avatarHtml(avatarUrl: string) {
return `<img return `<img
@ -190,15 +191,7 @@ function ContractsTable() {
} }
export default function Admin() { export default function Admin() {
const user = useUser() return useAdmin() ? (
const adminIds = [
'igi2zGXsfxYPgB0DJTXVJVmwCOr2', // Austin
'5LZ4LgYuySdL1huCWe7bti02ghx2', // James
'tlmGNz9kjXc2EteizMORes4qvWl2', // Stephen
'IPTOzEqrpkWmEzh6hwvAyY9PqFb2', // Manifold
]
const isAdmin = adminIds.includes(user?.id || '')
return isAdmin ? (
<Page wide> <Page wide>
<UsersTable /> <UsersTable />
<ContractsTable /> <ContractsTable />