508ffe5f84
- Updated AddInternsModal to allow selection of interns via dropdown instead of email input. - Introduced CertResultTable component for displaying certificate results with search and pagination. - Modified CreateProjectDialog to accept an onSuccess callback for better project creation handling. - Integrated certificate generation functionality in EligibleInternTable with improved error handling. - Enhanced HomeMentorProject and MentorProjectCard to dynamically display project icons and statuses. - Refactored SidebarMentor to improve active route detection. - Added utility functions for certificate preview and download in Certificates.ts. - Removed unused DownloadImagetoDevice utility and related hooks. - Implemented useProjectsByRole hook for fetching projects based on user role. - Updated types for certificates and projects to reflect recent changes in the data structure.
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { useCallback } from 'react';
|
|
import { useCurrentUser } from './useUser';
|
|
import type { Project } from '../types/project.types';
|
|
import { getProjects, getMentorProjects } from '../services/ProjectService';
|
|
|
|
export const useProjectsByRole = (
|
|
status?: "active" | "completed" | "archived"
|
|
) => {
|
|
const {
|
|
user,
|
|
loading: userLoading,
|
|
error: userError,
|
|
} = useCurrentUser();
|
|
|
|
const [projects, setProjects] = useState<Project[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const refetch = useCallback(() => {
|
|
if (!user) return;
|
|
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
const fetcher =
|
|
user.role === "admin"
|
|
? getProjects(status)
|
|
: user.role === "mentor"
|
|
? getMentorProjects()
|
|
: Promise.resolve([]);
|
|
|
|
fetcher
|
|
.then(setProjects)
|
|
.catch(() => setError("Gagal memuat projects."))
|
|
.finally(() => setLoading(false));
|
|
}, [user, status]);
|
|
|
|
useEffect(() => {
|
|
if (!userLoading && user) {
|
|
refetch();
|
|
}
|
|
}, [userLoading, user, refetch]);
|
|
|
|
return {
|
|
projects,
|
|
loading: userLoading || loading,
|
|
error: userError || error,
|
|
refetch,
|
|
role: user?.role ?? null,
|
|
};
|
|
}; |