Clean up Featured code

This commit is contained in:
Austin Chen 2022-08-25 19:42:40 -07:00 committed by mantikoros
parent ba106a258a
commit 539ef017e0
2 changed files with 29 additions and 37 deletions

View File

@ -17,6 +17,7 @@ import { useAdmin, useDev } from 'web/hooks/use-admin'
import { SiteLink } from '../site-link' import { SiteLink } from '../site-link'
import { firestoreConsolePath } from 'common/envs/constants' import { firestoreConsolePath } from 'common/envs/constants'
import { deleteField } from 'firebase/firestore' import { deleteField } from 'firebase/firestore'
import ShortToggle from '../widgets/short-toggle'
export const contractDetailsButtonClassName = export const contractDetailsButtonClassName =
'group flex items-center rounded-md px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-100 text-gray-400 hover:text-gray-500' 'group flex items-center rounded-md px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-100 text-gray-400 hover:text-gray-500'
@ -50,6 +51,21 @@ export function ContractInfoDialog(props: { contract: Contract; bets: Bet[] }) {
? 'Multiple choice' ? 'Multiple choice'
: 'Numeric' : 'Numeric'
const onFeaturedToggle = async (enabled: boolean) => {
if (
enabled &&
(contract.featuredOnHomeRank === 0 || !contract?.featuredOnHomeRank)
) {
await updateContract(id, { featuredOnHomeRank: 1 })
setFeatured(true)
} else if (!enabled && (contract?.featuredOnHomeRank ?? 0) > 0) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
await updateContract(id, { featuredOnHomeRank: deleteField() })
setFeatured(false)
}
}
return ( return (
<> <>
<button <button
@ -144,43 +160,13 @@ export function ContractInfoDialog(props: { contract: Contract; bets: Bet[] }) {
)} )}
{isAdmin && ( {isAdmin && (
<tr> <tr>
<td>Set featured</td> <td>[ADMIN] Featured</td>
<td> <td>
<select <ShortToggle
className="select select-bordered" enabled={featured}
value={featured ? 'true' : 'false'} setEnabled={setFeatured}
onChange={(e) => { onChange={onFeaturedToggle}
const newVal = e.target.value === 'true' />
if (
newVal &&
(contract.featuredOnHomeRank === 0 ||
!contract?.featuredOnHomeRank)
)
updateContract(id, {
featuredOnHomeRank: 1,
})
.then(() => {
setFeatured(true)
})
.catch(console.error)
else if (
!newVal &&
(contract?.featuredOnHomeRank ?? 0) > 0
)
updateContract(id, {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
featuredOnHomeRank: deleteField(),
})
.then(() => {
setFeatured(false)
})
.catch(console.error)
}}
>
<option value="false">false</option>
<option value="true">true</option>
</select>
</td> </td>
</tr> </tr>
)} )}

View File

@ -5,13 +5,19 @@ import clsx from 'clsx'
export default function ShortToggle(props: { export default function ShortToggle(props: {
enabled: boolean enabled: boolean
setEnabled: (enabled: boolean) => void setEnabled: (enabled: boolean) => void
onChange?: (enabled: boolean) => void
}) { }) {
const { enabled, setEnabled } = props const { enabled, setEnabled } = props
return ( return (
<Switch <Switch
checked={enabled} checked={enabled}
onChange={setEnabled} onChange={(e: boolean) => {
setEnabled(e)
if (props.onChange) {
props.onChange(e)
}
}}
className="group relative inline-flex h-5 w-10 flex-shrink-0 cursor-pointer items-center justify-center rounded-full focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2" className="group relative inline-flex h-5 w-10 flex-shrink-0 cursor-pointer items-center justify-center rounded-full focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
> >
<span className="sr-only">Use setting</span> <span className="sr-only">Use setting</span>