feat: Implement multi-tenancy with default organization, global courses, user profiles, and new UI components like OrganizationSelector and Combobox.

This commit is contained in:
2026-01-16 12:15:15 -03:00
parent 663950aa0e
commit 2dffbd8b71
20 changed files with 942 additions and 153 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ export default function CatalogPage() {
useEffect(() => {
const fetchData = async () => {
try {
const coursesData = await lmsApi.getCatalog(user?.organization_id);
const coursesData = await lmsApi.getCatalog(user?.organization_id, user?.id);
setCourses(coursesData);
if (user) {
+156
View File
@@ -0,0 +1,156 @@
"use client";
import { useState, useEffect } from "react";
import { useAuth } from "@/context/AuthContext";
import { lmsApi } from "@/lib/api";
import { User, Save, Shield, Mail, User as UserIcon, Building, Trophy, Flame } from "lucide-react";
export default function ProfilePage() {
const { user, logout } = useAuth();
const [fullName, setFullName] = useState(user?.full_name || "");
const [email, setEmail] = useState(user?.email || "");
const [saving, setSaving] = useState(false);
const [message, setMessage] = useState<{ type: 'success' | 'error', text: string } | null>(null);
useEffect(() => {
if (user) {
setFullName(user.full_name);
setEmail(user.email);
}
}, [user]);
const handleSave = async (e: React.FormEvent) => {
e.preventDefault();
if (!user) return;
try {
setSaving(true);
setMessage(null);
await lmsApi.updateUser(user.id, {
full_name: fullName
});
setMessage({ type: 'success', text: 'Profile updated successfully!' });
} catch (err) {
console.error(err);
setMessage({ type: 'error', text: 'Failed to update profile.' });
} finally {
setSaving(false);
}
};
if (!user) return null;
return (
<div className="max-w-4xl mx-auto py-12 px-6">
<div className="mb-12">
<h1 className="text-4xl font-black tracking-tight text-white mb-2">My Profile</h1>
<p className="text-gray-400">Personalize your learning experience and track your progress.</p>
</div>
<div className="grid grid-cols-1 md:grid-cols-3 gap-8">
{/* Profile Card & Stats */}
<div className="md:col-span-1 space-y-6">
<div className="glass p-8 rounded-3xl border border-white/5 flex flex-col items-center text-center">
<div className="w-24 h-24 rounded-full bg-blue-600/20 border-2 border-blue-500/30 flex items-center justify-center font-black text-3xl text-blue-400 mb-4 shadow-2xl shadow-blue-500/20">
{user.full_name.charAt(0)}
</div>
<h2 className="text-xl font-bold text-white">{user.full_name}</h2>
<span className="text-xs font-black uppercase tracking-widest text-blue-500 mt-1">Student</span>
<div className="w-full h-px bg-white/5 my-6" />
<div className="w-full flex flex-col gap-4 text-left">
<div className="flex items-center justify-between p-3 rounded-xl bg-white/5 border border-white/5">
<div className="flex items-center gap-3">
<Trophy size={16} className="text-yellow-500" />
<span className="text-xs font-bold text-white uppercase tracking-tighter">Level</span>
</div>
<span className="text-lg font-black text-white">{user.level || 1}</span>
</div>
<div className="flex items-center justify-between p-3 rounded-xl bg-white/5 border border-white/5">
<div className="flex items-center gap-3">
<Flame size={16} className="text-orange-500" />
<span className="text-xs font-bold text-white uppercase tracking-tighter">XP</span>
</div>
<span className="text-lg font-black text-white">{user.xp || 0}</span>
</div>
</div>
<button
onClick={logout}
className="mt-8 w-full py-3 rounded-xl border border-white/10 text-sm font-bold text-gray-400 hover:text-white hover:bg-white/5 transition-all"
>
Logout Session
</button>
</div>
</div>
{/* Settings Form */}
<div className="md:col-span-2 space-y-6">
<form onSubmit={handleSave} className="glass p-8 rounded-3xl border border-white/5 space-y-6">
<div className="space-y-2">
<label className="text-xs font-black uppercase tracking-widest text-gray-500 flex items-center gap-2">
<UserIcon size={14} /> Full Name
</label>
<input
type="text"
value={fullName}
onChange={(e) => setFullName(e.target.value)}
className="w-full bg-black/40 border border-white/10 rounded-xl px-4 py-3 text-white focus:outline-none focus:border-blue-500/50 transition-colors placeholder:text-gray-700"
placeholder="Enter your full name"
required
/>
</div>
<div className="space-y-2 opacity-60">
<label className="text-xs font-black uppercase tracking-widest text-gray-500 flex items-center gap-2">
<Mail size={14} /> Email Address
</label>
<input
type="email"
value={email}
disabled
className="w-full bg-black/40 border border-white/10 rounded-xl px-4 py-3 text-white/50 cursor-not-allowed"
/>
<p className="text-[10px] text-gray-500 italic">Email cannot be changed currently.</p>
</div>
{message && (
<div className={`p-4 rounded-xl text-sm font-medium ${message.type === 'success' ? 'bg-green-500/10 text-green-400 border border-green-500/20' : 'bg-red-500/10 text-red-400 border border-red-500/20'}`}>
{message.text}
</div>
)}
<button
type="submit"
disabled={saving}
className="w-full py-4 bg-blue-600 hover:bg-blue-500 disabled:bg-blue-600/50 rounded-xl font-black text-white shadow-lg shadow-blue-500/20 transition-all active:scale-[0.98] flex items-center justify-center gap-3"
>
{saving ? (
<div className="w-5 h-5 border-2 border-white/20 border-t-white rounded-full animate-spin" />
) : (
<Save size={20} />
)}
Save Changes
</button>
</form>
<div className="glass p-6 rounded-3xl border border-white/5 flex items-center justify-between">
<div className="flex items-center gap-4">
<div className="w-10 h-10 rounded-xl bg-white/5 flex items-center justify-center">
<Shield size={18} className="text-gray-400" />
</div>
<div>
<h3 className="text-white font-bold text-sm">Organization</h3>
<p className="text-xs text-gray-500 mt-0.5 truncate max-w-[200px]">{user.organization_id}</p>
</div>
</div>
<span className="text-[10px] font-black uppercase tracking-widest text-blue-500 bg-blue-500/10 px-3 py-1 rounded-full border border-blue-500/20">Active Tenant</span>
</div>
</div>
</div>
</div>
);
}
+19 -2
View File
@@ -2,9 +2,12 @@
import Link from "next/link";
import { useBranding } from "@/context/BrandingContext";
import { useAuth } from "@/context/AuthContext";
import { LogOut } from "lucide-react";
export default function AppHeader() {
const { branding } = useBranding();
const { user, logout } = useAuth();
return (
<header className="h-16 glass sticky top-0 z-50 px-6 flex items-center justify-between backdrop-blur-xl bg-black/40 border-b border-white/5">
@@ -26,8 +29,22 @@ export default function AppHeader() {
<nav className="hidden md:flex items-center gap-8">
<Link href="/" className="text-[10px] font-black uppercase tracking-[0.2em] text-gray-400 hover:text-white transition-colors">Catalog</Link>
<Link href="#" className="text-[10px] font-black uppercase tracking-[0.2em] text-gray-400 hover:text-white transition-colors">My Learning</Link>
<div className="w-8 h-8 rounded-full bg-white/5 border border-white/10" />
<Link href="/my-learning" className="text-[10px] font-black uppercase tracking-[0.2em] text-gray-400 hover:text-white transition-colors">My Learning</Link>
<div className="flex items-center gap-4 pl-4 border-l border-white/10">
<Link href="/profile" className="flex items-center gap-2 group/profile">
<div className="w-8 h-8 rounded-full bg-white/5 border border-white/10 flex items-center justify-center font-bold text-xs text-blue-400 group-hover/profile:border-blue-500/50 transition-colors">
{user?.full_name?.charAt(0) || 'U'}
</div>
</Link>
<button
onClick={logout}
className="p-2 hover:bg-red-500/10 rounded-full text-gray-400 hover:text-red-400 transition-colors"
title="Logout"
>
<LogOut size={16} />
</button>
</div>
</nav>
</header>
);
+12 -2
View File
@@ -149,8 +149,11 @@ const apiFetch = async (url: string, options: RequestInit = {}, isCMS: boolean =
};
export const lmsApi = {
async getCatalog(orgId?: string): Promise<Course[]> {
const query = orgId ? `?organization_id=${orgId}` : '';
async getCatalog(orgId?: string, userId?: string): Promise<Course[]> {
const params = new URLSearchParams();
if (orgId) params.append('organization_id', orgId);
if (userId) params.append('user_id', userId);
const query = params.toString() ? `?${params.toString()}` : '';
return apiFetch(`/catalog${query}`);
},
@@ -208,5 +211,12 @@ export const lmsApi = {
async getBranding(orgId: string): Promise<Organization> {
return apiFetch(`/organizations/${orgId}/branding`, {}, true);
},
async updateUser(userId: string, payload: { full_name?: string }): Promise<void> {
return apiFetch(`/users/${userId}`, {
method: 'POST',
body: JSON.stringify(payload)
});
}
};