profile editing
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
'use server';
|
'use server';
|
||||||
import { signOut } from '@/auth';
|
import { signOut } from '@/auth';
|
||||||
import { db, applications, jobs, students, resumes } from '@workspace/db';
|
import { db, applications, jobs, students, resumes, grades, internships } from '@workspace/db';
|
||||||
import { eq, and } from '@workspace/db/drizzle';
|
import { eq, and } from '@workspace/db/drizzle';
|
||||||
import { revalidatePath } from 'next/cache';
|
import { revalidatePath } from 'next/cache';
|
||||||
|
|
||||||
@@ -76,19 +76,116 @@ export async function getStudentProfile(studentId: number) {
|
|||||||
|
|
||||||
export async function updateStudentProfile(studentId: number, data: any) {
|
export async function updateStudentProfile(studentId: number, data: any) {
|
||||||
try {
|
try {
|
||||||
await db
|
const { section, ...updateData } = data;
|
||||||
.update(students)
|
|
||||||
.set({
|
switch (section) {
|
||||||
...data,
|
case 'grades':
|
||||||
updatedAt: new Date(),
|
return await updateGrades(studentId, updateData.grades);
|
||||||
})
|
|
||||||
.where(eq(students.id, studentId));
|
case 'internships':
|
||||||
|
return await updateInternships(studentId, updateData.internships);
|
||||||
|
|
||||||
|
case 'resumes':
|
||||||
|
return await updateResumes(studentId, updateData.resumes);
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Update student basic information
|
||||||
|
await db
|
||||||
|
.update(students)
|
||||||
|
.set({
|
||||||
|
...updateData,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(students.id, studentId));
|
||||||
|
|
||||||
|
revalidatePath('/profile');
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error updating student profile:', error);
|
||||||
|
return { success: false, error: 'Failed to update profile' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function updateGrades(studentId: number, gradesList: any[]) {
|
||||||
|
try {
|
||||||
|
// Delete existing grades for this student
|
||||||
|
await db.delete(grades).where(eq(grades.studentId, studentId));
|
||||||
|
|
||||||
|
// Insert new grades
|
||||||
|
if (gradesList && gradesList.length > 0) {
|
||||||
|
const validGrades = gradesList.filter(grade => grade.sgpi > 0); // Only save grades with SGPI > 0
|
||||||
|
|
||||||
|
if (validGrades.length > 0) {
|
||||||
|
await db.insert(grades).values(
|
||||||
|
validGrades.map(grade => ({
|
||||||
|
studentId,
|
||||||
|
sem: parseInt(grade.sem),
|
||||||
|
sgpi: parseFloat(grade.sgpi).toFixed(2),
|
||||||
|
isKT: Boolean(grade.isKT),
|
||||||
|
deadKT: Boolean(grade.deadKT),
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
revalidatePath('/profile');
|
revalidatePath('/profile');
|
||||||
return { success: true };
|
return { success: true };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error updating student profile:', error);
|
console.error('Error updating grades:', error);
|
||||||
return { success: false, error: 'Failed to update profile' };
|
return { success: false, error: 'Failed to update grades' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function updateInternships(studentId: number, internshipsList: any[]) {
|
||||||
|
try {
|
||||||
|
// Delete existing internships for this student
|
||||||
|
await db.delete(internships).where(eq(internships.studentId, studentId));
|
||||||
|
|
||||||
|
// Insert new internships
|
||||||
|
if (internshipsList && internshipsList.length > 0) {
|
||||||
|
await db.insert(internships).values(
|
||||||
|
internshipsList.map(internship => ({
|
||||||
|
studentId,
|
||||||
|
title: internship.title,
|
||||||
|
company: internship.company,
|
||||||
|
location: internship.location,
|
||||||
|
description: internship.description,
|
||||||
|
startDate: new Date(internship.startDate),
|
||||||
|
endDate: new Date(internship.endDate),
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath('/profile');
|
||||||
|
return { success: true };
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error updating internships:', error);
|
||||||
|
return { success: false, error: 'Failed to update internships' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function updateResumes(studentId: number, resumesList: any[]) {
|
||||||
|
try {
|
||||||
|
// Delete existing resumes for this student
|
||||||
|
await db.delete(resumes).where(eq(resumes.studentId, studentId));
|
||||||
|
|
||||||
|
// Insert new resumes
|
||||||
|
if (resumesList && resumesList.length > 0) {
|
||||||
|
await db.insert(resumes).values(
|
||||||
|
resumesList.map(resume => ({
|
||||||
|
studentId,
|
||||||
|
title: resume.title,
|
||||||
|
link: resume.link,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath('/profile');
|
||||||
|
return { success: true };
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error updating resumes:', error);
|
||||||
|
return { success: false, error: 'Failed to update resumes' };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
|
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect, useTransition } from 'react';
|
||||||
import { useSession } from 'next-auth/react';
|
import { useSession } from 'next-auth/react';
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@workspace/ui/components/card"
|
import { Card, CardContent, CardHeader, CardTitle } from "@workspace/ui/components/card"
|
||||||
import { Button } from "@workspace/ui/components/button"
|
import { Button } from "@workspace/ui/components/button"
|
||||||
@@ -53,6 +53,8 @@ export default function ProfilePage() {
|
|||||||
const [editingGrades, setEditingGrades] = useState<any[]>([]);
|
const [editingGrades, setEditingGrades] = useState<any[]>([]);
|
||||||
const [editingInternships, setEditingInternships] = useState<any[]>([]);
|
const [editingInternships, setEditingInternships] = useState<any[]>([]);
|
||||||
const [editingResumes, setEditingResumes] = useState<any[]>([]);
|
const [editingResumes, setEditingResumes] = useState<any[]>([]);
|
||||||
|
const [successMessage, setSuccessMessage] = useState<string | null>(null);
|
||||||
|
const [isPending, startTransition] = useTransition();
|
||||||
const [newInternship, setNewInternship] = useState({
|
const [newInternship, setNewInternship] = useState({
|
||||||
title: '',
|
title: '',
|
||||||
company: '',
|
company: '',
|
||||||
@@ -92,6 +94,7 @@ export default function ProfilePage() {
|
|||||||
const handleEdit = (section: string) => {
|
const handleEdit = (section: string) => {
|
||||||
setEditingSection(section);
|
setEditingSection(section);
|
||||||
setEditData({});
|
setEditData({});
|
||||||
|
setError(null); // Clear any previous errors
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCancel = () => {
|
const handleCancel = () => {
|
||||||
@@ -100,21 +103,22 @@ export default function ProfilePage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSave = async (section: string) => {
|
const handleSave = async (section: string) => {
|
||||||
setIsSaving(true);
|
startTransition(async () => {
|
||||||
try {
|
try {
|
||||||
const result = await updateStudentProfile(student.id, editData);
|
const result = await updateStudentProfile(student.id, editData);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
setStudent((prev: Record<string, any> | null) => ({ ...(prev || {}), ...editData }));
|
setStudent((prev: Record<string, any> | null) => ({ ...(prev || {}), ...editData }));
|
||||||
setEditingSection(null);
|
setEditingSection(null);
|
||||||
setEditData({});
|
setEditData({});
|
||||||
} else {
|
setSuccessMessage('Profile updated successfully!');
|
||||||
setError(result.error || 'Failed to update profile');
|
setTimeout(() => setSuccessMessage(null), 3000);
|
||||||
|
} else {
|
||||||
|
setError(result.error || 'Failed to update profile');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
setError('Failed to update profile');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
});
|
||||||
setError('Failed to update profile');
|
|
||||||
} finally {
|
|
||||||
setIsSaving(false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleInputChange = (field: string, value: string) => {
|
const handleInputChange = (field: string, value: string) => {
|
||||||
@@ -143,40 +147,59 @@ export default function ProfilePage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const saveSkills = async () => {
|
const saveSkills = async () => {
|
||||||
setIsSaving(true);
|
startTransition(async () => {
|
||||||
try {
|
try {
|
||||||
const result = await updateStudentProfile(student.id, { skills: editingSkills });
|
const result = await updateStudentProfile(student.id, { skills: editingSkills });
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
setStudent((prev: Record<string, any> | null) => ({ ...(prev || {}), skills: editingSkills }));
|
setStudent((prev: Record<string, any> | null) => ({ ...(prev || {}), skills: editingSkills }));
|
||||||
setEditingSection(null);
|
setEditingSection(null);
|
||||||
} else {
|
setSuccessMessage('Skills updated successfully!');
|
||||||
setError(result.error || 'Failed to update skills');
|
setTimeout(() => setSuccessMessage(null), 3000);
|
||||||
|
} else {
|
||||||
|
setError(result.error || 'Failed to update skills');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
setError('Failed to update skills');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
});
|
||||||
setError('Failed to update skills');
|
|
||||||
} finally {
|
|
||||||
setIsSaving(false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleEditSkills = () => {
|
const handleEditSkills = () => {
|
||||||
setEditingSkills(student.skills || []);
|
setEditingSkills([...(student.skills || [])]);
|
||||||
setEditingSection('skills');
|
setEditingSection('skills');
|
||||||
|
setError(null); // Clear any previous errors
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleEditGrades = () => {
|
const handleEditGrades = () => {
|
||||||
setEditingGrades(student.grades || []);
|
// Initialize with existing grades or create default grades for 8 semesters
|
||||||
|
const existingGrades = student.grades || [];
|
||||||
|
const defaultGrades = [];
|
||||||
|
|
||||||
|
for (let sem = 1; sem <= 8; sem++) {
|
||||||
|
const existingGrade = existingGrades.find((g: any) => g.sem === sem);
|
||||||
|
defaultGrades.push({
|
||||||
|
sem,
|
||||||
|
sgpi: existingGrade?.sgpi || 0,
|
||||||
|
isKT: existingGrade?.isKT || false,
|
||||||
|
deadKT: existingGrade?.deadKT || false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setEditingGrades(defaultGrades);
|
||||||
setEditingSection('grades');
|
setEditingSection('grades');
|
||||||
|
setError(null); // Clear any previous errors
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleEditInternships = () => {
|
const handleEditInternships = () => {
|
||||||
setEditingInternships(student.internships || []);
|
setEditingInternships([...(student.internships || [])]);
|
||||||
setEditingSection('internships');
|
setEditingSection('internships');
|
||||||
|
setError(null); // Clear any previous errors
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleEditResumes = () => {
|
const handleEditResumes = () => {
|
||||||
setEditingResumes(student.resumes || []);
|
setEditingResumes([...(student.resumes || [])]);
|
||||||
setEditingSection('resumes');
|
setEditingSection('resumes');
|
||||||
|
setError(null); // Clear any previous errors
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateGrade = (sem: number, field: string, value: any) => {
|
const updateGrade = (sem: number, field: string, value: any) => {
|
||||||
@@ -217,54 +240,57 @@ export default function ProfilePage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const saveGrades = async () => {
|
const saveGrades = async () => {
|
||||||
setIsSaving(true);
|
startTransition(async () => {
|
||||||
try {
|
try {
|
||||||
const result = await updateStudentProfile(student.id, { grades: editingGrades });
|
const result = await updateStudentProfile(student.id, { section: 'grades', grades: editingGrades });
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
setStudent((prev: Record<string, any> | null) => ({ ...(prev || {}), grades: editingGrades }));
|
setStudent((prev: Record<string, any> | null) => ({ ...(prev || {}), grades: editingGrades }));
|
||||||
setEditingSection(null);
|
setEditingSection(null);
|
||||||
} else {
|
setSuccessMessage('Grades updated successfully!');
|
||||||
setError(result.error || 'Failed to update grades');
|
setTimeout(() => setSuccessMessage(null), 3000);
|
||||||
|
} else {
|
||||||
|
setError(result.error || 'Failed to update grades');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
setError('Failed to update grades');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
});
|
||||||
setError('Failed to update grades');
|
|
||||||
} finally {
|
|
||||||
setIsSaving(false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const saveInternships = async () => {
|
const saveInternships = async () => {
|
||||||
setIsSaving(true);
|
startTransition(async () => {
|
||||||
try {
|
try {
|
||||||
const result = await updateStudentProfile(student.id, { internships: editingInternships });
|
const result = await updateStudentProfile(student.id, { section: 'internships', internships: editingInternships });
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
setStudent((prev: Record<string, any> | null) => ({ ...(prev || {}), internships: editingInternships }));
|
setStudent((prev: Record<string, any> | null) => ({ ...(prev || {}), internships: editingInternships }));
|
||||||
setEditingSection(null);
|
setEditingSection(null);
|
||||||
} else {
|
setSuccessMessage('Internships updated successfully!');
|
||||||
setError(result.error || 'Failed to update internships');
|
setTimeout(() => setSuccessMessage(null), 3000);
|
||||||
|
} else {
|
||||||
|
setError(result.error || 'Failed to update internships');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
setError('Failed to update internships');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
});
|
||||||
setError('Failed to update internships');
|
|
||||||
} finally {
|
|
||||||
setIsSaving(false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const saveResumes = async () => {
|
const saveResumes = async () => {
|
||||||
setIsSaving(true);
|
startTransition(async () => {
|
||||||
try {
|
try {
|
||||||
const result = await updateStudentProfile(student.id, { resumes: editingResumes });
|
const result = await updateStudentProfile(student.id, { section: 'resumes', resumes: editingResumes });
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
setStudent((prev: Record<string, any> | null) => ({ ...(prev || {}), resumes: editingResumes }));
|
setStudent((prev: Record<string, any> | null) => ({ ...(prev || {}), resumes: editingResumes }));
|
||||||
setEditingSection(null);
|
setEditingSection(null);
|
||||||
} else if ('error' in result) {
|
setSuccessMessage('Resumes updated successfully!');
|
||||||
setError(result.error || 'Failed to update resumes');
|
setTimeout(() => setSuccessMessage(null), 3000);
|
||||||
|
} else {
|
||||||
|
setError(result.error || 'Failed to update resumes');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
setError('Failed to update resumes');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
});
|
||||||
setError('Failed to update resumes');
|
|
||||||
} finally {
|
|
||||||
setIsSaving(false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDragOver = (e: React.DragEvent) => {
|
const handleDragOver = (e: React.DragEvent) => {
|
||||||
@@ -344,6 +370,12 @@ export default function ProfilePage() {
|
|||||||
{error}
|
{error}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{successMessage && (
|
||||||
|
<div className="mt-4 p-3 bg-green-100 text-green-700 rounded-lg">
|
||||||
|
<CheckCircle className="w-4 h-4 inline mr-2" />
|
||||||
|
{successMessage}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="grid grid-cols-1 lg:grid-cols-3 gap-8">
|
<div className="grid grid-cols-1 lg:grid-cols-3 gap-8">
|
||||||
@@ -467,9 +499,9 @@ export default function ProfilePage() {
|
|||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Button size="sm" onClick={() => handleSave('personal')} disabled={isSaving}>
|
<Button size="sm" onClick={() => handleSave('personal')} disabled={isPending}>
|
||||||
<Save className="w-4 h-4 mr-2" />
|
<Save className="w-4 h-4 mr-2" />
|
||||||
{isSaving ? 'Saving...' : 'Save'}
|
{isPending ? 'Saving...' : 'Save'}
|
||||||
</Button>
|
</Button>
|
||||||
<Button size="sm" variant="outline" onClick={handleCancel}>
|
<Button size="sm" variant="outline" onClick={handleCancel}>
|
||||||
<X className="w-4 h-4 mr-2" />
|
<X className="w-4 h-4 mr-2" />
|
||||||
@@ -706,9 +738,9 @@ export default function ProfilePage() {
|
|||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Button size="sm" onClick={() => handleSave('academic')} disabled={isSaving}>
|
<Button size="sm" onClick={() => handleSave('academic')} disabled={isPending}>
|
||||||
<Save className="w-4 h-4 mr-2" />
|
<Save className="w-4 h-4 mr-2" />
|
||||||
{isSaving ? 'Saving...' : 'Save'}
|
{isPending ? 'Saving...' : 'Save'}
|
||||||
</Button>
|
</Button>
|
||||||
<Button size="sm" variant="outline" onClick={handleCancel}>
|
<Button size="sm" variant="outline" onClick={handleCancel}>
|
||||||
<X className="w-4 h-4 mr-2" />
|
<X className="w-4 h-4 mr-2" />
|
||||||
@@ -857,9 +889,9 @@ export default function ProfilePage() {
|
|||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Button size="sm" onClick={saveSkills} disabled={isSaving}>
|
<Button size="sm" onClick={saveSkills} disabled={isPending}>
|
||||||
<Save className="w-4 h-4 mr-2" />
|
<Save className="w-4 h-4 mr-2" />
|
||||||
{isSaving ? 'Saving...' : 'Save'}
|
{isPending ? 'Saving...' : 'Save'}
|
||||||
</Button>
|
</Button>
|
||||||
<Button size="sm" variant="outline" onClick={handleCancel}>
|
<Button size="sm" variant="outline" onClick={handleCancel}>
|
||||||
<X className="w-4 h-4 mr-2" />
|
<X className="w-4 h-4 mr-2" />
|
||||||
@@ -927,9 +959,9 @@ export default function ProfilePage() {
|
|||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Button size="sm" onClick={saveGrades} disabled={isSaving}>
|
<Button size="sm" onClick={saveGrades} disabled={isPending}>
|
||||||
<Save className="w-4 h-4 mr-2" />
|
<Save className="w-4 h-4 mr-2" />
|
||||||
{isSaving ? 'Saving...' : 'Save'}
|
{isPending ? 'Saving...' : 'Save'}
|
||||||
</Button>
|
</Button>
|
||||||
<Button size="sm" variant="outline" onClick={handleCancel}>
|
<Button size="sm" variant="outline" onClick={handleCancel}>
|
||||||
<X className="w-4 h-4 mr-2" />
|
<X className="w-4 h-4 mr-2" />
|
||||||
@@ -959,13 +991,14 @@ export default function ProfilePage() {
|
|||||||
value={grade.sgpi || ''}
|
value={grade.sgpi || ''}
|
||||||
onChange={(e) => updateGrade(grade.sem, 'sgpi', parseFloat(e.target.value) || 0)}
|
onChange={(e) => updateGrade(grade.sem, 'sgpi', parseFloat(e.target.value) || 0)}
|
||||||
type="number"
|
type="number"
|
||||||
step="1"
|
step="0.01"
|
||||||
min="0"
|
min="0"
|
||||||
max="10"
|
max="10"
|
||||||
className="w-20"
|
className="w-20"
|
||||||
|
placeholder="0.00"
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
grade.sgpi
|
typeof grade.sgpi === 'number' ? grade.sgpi.toFixed(2) : (parseFloat(grade.sgpi) || 0).toFixed(2)
|
||||||
)}
|
)}
|
||||||
</td>
|
</td>
|
||||||
<td className="py-2">
|
<td className="py-2">
|
||||||
@@ -1036,9 +1069,9 @@ export default function ProfilePage() {
|
|||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Button size="sm" onClick={saveInternships} disabled={isSaving}>
|
<Button size="sm" onClick={saveInternships} disabled={isPending}>
|
||||||
<Save className="w-4 h-4 mr-2" />
|
<Save className="w-4 h-4 mr-2" />
|
||||||
{isSaving ? 'Saving...' : 'Save'}
|
{isPending ? 'Saving...' : 'Save'}
|
||||||
</Button>
|
</Button>
|
||||||
<Button size="sm" variant="outline" onClick={handleCancel}>
|
<Button size="sm" variant="outline" onClick={handleCancel}>
|
||||||
<X className="w-4 h-4 mr-2" />
|
<X className="w-4 h-4 mr-2" />
|
||||||
@@ -1172,9 +1205,9 @@ export default function ProfilePage() {
|
|||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Button size="sm" onClick={saveResumes} disabled={isSaving}>
|
<Button size="sm" onClick={saveResumes} disabled={isPending}>
|
||||||
<Save className="w-4 h-4 mr-2" />
|
<Save className="w-4 h-4 mr-2" />
|
||||||
{isSaving ? 'Saving...' : 'Save'}
|
{isPending ? 'Saving...' : 'Save'}
|
||||||
</Button>
|
</Button>
|
||||||
<Button size="sm" variant="outline" onClick={handleCancel}>
|
<Button size="sm" variant="outline" onClick={handleCancel}>
|
||||||
<X className="w-4 h-4 mr-2" />
|
<X className="w-4 h-4 mr-2" />
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState, useTransition } from 'react';
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@workspace/ui/components/card"
|
import { Card, CardContent, CardHeader, CardTitle } from "@workspace/ui/components/card"
|
||||||
import { Button } from "@workspace/ui/components/button"
|
import { Button } from "@workspace/ui/components/button"
|
||||||
import { Badge } from "@workspace/ui/components/badge"
|
import { Badge } from "@workspace/ui/components/badge"
|
||||||
@@ -37,7 +37,7 @@ interface JobApplicationModalProps {
|
|||||||
export default function JobApplicationModal({ job, studentId, resumes, isApplied = false }: JobApplicationModalProps) {
|
export default function JobApplicationModal({ job, studentId, resumes, isApplied = false }: JobApplicationModalProps) {
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [selectedResume, setSelectedResume] = useState<string>('');
|
const [selectedResume, setSelectedResume] = useState<string>('');
|
||||||
const [isApplying, setIsApplying] = useState(false);
|
const [isPending, startTransition] = useTransition();
|
||||||
const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null);
|
const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null);
|
||||||
|
|
||||||
const handleApply = async () => {
|
const handleApply = async () => {
|
||||||
@@ -46,27 +46,26 @@ export default function JobApplicationModal({ job, studentId, resumes, isApplied
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setIsApplying(true);
|
|
||||||
setMessage(null);
|
setMessage(null);
|
||||||
|
|
||||||
try {
|
startTransition(async () => {
|
||||||
const result = await applyForJob(job.id, studentId, parseInt(selectedResume));
|
try {
|
||||||
|
const result = await applyForJob(job.id, studentId, parseInt(selectedResume));
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
setMessage({ type: 'success', text: 'Application submitted successfully!' });
|
setMessage({ type: 'success', text: 'Application submitted successfully!' });
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
setMessage(null);
|
setMessage(null);
|
||||||
setSelectedResume('');
|
setSelectedResume('');
|
||||||
}, 2000);
|
}, 2000);
|
||||||
} else {
|
} else {
|
||||||
setMessage({ type: 'error', text: result.error || 'Failed to submit application' });
|
setMessage({ type: 'error', text: result.error || 'Failed to submit application' });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
setMessage({ type: 'error', text: 'An error occurred while submitting your application' });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
});
|
||||||
setMessage({ type: 'error', text: 'An error occurred while submitting your application' });
|
|
||||||
} finally {
|
|
||||||
setIsApplying(false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const isDeadlinePassed = new Date() > new Date(job.applicationDeadline as any);
|
const isDeadlinePassed = new Date() > new Date(job.applicationDeadline as any);
|
||||||
@@ -192,15 +191,15 @@ export default function JobApplicationModal({ job, studentId, resumes, isApplied
|
|||||||
<div className="flex gap-3 pt-4">
|
<div className="flex gap-3 pt-4">
|
||||||
<Button
|
<Button
|
||||||
onClick={handleApply}
|
onClick={handleApply}
|
||||||
disabled={isApplying || resumes.length === 0 || isApplied}
|
disabled={isPending || resumes.length === 0 || isApplied}
|
||||||
className="flex-1 bg-blue-600 hover:bg-blue-700"
|
className="flex-1 bg-blue-600 hover:bg-blue-700"
|
||||||
>
|
>
|
||||||
{isApplying ? 'Submitting...' : 'Submit Application'}
|
{isPending ? 'Submitting...' : 'Submit Application'}
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
onClick={() => setIsOpen(false)}
|
onClick={() => setIsOpen(false)}
|
||||||
disabled={isApplying}
|
disabled={isPending}
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user