Skip to content

Commit

Permalink
remove question api
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshal Ranjhani authored and Harshal Ranjhani committed Apr 23, 2024
1 parent db0eb05 commit 03300e8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/api/user/removeQuestion/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// api route to get user details

import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { cookies } from 'next/headers'

export async function POST(request: Request) {
const cookieStore = cookies()
const supabase = createRouteHandlerClient({
cookies: () => cookieStore
})

const body = await request.json()
const { user_id, question_id } = body

// delete the question from the questions table where the question_id field matches the question_id passed in the request and the user field matches the user_id passed in the request
const { data } = await supabase
.from('questions')
.delete()
.match({ id: question_id, user: user_id })
.throwOnError()

return new Response(JSON.stringify(data), { status: 200 })
}
34 changes: 34 additions & 0 deletions components/questions-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ import { IconCheck, IconClose, IconEdit, IconExternalLink } from './ui/icons'
import EditQuestion from './edit-question-dialog'
import toast from 'react-hot-toast'

const removeQuestion = async (user_id: string, question_id: string) => {
try {
const response = await fetch('/api/user/removeQuestion', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
user_id,
question_id
})
})
if (!response.ok) {
throw new Error('Failed to remove question')
}
const data = await response.json()
toast.success('Question removed!')
} catch (e: any) {
toast.error(e.message)
}
}

export type Question = {
id: string
created_at: string
Expand Down Expand Up @@ -194,6 +216,18 @@ export const columns: ColumnDef<Question>[] = [
<DropdownMenuItem>
<EditQuestion question={question} buttonTitle="Edit" />
</DropdownMenuItem>
<DropdownMenuItem>
<Button
variant="outline"
className="w-[100%]"
onClick={() => {
removeQuestion(question.user, question.id)
}}
>
{' '}
Remove
</Button>
</DropdownMenuItem>
{/* <DropdownMenuItem>Remove Question</DropdownMenuItem> */}
</DropdownMenuContent>
</DropdownMenu>
Expand Down

0 comments on commit 03300e8

Please sign in to comment.