Remove FAQ management and the Settings initial assessment tab.
Drop FAQ routes, sidebar entry, and related admin UI; keep assessment thresholds under Content only. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
2b880f0386
commit
4f0190f301
|
|
@ -1,163 +0,0 @@
|
||||||
import http from "./http"
|
|
||||||
import { DEFAULT_TABLE_PAGE_SIZE } from "../lib/tablePagination"
|
|
||||||
import type {
|
|
||||||
CreateFAQRequest,
|
|
||||||
CreateFAQResponse,
|
|
||||||
DeleteFAQResponse,
|
|
||||||
FAQ,
|
|
||||||
FAQFilters,
|
|
||||||
FAQListData,
|
|
||||||
GetFAQResponse,
|
|
||||||
GetFAQsResponse,
|
|
||||||
UpdateFAQRequest,
|
|
||||||
UpdateFAQResponse,
|
|
||||||
} from "../types/faq.types"
|
|
||||||
|
|
||||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
||||||
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeFAQ(raw: unknown): FAQ | null {
|
|
||||||
if (!isRecord(raw)) return null
|
|
||||||
const id = Number(raw.id)
|
|
||||||
if (!Number.isFinite(id)) return null
|
|
||||||
|
|
||||||
const status = String(raw.status ?? "ACTIVE").toUpperCase()
|
|
||||||
const category = raw.category
|
|
||||||
const normalizedCategory =
|
|
||||||
category == null || String(category).trim() === "" ? null : String(category)
|
|
||||||
|
|
||||||
return {
|
|
||||||
id,
|
|
||||||
question: String(raw.question ?? ""),
|
|
||||||
answer: String(raw.answer ?? ""),
|
|
||||||
category: normalizedCategory,
|
|
||||||
display_order: Number(raw.display_order ?? 0),
|
|
||||||
status: status === "INACTIVE" ? "INACTIVE" : "ACTIVE",
|
|
||||||
created_at: String(raw.created_at ?? ""),
|
|
||||||
updated_at:
|
|
||||||
raw.updated_at == null || String(raw.updated_at).trim() === ""
|
|
||||||
? null
|
|
||||||
: String(raw.updated_at),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseFAQsList(body: unknown): FAQListData {
|
|
||||||
const empty: FAQListData = { faqs: [], total_count: 0 }
|
|
||||||
|
|
||||||
if (isRecord(body)) {
|
|
||||||
const data = body.data
|
|
||||||
if (isRecord(data) && Array.isArray(data.faqs)) {
|
|
||||||
const faqs = data.faqs
|
|
||||||
.map(normalizeFAQ)
|
|
||||||
.filter((row): row is FAQ => row !== null)
|
|
||||||
const total_count = Number(data.total_count ?? faqs.length)
|
|
||||||
return {
|
|
||||||
faqs,
|
|
||||||
total_count: Number.isFinite(total_count) ? total_count : faqs.length,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (Array.isArray(data)) {
|
|
||||||
const faqs = data.map(normalizeFAQ).filter((row): row is FAQ => row !== null)
|
|
||||||
return { faqs, total_count: faqs.length }
|
|
||||||
}
|
|
||||||
if (Array.isArray(body.faqs)) {
|
|
||||||
const faqs = body.faqs
|
|
||||||
.map(normalizeFAQ)
|
|
||||||
.filter((row): row is FAQ => row !== null)
|
|
||||||
const total_count = Number(body.total_count ?? faqs.length)
|
|
||||||
return {
|
|
||||||
faqs,
|
|
||||||
total_count: Number.isFinite(total_count) ? total_count : faqs.length,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(body)) {
|
|
||||||
const faqs = body.map(normalizeFAQ).filter((row): row is FAQ => row !== null)
|
|
||||||
return { faqs, total_count: faqs.length }
|
|
||||||
}
|
|
||||||
|
|
||||||
return empty
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseFAQMutation(body: unknown): FAQ | null {
|
|
||||||
if (isRecord(body) && body.data != null) {
|
|
||||||
return normalizeFAQ(body.data)
|
|
||||||
}
|
|
||||||
return normalizeFAQ(body)
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildFAQQuery(filters: FAQFilters = {}): Record<string, string | number> {
|
|
||||||
const params: Record<string, string | number> = {}
|
|
||||||
if (filters.status) params.status = filters.status
|
|
||||||
if (filters.category?.trim()) params.category = filters.category.trim()
|
|
||||||
params.limit = filters.limit ?? DEFAULT_TABLE_PAGE_SIZE
|
|
||||||
params.offset = filters.offset ?? 0
|
|
||||||
return params
|
|
||||||
}
|
|
||||||
|
|
||||||
/** GET /admin/faqs — list FAQs (all statuses). */
|
|
||||||
export const getFAQs = (filters: FAQFilters = {}) =>
|
|
||||||
http
|
|
||||||
.get<GetFAQsResponse>("/admin/faqs", { params: buildFAQQuery(filters) })
|
|
||||||
.then((res) => {
|
|
||||||
const parsed = parseFAQsList(res.data)
|
|
||||||
return {
|
|
||||||
...res,
|
|
||||||
data: parsed,
|
|
||||||
message: isRecord(res.data) ? String(res.data.message ?? "") : undefined,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
/** GET /admin/faqs/:id — get one FAQ for editing. */
|
|
||||||
export const getFAQById = (id: number) =>
|
|
||||||
http.get<GetFAQResponse>(`/admin/faqs/${id}`).then((res) => ({
|
|
||||||
...res,
|
|
||||||
data: parseFAQMutation(res.data),
|
|
||||||
message: isRecord(res.data) ? String(res.data.message ?? "") : undefined,
|
|
||||||
}))
|
|
||||||
|
|
||||||
/** POST /admin/faqs — create FAQ. */
|
|
||||||
export const createFAQ = (payload: CreateFAQRequest) =>
|
|
||||||
http.post<CreateFAQResponse>("/admin/faqs", payload).then((res) => ({
|
|
||||||
...res,
|
|
||||||
data: parseFAQMutation(res.data),
|
|
||||||
message: isRecord(res.data) ? String(res.data.message ?? "") : undefined,
|
|
||||||
}))
|
|
||||||
|
|
||||||
/** PUT /admin/faqs/:id — update FAQ. */
|
|
||||||
export const updateFAQ = (id: number, payload: UpdateFAQRequest) =>
|
|
||||||
http.put<UpdateFAQResponse>(`/admin/faqs/${id}`, payload).then((res) => ({
|
|
||||||
...res,
|
|
||||||
data: parseFAQMutation(res.data),
|
|
||||||
message: isRecord(res.data) ? String(res.data.message ?? "") : undefined,
|
|
||||||
}))
|
|
||||||
|
|
||||||
/** DELETE /admin/faqs/:id — hard delete FAQ. */
|
|
||||||
export const deleteFAQ = (id: number) =>
|
|
||||||
http.delete<DeleteFAQResponse>(`/admin/faqs/${id}`).then((res) => ({
|
|
||||||
...res,
|
|
||||||
message: isRecord(res.data) ? String(res.data.message ?? "") : undefined,
|
|
||||||
}))
|
|
||||||
|
|
||||||
/** GET /faqs/:id — public detail (preview only, ACTIVE FAQs). */
|
|
||||||
export const getPublicFAQById = (id: number) =>
|
|
||||||
http.get<GetFAQResponse>(`/faqs/${id}`).then((res) => ({
|
|
||||||
...res,
|
|
||||||
data: parseFAQMutation(res.data),
|
|
||||||
message: isRecord(res.data) ? String(res.data.message ?? "") : undefined,
|
|
||||||
}))
|
|
||||||
|
|
||||||
/** GET /faqs — public list (preview only, ACTIVE FAQs). */
|
|
||||||
export const getPublicFAQs = (filters: Pick<FAQFilters, "category" | "limit" | "offset"> = {}) =>
|
|
||||||
http
|
|
||||||
.get<GetFAQsResponse>("/faqs", { params: buildFAQQuery(filters) })
|
|
||||||
.then((res) => {
|
|
||||||
const parsed = parseFAQsList(res.data)
|
|
||||||
return {
|
|
||||||
...res,
|
|
||||||
data: parsed,
|
|
||||||
message: isRecord(res.data) ? String(res.data.message ?? "") : undefined,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
@ -75,8 +75,6 @@ import { AboutPage } from "../pages/AboutPage";
|
||||||
import { TermsPage } from "../pages/TermsPage";
|
import { TermsPage } from "../pages/TermsPage";
|
||||||
import { PrivacyPage } from "../pages/PrivacyPage";
|
import { PrivacyPage } from "../pages/PrivacyPage";
|
||||||
import { AccountDeletionPage } from "../pages/AccountDeletionPage";
|
import { AccountDeletionPage } from "../pages/AccountDeletionPage";
|
||||||
import { FaqsPage } from "../pages/faqs/FaqsPage";
|
|
||||||
import { EditFaqPage } from "../pages/faqs/EditFaqPage";
|
|
||||||
import { AppReviewsPage } from "../pages/ratings/AppReviewsPage";
|
import { AppReviewsPage } from "../pages/ratings/AppReviewsPage";
|
||||||
import { PersonasPage } from "../pages/personas/PersonasPage";
|
import { PersonasPage } from "../pages/personas/PersonasPage";
|
||||||
|
|
||||||
|
|
@ -352,8 +350,6 @@ export function AppRoutes() {
|
||||||
<Route path="/payments" element={<PaymentsPage />} />
|
<Route path="/payments" element={<PaymentsPage />} />
|
||||||
<Route path="/subscriptions/export" element={<SubscriptionsExportPage />} />
|
<Route path="/subscriptions/export" element={<SubscriptionsExportPage />} />
|
||||||
<Route path="/user-log" element={<UserLogPage />} />
|
<Route path="/user-log" element={<UserLogPage />} />
|
||||||
<Route path="/help/faqs" element={<FaqsPage />} />
|
|
||||||
<Route path="/help/faqs/:id/edit" element={<EditFaqPage />} />
|
|
||||||
<Route path="/app-reviews" element={<AppReviewsPage />} />
|
<Route path="/app-reviews" element={<AppReviewsPage />} />
|
||||||
<Route path="/admin/app-reviews" element={<AppReviewsPage />} />
|
<Route path="/admin/app-reviews" element={<AppReviewsPage />} />
|
||||||
<Route path="/operations/app-reviews" element={<AppReviewsPage />} />
|
<Route path="/operations/app-reviews" element={<AppReviewsPage />} />
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ import {
|
||||||
BookOpen,
|
BookOpen,
|
||||||
ChevronLeft,
|
ChevronLeft,
|
||||||
ChevronRight,
|
ChevronRight,
|
||||||
CircleHelp,
|
|
||||||
ClipboardList,
|
ClipboardList,
|
||||||
CreditCard,
|
CreditCard,
|
||||||
LayoutDashboard,
|
LayoutDashboard,
|
||||||
|
|
@ -22,7 +21,6 @@ import { cn } from "../../lib/utils";
|
||||||
import { SidebarLogo } from "./SidebarLogo";
|
import { SidebarLogo } from "./SidebarLogo";
|
||||||
import { getUnreadCount } from "../../api/notifications.api";
|
import { getUnreadCount } from "../../api/notifications.api";
|
||||||
import { useTeamPermissions } from "../../hooks/useTeamPermissions";
|
import { useTeamPermissions } from "../../hooks/useTeamPermissions";
|
||||||
import { hasFaqPermission } from "../../lib/faqPermissions";
|
|
||||||
import { hasRatingsPermission } from "../../lib/ratingsPermissions";
|
import { hasRatingsPermission } from "../../lib/ratingsPermissions";
|
||||||
import { hasPersonaPermission } from "../../lib/personasPermissions";
|
import { hasPersonaPermission } from "../../lib/personasPermissions";
|
||||||
import { hasActivityLogPermission } from "../../lib/activityLogPermissions";
|
import { hasActivityLogPermission } from "../../lib/activityLogPermissions";
|
||||||
|
|
@ -127,13 +125,6 @@ const navEntries: NavEntry[] = [
|
||||||
icon: ClipboardList,
|
icon: ClipboardList,
|
||||||
permission: "activity_logs.list",
|
permission: "activity_logs.list",
|
||||||
},
|
},
|
||||||
{
|
|
||||||
kind: "link",
|
|
||||||
label: "FAQs",
|
|
||||||
to: "/help/faqs",
|
|
||||||
icon: CircleHelp,
|
|
||||||
permission: "faqs.list",
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
kind: "link",
|
kind: "link",
|
||||||
label: "App reviews",
|
label: "App reviews",
|
||||||
|
|
@ -299,13 +290,6 @@ export function Sidebar({
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entry.kind === "link") {
|
if (entry.kind === "link") {
|
||||||
if (
|
|
||||||
entry.permission === "faqs.list" &&
|
|
||||||
!permissionsLoading &&
|
|
||||||
!hasFaqPermission("faqs.list", permissions)
|
|
||||||
) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (
|
if (
|
||||||
entry.permission === "ratings.list_by_target" &&
|
entry.permission === "ratings.list_by_target" &&
|
||||||
!permissionsLoading &&
|
!permissionsLoading &&
|
||||||
|
|
@ -336,7 +320,6 @@ export function Sidebar({
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
entry.permission &&
|
entry.permission &&
|
||||||
entry.permission !== "faqs.list" &&
|
|
||||||
entry.permission !== "ratings.list_by_target" &&
|
entry.permission !== "ratings.list_by_target" &&
|
||||||
entry.permission !== "personas.list" &&
|
entry.permission !== "personas.list" &&
|
||||||
entry.permission !== "activity_logs.list" &&
|
entry.permission !== "activity_logs.list" &&
|
||||||
|
|
|
||||||
|
|
@ -1,39 +0,0 @@
|
||||||
import { useMemo } from "react"
|
|
||||||
import { hasFaqPermission } from "../lib/faqPermissions"
|
|
||||||
import { useTeamPermissions } from "./useTeamPermissions"
|
|
||||||
|
|
||||||
export function useFaqPermissions() {
|
|
||||||
const { permissions, loading, reload } = useTeamPermissions()
|
|
||||||
|
|
||||||
const canList = useMemo(
|
|
||||||
() => hasFaqPermission("faqs.list", permissions),
|
|
||||||
[permissions],
|
|
||||||
)
|
|
||||||
const canGet = useMemo(
|
|
||||||
() => hasFaqPermission("faqs.get", permissions),
|
|
||||||
[permissions],
|
|
||||||
)
|
|
||||||
const canCreate = useMemo(
|
|
||||||
() => hasFaqPermission("faqs.create", permissions),
|
|
||||||
[permissions],
|
|
||||||
)
|
|
||||||
const canUpdate = useMemo(
|
|
||||||
() => hasFaqPermission("faqs.update", permissions),
|
|
||||||
[permissions],
|
|
||||||
)
|
|
||||||
const canDelete = useMemo(
|
|
||||||
() => hasFaqPermission("faqs.delete", permissions),
|
|
||||||
[permissions],
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
permissions,
|
|
||||||
loading,
|
|
||||||
reload,
|
|
||||||
canList,
|
|
||||||
canGet,
|
|
||||||
canCreate,
|
|
||||||
canUpdate,
|
|
||||||
canDelete,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -336,8 +336,6 @@ export function activityLogResourcePath(
|
||||||
return `/users/${resourceId}`
|
return `/users/${resourceId}`
|
||||||
case "TEAM_MEMBER":
|
case "TEAM_MEMBER":
|
||||||
return `/team/${resourceId}`
|
return `/team/${resourceId}`
|
||||||
case "FAQ":
|
|
||||||
return `/help/faqs/${resourceId}/edit`
|
|
||||||
case "PERSONA":
|
case "PERSONA":
|
||||||
return `/personas`
|
return `/personas`
|
||||||
case "QUESTION_TYPE_DEFINITION":
|
case "QUESTION_TYPE_DEFINITION":
|
||||||
|
|
|
||||||
|
|
@ -1,37 +0,0 @@
|
||||||
import type { FAQ, FAQStatus } from "../types/faq.types"
|
|
||||||
import { formatAppDateTime } from "./datetime"
|
|
||||||
|
|
||||||
export function faqStatusBadgeVariant(status: string): "success" | "secondary" {
|
|
||||||
const normalized = status.toUpperCase()
|
|
||||||
if (normalized === "ACTIVE") return "success"
|
|
||||||
return "secondary"
|
|
||||||
}
|
|
||||||
|
|
||||||
export function faqStatusLabel(status: FAQStatus | string): string {
|
|
||||||
const normalized = status.toUpperCase()
|
|
||||||
if (normalized === "ACTIVE") return "Published"
|
|
||||||
if (normalized === "INACTIVE") return "Draft"
|
|
||||||
return status
|
|
||||||
}
|
|
||||||
|
|
||||||
export function formatFaqDate(raw: string | null | undefined): string {
|
|
||||||
if (raw == null || String(raw).trim() === "") {
|
|
||||||
return "unassigned"
|
|
||||||
}
|
|
||||||
const text = String(raw)
|
|
||||||
const parsed = new Date(text)
|
|
||||||
if (Number.isNaN(parsed.getTime())) {
|
|
||||||
return text.split(" +")[0]?.trim() || text
|
|
||||||
}
|
|
||||||
return formatAppDateTime(text)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function deriveFaqCategories(faqs: FAQ[]): string[] {
|
|
||||||
return [...new Set(faqs.map((f) => f.category).filter(Boolean) as string[])].sort()
|
|
||||||
}
|
|
||||||
|
|
||||||
export function suggestNextDisplayOrder(faqs: FAQ[]): number {
|
|
||||||
if (faqs.length === 0) return 1
|
|
||||||
const max = Math.max(...faqs.map((f) => f.display_order ?? 0))
|
|
||||||
return max + 1
|
|
||||||
}
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
export function getFaqApiErrorMessage(error: unknown, fallback: string): string {
|
|
||||||
const data = (error as { response?: { data?: { message?: string; error?: string } } })
|
|
||||||
?.response?.data
|
|
||||||
return data?.error ?? data?.message ?? fallback
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isFaqForbiddenError(error: unknown): boolean {
|
|
||||||
return (error as { response?: { status?: number } })?.response?.status === 403
|
|
||||||
}
|
|
||||||
|
|
||||||
export const FAQ_FORBIDDEN_MESSAGE =
|
|
||||||
"You don't have permission to access this resource"
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
/** Team roles whose JWT maps to ADMIN/SUPER_ADMIN and bypass FAQ permission checks. */
|
|
||||||
const FAQ_BYPASS_TEAM_ROLES = new Set([
|
|
||||||
"SUPER_ADMIN",
|
|
||||||
"ADMIN",
|
|
||||||
])
|
|
||||||
|
|
||||||
export function getSessionTeamRole(): string {
|
|
||||||
return localStorage.getItem("role")?.trim() ?? ""
|
|
||||||
}
|
|
||||||
|
|
||||||
export function hasFaqRoleBypass(): boolean {
|
|
||||||
const role = getSessionTeamRole().toUpperCase()
|
|
||||||
return FAQ_BYPASS_TEAM_ROLES.has(role)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function hasFaqPermission(
|
|
||||||
permission: string,
|
|
||||||
permissions: string[],
|
|
||||||
): boolean {
|
|
||||||
if (hasFaqRoleBypass()) return true
|
|
||||||
return permissions.includes(permission)
|
|
||||||
}
|
|
||||||
|
|
@ -3,7 +3,6 @@ import React, { useEffect, useState } from "react";
|
||||||
import {
|
import {
|
||||||
Eye,
|
Eye,
|
||||||
EyeOff,
|
EyeOff,
|
||||||
Gauge,
|
|
||||||
Globe,
|
Globe,
|
||||||
Lock,
|
Lock,
|
||||||
Moon,
|
Moon,
|
||||||
|
|
@ -31,21 +30,18 @@ import type { TeamMeProfile } from "../types/team.types";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { AppVersionsTab } from "./settings/AppVersionsTab";
|
import { AppVersionsTab } from "./settings/AppVersionsTab";
|
||||||
import { SubscriptionPlansTab } from "./settings/SubscriptionPlansTab";
|
import { SubscriptionPlansTab } from "./settings/SubscriptionPlansTab";
|
||||||
import { InitialAssessmentThresholdsTab } from "./settings/InitialAssessmentThresholdsTab";
|
|
||||||
import { ThemeModePreview } from "./settings/components/ThemeModePreview";
|
import { ThemeModePreview } from "./settings/components/ThemeModePreview";
|
||||||
import { useTheme } from "../contexts/ThemeContext";
|
import { useTheme } from "../contexts/ThemeContext";
|
||||||
|
|
||||||
type SettingsTab =
|
type SettingsTab =
|
||||||
| "subscription"
|
| "subscription"
|
||||||
| "app-versions"
|
| "app-versions"
|
||||||
| "assessment"
|
|
||||||
| "security"
|
| "security"
|
||||||
| "appearance";
|
| "appearance";
|
||||||
|
|
||||||
const tabs: { id: SettingsTab; label: string; icon: typeof Shield }[] = [
|
const tabs: { id: SettingsTab; label: string; icon: typeof Shield }[] = [
|
||||||
{ id: "subscription", label: "Subscription packages", icon: CreditCard },
|
{ id: "subscription", label: "Subscription packages", icon: CreditCard },
|
||||||
{ id: "app-versions", label: "App versions", icon: Smartphone },
|
{ id: "app-versions", label: "App versions", icon: Smartphone },
|
||||||
{ id: "assessment", label: "Initial assessment", icon: Gauge },
|
|
||||||
{ id: "security", label: "Security", icon: Shield },
|
{ id: "security", label: "Security", icon: Shield },
|
||||||
{ id: "appearance", label: "Appearance", icon: Palette },
|
{ id: "appearance", label: "Appearance", icon: Palette },
|
||||||
];
|
];
|
||||||
|
|
@ -445,7 +441,6 @@ export function SettingsPage() {
|
||||||
<main className="min-h-[400px] min-w-0 w-full">
|
<main className="min-h-[400px] min-w-0 w-full">
|
||||||
{activeTab === "subscription" && <SubscriptionPlansTab />}
|
{activeTab === "subscription" && <SubscriptionPlansTab />}
|
||||||
{activeTab === "app-versions" && <AppVersionsTab />}
|
{activeTab === "app-versions" && <AppVersionsTab />}
|
||||||
{activeTab === "assessment" && <InitialAssessmentThresholdsTab />}
|
|
||||||
{activeTab === "security" && <SecurityTab memberId={profile.id} />}
|
{activeTab === "security" && <SecurityTab memberId={profile.id} />}
|
||||||
{activeTab === "appearance" && <AppearanceTab />}
|
{activeTab === "appearance" && <AppearanceTab />}
|
||||||
</main>
|
</main>
|
||||||
|
|
|
||||||
|
|
@ -1,186 +0,0 @@
|
||||||
import { useCallback, useEffect, useState } from "react"
|
|
||||||
import { Link, useNavigate, useParams } from "react-router-dom"
|
|
||||||
import { ArrowLeft } from "lucide-react"
|
|
||||||
import { toast } from "sonner"
|
|
||||||
import { getFAQById, getFAQs, updateFAQ } from "../../api/faq.api"
|
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "../../components/ui/card"
|
|
||||||
import { SpinnerIcon } from "../../components/ui/spinner-icon"
|
|
||||||
import { useFaqPermissions } from "../../hooks/useFaqPermissions"
|
|
||||||
import { deriveFaqCategories } from "../../lib/faqDisplay"
|
|
||||||
import { notifyApiError } from "../../lib/apiErrors"
|
|
||||||
import { isFaqForbiddenError } from "../../lib/faqErrors"
|
|
||||||
import type { FAQ, FAQStatus } from "../../types/faq.types"
|
|
||||||
import { FaqAccessDenied } from "./components/FaqAccessDenied"
|
|
||||||
import {
|
|
||||||
draftToUpdatePayload,
|
|
||||||
FaqForm,
|
|
||||||
validateFaqDraft,
|
|
||||||
type FaqFormDraft,
|
|
||||||
} from "./components/FaqForm"
|
|
||||||
|
|
||||||
function faqToDraft(faq: FAQ): FaqFormDraft {
|
|
||||||
return {
|
|
||||||
question: faq.question,
|
|
||||||
answer: faq.answer,
|
|
||||||
category: faq.category ?? "",
|
|
||||||
display_order: String(faq.display_order ?? 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function EditFaqPage() {
|
|
||||||
const { id } = useParams<{ id: string }>()
|
|
||||||
const navigate = useNavigate()
|
|
||||||
const faqId = Number(id)
|
|
||||||
const { canGet, canUpdate, loading: permissionsLoading } = useFaqPermissions()
|
|
||||||
|
|
||||||
const [loading, setLoading] = useState(true)
|
|
||||||
const [permissionDenied, setPermissionDenied] = useState(false)
|
|
||||||
const [saving, setSaving] = useState(false)
|
|
||||||
const [savingAction, setSavingAction] = useState<"draft" | "publish" | null>(null)
|
|
||||||
const [draft, setDraft] = useState<FaqFormDraft | null>(null)
|
|
||||||
const [currentStatus, setCurrentStatus] = useState<FAQStatus>("INACTIVE")
|
|
||||||
const [categories, setCategories] = useState<string[]>([])
|
|
||||||
|
|
||||||
const load = useCallback(async () => {
|
|
||||||
if (!Number.isFinite(faqId) || faqId <= 0) {
|
|
||||||
toast.error("Invalid FAQ ID")
|
|
||||||
navigate("/help/faqs", { replace: true })
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!canGet) {
|
|
||||||
setLoading(false)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
setLoading(true)
|
|
||||||
setPermissionDenied(false)
|
|
||||||
try {
|
|
||||||
const [faqRes, listRes] = await Promise.all([
|
|
||||||
getFAQById(faqId),
|
|
||||||
getFAQs({ limit: 200, offset: 0 }),
|
|
||||||
])
|
|
||||||
|
|
||||||
if (!faqRes.data) {
|
|
||||||
toast.error("FAQ not found")
|
|
||||||
navigate("/help/faqs", { replace: true })
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
setDraft(faqToDraft(faqRes.data))
|
|
||||||
setCurrentStatus(faqRes.data.status)
|
|
||||||
setCategories(deriveFaqCategories(listRes.data.faqs))
|
|
||||||
} catch (e: unknown) {
|
|
||||||
console.error(e)
|
|
||||||
const status = (e as { response?: { status?: number } })?.response?.status
|
|
||||||
if (status === 404) {
|
|
||||||
toast.error("FAQ not found")
|
|
||||||
navigate("/help/faqs", { replace: true })
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (isFaqForbiddenError(e)) {
|
|
||||||
setPermissionDenied(true)
|
|
||||||
notifyApiError(e, "You do not have permission to view this FAQ")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
notifyApiError(e, "Failed to load FAQ")
|
|
||||||
} finally {
|
|
||||||
setLoading(false)
|
|
||||||
}
|
|
||||||
}, [faqId, canGet, navigate])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
void load()
|
|
||||||
}, [load])
|
|
||||||
|
|
||||||
const handleSave = async (status: FAQStatus) => {
|
|
||||||
if (!draft) return
|
|
||||||
if (!canUpdate) {
|
|
||||||
toast.error("You do not have permission to update FAQs")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const validationError = validateFaqDraft(draft)
|
|
||||||
if (validationError) {
|
|
||||||
toast.error(validationError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
setSaving(true)
|
|
||||||
setSavingAction(status === "ACTIVE" ? "publish" : "draft")
|
|
||||||
try {
|
|
||||||
const response = await updateFAQ(faqId, draftToUpdatePayload(draft, status))
|
|
||||||
if (!response.data) {
|
|
||||||
throw new Error("Empty update response")
|
|
||||||
}
|
|
||||||
setDraft(faqToDraft(response.data))
|
|
||||||
setCurrentStatus(response.data.status)
|
|
||||||
toast.success(
|
|
||||||
status === "ACTIVE"
|
|
||||||
? response.message ?? "FAQ published successfully"
|
|
||||||
: response.message ?? "FAQ saved as draft",
|
|
||||||
)
|
|
||||||
} catch (e: unknown) {
|
|
||||||
console.error(e)
|
|
||||||
notifyApiError(e, "Failed to update FAQ")
|
|
||||||
} finally {
|
|
||||||
setSaving(false)
|
|
||||||
setSavingAction(null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!permissionsLoading && !canGet) {
|
|
||||||
return <FaqAccessDenied />
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!permissionsLoading && permissionDenied) {
|
|
||||||
return <FaqAccessDenied apiForbidden />
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="mx-auto w-full max-w-3xl space-y-6 pb-12">
|
|
||||||
<Link
|
|
||||||
to="/help/faqs"
|
|
||||||
className="group flex w-fit items-center gap-2 text-sm font-semibold text-grayScale-600 transition-colors hover:text-brand-500"
|
|
||||||
>
|
|
||||||
<ArrowLeft className="h-4 w-4 transition-transform group-hover:-translate-x-0.5" />
|
|
||||||
Back to FAQs
|
|
||||||
</Link>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<p className="text-sm font-semibold text-grayScale-500">Help center</p>
|
|
||||||
<h1 className="text-2xl font-semibold tracking-tight text-grayScale-900">
|
|
||||||
Edit FAQ
|
|
||||||
</h1>
|
|
||||||
<p className="max-w-2xl text-sm text-grayScale-500">
|
|
||||||
Update question, answer, category, or display order. Save as draft or publish when ready.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Card className="border border-grayScale-100 shadow-none">
|
|
||||||
<CardHeader className="border-b border-grayScale-100 pb-4">
|
|
||||||
<CardTitle className="text-lg">FAQ details</CardTitle>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="p-6 sm:p-8">
|
|
||||||
{loading || !draft ? (
|
|
||||||
<div className="flex justify-center py-8">
|
|
||||||
<SpinnerIcon className="h-6 w-6" />
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<FaqForm
|
|
||||||
draft={draft}
|
|
||||||
saving={saving}
|
|
||||||
savingAction={savingAction}
|
|
||||||
categories={categories}
|
|
||||||
currentStatus={currentStatus}
|
|
||||||
onChange={(patch) => setDraft((prev) => (prev ? { ...prev, ...patch } : prev))}
|
|
||||||
onSaveDraft={() => void handleSave("INACTIVE")}
|
|
||||||
onPublish={() => void handleSave("ACTIVE")}
|
|
||||||
onCancel={() => navigate("/help/faqs")}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
@ -1,472 +0,0 @@
|
||||||
import { useCallback, useEffect, useMemo, useState } from "react"
|
|
||||||
import { Link } from "react-router-dom"
|
|
||||||
import {
|
|
||||||
ChevronLeft,
|
|
||||||
ChevronRight,
|
|
||||||
CircleHelp,
|
|
||||||
Eye,
|
|
||||||
EyeOff,
|
|
||||||
ExternalLink,
|
|
||||||
Pencil,
|
|
||||||
Plus,
|
|
||||||
RefreshCw,
|
|
||||||
Search,
|
|
||||||
Trash2,
|
|
||||||
} from "lucide-react"
|
|
||||||
import { toast } from "sonner"
|
|
||||||
import { getFAQs, updateFAQ } from "../../api/faq.api"
|
|
||||||
import { Badge } from "../../components/ui/badge"
|
|
||||||
import { Button } from "../../components/ui/button"
|
|
||||||
import { Card, CardContent } from "../../components/ui/card"
|
|
||||||
import { AdminFiltersPanel } from "../../components/filters/AdminFiltersPanel"
|
|
||||||
import { Input } from "../../components/ui/input"
|
|
||||||
import { SpinnerIcon } from "../../components/ui/spinner-icon"
|
|
||||||
import {
|
|
||||||
Table,
|
|
||||||
TableBody,
|
|
||||||
TableCell,
|
|
||||||
TableHead,
|
|
||||||
TableHeader,
|
|
||||||
TableRow,
|
|
||||||
} from "../../components/ui/table"
|
|
||||||
import { useFaqPermissions } from "../../hooks/useFaqPermissions"
|
|
||||||
import {
|
|
||||||
deriveFaqCategories,
|
|
||||||
faqStatusBadgeVariant,
|
|
||||||
faqStatusLabel,
|
|
||||||
formatFaqDate,
|
|
||||||
suggestNextDisplayOrder,
|
|
||||||
} from "../../lib/faqDisplay"
|
|
||||||
import { notifyApiError } from "../../lib/apiErrors"
|
|
||||||
import { isFaqForbiddenError } from "../../lib/faqErrors"
|
|
||||||
import { countActiveFilters } from "../../lib/adminFilterUtils"
|
|
||||||
import { fetchAllOffsetPages } from "../../lib/fetchAllOffsetPages"
|
|
||||||
import { DEFAULT_TABLE_PAGE_SIZE, TABLE_PAGE_SIZE_OPTIONS } from "../../lib/tablePagination"
|
|
||||||
import { cn } from "../../lib/utils"
|
|
||||||
import type { FAQ, FAQStatus } from "../../types/faq.types"
|
|
||||||
import { CreateFaqDialog } from "./components/CreateFaqDialog"
|
|
||||||
import { FaqDeleteDialog } from "./components/FaqDeleteDialog"
|
|
||||||
import { FaqAccessDenied } from "./components/FaqAccessDenied"
|
|
||||||
import { FaqPublicPreviewDialog } from "./components/FaqPublicPreviewDialog"
|
|
||||||
import { UnassignedLabel } from "../../lib/displayValue"
|
|
||||||
import { SearchHighlight } from "../../components/SearchHighlight"
|
|
||||||
|
|
||||||
type StatusFilter = "all" | FAQStatus
|
|
||||||
|
|
||||||
export function FaqsPage() {
|
|
||||||
const { canList, canCreate, canUpdate, canDelete, canGet, loading: permissionsLoading } =
|
|
||||||
useFaqPermissions()
|
|
||||||
|
|
||||||
const [loading, setLoading] = useState(true)
|
|
||||||
const [error, setError] = useState(false)
|
|
||||||
const [permissionDenied, setPermissionDenied] = useState(false)
|
|
||||||
const [faqs, setFaqs] = useState<FAQ[]>([])
|
|
||||||
const [page, setPage] = useState(1)
|
|
||||||
const [pageSize, setPageSize] = useState(DEFAULT_TABLE_PAGE_SIZE)
|
|
||||||
const [query, setQuery] = useState("")
|
|
||||||
const [statusFilter, setStatusFilter] = useState<StatusFilter>("all")
|
|
||||||
const [categoryFilter, setCategoryFilter] = useState("")
|
|
||||||
const [allCategories, setAllCategories] = useState<string[]>([])
|
|
||||||
const [faqPendingDelete, setFaqPendingDelete] = useState<FAQ | null>(null)
|
|
||||||
const [togglingId, setTogglingId] = useState<number | null>(null)
|
|
||||||
const [previewOpen, setPreviewOpen] = useState(false)
|
|
||||||
const [createOpen, setCreateOpen] = useState(false)
|
|
||||||
const [suggestedDisplayOrder, setSuggestedDisplayOrder] = useState(1)
|
|
||||||
|
|
||||||
const load = useCallback(async () => {
|
|
||||||
if (!canList) {
|
|
||||||
setLoading(false)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
setLoading(true)
|
|
||||||
setError(false)
|
|
||||||
setPermissionDenied(false)
|
|
||||||
try {
|
|
||||||
const allFaqs = await fetchAllOffsetPages(async (batchOffset, limit) => {
|
|
||||||
const res = await getFAQs({
|
|
||||||
status: statusFilter === "all" ? undefined : statusFilter,
|
|
||||||
category: categoryFilter || undefined,
|
|
||||||
limit,
|
|
||||||
offset: batchOffset,
|
|
||||||
})
|
|
||||||
return {
|
|
||||||
items: res.data.faqs,
|
|
||||||
total_count: res.data.total_count,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
setFaqs(allFaqs)
|
|
||||||
|
|
||||||
const categoryRes = await getFAQs({ limit: 200, offset: 0 })
|
|
||||||
setAllCategories(deriveFaqCategories(categoryRes.data.faqs))
|
|
||||||
setSuggestedDisplayOrder(suggestNextDisplayOrder(categoryRes.data.faqs))
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
setError(true)
|
|
||||||
setFaqs([])
|
|
||||||
if (isFaqForbiddenError(e)) {
|
|
||||||
setPermissionDenied(true)
|
|
||||||
notifyApiError(e, "You do not have permission to view FAQs")
|
|
||||||
} else {
|
|
||||||
notifyApiError(e, "Failed to load FAQs")
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
setLoading(false)
|
|
||||||
}
|
|
||||||
}, [canList, statusFilter, categoryFilter])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
void load()
|
|
||||||
}, [load])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setPage(1)
|
|
||||||
}, [statusFilter, categoryFilter, pageSize, query])
|
|
||||||
|
|
||||||
const filtered = useMemo(() => {
|
|
||||||
const q = query.trim().toLowerCase()
|
|
||||||
if (!q) return faqs
|
|
||||||
return faqs.filter((faq) => {
|
|
||||||
const haystack = [faq.question, faq.answer, faq.category ?? ""]
|
|
||||||
.join(" ")
|
|
||||||
.toLowerCase()
|
|
||||||
return haystack.includes(q)
|
|
||||||
})
|
|
||||||
}, [faqs, query])
|
|
||||||
|
|
||||||
const totalCount = filtered.length
|
|
||||||
const totalPages = Math.max(1, Math.ceil(totalCount / pageSize))
|
|
||||||
const safePage = Math.min(page, totalPages)
|
|
||||||
const paginated = filtered.slice((safePage - 1) * pageSize, safePage * pageSize)
|
|
||||||
const pageStart = totalCount === 0 ? 0 : (safePage - 1) * pageSize + 1
|
|
||||||
const pageEnd = Math.min(safePage * pageSize, totalCount)
|
|
||||||
const canPrev = page > 1
|
|
||||||
const canNext = page < totalPages
|
|
||||||
|
|
||||||
const activeFilterCount = countActiveFilters([
|
|
||||||
{ value: statusFilter, defaultValue: "all" },
|
|
||||||
{ value: categoryFilter },
|
|
||||||
])
|
|
||||||
|
|
||||||
const clearFilters = () => {
|
|
||||||
setStatusFilter("all")
|
|
||||||
setCategoryFilter("")
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleToggleStatus = async (faq: FAQ) => {
|
|
||||||
if (!canUpdate) {
|
|
||||||
toast.error("You do not have permission to update FAQs")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const nextStatus: FAQStatus = faq.status === "ACTIVE" ? "INACTIVE" : "ACTIVE"
|
|
||||||
setTogglingId(faq.id)
|
|
||||||
try {
|
|
||||||
const res = await updateFAQ(faq.id, { status: nextStatus })
|
|
||||||
if (res.data) {
|
|
||||||
setFaqs((prev) => prev.map((row) => (row.id === faq.id ? res.data! : row)))
|
|
||||||
}
|
|
||||||
toast.success(
|
|
||||||
nextStatus === "ACTIVE" ? "FAQ published" : "FAQ moved to draft",
|
|
||||||
)
|
|
||||||
} catch (e: unknown) {
|
|
||||||
console.error(e)
|
|
||||||
notifyApiError(e, "Failed to update FAQ status")
|
|
||||||
} finally {
|
|
||||||
setTogglingId(null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!permissionsLoading && !canList) {
|
|
||||||
return <FaqAccessDenied />
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!permissionsLoading && permissionDenied) {
|
|
||||||
return <FaqAccessDenied apiForbidden />
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="mx-auto w-full max-w-6xl space-y-6">
|
|
||||||
<div className="flex flex-wrap items-start justify-between gap-4">
|
|
||||||
<div>
|
|
||||||
<p className="text-sm font-semibold text-grayScale-500">Help center</p>
|
|
||||||
<h1 className="text-2xl font-semibold tracking-tight text-grayScale-800">
|
|
||||||
FAQs
|
|
||||||
</h1>
|
|
||||||
<p className="mt-1 max-w-2xl text-sm text-grayScale-500">
|
|
||||||
Manage frequently asked questions shown in the learner help center.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<div className="flex flex-wrap gap-2">
|
|
||||||
<Button variant="outline" className="shrink-0" onClick={() => setPreviewOpen(true)}>
|
|
||||||
<ExternalLink className="mr-2 h-4 w-4" />
|
|
||||||
Preview as learner
|
|
||||||
</Button>
|
|
||||||
{canCreate ? (
|
|
||||||
<Button
|
|
||||||
className="shrink-0 bg-brand-500 text-white hover:bg-brand-600"
|
|
||||||
onClick={() => setCreateOpen(true)}
|
|
||||||
>
|
|
||||||
<Plus className="mr-2 h-4 w-4" />
|
|
||||||
Add FAQ
|
|
||||||
</Button>
|
|
||||||
) : null}
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
className="shrink-0"
|
|
||||||
disabled={loading}
|
|
||||||
onClick={() => void load()}
|
|
||||||
>
|
|
||||||
<RefreshCw className={cn("mr-2 h-4 w-4", loading && "animate-spin")} />
|
|
||||||
Refresh
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<AdminFiltersPanel
|
|
||||||
activeFilterCount={activeFilterCount}
|
|
||||||
onClearFilters={clearFilters}
|
|
||||||
summary={
|
|
||||||
loading
|
|
||||||
? "Loading…"
|
|
||||||
: `${totalCount} shown · ${faqs.length} loaded`
|
|
||||||
}
|
|
||||||
search={
|
|
||||||
<div className="relative w-full">
|
|
||||||
<Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-grayScale-400" />
|
|
||||||
<Input
|
|
||||||
className="pl-9"
|
|
||||||
placeholder="Search question or answer…"
|
|
||||||
value={query}
|
|
||||||
onChange={(e) => setQuery(e.target.value)}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<div className="flex flex-wrap items-center gap-2">
|
|
||||||
{(["all", "ACTIVE", "INACTIVE"] as const).map((tab) => (
|
|
||||||
<button
|
|
||||||
key={tab}
|
|
||||||
type="button"
|
|
||||||
onClick={() => setStatusFilter(tab)}
|
|
||||||
className={cn(
|
|
||||||
"h-9 rounded-full px-3 text-xs font-semibold transition-colors",
|
|
||||||
statusFilter === tab
|
|
||||||
? "bg-brand-500 text-white"
|
|
||||||
: "bg-grayScale-100 text-grayScale-600 hover:bg-grayScale-200",
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
{tab === "all" ? "All" : tab === "ACTIVE" ? "Published" : "Draft"}
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<label htmlFor="faq-category-filter" className="text-xs font-semibold text-grayScale-500">
|
|
||||||
Category
|
|
||||||
</label>
|
|
||||||
<select
|
|
||||||
id="faq-category-filter"
|
|
||||||
value={categoryFilter}
|
|
||||||
onChange={(e) => setCategoryFilter(e.target.value)}
|
|
||||||
className="h-9 rounded-lg border border-grayScale-200 bg-white px-3 text-sm text-grayScale-800"
|
|
||||||
>
|
|
||||||
<option value="">All categories</option>
|
|
||||||
{allCategories.map((category) => (
|
|
||||||
<option key={category} value={category}>
|
|
||||||
{category}
|
|
||||||
</option>
|
|
||||||
))}
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
</AdminFiltersPanel>
|
|
||||||
|
|
||||||
{loading ? (
|
|
||||||
<div className="flex justify-center py-16">
|
|
||||||
<SpinnerIcon className="h-6 w-6" />
|
|
||||||
</div>
|
|
||||||
) : error ? (
|
|
||||||
<Card className="border border-grayScale-100 shadow-none">
|
|
||||||
<CardContent className="flex flex-col items-center gap-3 py-16 text-center">
|
|
||||||
<p className="text-sm text-destructive">Could not load FAQs.</p>
|
|
||||||
<Button variant="outline" size="sm" onClick={() => void load()}>
|
|
||||||
Retry
|
|
||||||
</Button>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
) : filtered.length === 0 ? (
|
|
||||||
<Card className="border border-grayScale-100 shadow-none">
|
|
||||||
<CardContent className="flex flex-col items-center gap-4 px-6 py-16 text-center">
|
|
||||||
<div className="grid h-14 w-14 place-items-center rounded-2xl bg-brand-100/50 text-brand-500">
|
|
||||||
<CircleHelp className="h-7 w-7" />
|
|
||||||
</div>
|
|
||||||
<p className="text-sm text-grayScale-500">
|
|
||||||
{faqs.length === 0
|
|
||||||
? "No FAQs yet. Create your first FAQ to populate the help center."
|
|
||||||
: "No FAQs match your search or filters."}
|
|
||||||
</p>
|
|
||||||
{canCreate && faqs.length === 0 ? (
|
|
||||||
<Button
|
|
||||||
className="bg-brand-500 text-white hover:bg-brand-600"
|
|
||||||
onClick={() => setCreateOpen(true)}
|
|
||||||
>
|
|
||||||
<Plus className="mr-2 h-4 w-4" />
|
|
||||||
Add FAQ
|
|
||||||
</Button>
|
|
||||||
) : null}
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
) : (
|
|
||||||
<Card className="overflow-hidden border border-grayScale-100 shadow-none">
|
|
||||||
<div className="overflow-x-auto">
|
|
||||||
<Table>
|
|
||||||
<TableHeader>
|
|
||||||
<TableRow>
|
|
||||||
<TableHead>Question</TableHead>
|
|
||||||
<TableHead>Category</TableHead>
|
|
||||||
<TableHead className="w-20 text-center">Order</TableHead>
|
|
||||||
<TableHead>Status</TableHead>
|
|
||||||
<TableHead>Updated</TableHead>
|
|
||||||
<TableHead className="text-right">Actions</TableHead>
|
|
||||||
</TableRow>
|
|
||||||
</TableHeader>
|
|
||||||
<TableBody>
|
|
||||||
{paginated.map((faq) => (
|
|
||||||
<TableRow key={faq.id}>
|
|
||||||
<TableCell className="max-w-xs">
|
|
||||||
<p className="line-clamp-2 font-medium text-grayScale-900">
|
|
||||||
<SearchHighlight text={faq.question} query={query} />
|
|
||||||
</p>
|
|
||||||
</TableCell>
|
|
||||||
<TableCell>
|
|
||||||
{faq.category ? (
|
|
||||||
<Badge variant="secondary">{faq.category}</Badge>
|
|
||||||
) : (
|
|
||||||
<span className="text-xs text-grayScale-400"><UnassignedLabel /></span>
|
|
||||||
)}
|
|
||||||
</TableCell>
|
|
||||||
<TableCell className="text-center text-sm text-grayScale-600">
|
|
||||||
{faq.display_order}
|
|
||||||
</TableCell>
|
|
||||||
<TableCell>
|
|
||||||
<Badge variant={faqStatusBadgeVariant(faq.status)}>
|
|
||||||
{faqStatusLabel(faq.status)}
|
|
||||||
</Badge>
|
|
||||||
</TableCell>
|
|
||||||
<TableCell className="text-sm text-grayScale-500">
|
|
||||||
{formatFaqDate(faq.updated_at || faq.created_at)}
|
|
||||||
</TableCell>
|
|
||||||
<TableCell>
|
|
||||||
<div className="flex items-center justify-end gap-1">
|
|
||||||
{canUpdate ? (
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
disabled={togglingId === faq.id}
|
|
||||||
onClick={() => void handleToggleStatus(faq)}
|
|
||||||
title={
|
|
||||||
faq.status === "ACTIVE" ? "Unpublish" : "Publish"
|
|
||||||
}
|
|
||||||
>
|
|
||||||
{faq.status === "ACTIVE" ? (
|
|
||||||
<EyeOff className="h-3.5 w-3.5" />
|
|
||||||
) : (
|
|
||||||
<Eye className="h-3.5 w-3.5" />
|
|
||||||
)}
|
|
||||||
<span className="sr-only">
|
|
||||||
{faq.status === "ACTIVE" ? "Unpublish" : "Publish"}
|
|
||||||
</span>
|
|
||||||
</Button>
|
|
||||||
) : null}
|
|
||||||
{canGet ? (
|
|
||||||
<Button variant="outline" size="sm" asChild>
|
|
||||||
<Link to={`/help/faqs/${faq.id}/edit`}>
|
|
||||||
<Pencil className="h-3.5 w-3.5" />
|
|
||||||
<span className="sr-only">Edit</span>
|
|
||||||
</Link>
|
|
||||||
</Button>
|
|
||||||
) : null}
|
|
||||||
{canDelete ? (
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
className="text-destructive hover:bg-destructive/10 hover:text-destructive"
|
|
||||||
onClick={() => setFaqPendingDelete(faq)}
|
|
||||||
>
|
|
||||||
<Trash2 className="h-3.5 w-3.5" />
|
|
||||||
<span className="sr-only">Delete</span>
|
|
||||||
</Button>
|
|
||||||
) : null}
|
|
||||||
</div>
|
|
||||||
</TableCell>
|
|
||||||
</TableRow>
|
|
||||||
))}
|
|
||||||
</TableBody>
|
|
||||||
</Table>
|
|
||||||
</div>
|
|
||||||
</Card>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{!loading && !error && totalCount > 0 ? (
|
|
||||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
|
||||||
<p className="text-sm text-grayScale-500">
|
|
||||||
Showing {pageStart}–{pageEnd} of {totalCount}
|
|
||||||
</p>
|
|
||||||
<div className="flex flex-wrap items-center gap-2">
|
|
||||||
<select
|
|
||||||
value={pageSize}
|
|
||||||
onChange={(e) => setPageSize(Number(e.target.value))}
|
|
||||||
className="h-9 rounded-lg border border-grayScale-200 bg-white px-3 text-sm"
|
|
||||||
>
|
|
||||||
{TABLE_PAGE_SIZE_OPTIONS.map((size) => (
|
|
||||||
<option key={size} value={size}>
|
|
||||||
{size} per page
|
|
||||||
</option>
|
|
||||||
))}
|
|
||||||
</select>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
disabled={!canPrev}
|
|
||||||
onClick={() => setPage((p) => Math.max(1, p - 1))}
|
|
||||||
>
|
|
||||||
<ChevronLeft className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
<span className="text-sm text-grayScale-600">
|
|
||||||
Page {page} of {totalPages}
|
|
||||||
</span>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
disabled={!canNext}
|
|
||||||
onClick={() => setPage((p) => p + 1)}
|
|
||||||
>
|
|
||||||
<ChevronRight className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
) : null}
|
|
||||||
|
|
||||||
<FaqPublicPreviewDialog open={previewOpen} onOpenChange={setPreviewOpen} />
|
|
||||||
|
|
||||||
<CreateFaqDialog
|
|
||||||
open={createOpen}
|
|
||||||
onOpenChange={setCreateOpen}
|
|
||||||
categories={allCategories}
|
|
||||||
suggestedDisplayOrder={suggestedDisplayOrder}
|
|
||||||
onCreated={() => void load()}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<FaqDeleteDialog
|
|
||||||
faq={faqPendingDelete}
|
|
||||||
open={faqPendingDelete !== null}
|
|
||||||
onOpenChange={(open) => {
|
|
||||||
if (!open) setFaqPendingDelete(null)
|
|
||||||
}}
|
|
||||||
onDeleted={() => {
|
|
||||||
setFaqPendingDelete(null)
|
|
||||||
void load()
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
@ -1,124 +0,0 @@
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import { Plus } from "lucide-react";
|
|
||||||
import { toast } from "sonner";
|
|
||||||
import { createFAQ } from "../../../api/faq.api";
|
|
||||||
import {
|
|
||||||
Dialog,
|
|
||||||
DialogContent,
|
|
||||||
DialogDescription,
|
|
||||||
DialogTitle,
|
|
||||||
} from "../../../components/ui/dialog";
|
|
||||||
import { Stepper } from "../../../components/ui/stepper";
|
|
||||||
import { notifyApiError } from "../../../lib/apiErrors";
|
|
||||||
import {
|
|
||||||
draftToCreatePayload,
|
|
||||||
EMPTY_FAQ_FORM_DRAFT,
|
|
||||||
FaqForm,
|
|
||||||
validateFaqDraft,
|
|
||||||
type FaqFormDraft,
|
|
||||||
} from "./FaqForm";
|
|
||||||
import type { FAQStatus } from "../../../types/faq.types";
|
|
||||||
|
|
||||||
const STEPS = ["Question & Answer", "Category & Details"];
|
|
||||||
|
|
||||||
type CreateFaqDialogProps = {
|
|
||||||
open: boolean;
|
|
||||||
onOpenChange: (open: boolean) => void;
|
|
||||||
categories: string[];
|
|
||||||
suggestedDisplayOrder: number;
|
|
||||||
onCreated?: () => void;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function CreateFaqDialog({
|
|
||||||
open,
|
|
||||||
onOpenChange,
|
|
||||||
categories,
|
|
||||||
suggestedDisplayOrder,
|
|
||||||
onCreated,
|
|
||||||
}: CreateFaqDialogProps) {
|
|
||||||
const [draft, setDraft] = useState<FaqFormDraft>(EMPTY_FAQ_FORM_DRAFT);
|
|
||||||
const [saving, setSaving] = useState(false);
|
|
||||||
const [savingAction, setSavingAction] = useState<"draft" | "publish" | null>(
|
|
||||||
null,
|
|
||||||
);
|
|
||||||
const [currentStep, setCurrentStep] = useState(1);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (open) {
|
|
||||||
setDraft({
|
|
||||||
...EMPTY_FAQ_FORM_DRAFT,
|
|
||||||
display_order: String(suggestedDisplayOrder),
|
|
||||||
});
|
|
||||||
setSavingAction(null);
|
|
||||||
setCurrentStep(1);
|
|
||||||
}
|
|
||||||
}, [open, suggestedDisplayOrder]);
|
|
||||||
|
|
||||||
const handleOpenChange = (next: boolean) => {
|
|
||||||
if (!next && !saving) onOpenChange(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleCreate = async (status: FAQStatus) => {
|
|
||||||
const validationError = validateFaqDraft(draft);
|
|
||||||
if (validationError) {
|
|
||||||
toast.error(validationError);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setSaving(true);
|
|
||||||
setSavingAction(status === "ACTIVE" ? "publish" : "draft");
|
|
||||||
try {
|
|
||||||
const response = await createFAQ(draftToCreatePayload(draft, status));
|
|
||||||
if (!response.data) {
|
|
||||||
throw new Error("Empty create response");
|
|
||||||
}
|
|
||||||
toast.success(
|
|
||||||
status === "ACTIVE"
|
|
||||||
? (response.message ?? "FAQ published successfully")
|
|
||||||
: (response.message ?? "FAQ saved as draft"),
|
|
||||||
);
|
|
||||||
onOpenChange(false);
|
|
||||||
onCreated?.();
|
|
||||||
} catch (e: unknown) {
|
|
||||||
console.error(e);
|
|
||||||
notifyApiError(e, "Failed to create FAQ");
|
|
||||||
} finally {
|
|
||||||
setSaving(false);
|
|
||||||
setSavingAction(null);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Dialog open={open} onOpenChange={handleOpenChange}>
|
|
||||||
<DialogContent className="max-h-[90vh] max-w-lg p-0 overflow-hidden">
|
|
||||||
<div className="flex items-center gap-3 border-b border-grayScale-100 px-6 py-5 pr-14">
|
|
||||||
<div>
|
|
||||||
<DialogTitle className="text-[15px] font-semibold text-grayScale-900">
|
|
||||||
New FAQ
|
|
||||||
</DialogTitle>
|
|
||||||
<DialogDescription className="mt-0.5 text-xs text-grayScale-500">
|
|
||||||
Add a question and answer for the learner help center.
|
|
||||||
</DialogDescription>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="px-6 pt-5">
|
|
||||||
<Stepper steps={STEPS} currentStep={currentStep} />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<FaqForm
|
|
||||||
draft={draft}
|
|
||||||
saving={saving}
|
|
||||||
savingAction={savingAction}
|
|
||||||
categories={categories}
|
|
||||||
currentStep={currentStep}
|
|
||||||
onChange={(patch) => setDraft((prev) => ({ ...prev, ...patch }))}
|
|
||||||
onStepChange={setCurrentStep}
|
|
||||||
onSaveDraft={() => void handleCreate("INACTIVE")}
|
|
||||||
onPublish={() => void handleCreate("ACTIVE")}
|
|
||||||
onCancel={() => onOpenChange(false)}
|
|
||||||
/>
|
|
||||||
</DialogContent>
|
|
||||||
</Dialog>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
@ -1,95 +0,0 @@
|
||||||
import { useState } from "react"
|
|
||||||
import { Link } from "react-router-dom"
|
|
||||||
import { CircleHelp, LogOut, RefreshCw } from "lucide-react"
|
|
||||||
import { toast } from "sonner"
|
|
||||||
import { syncRbacPermissions } from "../../../api/rbac.api"
|
|
||||||
import { Button } from "../../../components/ui/button"
|
|
||||||
import { Card, CardContent } from "../../../components/ui/card"
|
|
||||||
import { SpinnerIcon } from "../../../components/ui/spinner-icon"
|
|
||||||
import { getSessionTeamRole, hasFaqRoleBypass } from "../../../lib/faqPermissions"
|
|
||||||
import { notifyApiError } from "../../../lib/apiErrors"
|
|
||||||
import { FAQ_FORBIDDEN_MESSAGE } from "../../../lib/faqErrors"
|
|
||||||
|
|
||||||
type FaqAccessDeniedProps = {
|
|
||||||
apiForbidden?: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export function FaqAccessDenied({ apiForbidden = false }: FaqAccessDeniedProps) {
|
|
||||||
const [syncing, setSyncing] = useState(false)
|
|
||||||
const teamRole = getSessionTeamRole()
|
|
||||||
const canSync = hasFaqRoleBypass()
|
|
||||||
|
|
||||||
const handleSync = async () => {
|
|
||||||
setSyncing(true)
|
|
||||||
try {
|
|
||||||
const res = await syncRbacPermissions()
|
|
||||||
toast.success(res.data?.message ?? "RBAC permissions synced. Please sign in again.")
|
|
||||||
} catch (e: unknown) {
|
|
||||||
console.error(e)
|
|
||||||
notifyApiError(e, "Failed to sync RBAC permissions")
|
|
||||||
} finally {
|
|
||||||
setSyncing(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="mx-auto w-full max-w-2xl space-y-6">
|
|
||||||
<Card className="border border-grayScale-100 shadow-none">
|
|
||||||
<CardContent className="flex flex-col items-center gap-4 px-6 py-16 text-center">
|
|
||||||
<CircleHelp className="h-10 w-10 text-grayScale-300" />
|
|
||||||
<div className="space-y-2">
|
|
||||||
<h2 className="text-lg font-semibold text-grayScale-800">
|
|
||||||
{apiForbidden ? "FAQ access denied" : "No permission to view FAQs"}
|
|
||||||
</h2>
|
|
||||||
<p className="text-sm text-grayScale-600">
|
|
||||||
{apiForbidden
|
|
||||||
? FAQ_FORBIDDEN_MESSAGE
|
|
||||||
: "Your account does not have the faqs.list permission."}
|
|
||||||
</p>
|
|
||||||
{teamRole ? (
|
|
||||||
<p className="text-xs text-grayScale-500">
|
|
||||||
Signed in as team role: <span className="font-medium">{teamRole}</span>
|
|
||||||
</p>
|
|
||||||
) : null}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="w-full max-w-md rounded-xl border border-grayScale-200 bg-grayScale-50 px-4 py-3 text-left text-xs text-grayScale-600">
|
|
||||||
<p className="font-semibold text-grayScale-700">If you expect access:</p>
|
|
||||||
<ul className="mt-2 list-disc space-y-1 pl-4">
|
|
||||||
<li>Use a team account mapped to ADMIN, SUPER_ADMIN, or INSTRUCTOR with faqs.* permissions</li>
|
|
||||||
<li>Ensure migrations 000059 and 000084 are applied on the API database</li>
|
|
||||||
<li>After RBAC sync or deploy, sign out and sign in again for a fresh token</li>
|
|
||||||
<li>Custom roles need faqs.list, faqs.get, faqs.create, faqs.update, and faqs.delete assigned</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="flex flex-wrap items-center justify-center gap-2">
|
|
||||||
{canSync ? (
|
|
||||||
<Button variant="outline" disabled={syncing} onClick={() => void handleSync()}>
|
|
||||||
{syncing ? (
|
|
||||||
<SpinnerIcon className="mr-2 h-4 w-4" />
|
|
||||||
) : (
|
|
||||||
<RefreshCw className="mr-2 h-4 w-4" />
|
|
||||||
)}
|
|
||||||
Sync RBAC permissions
|
|
||||||
</Button>
|
|
||||||
) : null}
|
|
||||||
<Button variant="outline" asChild>
|
|
||||||
<Link to="/roles">Manage roles</Link>
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
onClick={() => {
|
|
||||||
localStorage.clear()
|
|
||||||
window.location.href = "/login"
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<LogOut className="mr-2 h-4 w-4" />
|
|
||||||
Sign in again
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
@ -1,92 +0,0 @@
|
||||||
import { useState } from "react"
|
|
||||||
import { Trash2 } from "lucide-react"
|
|
||||||
import { toast } from "sonner"
|
|
||||||
import { deleteFAQ } from "../../../api/faq.api"
|
|
||||||
import { Button } from "../../../components/ui/button"
|
|
||||||
import { notifyApiError } from "../../../lib/apiErrors"
|
|
||||||
import {
|
|
||||||
Dialog,
|
|
||||||
DialogContent,
|
|
||||||
DialogDescription,
|
|
||||||
DialogTitle,
|
|
||||||
} from "../../../components/ui/dialog"
|
|
||||||
import type { FAQ } from "../../../types/faq.types"
|
|
||||||
|
|
||||||
type FaqDeleteDialogProps = {
|
|
||||||
faq: FAQ | null
|
|
||||||
open: boolean
|
|
||||||
onOpenChange: (open: boolean) => void
|
|
||||||
onDeleted?: () => void
|
|
||||||
}
|
|
||||||
|
|
||||||
export function FaqDeleteDialog({
|
|
||||||
faq,
|
|
||||||
open,
|
|
||||||
onOpenChange,
|
|
||||||
onDeleted,
|
|
||||||
}: FaqDeleteDialogProps) {
|
|
||||||
const [deleting, setDeleting] = useState(false)
|
|
||||||
|
|
||||||
const handleOpenChange = (next: boolean) => {
|
|
||||||
if (!next && !deleting) onOpenChange(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleConfirm = async () => {
|
|
||||||
if (!faq) return
|
|
||||||
|
|
||||||
setDeleting(true)
|
|
||||||
try {
|
|
||||||
const response = await deleteFAQ(faq.id)
|
|
||||||
toast.success(response.message ?? "FAQ deleted successfully")
|
|
||||||
onOpenChange(false)
|
|
||||||
onDeleted?.()
|
|
||||||
} catch (e: unknown) {
|
|
||||||
console.error(e)
|
|
||||||
notifyApiError(e, "Failed to delete FAQ")
|
|
||||||
} finally {
|
|
||||||
setDeleting(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Dialog open={open} onOpenChange={handleOpenChange}>
|
|
||||||
<DialogContent className="max-w-md overflow-hidden p-0">
|
|
||||||
<div className="flex items-center gap-3 border-b border-grayScale-100 px-6 py-5 pr-14">
|
|
||||||
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-[8px] bg-red-50 dark:bg-red-500/10">
|
|
||||||
<Trash2 className="h-5 w-5 text-red-500" />
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<DialogTitle className="text-[15px] font-semibold text-grayScale-900">
|
|
||||||
Delete FAQ?
|
|
||||||
</DialogTitle>
|
|
||||||
<DialogDescription className="mt-0.5 text-xs text-grayScale-500">
|
|
||||||
This permanently removes the FAQ from the help center.
|
|
||||||
</DialogDescription>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{faq ? (
|
|
||||||
<div className="mx-6 mt-4 rounded-[8px] border border-grayScale-100 bg-grayScale-50 px-4 py-3">
|
|
||||||
<p className="text-sm font-medium text-grayScale-900">{faq.question}</p>
|
|
||||||
<p className="mt-0.5 text-[11px] text-grayScale-400">#{faq.id}</p>
|
|
||||||
</div>
|
|
||||||
) : null}
|
|
||||||
<div className="flex items-center justify-end gap-2 border-t border-grayScale-100 px-6 py-4">
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
disabled={deleting}
|
|
||||||
onClick={() => onOpenChange(false)}
|
|
||||||
>
|
|
||||||
Cancel
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
variant="destructive"
|
|
||||||
disabled={deleting || !faq}
|
|
||||||
onClick={() => void handleConfirm()}
|
|
||||||
>
|
|
||||||
{deleting ? "Deleting…" : "Delete FAQ"}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</DialogContent>
|
|
||||||
</Dialog>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
@ -1,239 +0,0 @@
|
||||||
import { Button } from "../../../components/ui/button"
|
|
||||||
import { Input } from "../../../components/ui/input"
|
|
||||||
import { Textarea } from "../../../components/ui/textarea"
|
|
||||||
import type { FAQStatus } from "../../../types/faq.types"
|
|
||||||
import { UnassignedLabel } from "../../../lib/displayValue"
|
|
||||||
|
|
||||||
export type FaqFormDraft = {
|
|
||||||
question: string
|
|
||||||
answer: string
|
|
||||||
category: string
|
|
||||||
display_order: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export const EMPTY_FAQ_FORM_DRAFT: FaqFormDraft = {
|
|
||||||
question: "",
|
|
||||||
answer: "",
|
|
||||||
category: "",
|
|
||||||
display_order: "0",
|
|
||||||
}
|
|
||||||
|
|
||||||
type FaqFormProps = {
|
|
||||||
draft: FaqFormDraft
|
|
||||||
saving: boolean
|
|
||||||
savingAction?: "draft" | "publish" | null
|
|
||||||
categories: string[]
|
|
||||||
currentStep?: number
|
|
||||||
onChange: (patch: Partial<FaqFormDraft>) => void
|
|
||||||
onStepChange?: (step: number) => void
|
|
||||||
onSaveDraft: () => void
|
|
||||||
onPublish: () => void
|
|
||||||
onCancel: () => void
|
|
||||||
}
|
|
||||||
|
|
||||||
export function FaqForm({
|
|
||||||
draft,
|
|
||||||
saving,
|
|
||||||
savingAction = null,
|
|
||||||
categories,
|
|
||||||
currentStep = 2,
|
|
||||||
onChange,
|
|
||||||
onStepChange,
|
|
||||||
onSaveDraft,
|
|
||||||
onPublish,
|
|
||||||
onCancel,
|
|
||||||
}: FaqFormProps) {
|
|
||||||
const stepped = Boolean(onStepChange)
|
|
||||||
const showAll = !stepped
|
|
||||||
|
|
||||||
const canContinue =
|
|
||||||
currentStep === 1
|
|
||||||
? draft.question.trim().length > 0 && draft.answer.trim().length > 0
|
|
||||||
: true
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="flex max-h-[calc(90vh-140px)] flex-col overflow-hidden">
|
|
||||||
<div className="space-y-5 overflow-y-auto px-6 py-5">
|
|
||||||
{(showAll || currentStep === 1) && (
|
|
||||||
<>
|
|
||||||
<div>
|
|
||||||
<label className="mb-1.5 block text-xs font-medium text-grayScale-600">
|
|
||||||
Question <span className="text-brand-500">*</span>
|
|
||||||
</label>
|
|
||||||
<Input
|
|
||||||
value={draft.question}
|
|
||||||
onChange={(e) => onChange({ question: e.target.value })}
|
|
||||||
placeholder="How do I reset my password?"
|
|
||||||
disabled={saving}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<label className="mb-1.5 block text-xs font-medium text-grayScale-600">
|
|
||||||
Answer <span className="text-brand-500">*</span>
|
|
||||||
</label>
|
|
||||||
<Textarea
|
|
||||||
value={draft.answer}
|
|
||||||
onChange={(e) => onChange({ answer: e.target.value })}
|
|
||||||
placeholder="Go to login and click 'Forgot Password'."
|
|
||||||
rows={6}
|
|
||||||
disabled={saving}
|
|
||||||
/>
|
|
||||||
<p className="mt-1 text-[11px] text-grayScale-400">Plain text only.</p>
|
|
||||||
</div>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{(showAll || currentStep === 2) && (
|
|
||||||
<>
|
|
||||||
<div>
|
|
||||||
<label className="mb-1.5 block text-xs font-medium text-grayScale-600">
|
|
||||||
Category
|
|
||||||
</label>
|
|
||||||
<Input
|
|
||||||
value={draft.category}
|
|
||||||
onChange={(e) => onChange({ category: e.target.value })}
|
|
||||||
placeholder="Account"
|
|
||||||
list="faq-category-suggestions"
|
|
||||||
disabled={saving}
|
|
||||||
/>
|
|
||||||
{categories.length > 0 ? (
|
|
||||||
<datalist id="faq-category-suggestions">
|
|
||||||
{categories.map((category) => (
|
|
||||||
<option key={category} value={category} />
|
|
||||||
))}
|
|
||||||
</datalist>
|
|
||||||
) : null}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<label className="mb-1.5 block text-xs font-medium text-grayScale-600">
|
|
||||||
Display order
|
|
||||||
</label>
|
|
||||||
<Input
|
|
||||||
type="number"
|
|
||||||
min={0}
|
|
||||||
value={draft.display_order}
|
|
||||||
onChange={(e) => onChange({ display_order: e.target.value })}
|
|
||||||
disabled={saving}
|
|
||||||
/>
|
|
||||||
<p className="mt-1 text-[11px] text-grayScale-400">
|
|
||||||
Lower numbers appear first.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{showAll && (
|
|
||||||
<div className="rounded-[8px] border border-grayScale-100 bg-grayScale-50/70 px-4 py-3.5">
|
|
||||||
<p className="text-[11px] font-semibold uppercase tracking-wide text-grayScale-400">
|
|
||||||
Preview
|
|
||||||
</p>
|
|
||||||
<p className="mt-2 text-sm font-medium text-grayScale-900">
|
|
||||||
{draft.question || <UnassignedLabel />}
|
|
||||||
</p>
|
|
||||||
<p className="mt-1 whitespace-pre-wrap text-xs leading-relaxed text-grayScale-600">
|
|
||||||
{draft.answer || <UnassignedLabel />}
|
|
||||||
</p>
|
|
||||||
{(draft.category || draft.display_order) && (
|
|
||||||
<div className="mt-2.5 flex items-center gap-2 text-[11px] text-grayScale-400">
|
|
||||||
{draft.category && <span>Category: {draft.category}</span>}
|
|
||||||
{draft.display_order && <span>Order: {draft.display_order}</span>}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{stepped && currentStep === 2 && (
|
|
||||||
<div className="rounded-[8px] border border-grayScale-100 bg-grayScale-50/70 px-4 py-3.5">
|
|
||||||
<p className="text-[11px] font-semibold uppercase tracking-wide text-grayScale-400">
|
|
||||||
Preview
|
|
||||||
</p>
|
|
||||||
<p className="mt-2 text-sm font-medium text-grayScale-900">
|
|
||||||
{draft.question || <UnassignedLabel />}
|
|
||||||
</p>
|
|
||||||
<p className="mt-1 whitespace-pre-wrap text-xs leading-relaxed text-grayScale-600">
|
|
||||||
{draft.answer || <UnassignedLabel />}
|
|
||||||
</p>
|
|
||||||
{(draft.category || draft.display_order) && (
|
|
||||||
<div className="mt-2.5 flex items-center gap-2 text-[11px] text-grayScale-400">
|
|
||||||
{draft.category && <span>Category: {draft.category}</span>}
|
|
||||||
{draft.display_order && <span>Order: {draft.display_order}</span>}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="flex shrink-0 items-center justify-between border-t border-grayScale-100 px-6 py-4">
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<Button variant="outline" disabled={saving} onClick={onCancel}>
|
|
||||||
Cancel
|
|
||||||
</Button>
|
|
||||||
{stepped && currentStep === 2 && (
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
disabled={saving}
|
|
||||||
onClick={() => onStepChange?.(1)}
|
|
||||||
>
|
|
||||||
Back
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
{stepped && currentStep === 1 ? (
|
|
||||||
<Button
|
|
||||||
className="bg-brand-500 text-white hover:bg-brand-600"
|
|
||||||
disabled={!canContinue}
|
|
||||||
onClick={() => onStepChange?.(2)}
|
|
||||||
>
|
|
||||||
Continue
|
|
||||||
</Button>
|
|
||||||
) : (
|
|
||||||
<>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
disabled={saving}
|
|
||||||
onClick={onSaveDraft}
|
|
||||||
>
|
|
||||||
{saving && savingAction === "draft" ? "Saving…" : "Save as draft"}
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
className="bg-brand-500 text-white hover:bg-brand-600"
|
|
||||||
disabled={saving}
|
|
||||||
onClick={onPublish}
|
|
||||||
>
|
|
||||||
{saving && savingAction === "publish" ? "Publishing…" : "Publish"}
|
|
||||||
</Button>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function validateFaqDraft(draft: FaqFormDraft): string | null {
|
|
||||||
if (!draft.question.trim()) return "Question is required"
|
|
||||||
if (!draft.answer.trim()) return "Answer is required"
|
|
||||||
const order = Number(draft.display_order)
|
|
||||||
if (!Number.isFinite(order) || order < 0) {
|
|
||||||
return "Display order must be a non-negative number"
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
export function draftToCreatePayload(draft: FaqFormDraft, status: FAQStatus) {
|
|
||||||
const category = draft.category.trim()
|
|
||||||
return {
|
|
||||||
question: draft.question.trim(),
|
|
||||||
answer: draft.answer.trim(),
|
|
||||||
category: category || null,
|
|
||||||
display_order: Number(draft.display_order),
|
|
||||||
status,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function draftToUpdatePayload(draft: FaqFormDraft, status: FAQStatus) {
|
|
||||||
return draftToCreatePayload(draft, status)
|
|
||||||
}
|
|
||||||
|
|
@ -1,140 +0,0 @@
|
||||||
import { notifyApiError } from "../../../lib/apiErrors"
|
|
||||||
import { useCallback, useEffect, useState } from "react"
|
|
||||||
import { Eye } from "lucide-react"
|
|
||||||
import { toast } from "sonner"
|
|
||||||
import { getPublicFAQs } from "../../../api/faq.api"
|
|
||||||
import { Badge } from "../../../components/ui/badge"
|
|
||||||
import { Button } from "../../../components/ui/button"
|
|
||||||
import {
|
|
||||||
Dialog,
|
|
||||||
DialogContent,
|
|
||||||
DialogDescription,
|
|
||||||
DialogTitle,
|
|
||||||
} from "../../../components/ui/dialog"
|
|
||||||
import { SpinnerIcon } from "../../../components/ui/spinner-icon"
|
|
||||||
import { deriveFaqCategories } from "../../../lib/faqDisplay"
|
|
||||||
import type { FAQ } from "../../../types/faq.types"
|
|
||||||
|
|
||||||
type FaqPublicPreviewDialogProps = {
|
|
||||||
open: boolean
|
|
||||||
onOpenChange: (open: boolean) => void
|
|
||||||
}
|
|
||||||
|
|
||||||
export function FaqPublicPreviewDialog({
|
|
||||||
open,
|
|
||||||
onOpenChange,
|
|
||||||
}: FaqPublicPreviewDialogProps) {
|
|
||||||
const [loading, setLoading] = useState(false)
|
|
||||||
const [faqs, setFaqs] = useState<FAQ[]>([])
|
|
||||||
const [categoryFilter, setCategoryFilter] = useState("")
|
|
||||||
|
|
||||||
const load = useCallback(async () => {
|
|
||||||
setLoading(true)
|
|
||||||
try {
|
|
||||||
const res = await getPublicFAQs({ limit: 200, offset: 0 })
|
|
||||||
setFaqs(res.data.faqs)
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
setFaqs([])
|
|
||||||
notifyApiError(e, "Failed to load public FAQ preview")
|
|
||||||
} finally {
|
|
||||||
setLoading(false)
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (open) {
|
|
||||||
void load()
|
|
||||||
}
|
|
||||||
}, [open, load])
|
|
||||||
|
|
||||||
const categories = deriveFaqCategories(faqs)
|
|
||||||
const filtered = categoryFilter
|
|
||||||
? faqs.filter((faq) => faq.category === categoryFilter)
|
|
||||||
: faqs
|
|
||||||
|
|
||||||
const grouped = filtered.reduce<Record<string, FAQ[]>>((acc, faq) => {
|
|
||||||
const key = faq.category?.trim() || "General"
|
|
||||||
if (!acc[key]) acc[key] = []
|
|
||||||
acc[key].push(faq)
|
|
||||||
return acc
|
|
||||||
}, {})
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
||||||
<DialogContent className="max-h-[85vh] max-w-2xl overflow-hidden p-0">
|
|
||||||
<div className="flex items-center gap-3 border-b border-grayScale-100 px-6 py-5 pr-14">
|
|
||||||
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-[8px] bg-brand-100/60">
|
|
||||||
<Eye className="h-5 w-5 text-brand-600" />
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<DialogTitle className="text-[15px] font-semibold text-grayScale-900">
|
|
||||||
Preview as learner
|
|
||||||
</DialogTitle>
|
|
||||||
<DialogDescription className="mt-0.5 text-xs text-grayScale-500">
|
|
||||||
Published FAQs from GET /faqs (ACTIVE only, no authentication).
|
|
||||||
</DialogDescription>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="flex items-center gap-2 px-6 pt-4">
|
|
||||||
<label htmlFor="preview-category" className="text-xs font-medium text-grayScale-600">
|
|
||||||
Category
|
|
||||||
</label>
|
|
||||||
<select
|
|
||||||
id="preview-category"
|
|
||||||
value={categoryFilter}
|
|
||||||
onChange={(e) => setCategoryFilter(e.target.value)}
|
|
||||||
className="h-8 rounded-lg border border-grayScale-200 bg-white px-3 text-xs dark:border-grayScale-200 dark:bg-grayScale-100 dark:text-grayScale-900"
|
|
||||||
>
|
|
||||||
<option value="">All categories</option>
|
|
||||||
{categories.map((category) => (
|
|
||||||
<option key={category} value={category}>
|
|
||||||
{category}
|
|
||||||
</option>
|
|
||||||
))}
|
|
||||||
</select>
|
|
||||||
<Button variant="outline" size="sm" disabled={loading} onClick={() => void load()}>
|
|
||||||
Refresh
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="max-h-[55vh] space-y-4 overflow-y-auto px-6 py-5">
|
|
||||||
{loading ? (
|
|
||||||
<div className="flex justify-center py-12">
|
|
||||||
<SpinnerIcon className="h-6 w-6" />
|
|
||||||
</div>
|
|
||||||
) : filtered.length === 0 ? (
|
|
||||||
<p className="py-12 text-center text-sm text-grayScale-500">
|
|
||||||
No published FAQs are visible on the public help center.
|
|
||||||
</p>
|
|
||||||
) : (
|
|
||||||
Object.entries(grouped).map(([category, rows]) => (
|
|
||||||
<div key={category} className="space-y-2">
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<h3 className="text-sm font-semibold text-grayScale-800">{category}</h3>
|
|
||||||
<Badge variant="secondary" className="text-[10px]">
|
|
||||||
{rows.length}
|
|
||||||
</Badge>
|
|
||||||
</div>
|
|
||||||
<div className="space-y-2">
|
|
||||||
{rows.map((faq) => (
|
|
||||||
<div
|
|
||||||
key={faq.id}
|
|
||||||
className="rounded-[8px] border border-grayScale-100 bg-grayScale-50 px-4 py-3"
|
|
||||||
>
|
|
||||||
<p className="text-sm font-medium text-grayScale-900">{faq.question}</p>
|
|
||||||
<p className="mt-1 whitespace-pre-wrap text-xs leading-relaxed text-grayScale-600">
|
|
||||||
{faq.answer}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
))
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</DialogContent>
|
|
||||||
</Dialog>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
@ -1,80 +0,0 @@
|
||||||
export type FAQStatus = "ACTIVE" | "INACTIVE"
|
|
||||||
|
|
||||||
export interface FAQ {
|
|
||||||
id: number
|
|
||||||
question: string
|
|
||||||
answer: string
|
|
||||||
category?: string | null
|
|
||||||
display_order: number
|
|
||||||
status: FAQStatus
|
|
||||||
created_at: string
|
|
||||||
updated_at?: string | null
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface FAQListData {
|
|
||||||
faqs: FAQ[]
|
|
||||||
total_count: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface GetFAQsResponse {
|
|
||||||
message: string
|
|
||||||
data: FAQListData
|
|
||||||
success?: boolean
|
|
||||||
status_code?: number
|
|
||||||
metadata?: unknown | null
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface GetFAQResponse {
|
|
||||||
message: string
|
|
||||||
data: FAQ
|
|
||||||
success?: boolean
|
|
||||||
status_code?: number
|
|
||||||
metadata?: unknown | null
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface CreateFAQRequest {
|
|
||||||
question: string
|
|
||||||
answer: string
|
|
||||||
category?: string | null
|
|
||||||
display_order?: number
|
|
||||||
status?: FAQStatus
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UpdateFAQRequest {
|
|
||||||
question?: string
|
|
||||||
answer?: string
|
|
||||||
category?: string | null
|
|
||||||
display_order?: number
|
|
||||||
status?: FAQStatus
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface CreateFAQResponse {
|
|
||||||
message: string
|
|
||||||
data: FAQ
|
|
||||||
success?: boolean
|
|
||||||
status_code?: number
|
|
||||||
metadata?: unknown | null
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UpdateFAQResponse {
|
|
||||||
message: string
|
|
||||||
data: FAQ
|
|
||||||
success?: boolean
|
|
||||||
status_code?: number
|
|
||||||
metadata?: unknown | null
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface DeleteFAQResponse {
|
|
||||||
message: string
|
|
||||||
data?: { id: number }
|
|
||||||
success?: boolean
|
|
||||||
status_code?: number
|
|
||||||
metadata?: unknown | null
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface FAQFilters {
|
|
||||||
status?: FAQStatus
|
|
||||||
category?: string
|
|
||||||
limit?: number
|
|
||||||
offset?: number
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user