🗑️ allow question askers and moderators to delete pool questions
Browse files at this commit12aead7b34ef4eca6199bf3808a6d73fef936d3aParent: 27e0370- feat
- 🗑️ allow question askers and moderators to delete pool questions
2 changed files +64 -15
src/pages/question-detail-page.tsxmodified+10 -8
| @@ -105,18 +105,20 @@ function QuestionDetailContent() { | |||
| 105 | <span>{new Date(q().asked_at).toLocaleString()}</span> | 105 | <span>{new Date(q().asked_at).toLocaleString()}</span> |
| 106 | </div> | 106 | </div> |
| 107 | </div> | 107 | </div> |
| 108 | - <Show when={q().status === "pending" && isModerator()}> | 108 | + <div class="flex flex-wrap items-center gap-2"> |
| 109 | - <div class="flex gap-2"> | 109 | + <Show when={q().status === "pending" && isModerator()}> |
| 110 | - <Button variant="destructive" onClick={() => setDeleteConfirmQ(true)}> | ||
| 111 | - <IconX class="mr-2 h-4 w-4" /> | ||
| 112 | - {t("common.reject")} | ||
| 113 | - </Button> | ||
| 114 | <Button onClick={handleApprove}> | 110 | <Button onClick={handleApprove}> |
| 115 | <IconCheck class="mr-2 h-4 w-4" /> | 111 | <IconCheck class="mr-2 h-4 w-4" /> |
| 116 | {t("common.approve")} | 112 | {t("common.approve")} |
| 117 | </Button> | 113 | </Button> |
| 118 | - </div> | 114 | + </Show> |
| 119 | - </Show> | 115 | + <Show when={q().asker.id === auth.user()?.id || isModerator()}> |
| 116 | + <Button variant="destructive" onClick={() => setDeleteConfirmQ(true)}> | ||
| 117 | + <IconTrash class="mr-2 h-4 w-4" /> | ||
| 118 | + {q().status === "pending" && isModerator() ? t("common.reject") : t("common.delete")} | ||
| 119 | + </Button> | ||
| 120 | + </Show> | ||
| 121 | + </div> | ||
| 120 | </div> | 122 | </div> |
| 121 | 123 | ||
| 122 | {error() && <p class="rounded-md bg-destructive/10 px-3 py-2 text-sm text-destructive">{error()}</p>} | 124 | {error() && <p class="rounded-md bg-destructive/10 px-3 py-2 text-sm text-destructive">{error()}</p>} |
src/pages/questions-page.tsxmodified+54 -7
| @@ -1,6 +1,6 @@ | |||
| 1 | import { For, Show, Suspense, createEffect, createResource, createSignal, lazy } from "solid-js"; | 1 | import { For, Show, Suspense, createEffect, createResource, createSignal, lazy } from "solid-js"; |
| 2 | import { Link, useLocation, useNavigate, useSearch } from "@tanstack/solid-router"; | 2 | import { Link, useLocation, useNavigate, useSearch } from "@tanstack/solid-router"; |
| 3 | -import { getQuestions, postQuestion } from "@/api/shared"; | 3 | +import { getQuestions, postQuestion, deleteQuestionById } from "@/api/shared"; |
| 4 | import { getSettings } from "@/api/settings"; | 4 | import { getSettings } from "@/api/settings"; |
| 5 | import { formatApiError } from "@/api/client"; | 5 | import { formatApiError } from "@/api/client"; |
| 6 | import { RouteGuard } from "@/components/layout/route-guard"; | 6 | import { RouteGuard } from "@/components/layout/route-guard"; |
| @@ -9,11 +9,13 @@ import { PageSpinner } from "@/components/ui/page-spinner"; | |||
| 9 | import { EmptyState } from "@/components/ui/empty-state"; | 9 | import { EmptyState } from "@/components/ui/empty-state"; |
| 10 | import { Button } from "@/components/ui/button"; | 10 | import { Button } from "@/components/ui/button"; |
| 11 | import { FormDialog } from "@/components/ui/form-dialog"; | 11 | import { FormDialog } from "@/components/ui/form-dialog"; |
| 12 | +import { ConfirmDialog } from "@/components/ui/confirm-dialog"; | ||
| 12 | import { Label } from "@/components/ui/label"; | 13 | import { Label } from "@/components/ui/label"; |
| 13 | import { Input } from "@/components/ui/input"; | 14 | import { Input } from "@/components/ui/input"; |
| 14 | import { Textarea } from "@/components/ui/textarea"; | 15 | import { Textarea } from "@/components/ui/textarea"; |
| 15 | -import { IconPlus, IconClock, IconCheck, IconPhoto, IconX, IconEdit } from "@/components/ui/icons"; | 16 | +import { IconPlus, IconClock, IconCheck, IconPhoto, IconX, IconEdit, IconTrash } from "@/components/ui/icons"; |
| 16 | import { personLabel } from "@/lib/person"; | 17 | import { personLabel } from "@/lib/person"; |
| 18 | +import { hasMinRole } from "@/lib/roles"; | ||
| 17 | 19 | ||
| 18 | import { ErrorAlert } from "@/components/ui/error-alert"; | 20 | import { ErrorAlert } from "@/components/ui/error-alert"; |
| 19 | import { showToast } from "@/components/ui/toast"; | 21 | import { showToast } from "@/components/ui/toast"; |
| @@ -48,12 +50,31 @@ function QuestionsContent() { | |||
| 48 | ); | 50 | ); |
| 49 | 51 | ||
| 50 | const [askOpen, setAskOpen] = createSignal(location().searchStr.includes("action=new")); | 52 | const [askOpen, setAskOpen] = createSignal(location().searchStr.includes("action=new")); |
| 53 | + const [questionToDelete, setQuestionToDelete] = createSignal<any | null>(null); | ||
| 54 | + | ||
| 51 | createEffect(() => { | 55 | createEffect(() => { |
| 52 | if (location().searchStr.includes("action=new")) { | 56 | if (location().searchStr.includes("action=new")) { |
| 53 | setAskOpen(true); | 57 | setAskOpen(true); |
| 54 | } | 58 | } |
| 55 | }); | 59 | }); |
| 56 | 60 | ||
| 61 | + const canDelete = (question: any) => | ||
| 62 | + question.asker.id === auth.user()?.id || hasMinRole(auth.user()?.role, "teacher"); | ||
| 63 | + | ||
| 64 | + const handleDeleteConfirmed = async () => { | ||
| 65 | + const q = questionToDelete(); | ||
| 66 | + if (!q) return; | ||
| 67 | + try { | ||
| 68 | + await deleteQuestionById(q.id); | ||
| 69 | + showToast({ title: t("common.deleted") }); | ||
| 70 | + setQuestionToDelete(null); | ||
| 71 | + refetch(); | ||
| 72 | + } catch (err: unknown) { | ||
| 73 | + showToast({ title: formatApiError(err) }); | ||
| 74 | + setQuestionToDelete(null); | ||
| 75 | + } | ||
| 76 | + }; | ||
| 77 | + | ||
| 57 | return ( | 78 | return ( |
| 58 | <div class="space-y-6"> | 79 | <div class="space-y-6"> |
| 59 | <PageHeader | 80 | <PageHeader |
| @@ -95,8 +116,8 @@ function QuestionsContent() { | |||
| 95 | <div class="divide-y divide-border"> | 116 | <div class="divide-y divide-border"> |
| 96 | <For each={list()}> | 117 | <For each={list()}> |
| 97 | {(question) => ( | 118 | {(question) => ( |
| 98 | - <Link to="/questions/$id" params={{ id: question.id }} class="group flex flex-col gap-2 p-4 transition-colors hover:bg-muted/40 sm:flex-row sm:items-center sm:justify-between"> | 119 | + <div class="group flex flex-col gap-2 p-4 transition-colors hover:bg-muted/40 sm:flex-row sm:items-center sm:justify-between"> |
| 99 | - <div class="min-w-0 flex-1"> | 120 | + <Link to="/questions/$id" params={{ id: question.id }} class="min-w-0 flex-1"> |
| 100 | <div class="flex items-center gap-2"> | 121 | <div class="flex items-center gap-2"> |
| 101 | <h3 class="truncate text-base font-semibold text-foreground group-hover:text-primary transition-colors">{question.title}</h3> | 122 | <h3 class="truncate text-base font-semibold text-foreground group-hover:text-primary transition-colors">{question.title}</h3> |
| 102 | <Show when={question.image}> | 123 | <Show when={question.image}> |
| @@ -114,16 +135,34 @@ function QuestionsContent() { | |||
| 114 | <span>•</span> | 135 | <span>•</span> |
| 115 | <span>{new Date(question.asked_at).toLocaleDateString()}</span> | 136 | <span>{new Date(question.asked_at).toLocaleDateString()}</span> |
| 116 | </div> | 137 | </div> |
| 117 | - </div> | 138 | + </Link> |
| 118 | - <div class="mt-2 sm:mt-0 sm:pl-4"> | 139 | + |
| 140 | + <div class="mt-2 flex items-center gap-3 sm:mt-0 sm:pl-4 shrink-0"> | ||
| 119 | <Show | 141 | <Show |
| 120 | when={question.status === "approved"} | 142 | when={question.status === "approved"} |
| 121 | fallback={<span class="inline-flex items-center rounded-full bg-amber-500/10 px-2.5 py-0.5 text-xs font-medium text-amber-600 dark:text-amber-400"><IconClock class="mr-1 h-3 w-3" /> {t("pool.pending")}</span>} | 143 | fallback={<span class="inline-flex items-center rounded-full bg-amber-500/10 px-2.5 py-0.5 text-xs font-medium text-amber-600 dark:text-amber-400"><IconClock class="mr-1 h-3 w-3" /> {t("pool.pending")}</span>} |
| 122 | > | 144 | > |
| 123 | <span class="inline-flex items-center rounded-full bg-emerald-500/10 px-2.5 py-0.5 text-xs font-medium text-emerald-600 dark:text-emerald-400"><IconCheck class="mr-1 h-3 w-3" /> {t("pool.approved")}</span> | 145 | <span class="inline-flex items-center rounded-full bg-emerald-500/10 px-2.5 py-0.5 text-xs font-medium text-emerald-600 dark:text-emerald-400"><IconCheck class="mr-1 h-3 w-3" /> {t("pool.approved")}</span> |
| 124 | </Show> | 146 | </Show> |
| 147 | + | ||
| 148 | + <Show when={canDelete(question)}> | ||
| 149 | + <Button | ||
| 150 | + type="button" | ||
| 151 | + variant="ghost" | ||
| 152 | + size="sm" | ||
| 153 | + class="h-8 w-8 p-0 text-muted-foreground hover:bg-destructive/10 hover:text-destructive rounded-lg" | ||
| 154 | + title={t("common.delete")} | ||
| 155 | + onClick={(e) => { | ||
| 156 | + e.preventDefault(); | ||
| 157 | + e.stopPropagation(); | ||
| 158 | + setQuestionToDelete(question); | ||
| 159 | + }} | ||
| 160 | + > | ||
| 161 | + <IconTrash class="h-4 w-4" /> | ||
| 162 | + </Button> | ||
| 163 | + </Show> | ||
| 125 | </div> | 164 | </div> |
| 126 | - </Link> | 165 | + </div> |
| 127 | )} | 166 | )} |
| 128 | </For> | 167 | </For> |
| 129 | </div> | 168 | </div> |
| @@ -143,6 +182,14 @@ function QuestionsContent() { | |||
| 143 | }} | 182 | }} |
| 144 | /> | 183 | /> |
| 145 | </Show> | 184 | </Show> |
| 185 | + | ||
| 186 | + <ConfirmDialog | ||
| 187 | + open={!!questionToDelete()} | ||
| 188 | + onOpenChange={(open) => !open && setQuestionToDelete(null)} | ||
| 189 | + title={t("common.delete")} | ||
| 190 | + description="Bu soruyu silmek istediğinizden emin misiniz?" | ||
| 191 | + onConfirm={handleDeleteConfirmed} | ||
| 192 | + /> | ||
| 146 | </div> | 193 | </div> |
| 147 | ); | 194 | ); |
| 148 | } | 195 | } |