job description file upload
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
'use server';
|
||||
import { db, companies, jobs } from '@workspace/db';
|
||||
import { writeFile, mkdir } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
|
||||
export async function createJob(formData: FormData) {
|
||||
const companyIdRaw = formData.get('companyId');
|
||||
const companyId = companyIdRaw ? Number(companyIdRaw) : undefined;
|
||||
const title = String(formData.get('title') ?? '').trim();
|
||||
const link = String(formData.get('link') ?? '').trim();
|
||||
const description = String(formData.get('description') ?? '').trim() || 'N/A';
|
||||
const description = String(formData.get('description') ?? '').trim() || '';
|
||||
const location = String(formData.get('location') ?? '').trim() || 'N/A';
|
||||
const imageURL =
|
||||
String(formData.get('imageURL') ?? '').trim() || 'https://via.placeholder.com/100x100?text=Job';
|
||||
@@ -19,13 +21,49 @@ export async function createJob(formData: FormData) {
|
||||
const allowDeadKT = formData.get('allowDeadKT') === 'on' || formData.get('allowDeadKT') === 'true';
|
||||
const allowLiveKT = formData.get('allowLiveKT') === 'on' || formData.get('allowLiveKT') === 'true';
|
||||
|
||||
// Handle file upload
|
||||
const descriptionFile = formData.get('descriptionFile') as File | null;
|
||||
const fileType = formData.get('fileType') as string | null;
|
||||
|
||||
let fileUrl: string | null = null;
|
||||
let fileName: string | null = null;
|
||||
|
||||
if (descriptionFile && descriptionFile.size > 0) {
|
||||
try {
|
||||
// Create uploads directory if it doesn't exist
|
||||
const uploadsDir = join(process.cwd(), 'public', 'uploads', 'job-descriptions');
|
||||
await mkdir(uploadsDir, { recursive: true });
|
||||
|
||||
// Generate unique filename
|
||||
const timestamp = Date.now();
|
||||
const originalName = descriptionFile.name.replace(/[^a-zA-Z0-9.-]/g, '_');
|
||||
fileName = `${timestamp}_${originalName}`;
|
||||
const filePath = join(uploadsDir, fileName);
|
||||
|
||||
// Write file to disk
|
||||
const bytes = await descriptionFile.arrayBuffer();
|
||||
await writeFile(filePath, Buffer.from(bytes));
|
||||
|
||||
// Set file URL for database
|
||||
fileUrl = `/uploads/job-descriptions/${fileName}`;
|
||||
} catch (error) {
|
||||
console.error('Error uploading file:', error);
|
||||
return { error: 'Failed to upload description file.' };
|
||||
}
|
||||
}
|
||||
|
||||
if (!companyId || !title) return { error: 'Company and title are required.' };
|
||||
|
||||
// Either description text OR file is required
|
||||
if (!description && !descriptionFile) {
|
||||
return { error: 'Either description text or description file is required.' };
|
||||
}
|
||||
|
||||
await db.insert(jobs).values({
|
||||
companyId,
|
||||
title,
|
||||
link,
|
||||
description,
|
||||
description: description || null,
|
||||
location,
|
||||
imageURL,
|
||||
salary,
|
||||
@@ -36,18 +74,20 @@ export async function createJob(formData: FormData) {
|
||||
minHSC,
|
||||
allowDeadKT,
|
||||
allowLiveKT,
|
||||
fileType: fileType || null,
|
||||
fileUrl: fileUrl || null,
|
||||
fileName: fileName || null,
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function createCompany(formData: FormData) {
|
||||
const name = String(formData.get('name') ?? '').trim();
|
||||
const email = String(formData.get('email') ?? '').trim();
|
||||
const link = String(formData.get('link') ?? '').trim();
|
||||
const description = String(formData.get('description') ?? '').trim();
|
||||
const imageURL = String(formData.get('imageURL') ?? '').trim() || 'https://via.placeholder.com/100x100?text=Company';
|
||||
if (!name || !email || !link || !description) return { error: 'All fields are required.' };
|
||||
const [inserted] = await db.insert(companies).values({ name, email, link, description, imageURL }).returning();
|
||||
if (!name || !link || !description) return { error: 'Name, link, and description are required.' };
|
||||
const [inserted] = await db.insert(companies).values({ name, link, description, imageURL }).returning();
|
||||
if (!inserted) return { error: 'Failed to add company.' };
|
||||
return { success: true, company: { id: inserted.id, name: inserted.name } };
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@ import {
|
||||
CheckCircle,
|
||||
AlertCircle,
|
||||
LinkIcon,
|
||||
Upload,
|
||||
FileText,
|
||||
X,
|
||||
} from "lucide-react"
|
||||
import { cn } from "@workspace/ui/lib/utils"
|
||||
import { Alert, AlertDescription } from "@workspace/ui/components/alert"
|
||||
@@ -62,6 +65,8 @@ function NewJobForm({ companies }: { companies: { id: number; name: string }[] }
|
||||
minHSC: 0,
|
||||
allowDeadKT: true,
|
||||
allowLiveKT: true,
|
||||
fileType: undefined,
|
||||
descriptionFile: undefined,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -74,7 +79,7 @@ function NewJobForm({ companies }: { companies: { id: number; name: string }[] }
|
||||
formData.append('companyId', String(data.companyId))
|
||||
formData.append('title', data.title)
|
||||
formData.append('link', data.link)
|
||||
formData.append('description', data.description)
|
||||
formData.append('description', data.description || '')
|
||||
formData.append('location', data.location)
|
||||
formData.append('imageURL', data.imageURL || '')
|
||||
formData.append('salary', data.salary)
|
||||
@@ -89,6 +94,12 @@ function NewJobForm({ companies }: { companies: { id: number; name: string }[] }
|
||||
formData.append('allowDeadKT', String(data.allowDeadKT))
|
||||
formData.append('allowLiveKT', String(data.allowLiveKT))
|
||||
|
||||
// Handle file upload
|
||||
if (data.descriptionFile) {
|
||||
formData.append('descriptionFile', data.descriptionFile)
|
||||
formData.append('fileType', data.fileType || (data.descriptionFile.type === 'application/pdf' ? 'pdf' : 'text'))
|
||||
}
|
||||
|
||||
const result = await createJob(formData)
|
||||
if (result?.success) {
|
||||
setSuccess(true)
|
||||
@@ -412,23 +423,102 @@ function NewJobForm({ companies }: { companies: { id: number; name: string }[] }
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="text-sm font-medium text-gray-700">Job Description *</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
placeholder="Describe the role, responsibilities, and requirements..."
|
||||
{...field}
|
||||
className="min-h-[120px] resize-none"
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
{/* Job Description Section - Text or File */}
|
||||
<div className="space-y-4">
|
||||
<FormLabel className="text-sm font-medium text-gray-700">Job Description *</FormLabel>
|
||||
<p className="text-sm text-gray-600">Provide either a text description or upload a PDF/text file</p>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="text-sm font-medium text-gray-700">Text Description</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
placeholder="Describe the role, responsibilities, and requirements..."
|
||||
{...field}
|
||||
className="min-h-[120px] resize-none"
|
||||
disabled={!!form.watch('descriptionFile')}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<div className="flex items-center justify-center">
|
||||
<div className="flex-1 border-t border-gray-300"></div>
|
||||
<div className="px-3 text-sm text-gray-500 bg-white">OR</div>
|
||||
<div className="flex-1 border-t border-gray-300"></div>
|
||||
</div>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="descriptionFile"
|
||||
render={({ field: { onChange, value, ...field } }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="text-sm font-medium text-gray-700">Upload Description File</FormLabel>
|
||||
<FormControl>
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-center w-full">
|
||||
<label className="flex flex-col items-center justify-center w-full h-32 border-2 border-gray-300 border-dashed rounded-lg cursor-pointer bg-gray-50 hover:bg-gray-100">
|
||||
<div className="flex flex-col items-center justify-center pt-5 pb-6">
|
||||
<Upload className="w-8 h-8 mb-4 text-gray-500" />
|
||||
<p className="mb-2 text-sm text-gray-500">
|
||||
<span className="font-semibold">Click to upload</span> or drag and drop
|
||||
</p>
|
||||
<p className="text-xs text-gray-500">PDF or TXT (MAX. 5MB)</p>
|
||||
</div>
|
||||
<input
|
||||
type="file"
|
||||
accept=".pdf,.txt,application/pdf,text/plain"
|
||||
className="hidden"
|
||||
onChange={(e) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (file) {
|
||||
onChange(file)
|
||||
// Auto-detect file type
|
||||
const fileType = file.type === 'application/pdf' ? 'pdf' : 'text'
|
||||
form.setValue('fileType', fileType)
|
||||
// Clear text description
|
||||
form.setValue('description', '')
|
||||
}
|
||||
}}
|
||||
disabled={!!form.watch('description')?.trim()}
|
||||
{...field}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{value && (
|
||||
<div className="flex items-center gap-2 p-3 bg-blue-50 rounded-lg border border-blue-200">
|
||||
<FileText className="w-4 h-4 text-blue-600" />
|
||||
<span className="text-sm text-blue-800 flex-1">{value.name}</span>
|
||||
<span className="text-xs text-blue-600">
|
||||
{(value.size / 1024 / 1024).toFixed(2)} MB
|
||||
</span>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
onChange(undefined)
|
||||
form.setValue('fileType', undefined)
|
||||
}}
|
||||
className="text-blue-600 hover:text-blue-800"
|
||||
>
|
||||
<X className="w-4 h-4" />
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Academic Requirements Section */}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
|
||||
const ACCEPTED_FILE_TYPES = ['application/pdf', 'text/plain'];
|
||||
|
||||
export const jobSchema = z.object({
|
||||
companyId: z.number().min(1, 'Company is required'),
|
||||
title: z.string().min(1, 'Title is required'),
|
||||
link: z.string().url('Invalid URL'),
|
||||
description: z.string().min(1, 'Description is required'),
|
||||
description: z.string().optional(), // Made optional since file can replace it
|
||||
location: z.string().min(1, 'Location is required'),
|
||||
imageURL: z.string().url('Invalid URL').or(z.literal('')).optional(),
|
||||
salary: z.string().min(1, 'Salary is required'),
|
||||
@@ -14,6 +17,21 @@ export const jobSchema = z.object({
|
||||
minHSC: z.coerce.number().min(0, 'Minimum HSC must be 0 or greater'),
|
||||
allowDeadKT: z.boolean(),
|
||||
allowLiveKT: z.boolean(),
|
||||
});
|
||||
// File upload fields
|
||||
fileType: z.enum(['pdf', 'text']).optional(),
|
||||
descriptionFile: z.instanceof(File)
|
||||
.refine(file => file?.size <= MAX_FILE_SIZE, 'File size must be less than 5MB')
|
||||
.refine(file => ACCEPTED_FILE_TYPES.includes(file?.type), 'Only PDF and text files are allowed')
|
||||
.optional(),
|
||||
}).refine(
|
||||
(data) => {
|
||||
// Either description text OR file is required
|
||||
return (data.description && data.description.trim().length > 0) || data.descriptionFile;
|
||||
},
|
||||
{
|
||||
message: 'Either provide a text description or upload a description file',
|
||||
path: ['description'],
|
||||
}
|
||||
);
|
||||
|
||||
export type JobFormData = z.infer<typeof jobSchema>;
|
||||
|
||||
Reference in New Issue
Block a user