Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { createSupabaseClient } from 'shared/supabase/init' | ||
import { APIError } from './helpers/endpoint' | ||
import { run } from 'common/supabase/utils' | ||
import { log } from 'shared/log' | ||
|
||
export const deleteGroup = async (props: { id: string } | { slug: string }) => { | ||
const db = createSupabaseClient() | ||
|
||
const q = db.from('groups').select('id') | ||
if ('id' in props) { | ||
q.eq('id', props.id) | ||
} else { | ||
q.eq('slug', props.slug) | ||
} | ||
|
||
const { data: groups } = await run(q) | ||
|
||
if (groups.length == 0) { | ||
throw new APIError(404, 'Group not found') | ||
} | ||
|
||
const id = groups[0].id | ||
|
||
// check if any contracts tagged with this | ||
const { count: contractCount } = await run( | ||
db | ||
.from('contract_groups') | ||
.select('*', { head: true, count: 'exact' }) | ||
.eq('groupId', id) | ||
) | ||
|
||
if (contractCount > 0) { | ||
throw new APIError( | ||
400, | ||
`Only topics with no questions can be deleted. There are still ${contractCount} questions tagged with this topic.` | ||
) | ||
} | ||
|
||
log('removing group members') | ||
await db.from('group_members').delete().eq('group_id', id) | ||
log('deleting group ', id) | ||
await db.from('groups').delete().eq('id', id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { type PrivacyStatusType } from 'common/group' | ||
import { useRouter } from 'next/router' | ||
import { useState } from 'react' | ||
import toast from 'react-hot-toast' | ||
import { api } from 'web/lib/firebase/api' | ||
import { Button } from '../buttons/button' | ||
import { Modal } from '../layout/modal' | ||
import { Input } from '../widgets/input' | ||
import { Title } from '../widgets/title' | ||
|
||
export function DeleteTopicModal(props: { | ||
group: { id: string; name: string; privacyStatus: PrivacyStatusType } | ||
open: boolean | ||
setOpen: (open: boolean) => void | ||
}) { | ||
const { open, setOpen } = props | ||
const { name, id, privacyStatus } = props.group | ||
|
||
const [loading, setLoading] = useState(false) | ||
const [confirm, setConfirm] = useState('') | ||
const [error, setError] = useState('') | ||
|
||
const router = useRouter() | ||
|
||
return ( | ||
<Modal | ||
open={open} | ||
setOpen={setOpen} | ||
className="bg-canvas-50 rounded-xl p-4 sm:p-6" | ||
size="md" | ||
> | ||
<Title>Delete {name}?</Title> | ||
<p className="mb-2"> | ||
Deleting a topic is permanent. All members will be removed and no one | ||
will be able to find this topic. | ||
</p> | ||
{privacyStatus === 'public' && ( | ||
<p className="mb-2"> | ||
Topics should only be deleted if they are low quality or duplicate. | ||
Ask #moderators channel on discord if you aren't sure. | ||
</p> | ||
)} | ||
|
||
<Input | ||
placeholder="Type the name of this group to confirm" | ||
className="mb-2 mt-2 w-full" | ||
value={confirm} | ||
onChange={(e) => setConfirm(e.target.value)} | ||
/> | ||
|
||
<Button | ||
onClick={() => { | ||
setLoading(true) | ||
api('group/by-id/:id/delete', { id }) | ||
.then(() => { | ||
setLoading(false) | ||
toast.success('Topic deleted') | ||
router.push('/browse') | ||
}) | ||
.catch((e) => { | ||
setLoading(false) | ||
console.error(e) | ||
setError(e.message || 'Failed to delete topic') | ||
}) | ||
}} | ||
color="red" | ||
disabled={loading || confirm != name} | ||
size="xl" | ||
className="w-full" | ||
> | ||
{loading ? 'Deleting...' : 'Delete Topic'} | ||
</Button> | ||
|
||
{error && <p className="mt-2 text-red-500">{error}</p>} | ||
</Modal> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters