Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 70 additions & 3 deletions app/(app)/jobs/create/_client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ import { Strong, Text } from "@/components/ui-components/text";
import { Textarea } from "@/components/ui-components/textarea";
import { saveJobsInput, saveJobsSchema } from "@/schema/job";
import { FEATURE_FLAGS, isFlagEnabled } from "@/utils/flags";
import { uploadFile } from "@/utils/s3helpers";
import { getUploadUrl } from "@/app/actions/getUploadUrl";
import { zodResolver } from "@hookform/resolvers/zod";
import Image from "next/image";
import { notFound } from "next/navigation";
import React, { useRef, useState } from "react";
import { Controller, SubmitHandler, useForm } from "react-hook-form";
import * as Sentry from "@sentry/nextjs";
import { toast } from "sonner";

export default function Content() {
const {
Expand All @@ -54,8 +58,70 @@ export default function Content() {
const flagEnabled = isFlagEnabled(FEATURE_FLAGS.JOBS);
const fileInputRef = useRef<HTMLInputElement>(null);
const [imgUrl, setImgUrl] = useState<string | null>(null);
const [uploadStatus, setUploadStatus] = useState<
"idle" | "loading" | "success" | "error"
>("idle");
const onSubmit: SubmitHandler<saveJobsInput> = (values) => {
console.log(values);
const formData = {
...values,
companyLogo: imgUrl || undefined,
};
console.log(formData);
};

const handleLogoUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
if (uploadStatus === "loading") {
return toast.info("Upload in progress, please wait...");
}

if (e.target.files && e.target.files.length > 0) {
setUploadStatus("loading");

const file = e.target.files[0];
const { size, type } = file;

if (size > 1048576) {
setUploadStatus("error");
return toast.error("File size too big (max 1MB).");
}

try {
const res = await getUploadUrl({
size,
type,
uploadType: "uploads",
});

const signedUrl = res?.data;

if (!signedUrl) {
setUploadStatus("error");
return toast.error(
"Something went wrong uploading the logo, please retry.",
);
}

const { fileLocation } = await uploadFile(signedUrl, file);
if (!fileLocation) {
setUploadStatus("error");
return toast.error(
"Something went wrong uploading the logo, please retry.",
);
}

setUploadStatus("success");
setImgUrl(fileLocation);
toast.success("Company logo uploaded successfully!");
} catch (error) {
setUploadStatus("error");
toast.error(
error instanceof Error
? error.message
: "An error occurred while uploading the logo.",
);
Sentry.captureException(error);
}
}
};
Comment on lines +72 to 125
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

File input should be reset after upload to allow re-uploading the same file.

After a successful (or failed) upload, the file input retains its value. If a user attempts to upload the same file again, the onChange event won't fire because the input value hasn't changed.

Add input reset after setting upload status:

   try {
     const res = await getUploadUrl({
       size,
       type,
       uploadType: "uploads",
     });

     const signedUrl = res?.data;

     if (!signedUrl) {
       setUploadStatus("error");
+      if (fileInputRef.current) fileInputRef.current.value = "";
       return toast.error(
         "Something went wrong uploading the logo, please retry.",
       );
     }

     const { fileLocation } = await uploadFile(signedUrl, file);
     if (!fileLocation) {
       setUploadStatus("error");
+      if (fileInputRef.current) fileInputRef.current.value = "";
       return toast.error(
         "Something went wrong uploading the logo, please retry.",
       );
     }

     setUploadStatus("success");
     setImgUrl(fileLocation);
+    if (fileInputRef.current) fileInputRef.current.value = "";
     toast.success("Company logo uploaded successfully!");
   } catch (error) {
     setUploadStatus("error");
+    if (fileInputRef.current) fileInputRef.current.value = "";
     toast.error(

Committable suggestion skipped: line range outside the PR's diff.

if (!flagEnabled) {
notFound();
Expand Down Expand Up @@ -89,15 +155,16 @@ export default function Content() {
onClick={() => {
fileInputRef.current?.click();
}}
disabled={uploadStatus === "loading"}
>
Change Logo
{uploadStatus === "loading" ? "Uploading..." : "Change Logo"}
</Button>
<Input
type="file"
id="file-input"
name="company-logo"
accept="image/png, image/gif, image/jpeg"
onChange={() => {}}
onChange={handleLogoUpload}
className="hidden"
ref={fileInputRef}
/>
Expand Down
1 change: 1 addition & 0 deletions schema/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const saveJobsSchema = z.object({
.url("Provide a valid url")
.optional()
.or(z.literal("")),
companyLogo: z.string().optional(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Add URL validation to the companyLogo field.

The companyLogo field should validate URL format for consistency with applicationUrl (lines 21-25) and to ensure only valid URLs are stored.

Apply this diff:

-  companyLogo: z.string().optional(),
+  companyLogo: z.string().url("Provide a valid logo URL").optional().or(z.literal("")),

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In schema/job.ts around line 26, the companyLogo field currently allows any
string; update its schema to validate URLs like applicationUrl does. Replace the
current definition with a string URL validator and keep it optional (e.g., use
the same z.string().url().optional() pattern as applicationUrl) so only valid
URL values are accepted and existing optional semantics are preserved.

remote: z.boolean().optional().default(false),
relocation: z.boolean().optional().default(false),
visa_sponsorship: z.boolean().optional().default(false),
Expand Down