All files / src/components/admin/blogs BlogMetaForm.tsx

93.18% Statements 41/44
92.85% Branches 26/28
100% Functions 20/20
100% Lines 38/38

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                                                                                                            62x 62x 62x   62x   11x 11x 11x 11x 8x 8x 7x   1x             62x 43x 43x     62x 4x 4x 2x       62x 2x 2x     62x 2x 2x     62x                   4x                             2x                                                                                                     1x                                         1x                     1x                     1x                               1x           124x                             1x         124x                                   1x                             4x                  
import { useState, useEffect, useRef, useCallback } from "react";
import { CheckCircle2, AlertCircle, Loader2 } from "lucide-react";
import { Input } from "../../ui/input";
import { MultiSelect } from "../../ui/multi-select";
import type { BlogType, BlogCategory } from "../../../lib/api/blogs";
import { cn } from "../../../lib/utils";
import { slugify } from "../../../lib/slugify";
import { adminBlogsApi } from "../../../lib/api/admin/blogs";
 
interface BlogMetaFormProps {
	title: string;
	slug: string;
	blogId?: string;
	metaDescription: string;
	blogType: BlogType;
	primaryKeyword: string;
	secondaryKeywords: string[];
	categoryId: string;
	cityId: string;
	categories: BlogCategory[];
	cities: Array<{ id: string; name: string }>;
	onTitleChange: (value: string) => void;
	onSlugChange: (value: string) => void;
	onMetaDescriptionChange: (value: string) => void;
	onBlogTypeChange: (value: BlogType) => void;
	onPrimaryKeywordChange: (value: string) => void;
	onSecondaryKeywordsChange: (values: string[]) => void;
	onCategoryChange: (value: string) => void;
	onCityChange: (value: string) => void;
	className?: string;
}
 
export function BlogMetaForm({
	title,
	slug,
	blogId,
	metaDescription,
	blogType,
	primaryKeyword,
	secondaryKeywords,
	categoryId,
	cityId,
	categories,
	cities,
	onTitleChange,
	onSlugChange,
	onMetaDescriptionChange,
	onBlogTypeChange,
	onPrimaryKeywordChange,
	onSecondaryKeywordsChange,
	onCategoryChange,
	onCityChange,
	className,
}: BlogMetaFormProps) {
	const slugTouchedRef = useRef(!!slug);
	const [slugStatus, setSlugStatus] = useState<"idle" | "checking" | "available" | "taken">("idle");
	const slugCheckTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
 
	const checkSlugUniqueness = useCallback(
		(value: string) => {
			Iif (slugCheckTimer.current) clearTimeout(slugCheckTimer.current);
			Iif (!value) { setSlugStatus("idle"); return; }
			setSlugStatus("checking");
			slugCheckTimer.current = setTimeout(async () => {
				try {
					const res = await adminBlogsApi.checkBlogSlug(value, blogId || undefined);
					setSlugStatus(res.data?.available ? "available" : "taken");
				} catch {
					setSlugStatus("idle");
				}
			}, 400);
		},
		[blogId],
	);
 
	useEffect(() => {
		if (slug) checkSlugUniqueness(slug);
		return () => { if (slugCheckTimer.current) clearTimeout(slugCheckTimer.current); };
	}, [slug, checkSlugUniqueness]);
 
	const handleTitleChange = (value: string) => {
		onTitleChange(value);
		if (!slugTouchedRef.current) {
			onSlugChange(slugify(value));
		}
	};
 
	const handleSlugInput = (value: string) => {
		slugTouchedRef.current = true;
		onSlugChange(slugify(value));
	};
 
	const generateSlug = () => {
		slugTouchedRef.current = true;
		onSlugChange(slugify(title));
	};
 
	return (
		<div className={cn("space-y-6", className)}>
			{/* Title */}
			<div>
				<label htmlFor="title" className="block text-sm font-medium mb-2">
					Title <span className="text-error">*</span>
				</label>
				<Input
					id="title"
					value={title}
					onChange={(e) => handleTitleChange(e.target.value)}
					placeholder="How to Arrange Sofas in a Small Living Room"
					required
				/>
			</div>
 
			{/* Blog Unique ID */}
			<div>
				<label htmlFor="slug" className="block text-sm font-medium mb-2">
					Blog Unique ID
				</label>
				<div className="flex gap-2">
					<Input
						id="slug"
						value={slug}
						onChange={(e) => handleSlugInput(e.target.value)}
						placeholder="how-to-arrange-sofas-small-living-room"
						className={
							slugStatus === "taken"
								? "border-red-400 focus:ring-red-500/40"
								: slugStatus === "available"
									? "border-green-400 focus:ring-green-500/40"
									: ""
						}
					/>
					<button
						type="button"
						onClick={generateSlug}
						className="px-4 py-2 text-sm border border-border-default rounded-md hover:bg-background-muted"
					>
						Generate
					</button>
				</div>
				{slugStatus === "checking" && (
					<p className="mt-1 text-xs text-foreground-subtle flex items-center gap-1">
						<Loader2 className="h-3 w-3 animate-spin" /> Checking...
					</p>
				)}
				{slugStatus === "available" && (
					<p className="mt-1 text-xs text-green-600 flex items-center gap-1">
						<CheckCircle2 className="h-3 w-3" /> Identifier is available
					</p>
				)}
				{slugStatus === "taken" && (
					<p className="mt-1 text-xs text-red-600 flex items-center gap-1">
						<AlertCircle className="h-3 w-3" /> This identifier is already taken
					</p>
				)}
				{slugStatus === "idle" && (
					<p className="mt-1 text-xs text-foreground-subtle">
						URL: /blog/{slug || "your-unique-id-here"}
					</p>
				)}
			</div>
 
			{/* Meta Description */}
			<div>
				<label
					htmlFor="metaDescription"
					className="block text-sm font-medium mb-2"
				>
					Meta Description
				</label>
				<textarea
					id="metaDescription"
					value={metaDescription}
					onChange={(e) => onMetaDescriptionChange(e.target.value)}
					placeholder="A compelling description for search results (max 160 characters)"
					maxLength={160}
					rows={3}
					className="flex w-full rounded-md border border-border-default bg-background-elevated px-3 py-2 text-sm text-foreground-default placeholder:text-foreground-subtle focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
				/>
				<p className="mt-1 text-xs text-foreground-subtle">
					{metaDescription.length}/160 characters
				</p>
			</div>
 
			{/* Blog Type */}
			<fieldset>
				<legend className="block text-sm font-medium mb-2">Blog Type</legend>
				<div className="flex gap-4">
					<label className="flex items-center gap-2 cursor-pointer">
						<input
							type="radio"
							name="blogType"
							value="general"
							checked={blogType === "general"}
							onChange={(e) => onBlogTypeChange(e.target.value as BlogType)}
							className="h-4 w-4"
						/>
						<span className="text-sm">General/Educational</span>
					</label>
					<label className="flex items-center gap-2 cursor-pointer">
						<input
							type="radio"
							name="blogType"
							value="project_spotlight"
							checked={blogType === "project_spotlight"}
							onChange={(e) => onBlogTypeChange(e.target.value as BlogType)}
							className="h-4 w-4"
						/>
						<span className="text-sm">Project Spotlight</span>
					</label>
					<label className="flex items-center gap-2 cursor-pointer">
						<input
							type="radio"
							name="blogType"
							value="hybrid"
							checked={blogType === "hybrid"}
							onChange={(e) => onBlogTypeChange(e.target.value as BlogType)}
							className="h-4 w-4"
						/>
						<span className="text-sm">Hybrid</span>
					</label>
				</div>
			</fieldset>
 
			{/* Category */}
			<div>
				<label htmlFor="category" className="block text-sm font-medium mb-2">
					Category <span className="text-error">*</span>
				</label>
				<select
					id="category"
					value={categoryId}
					onChange={(e) => onCategoryChange(e.target.value)}
					className="flex h-10 w-full rounded-md border border-border-default bg-background-elevated px-3 py-2 text-sm text-foreground-default focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
					required
				>
					<option value="">Select a category</option>
					{categories.map((cat) => (
						<option key={cat.id} value={cat.id}>
							{cat.name}
						</option>
					))}
				</select>
			</div>
 
			{/* City */}
			<div>
				<label htmlFor="city" className="block text-sm font-medium mb-2">
					City (Optional)
				</label>
				<select
					id="city"
					value={cityId}
					onChange={(e) => onCityChange(e.target.value)}
					className="flex h-10 w-full rounded-md border border-border-default bg-background-elevated px-3 py-2 text-sm text-foreground-default focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
				>
					<option value="">No specific city</option>
					{cities.map((city) => (
						<option key={city.id} value={city.id}>
							{city.name}
						</option>
					))}
				</select>
			</div>
 
			{/* Keywords */}
			<div>
				<label
					htmlFor="primaryKeyword"
					className="block text-sm font-medium mb-2"
				>
					Keywords
				</label>
				<Input
					id="primaryKeyword"
					value={primaryKeyword}
					onChange={(e) => onPrimaryKeywordChange(e.target.value)}
					placeholder="arrange sofa small living room"
				/>
			</div>
 
			{/* Secondary Keywords */}
			<div>
				<label
					htmlFor="secondary-keywords"
					className="block text-sm font-medium mb-2"
				>
					Secondary Keywords
				</label>
				<MultiSelect
					id="secondary-keywords"
					options={secondaryKeywords.map((kw) => ({ value: kw, label: kw }))}
					selected={secondaryKeywords}
					onChange={onSecondaryKeywordsChange}
					placeholder="Type and press Enter to add keywords"
				/>
			</div>
		</div>
	);
}