source: src/components/header.tsx@ 50f2fb4

Last change on this file since 50f2fb4 was 50f2fb4, checked in by Stefan-Saveski <stefansaveski19@…>, 8 months ago

Added dual language feature.

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