Skip to content

Commit b8b6db9

Browse files
committed
.
1 parent 349d435 commit b8b6db9

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

app/recruitment/actions.js

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,59 @@
11
"use server";
2-
32
import { writeClient } from "@/lib/sanity/writeClient";
43

54
export async function submitApplication(formData) {
65
try {
76
const name = formData.get("name");
87
const email = formData.get("email");
98
const positionId = formData.get("positionId");
9+
const questionKeysRaw = formData.get("questionKeys");
10+
11+
if (!questionKeysRaw) {
12+
throw new Error("Missing form structure data.");
13+
}
1014

11-
const questionKeys = JSON.parse(formData.get("questionKeys"));
15+
const questionKeys = JSON.parse(questionKeysRaw);
16+
const answers = [];
1217

13-
const answers = await Promise.all(
14-
questionKeys.map(async (q) => {
15-
const fieldData = {
16-
_key: Math.random().toString(36).substring(2),
17-
question: q.text,
18-
};
18+
for (const q of questionKeys) {
19+
const fieldData = {
20+
_key: Math.random().toString(36).substring(2, 11),
21+
question: q.text,
22+
};
1923

20-
const value = formData.get(q.text);
24+
const value = formData.get(q.text);
2125

22-
if (q.type === "image upload") {
23-
if (value && value instanceof File && value.size > 0) {
26+
if (q.type === "image upload") {
27+
if (value && value instanceof File && value.size > 0) {
28+
try {
2429
const asset = await writeClient.assets.upload("image", value, {
2530
filename: value.name,
31+
contentType: value.type,
2632
});
33+
2734
fieldData.imageAnswer = {
2835
_type: "image",
2936
asset: {
3037
_type: "reference",
3138
_ref: asset._id,
3239
},
3340
};
41+
} catch (uploadError) {
42+
console.error(
43+
`Image upload failed for question: ${q.text}`,
44+
uploadError,
45+
);
46+
throw new Error(
47+
`Failed to upload image for "${q.text}". Please try a smaller file.`,
48+
);
3449
}
35-
} else {
36-
fieldData.textAnswer = value;
3750
}
51+
} else {
52+
fieldData.textAnswer = value?.toString() || "";
53+
}
3854

39-
return fieldData;
40-
}),
41-
);
55+
answers.push(fieldData);
56+
}
4257

4358
await writeClient.create({
4459
_type: "application",
@@ -54,11 +69,13 @@ export async function submitApplication(formData) {
5469

5570
return { success: true };
5671
} catch (error) {
57-
console.error("Submission Error:", error);
72+
console.error("CRITICAL SUBMISSION ERROR:", error);
73+
5874
return {
5975
success: false,
6076
error:
61-
error.message || "An error occurred while submitting your application.",
77+
error.message ||
78+
"A server error occurred. Please check your connection and try again.",
6279
};
6380
}
6481
}

components/RecruitmentForm.jsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { signOut } from "next-auth/react";
44
import RenderImage from "./RenderImage";
55
import { submitApplication } from "@/app/recruitment/actions";
66

7+
const MAX_FILE_SIZE = 10 * 1024 * 1024;
8+
79
export default function RecruitmentForm({ positions, user }) {
810
const [selectedPosition, setSelectedPosition] = useState(positions[0]);
911
const [status, setStatus] = useState("idle");
@@ -20,6 +22,19 @@ export default function RecruitmentForm({ positions, user }) {
2022
type: q.fieldType,
2123
}));
2224

25+
for (const q of questionKeys) {
26+
if (q.type === "image upload") {
27+
const file = formData.get(q.text);
28+
if (file && file instanceof File && file.size > MAX_FILE_SIZE) {
29+
setStatus("error");
30+
setErrorMessage(
31+
`The file for "${q.text}" is too large (Max 10MB). Please upload a smaller image.`
32+
);
33+
return;
34+
}
35+
}
36+
}
37+
2338
formData.append("questionKeys", JSON.stringify(questionKeys));
2439
formData.append("positionId", selectedPosition._id);
2540

@@ -32,8 +47,9 @@ export default function RecruitmentForm({ positions, user }) {
3247
setErrorMessage(result.error || "Failed to submit application.");
3348
}
3449
} catch (err) {
50+
console.error("Client-side submission error:", err);
3551
setStatus("error");
36-
setErrorMessage("An unexpected error occurred. Please try again.");
52+
setErrorMessage("An unexpected error occurred. Please check your internet connection.");
3753
}
3854
}
3955

@@ -70,7 +86,7 @@ export default function RecruitmentForm({ positions, user }) {
7086
Submit another application
7187
</button>
7288
<button
73-
onClick={() => signOut({ callbackUrl: "/recruitment" })} //
89+
onClick={() => signOut({ callbackUrl: "/recruitment" })}
7490
className="text-xs font-bold uppercase tracking-widest text-gray-500 hover:text-red-500 transition-colors"
7591
>
7692
Sign Out
@@ -175,6 +191,7 @@ export default function RecruitmentForm({ positions, user }) {
175191
className="w-full cursor-pointer text-sm text-gray-400 file:mr-4 file:rounded-full file:border-0 file:bg-neon-green/10 file:px-4 file:py-2 file:text-xs file:font-bold file:text-neon-green hover:file:bg-neon-green hover:file:text-darkest transition-all"
176192
required
177193
/>
194+
<p className="text-[10px] text-gray-600 mt-1">Max file size: 10MB</p>
178195
</div>
179196
)}
180197
</div>

0 commit comments

Comments
 (0)