Enhance image upload functionality for admin and improve course navigation. Added support for image uploads in course forms and generic forms, utilizing a new ImageUploadField component. Updated navigation to dynamically include course tags and refined layout components for better user experience. Configured Docker for persistent uploads with a dedicated volume.
This commit is contained in:
1 parent
ef1ccea0ad
commit
66a8e8f213
29 files changed
+748
-125
No files matched your search
@@ -2,10 +2,11 @@ import { getTranslations } from "next-intl/server";
|
||||
import { CampaignBanners } from "@/components/organisms/CampaignBanners";
|
||||
import { SiteFooter } from "@/components/organisms/SiteFooter";
|
||||
import { SiteHeader } from "@/components/organisms/SiteHeader";
|
||||
import { getCourses } from "@/lib/data/courses";
|
||||
import { buildHomeNavWithCourseTags } from "@/lib/course-nav";
|
||||
import { getCourses, listPublishedCourseTags } from "@/lib/data/courses";
|
||||
import { getPrograms } from "@/lib/data/programs";
|
||||
import { getCampaignBanners } from "@/lib/data/site";
|
||||
import { homeNav, siteShortName } from "@/lib/site-config";
|
||||
import { courseNavTagOrder, siteShortName } from "@/lib/site-config";
|
||||
|
||||
// Public CMS pages must read Postgres at request time — static bake at Docker
|
||||
// build often has no DATABASE_URL and ships empty lists.
|
||||
@@ -21,20 +22,25 @@ export default async function MainLayout({
|
||||
let courses: Awaited<ReturnType<typeof getCourses>> = [];
|
||||
let programs: Awaited<ReturnType<typeof getPrograms>> = [];
|
||||
let banners: Awaited<ReturnType<typeof getCampaignBanners>> = [];
|
||||
let courseTags: string[] = [...courseNavTagOrder];
|
||||
try {
|
||||
[courses, programs, banners] = await Promise.all([
|
||||
[courses, programs, banners, courseTags] = await Promise.all([
|
||||
getCourses(),
|
||||
getPrograms(),
|
||||
getCampaignBanners(),
|
||||
listPublishedCourseTags(),
|
||||
]);
|
||||
if (courseTags.length === 0) courseTags = [...courseNavTagOrder];
|
||||
} catch (err) {
|
||||
console.error("[main/layout] CMS fetch failed:", err);
|
||||
}
|
||||
|
||||
const navItems = buildHomeNavWithCourseTags(courseTags);
|
||||
|
||||
return (
|
||||
<>
|
||||
<SiteHeader
|
||||
items={homeNav}
|
||||
items={navItems}
|
||||
searchLabel={t("nav.search")}
|
||||
brandTitle={siteShortName}
|
||||
brandSubtitle={t("nav.brandSubtitle")}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { requireAdmin } from "@/lib/auth/admin";
|
||||
import { errorResponse } from "@/lib/api/response";
|
||||
import { saveUploadedImage } from "@/lib/uploads/image";
|
||||
import { isUploadFolder, type UploadFolder } from "@/lib/uploads/types";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
export const runtime = "nodejs";
|
||||
|
||||
// POST /api/admin/upload — multipart image upload (admin only)
|
||||
export async function POST(request: NextRequest) {
|
||||
const session = await requireAdmin();
|
||||
if (!session) return errorResponse("عدم دسترسی — ورود لازم است", 401);
|
||||
|
||||
let formData: FormData;
|
||||
try {
|
||||
formData = await request.formData();
|
||||
} catch {
|
||||
return errorResponse("بدنهٔ درخواست معتبر نیست", 400);
|
||||
}
|
||||
|
||||
const file = formData.get("file");
|
||||
if (!(file instanceof File)) {
|
||||
return errorResponse("فیلد file الزامی است", 400);
|
||||
}
|
||||
|
||||
const folderRaw = String(formData.get("folder") ?? "general");
|
||||
if (!isUploadFolder(folderRaw)) {
|
||||
return errorResponse("پوشهٔ مقصد نامعتبر است", 400);
|
||||
}
|
||||
const folder: UploadFolder = folderRaw;
|
||||
|
||||
const result = await saveUploadedImage(file, folder);
|
||||
if (!result.ok) {
|
||||
return errorResponse(result.error, result.status);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ ok: true, data: { url: result.url } },
|
||||
{ status: 201 },
|
||||
);
|
||||
}
|
||||
@@ -1,9 +1,15 @@
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { SiteFooter } from "@/components/organisms/SiteFooter";
|
||||
import { SiteHeader } from "@/components/organisms/SiteHeader";
|
||||
import { getCourses } from "@/lib/data/courses";
|
||||
import { buildCourseNavChildren } from "@/lib/course-nav";
|
||||
import { getCourses, listPublishedCourseTags } from "@/lib/data/courses";
|
||||
import { getPrograms } from "@/lib/data/programs";
|
||||
import { professionalNav, siteShortName } from "@/lib/site-config";
|
||||
import {
|
||||
courseNavParentTag,
|
||||
courseNavTagOrder,
|
||||
professionalNav,
|
||||
siteShortName,
|
||||
} from "@/lib/site-config";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
@@ -16,16 +22,31 @@ export default async function ProfessionalLayout({
|
||||
const year = new Date().getFullYear();
|
||||
let courses: Awaited<ReturnType<typeof getCourses>> = [];
|
||||
let programs: Awaited<ReturnType<typeof getPrograms>> = [];
|
||||
let courseTags: string[] = [...courseNavTagOrder];
|
||||
try {
|
||||
[courses, programs] = await Promise.all([getCourses(), getPrograms()]);
|
||||
[courses, programs, courseTags] = await Promise.all([
|
||||
getCourses(),
|
||||
getPrograms(),
|
||||
listPublishedCourseTags(),
|
||||
]);
|
||||
if (courseTags.length === 0) courseTags = [...courseNavTagOrder];
|
||||
} catch (err) {
|
||||
console.error("[professional/layout] CMS fetch failed:", err);
|
||||
}
|
||||
|
||||
const navItems = professionalNav.map((item) =>
|
||||
item.label === courseNavParentTag
|
||||
? {
|
||||
...item,
|
||||
children: buildCourseNavChildren(courseTags),
|
||||
}
|
||||
: item,
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<SiteHeader
|
||||
items={professionalNav}
|
||||
items={navItems}
|
||||
searchLabel={t("nav.search")}
|
||||
brandTitle={siteShortName}
|
||||
brandSubtitle={t("nav.brandSubtitle")}
|
||||
|
||||
@@ -20,8 +20,14 @@ export async function generateMetadata(): Promise<Metadata> {
|
||||
};
|
||||
}
|
||||
|
||||
export default async function ProfessionalPage() {
|
||||
export default async function ProfessionalPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<{ tag?: string }>;
|
||||
}) {
|
||||
const t = await getTranslations();
|
||||
const { tag: tagRaw } = await searchParams;
|
||||
const initialTag = tagRaw?.trim() ?? "";
|
||||
const [courses, stats, testimonials] = await Promise.all([
|
||||
getCourses(),
|
||||
getSiteStats(),
|
||||
@@ -50,6 +56,7 @@ export default async function ProfessionalPage() {
|
||||
courses={courses}
|
||||
stats={stats}
|
||||
testimonials={testimonials}
|
||||
initialTag={initialTag}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user