All files / src/pages/admin/blogs suggestions.tsx

98.18% Statements 54/55
93.75% Branches 30/32
100% Functions 15/15
100% Lines 53/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312                                62x 62x 62x 62x 62x     62x 62x   62x   62x     62x   62x 5x   4x 4x     4x             3x 1x       2x           2x   2x   4x       62x 8x 8x 8x     62x 4x   4x 4x 4x       3x 3x 3x 3x   1x   4x       62x 1x     62x 54x             54x                 62x                             3x                           1x                                           54x                                                                                       5x               8x                     1x                   1x                                                                             1x                               1x 1x 1x                                  
import { useState } from "react";
import { notify } from "../../../lib/notify";
import { useNavigate } from "@tanstack/react-router";
import { useQueryClient } from "@tanstack/react-query";
import { Check, X, Eye } from "lucide-react";
 
import { Card, CardContent } from "../../../components/ui/card";
import { Button } from "../../../components/ui/button";
import { GENERIC_ERROR_MESSAGE } from "../../../lib/api";
import { adminApi } from "../../../lib/api/admin";
import type { ProBlogSuggestion } from "../../../lib/api/blogs";
import { queryKeys } from "../../../lib/query-keys";
import { useAdminBlogSuggestions } from "../../../hooks/queries/useAdminBlogQueries";
import { useConfirmDialog } from "../../../hooks";
 
export function AdminBlogSuggestionsPage() {
	const navigate = useNavigate();
	const queryClient = useQueryClient();
	const { confirm, dialog: confirmDialog } = useConfirmDialog();
	const [filterStatus, setFilterStatus] = useState<string>("pending");
	const [processingId, setProcessingId] = useState<string | null>(null);
 
	// Modal state
	const [showRejectModal, setShowRejectModal] = useState(false);
	const [rejectingSuggestion, setRejectingSuggestion] =
		useState<ProBlogSuggestion | null>(null);
	const [rejectReason, setRejectReason] = useState("");
 
	const { data: suggestions = [], isLoading: loading, error: queryError } = useAdminBlogSuggestions(
		filterStatus ? { status: filterStatus } : undefined,
	);
	const loadError = queryError ? "Failed to load suggestions. Please try again." : null;
 
	const handleAccept = async (suggestion: ProBlogSuggestion) => {
		if (!(await confirm({ title: "Accept suggestion", description: "Create a draft blog from this suggestion?", confirmLabel: "Accept", variant: "default" }))) return;
 
		try {
			setProcessingId(suggestion.id);
 
			// Create a draft blog from the suggestion
			const blog = await adminApi.createBlog({
				title: `[Draft] ${suggestion.topicDescription.substring(0, 60)}...`,
				blogType: "general",
				ideaSource: "pro_request",
				ideaSourceProId: suggestion.proId,
			});
 
			if (!blog.data) {
				throw new Error("Failed to create blog");
			}
 
			// Update suggestion status
			await adminApi.updateProBlogSuggestion(suggestion.id, {
				status: "accepted",
				acceptedBlogId: blog.data.id,
			});
 
			// Navigate to edit the new blog
			navigate({ to: `/admin/blogs/${blog.data.id}/edit` });
		} catch (_error) {
			notify.error(GENERIC_ERROR_MESSAGE);
		} finally {
			setProcessingId(null);
		}
	};
 
	const handleReject = (suggestion: ProBlogSuggestion) => {
		setRejectingSuggestion(suggestion);
		setRejectReason("");
		setShowRejectModal(true);
	};
 
	const confirmReject = async () => {
		Iif (!rejectingSuggestion) return;
 
		try {
			setProcessingId(rejectingSuggestion.id);
			await adminApi.updateProBlogSuggestion(rejectingSuggestion.id, {
				status: "rejected",
				adminNotes: rejectReason,
			});
			await queryClient.invalidateQueries({ queryKey: queryKeys.admin.blogs.suggestions() });
			setShowRejectModal(false);
			setRejectingSuggestion(null);
			setRejectReason("");
		} catch (_error) {
			notify.error(GENERIC_ERROR_MESSAGE);
		} finally {
			setProcessingId(null);
		}
	};
 
	const handleViewPro = (proId: string) => {
		navigate({ to: `/admin/pros/${proId}` });
	};
 
	const getStatusBadge = (status: string) => {
		const colors: Record<string, string> = {
			pending: "bg-yellow-100 text-yellow-800",
			accepted: "bg-green-100 text-green-800",
			rejected: "bg-red-100 text-red-800",
			published: "bg-blue-100 text-blue-800",
		};
 
		return (
			<span
				className={`inline-flex items-center px-2 py-1 rounded text-xs font-medium ${colors[status] || "bg-background-subtle text-foreground-default"}`}
			>
				{status}
			</span>
		);
	};
 
	return (
		<>
			<div className="space-y-6">
				{/* Header */}
				<div>
					<h1 className="text-2xl font-bold">Pro Blog Suggestions</h1>
					<p className="text-foreground-subtle mt-1">
						Review topic ideas submitted by pros
					</p>
				</div>
 
				{/* Filters */}
				<div className="flex gap-4">
					<select
						value={filterStatus}
						onChange={(e) => setFilterStatus(e.target.value)}
						className="h-10 rounded-md border border-border-default bg-background-elevated px-3 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
					>
						<option value="">All Statuses</option>
						<option value="pending">Pending</option>
						<option value="accepted">Accepted</option>
						<option value="rejected">Rejected</option>
						<option value="published">Published</option>
					</select>
				</div>
 
				{loadError && (
					<div className="p-4 bg-notification-error-bg border border-notification-error-border rounded-lg text-notification-error-text flex items-center justify-between">
						<span>{loadError}</span>
						<button type="button" onClick={() => queryClient.invalidateQueries({ queryKey: queryKeys.admin.blogs.suggestions() })} className="text-sm underline hover:no-underline">
							Retry
						</button>
					</div>
				)}
 
				{/* Suggestions List */}
				<div className="space-y-4">
					{loading ? (
						<Card>
							<CardContent className="p-12 text-center">
								<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-600 mx-auto" />
							</CardContent>
						</Card>
					) : suggestions.length === 0 && !loadError ? (
						<Card>
							<CardContent className="p-12 text-center">
								<p className="text-foreground-subtle">No suggestions found</p>
							</CardContent>
						</Card>
					) : (
						suggestions.map((suggestion) => (
							<Card key={suggestion.id}>
								<CardContent className="p-6">
									<div className="flex items-start justify-between gap-4">
										<div className="flex-1">
											<div className="flex items-center gap-3 mb-2">
												<h3 className="text-lg font-medium">
													Topic Suggestion
												</h3>
												{getStatusBadge(suggestion.status)}
											</div>
											<p className="text-foreground-default mb-4">
												{suggestion.topicDescription}
											</p>
											<div className="flex items-center gap-4 text-sm text-foreground-subtle">
												<span>
													Submitted:{" "}
													{new Date(suggestion.createdAt).toLocaleDateString()}
												</span>
												{suggestion.reviewedAt && (
													<span>
														Reviewed:{" "}
														{new Date(
															suggestion.reviewedAt,
														).toLocaleDateString()}
													</span>
												)}
											</div>
											{suggestion.adminNotes && (
												<div className="mt-3 p-3 bg-background-muted rounded-md">
													<p className="text-sm font-medium mb-1">
														Admin Notes:
													</p>
													<p className="text-sm text-foreground-subtle">
														{suggestion.adminNotes}
													</p>
												</div>
											)}
										</div>
 
										{/* Actions */}
										<div className="flex flex-col gap-2 min-w-[140px]">
											{suggestion.status === "pending" && (
												<>
													<Button
														onClick={() => handleAccept(suggestion)}
														disabled={processingId === suggestion.id}
														size="sm"
													>
														<Check className="h-4 w-4" />
														Accept
													</Button>
													<Button
														onClick={() => handleReject(suggestion)}
														disabled={processingId === suggestion.id}
														variant="outline"
														size="sm"
													>
														<X className="h-4 w-4" />
														Reject
													</Button>
												</>
											)}
											<Button
												onClick={() => handleViewPro(suggestion.proId)}
												variant="ghost"
												size="sm"
											>
												<Eye className="h-4 w-4" />
												View Pro
											</Button>
											{suggestion.acceptedBlogId && (
												<Button
													onClick={() =>
														navigate({
															to: `/admin/blogs/${suggestion.acceptedBlogId}/edit`,
														})
													}
													variant="ghost"
													size="sm"
												>
													View Blog
												</Button>
											)}
										</div>
									</div>
								</CardContent>
							</Card>
						))
					)}
				</div>
 
				{/* Reject Modal */}
				{showRejectModal && rejectingSuggestion && (
					<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
						<Card className="w-full max-w-lg mx-4">
							<CardContent className="p-6">
								<h2 className="text-lg font-semibold mb-4">
									Reject Suggestion
								</h2>
								<p className="text-sm text-foreground-subtle mb-4">
									{rejectingSuggestion.topicDescription}
								</p>
								<div className="mb-4">
									<label
										htmlFor="rejectReason"
										className="block text-sm font-medium mb-2"
									>
										Reason for rejection (optional)
									</label>
									<textarea
										id="rejectReason"
										value={rejectReason}
										onChange={(e) => setRejectReason(e.target.value)}
										rows={4}
										className="flex w-full rounded-md border border-border-default bg-background-elevated px-3 py-2 text-sm"
										placeholder="Let the pro know why this topic wasn't suitable..."
									/>
								</div>
								<div className="flex gap-3">
									<Button
										onClick={confirmReject}
										disabled={processingId === rejectingSuggestion.id}
										className="flex-1"
									>
										Reject
									</Button>
									<Button
										onClick={() => {
											setShowRejectModal(false);
											setRejectingSuggestion(null);
											setRejectReason("");
										}}
										variant="outline"
										className="flex-1"
									>
										Cancel
									</Button>
								</div>
							</CardContent>
						</Card>
					</div>
				)}
			</div>
			{confirmDialog}
		</>
	);
}