| 1 | "use client"
|
|---|
| 2 |
|
|---|
| 3 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|---|
| 4 | import { faUser } from '@fortawesome/free-solid-svg-icons';
|
|---|
| 5 | import Image from 'next/image';
|
|---|
| 6 |
|
|---|
| 7 | import { useEffect, useState } from 'react';
|
|---|
| 8 | import { getAccessToken, clearAuthTokens } from '@/lib/auth';
|
|---|
| 9 | import { useTranslation } from 'react-i18next';
|
|---|
| 10 | import LanguageSwitcher from './LanguageSwitcher';
|
|---|
| 11 | import { apiUrl } from '@/lib/api';
|
|---|
| 12 |
|
|---|
| 13 | const Header = () => {
|
|---|
| 14 |
|
|---|
| 15 | const [userName, setUserName] = useState<string>('');
|
|---|
| 16 | const { t } = useTranslation();
|
|---|
| 17 |
|
|---|
| 18 | useEffect(() => {
|
|---|
| 19 | async function fetchUser() {
|
|---|
| 20 | const token = getAccessToken();
|
|---|
| 21 | if (!token) return;
|
|---|
| 22 | try {
|
|---|
| 23 | const response = await fetch(apiUrl('/api/user/getUser'), {
|
|---|
| 24 | method: 'GET',
|
|---|
| 25 | headers: { Authorization: `Bearer ${token}` },
|
|---|
| 26 | });
|
|---|
| 27 | if (!response.ok) return;
|
|---|
| 28 | const data = await response.json();
|
|---|
| 29 | // Try both student and professor shape
|
|---|
| 30 | if (data?.personalInfo) {
|
|---|
| 31 | const p = data.personalInfo;
|
|---|
| 32 | setUserName([p.firstName, p.middleName, p.lastName].filter(Boolean).join(' '));
|
|---|
| 33 | }
|
|---|
| 34 | } catch {}
|
|---|
| 35 | }
|
|---|
| 36 | fetchUser();
|
|---|
| 37 | return () => {};
|
|---|
| 38 | }, []);
|
|---|
| 39 |
|
|---|
| 40 | const handleLogout = () => {
|
|---|
| 41 | clearAuthTokens();
|
|---|
| 42 | if (typeof window !== 'undefined') {
|
|---|
| 43 | window.location.href = '/';
|
|---|
| 44 | }
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| 47 | return (
|
|---|
| 48 | <header className="bg-card shadow-sm border-b border-border">
|
|---|
| 49 | <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
|---|
| 50 | <div className="flex flex-col sm:flex-row sm:justify-between sm:items-center py-4 gap-4 sm:gap-0">
|
|---|
| 51 | {/* Left side - Logos and University Info */}
|
|---|
| 52 | <div className="flex items-center gap-2 justify-center sm:justify-start">
|
|---|
| 53 | {/* UKIM Logo */}
|
|---|
| 54 | <div className="flex-shrink-0">
|
|---|
| 55 | <Image
|
|---|
| 56 | src="/ukim-logo.png"
|
|---|
| 57 | alt="UKIM Logo"
|
|---|
| 58 | width={40}
|
|---|
| 59 | height={40}
|
|---|
| 60 | className="h-10 w-auto"
|
|---|
| 61 | />
|
|---|
| 62 | </div>
|
|---|
| 63 | {/* FINKI Logo */}
|
|---|
| 64 | <div className="flex-shrink-0">
|
|---|
| 65 | <Image
|
|---|
| 66 | src="/finki-logo.png"
|
|---|
| 67 | alt="FINKI Logo"
|
|---|
| 68 | width={40}
|
|---|
| 69 | height={40}
|
|---|
| 70 | className="h-10 w-auto"
|
|---|
| 71 | />
|
|---|
| 72 | </div>
|
|---|
| 73 | {/* University Text */}
|
|---|
| 74 | <div className="hidden sm:block">
|
|---|
| 75 | <div className="text-card-foreground font-medium text-sm">
|
|---|
| 76 | {t('university')}
|
|---|
| 77 | </div>
|
|---|
| 78 | <div className="text-muted-foreground text-sm">
|
|---|
| 79 | {t('faculty')}
|
|---|
| 80 | </div>
|
|---|
| 81 | </div>
|
|---|
| 82 | </div>
|
|---|
| 83 | {/* Right side - User Profile */}
|
|---|
| 84 | <div className="flex flex-col sm:flex-row items-center gap-2 sm:gap-4 w-full sm:w-auto justify-center sm:justify-end">
|
|---|
| 85 | {/* Language Switcher - now left of user info */}
|
|---|
| 86 | <div className="sm:mr-2 w-full sm:w-auto flex justify-center sm:justify-end">
|
|---|
| 87 | <LanguageSwitcher />
|
|---|
| 88 | </div>
|
|---|
| 89 | <div className="flex items-center gap-2 sm:gap-3 w-full sm:w-auto justify-center sm:justify-end">
|
|---|
| 90 | {/* User Avatar */}
|
|---|
| 91 | <div className="hidden sm:flex items-center justify-center w-10 h-10 bg-primary text-primary-foreground rounded-full">
|
|---|
| 92 | <FontAwesomeIcon icon={faUser} className="text-sm" />
|
|---|
| 93 | </div>
|
|---|
| 94 | {/* User Name */}
|
|---|
| 95 | <div className="hidden sm:block text-card-foreground font-medium">
|
|---|
| 96 | {userName || '...'}
|
|---|
| 97 | </div>
|
|---|
| 98 | {/* Logout Button - more stylish and full width on mobile */}
|
|---|
| 99 | <button
|
|---|
| 100 | className="w-full sm:w-auto ml-0 sm:ml-2 px-4 py-2 border-2 border-primary text-primary bg-card rounded-lg font-semibold flex items-center justify-center gap-2 hover:bg-accent transition-all duration-200 shadow-sm"
|
|---|
| 101 | onClick={handleLogout}
|
|---|
| 102 | >
|
|---|
| 103 | <svg xmlns='http://www.w3.org/2000/svg' className='h-4 w-4' fill='none' viewBox='0 0 24 24' stroke='currentColor'><path strokeLinecap='round' strokeLinejoin='round' strokeWidth={2} d='M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a2 2 0 01-2 2H7a2 2 0 01-2-2V7a2 2 0 012-2h4a2 2 0 012 2v1' /></svg>
|
|---|
| 104 | {t('logout')}
|
|---|
| 105 | </button>
|
|---|
| 106 | </div>
|
|---|
| 107 | </div>
|
|---|
| 108 | </div>
|
|---|
| 109 | </div>
|
|---|
| 110 | </header>
|
|---|
| 111 | );
|
|---|
| 112 | };
|
|---|
| 113 |
|
|---|
| 114 | export default Header;
|
|---|