"use client";
import { useState, useRef, type FormEvent, type ChangeEvent } from "react";
import { Link, useLocale, useTranslation } from "@/app/digital/i18n/navigation";
const positionOptions = [
{ value: "content", labelZh: "内容运营", labelEn: "Content Operations" },
{ value: "dev", labelZh: "前端/全栈开发", labelEn: "Frontend / Full-stack Dev" },
{ value: "design", labelZh: "UI/UX 设计", labelEn: "UI/UX Design" },
{ value: "marketing", labelZh: "市场推广", labelEn: "Marketing" },
{ value: "other", labelZh: "其他", labelEn: "Other" },
];
interface JobFormData {
position: string;
name: string;
email: string;
phone: string;
intro: string;
}
function Field({
label,
children,
}: {
label: string;
children: React.ReactNode;
}) {
return (
);
}
function ChevronDown() {
return (
);
}
export default function JobsContent() {
const { t } = useTranslation("jobs");
const locale = useLocale();
const [form, setForm] = useState({
position: "",
name: "",
email: "",
phone: "",
intro: "",
});
const [resume, setResume] = useState(null);
const [resumeError, setResumeError] = useState(null);
const [submitting, setSubmitting] = useState(false);
const [submitMessage, setSubmitMessage] = useState("");
const fileRef = useRef(null);
const set = (key: keyof JobFormData, value: string) =>
setForm((prev) => ({ ...prev, [key]: value }));
const handleSubmit = async (e: FormEvent) => {
e.preventDefault();
setResumeError(null);
setSubmitMessage("");
if (!resume) {
setResumeError(locale === "zh" ? "请上传简历(PDF 或 Word)" : "Please upload your resume (PDF or Word)");
return;
}
setSubmitting(true);
try {
const uploadForm = new window.FormData();
uploadForm.append("file", resume);
const uploadRes = await fetch("/api/digital/upload-media", {
method: "POST",
body: uploadForm,
});
const uploadData = await uploadRes.json().catch(() => ({}));
if (!uploadRes.ok || !uploadData?.url) {
throw new Error(uploadData?.error || (locale === "zh" ? "简历上传失败" : "Resume upload failed"));
}
const submitRes = await fetch("/api/digital/jobs", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ...form, resumeUrl: uploadData.url }),
});
const submitData = await submitRes.json().catch(() => ({}));
if (!submitRes.ok || !submitData?.ok) {
throw new Error(submitData?.error || (locale === "zh" ? "提交失败" : "Submit failed"));
}
setForm({ position: "", name: "", email: "", phone: "", intro: "" });
removeResume();
setSubmitMessage(locale === "zh" ? "申请已提交,我们会尽快联系你。" : "Application submitted. We will contact you soon.");
} catch (error) {
setResumeError(error instanceof Error ? error.message : (locale === "zh" ? "提交失败" : "Submit failed"));
} finally {
setSubmitting(false);
}
};
const handleFileChange = (e: ChangeEvent) => {
const file = e.target.files?.[0];
if (!file) return;
const ext = file.name.split(".").pop()?.toLowerCase();
const allowed = ["pdf", "doc", "docx"];
if (!ext || !allowed.includes(ext)) {
setResumeError(locale === "zh" ? "请上传 PDF、DOC 或 DOCX 格式" : "Please upload PDF, DOC or DOCX");
return;
}
if (file.size > 10 * 1024 * 1024) {
setResumeError(locale === "zh" ? "文件大小不能超过 10MB" : "File size must be under 10MB");
return;
}
setResumeError(null);
setResume(file);
};
const removeResume = () => {
setResume(null);
setResumeError(null);
if (fileRef.current) fileRef.current.value = "";
};
const getLabel = (opt: (typeof positionOptions)[0]) =>
locale === "zh" ? opt.labelZh : opt.labelEn;
return (
{/* Banner */}
💼
{locale === "zh" ? "加入我们" : "Join Us"}
{t("title")}
{t("subtitle")}
{/* Form */}
← {locale === "zh" ? "返回首页" : "Back to Home"}
);
}