Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions frontend/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# Visitor country from Cloudflare's CF-IPCountry header, handed to the SPA as a geo_country cookie so it
# picks the UI language (Indonesian in Indonesia, English elsewhere) before the first render.
# - Only on page navigations: a Set-Cookie on static files or API responses would stop Cloudflare from
# caching them. $request_uri is used because try_files rewrites $uri to /index.html.
# - Only a two-letter code passes, so nothing else from the header can reach the cookie.
# - Without Cloudflare (local runs) there is no header and no cookie; the SPA falls back to the time zone.
map $request_uri $geo_cookie_allowed {
~^/(api|ws|actuator)(/|\?|$) 0;
~^[^?]*\.[A-Za-z0-9]+(\?|$) 0;
default 1;
}

map "$geo_cookie_allowed:$http_cf_ipcountry" $geo_country_cookie {
"~^1:(?<geo_cc>[A-Z]{2})$" "geo_country=$geo_cc; Path=/; SameSite=Lax";
default "";
}

server {
listen 80;
server_name localhost;
Expand All @@ -17,6 +34,8 @@ server {
# 'unsafe-inline' for styles only: React and some libraries set inline style attributes.
add_header Content-Security-Policy "default-src 'self'; script-src 'self' ${CSP_SCRIPT_SRC_EXTRA}; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; media-src 'self' blob:; connect-src 'self' ${CSP_CONNECT_SRC_EXTRA}; frame-src 'self' blob:; frame-ancestors 'self'; object-src 'none'; base-uri 'self'; form-action 'self'" always;
# HSTS belongs on the TLS-terminating host nginx / Cloudflare, not here (this server speaks plain HTTP).
# Empty value (no country, or not a page navigation) = header not sent.
add_header Set-Cookie $geo_country_cookie;

location / {
root /usr/share/nginx/html;
Expand Down
42 changes: 42 additions & 0 deletions frontend/src/components/common/LanguageSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react';
import { Languages } from 'lucide-react';
import { useI18n, type Lang } from '../../i18n';

const OPTIONS: { lang: Lang; label: string; name: string }[] = [
{ lang: 'id', label: 'ID', name: 'Bahasa Indonesia' },
{ lang: 'en', label: 'EN', name: 'English' },
];

/** ID / EN toggle. The choice is remembered and overrides the location-based default. */
export const LanguageSwitcher: React.FC<{ className?: string }> = ({ className = '' }) => {
const { lang, setLang, t } = useI18n();

return (
<div
role="group"
aria-label={t('common.language')}
className={`inline-flex items-center gap-1 rounded-lg border border-gray-200 bg-white p-0.5 text-xs shadow-xs ${className}`}
>
<Languages className="w-3.5 h-3.5 text-gray-400 ml-1.5" aria-hidden="true" />
{OPTIONS.map((option) => {
const selected = option.lang === lang;
return (
<button
key={option.lang}
type="button"
lang={option.lang}
title={option.name}
aria-label={option.name}
aria-pressed={selected}
onClick={() => setLang(option.lang)}
className={`rounded-md px-2 py-1 font-semibold transition cursor-pointer ${
selected ? 'bg-emerald-600 text-white' : 'text-gray-600 hover:text-emerald-700 hover:bg-emerald-50'
}`}
>
{option.label}
</button>
);
})}
</div>
);
};
31 changes: 18 additions & 13 deletions frontend/src/components/common/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React from 'react';
import { Link } from 'react-router-dom';
import { useAuthStore } from '../../store/authStore';
import { LogOut, UserCheck, ShieldAlert, Menu, X, KeyRound } from 'lucide-react';
import { useI18n } from '../../i18n';
import { LanguageSwitcher } from './LanguageSwitcher';

interface NavbarProps {
onToggleMobileMenu?: () => void;
Expand All @@ -10,26 +12,27 @@ interface NavbarProps {

export const Navbar: React.FC<NavbarProps> = ({ onToggleMobileMenu, isMobileOpen }) => {
const { user, logout, stopImpersonate } = useAuthStore();
const { t } = useI18n();

const getRoleBadge = () => {
if (!user) return null;
if (user.roles.includes('ROLE_ADMIN')) {
return (
<span className="bg-emerald-100 text-emerald-800 text-xs font-semibold px-2.5 py-0.5 rounded-full border border-emerald-200">
Admin
{t('common.roleAdmin')}
</span>
);
}
if (user.roles.includes('ROLE_TEACHER')) {
return (
<span className="bg-amber-100 text-amber-800 text-xs font-semibold px-2.5 py-0.5 rounded-full border border-amber-200">
Guru
{t('common.roleTeacher')}
</span>
);
}
return (
<span className="bg-teal-100 text-teal-800 text-xs font-semibold px-2.5 py-0.5 rounded-full border border-teal-200">
Siswa
{t('common.roleStudent')}
</span>
);
};
Expand All @@ -41,15 +44,15 @@ export const Navbar: React.FC<NavbarProps> = ({ onToggleMobileMenu, isMobileOpen
<div className="flex items-center space-x-2 truncate">
<ShieldAlert className="w-4 h-4 sm:w-5 sm:h-5 shrink-0" />
<span className="truncate">
Mode Login As: <strong>{user.name}</strong> ({user.email})
{t('nav.impersonating')} <strong>{user.name}</strong> ({user.email})
</span>
</div>
<button
onClick={() => stopImpersonate()}
className="bg-white text-amber-700 hover:bg-amber-50 px-3 py-1 rounded-md text-xs font-bold transition shadow-xs flex items-center shrink-0 ml-2 cursor-pointer"
>
<UserCheck className="w-3.5 h-3.5 mr-1" />
Keluar
{t('nav.stopImpersonating')}
</button>
</div>
)}
Expand All @@ -61,7 +64,7 @@ export const Navbar: React.FC<NavbarProps> = ({ onToggleMobileMenu, isMobileOpen
type="button"
onClick={onToggleMobileMenu}
className="-m-2 p-2 text-gray-600 hover:text-emerald-700 hover:bg-emerald-50 rounded-lg md:hidden transition"
aria-label="Toggle mobile menu"
aria-label={t('nav.toggleMenu')}
>
{isMobileOpen ? <X className="w-6 h-6" /> : <Menu className="w-6 h-6" />}
</button>
Expand All @@ -71,22 +74,24 @@ export const Navbar: React.FC<NavbarProps> = ({ onToggleMobileMenu, isMobileOpen
<div className="flex items-center gap-3">
<img
src="/logo.png"
alt="Spring E-Learning Logo"
alt={t('common.logoAlt')}
className="w-9 h-9 rounded-xl object-cover shadow-xs border border-emerald-100"
/>
<div>
<h1 className="text-base sm:text-lg font-bold text-gray-900 tracking-tight leading-none">
Spring E-Learning
{t('common.appName')}
</h1>
<p className="text-[11px] text-gray-500 mt-0.5 hidden sm:block">
Learning Management & CBT System
{t('common.appTagline')}
</p>
</div>
</div>
</div>

{/* Right: User Profile & Actions */}
<div className="flex items-center space-x-3 sm:space-x-4">
<LanguageSwitcher />

<div className="flex items-center space-x-3 pr-2 sm:pr-4 border-r border-gray-200">
<div className="text-right hidden sm:block">
<div className="text-sm font-semibold text-gray-800 leading-tight">
Expand All @@ -100,21 +105,21 @@ export const Navbar: React.FC<NavbarProps> = ({ onToggleMobileMenu, isMobileOpen
{!user?.impersonatedBy && (
<Link
to="/change-password"
title="Ganti Password"
title={t('nav.changePassword')}
className="flex items-center space-x-1.5 text-sm text-gray-600 hover:text-emerald-700 transition px-2.5 sm:px-3 py-2 rounded-lg hover:bg-emerald-50"
>
<KeyRound className="w-4 h-4" />
<span className="font-medium hidden lg:inline">Password</span>
<span className="font-medium hidden lg:inline">{t('nav.passwordShort')}</span>
</Link>
)}

<button
onClick={logout}
title="Keluar dari Aplikasi"
title={t('nav.logoutTitle')}
className="flex items-center space-x-1.5 text-sm text-gray-600 hover:text-rose-600 transition px-2.5 sm:px-3 py-2 rounded-lg hover:bg-rose-50 cursor-pointer"
>
<LogOut className="w-4 h-4" />
<span className="font-medium hidden sm:inline">Keluar</span>
<span className="font-medium hidden sm:inline">{t('nav.logout')}</span>
</button>
</div>
</div>
Expand Down
40 changes: 21 additions & 19 deletions frontend/src/components/common/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import {
Award,
X,
} from 'lucide-react';
import { useI18n } from '../../i18n';

interface SidebarProps {
closeMobileMenu?: () => void;
}

export const Sidebar: React.FC<SidebarProps> = ({ closeMobileMenu }) => {
const { user } = useAuthStore();
const { t } = useI18n();
const isAdmin = user?.roles.includes('ROLE_ADMIN');
const isTeacher = user?.roles.includes('ROLE_TEACHER');

Expand All @@ -32,9 +34,9 @@ export const Sidebar: React.FC<SidebarProps> = ({ closeMobileMenu }) => {
}`;

const getRoleLabel = () => {
if (isAdmin) return 'Administrator';
if (isTeacher) return 'Guru Pengajar';
return 'Siswa';
if (isAdmin) return t('common.roleAdministrator');
if (isTeacher) return t('nav.roleTeacherLong');
return t('common.roleStudent');
};

return (
Expand All @@ -44,19 +46,19 @@ export const Sidebar: React.FC<SidebarProps> = ({ closeMobileMenu }) => {
<div className="flex items-center gap-3">
<img
src="/logo.png"
alt="Spring E-Learning Logo"
alt={t('common.logoAlt')}
className="w-9 h-9 rounded-xl object-cover shadow-xs border border-white/20"
/>
<span className="text-base font-bold text-white tracking-tight">
Spring E-Learning
{t('common.appName')}
</span>
</div>
{closeMobileMenu && (
<button
type="button"
onClick={closeMobileMenu}
className="md:hidden p-1.5 rounded-lg text-emerald-200 hover:text-white hover:bg-white/10 transition"
aria-label="Close sidebar"
aria-label={t('nav.closeSidebar')}
>
<X className="w-5 h-5" />
</button>
Expand All @@ -68,12 +70,12 @@ export const Sidebar: React.FC<SidebarProps> = ({ closeMobileMenu }) => {
{/* Main Menu */}
<div>
<p className="px-3 mb-2 text-[10px] font-bold uppercase tracking-widest text-emerald-300/60">
Menu Utama
{t('nav.sectionMain')}
</p>
<div className="space-y-0.5">
<NavLink to="/dashboard" onClick={closeMobileMenu} className={navClass}>
<LayoutDashboard className="w-5 h-5" />
<span>Dashboard</span>
<span>{t('nav.dashboard')}</span>
</NavLink>
</div>
</div>
Expand All @@ -82,28 +84,28 @@ export const Sidebar: React.FC<SidebarProps> = ({ closeMobileMenu }) => {
{isAdmin && (
<div>
<p className="px-3 mb-2 text-[10px] font-bold uppercase tracking-widest text-emerald-300/60">
Manajemen & User
{t('nav.sectionManagement')}
</p>
<div className="space-y-0.5">
<NavLink to="/teachers" onClick={closeMobileMenu} className={navClass}>
<UserCheck className="w-5 h-5" />
<span>Guru</span>
<span>{t('nav.teachers')}</span>
</NavLink>
<NavLink to="/students" onClick={closeMobileMenu} className={navClass}>
<Users className="w-5 h-5" />
<span>Siswa</span>
<span>{t('nav.students')}</span>
</NavLink>
<NavLink to="/classrooms" onClick={closeMobileMenu} className={navClass}>
<GraduationCap className="w-5 h-5" />
<span>Kelas</span>
<span>{t('nav.classrooms')}</span>
</NavLink>
<NavLink to="/subjects" onClick={closeMobileMenu} className={navClass}>
<BookMarked className="w-5 h-5" />
<span>Mata Pelajaran</span>
<span>{t('nav.subjects')}</span>
</NavLink>
<NavLink to="/announcements" onClick={closeMobileMenu} className={navClass}>
<Bell className="w-5 h-5" />
<span>Pengumuman</span>
<span>{t('nav.announcements')}</span>
</NavLink>
</div>
</div>
Expand All @@ -112,25 +114,25 @@ export const Sidebar: React.FC<SidebarProps> = ({ closeMobileMenu }) => {
{/* Academic & Learning CBT */}
<div>
<p className="px-3 mb-2 text-[10px] font-bold uppercase tracking-widest text-emerald-300/60">
Akademik & CBT
{t('nav.sectionAcademic')}
</p>
<div className="space-y-0.5">
<NavLink to="/exams" onClick={closeMobileMenu} className={navClass}>
<FileCheck2 className="w-5 h-5" />
<span>{isTeacher || isAdmin ? 'Ujian & CBT' : 'Ujian Saya'}</span>
<span>{isTeacher || isAdmin ? t('nav.examsManage') : t('nav.examsMine')}</span>
</NavLink>
<NavLink to="/materials" onClick={closeMobileMenu} className={navClass}>
<FileText className="w-5 h-5" />
<span>Materi Belajar</span>
<span>{t('nav.materials')}</span>
</NavLink>
<NavLink to="/assignments" onClick={closeMobileMenu} className={navClass}>
<MessageSquareShare className="w-5 h-5" />
<span>Tugas & Forum</span>
<span>{t('nav.assignments')}</span>
</NavLink>
{!isAdmin && !isTeacher && (
<NavLink to="/grades" onClick={closeMobileMenu} className={navClass}>
<Award className="w-5 h-5" />
<span>Nilai & Rapor</span>
<span>{t('nav.grades')}</span>
</NavLink>
)}
</div>
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/i18n/define.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Declares one namespace of UI text. Both languages must have exactly the same keys: a key that is
* missing in either one is a type error, so `npm run build` catches untranslated text.
*
* Placeholders use braces: "Hello {name}" with t('ns.key', { name }).
*/
export function defineMessages<K extends string>(messages: { id: Record<K, string>; en: Record<K, string> }) {
return messages;
}
57 changes: 57 additions & 0 deletions frontend/src/i18n/detect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export type Lang = 'id' | 'en';

const STORAGE_KEY = 'lang';

/** Set by the frontend nginx from Cloudflare's CF-IPCountry header (see nginx.conf). */
const COUNTRY_COOKIE = /(?:^|;\s*)geo_country=([A-Z]{2})(?:;|$)/;

/** WIB, WITA and WIT. */
const INDONESIAN_TIME_ZONES = new Set(['Asia/Jakarta', 'Asia/Pontianak', 'Asia/Makassar', 'Asia/Jayapura']);

const isLang = (value: unknown): value is Lang => value === 'id' || value === 'en';

/** The language the user picked with the selector, if any. Storage can be blocked (private mode). */
export function readSavedLanguage(): Lang | null {
try {
const value = localStorage.getItem(STORAGE_KEY);
return isLang(value) ? value : null;
} catch {
return null;
}
}

export function saveLanguage(lang: Lang): void {
try {
localStorage.setItem(STORAGE_KEY, lang);
} catch {
/* the choice then only lasts for this page load */
}
}

/** Two-letter country code of the visitor, or null when unknown (no Cloudflare, local dev, "XX"). */
export function readCountry(cookie: string = document.cookie): string | null {
const country = cookie.match(COUNTRY_COOKIE)?.[1];
return country && country !== 'XX' ? country : null;
}

function browserTimeZone(): string | undefined {
try {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
} catch {
return undefined;
}
}

/**
* Indonesian for visitors in Indonesia, English for everyone else. The IP country decides when it is
* known; otherwise the browser's time zone stands in for the location.
*/
export function languageForLocation(country: string | null, timeZone: string | undefined): Lang {
if (country) return country === 'ID' ? 'id' : 'en';
return timeZone && INDONESIAN_TIME_ZONES.has(timeZone) ? 'id' : 'en';
}

/** An explicit choice from the selector always wins over the location. */
export function detectLanguage(): Lang {
return readSavedLanguage() ?? languageForLocation(readCountry(), browserTimeZone());
}
Loading
Loading