Add frontend login/register forms and event list component

This commit is contained in:
Denis Urs Rudolph
2026-04-03 21:18:59 +02:00
parent b54e2265d9
commit 71ba829d6c
3 changed files with 364 additions and 0 deletions
+134
View File
@@ -0,0 +1,134 @@
'use client';
import { useState, useEffect } from 'react';
import { api, Event } from '@/lib/api';
export function EventList() {
const [events, setEvents] = useState<Event[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [category, setCategory] = useState('');
const [status, setStatus] = useState('');
useEffect(() => {
loadEvents();
}, [category, status]);
const loadEvents = async () => {
try {
setIsLoading(true);
const data = await api.getEvents({
category: category || undefined,
status: status || undefined,
});
setEvents(data);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load events');
} finally {
setIsLoading(false);
}
};
const categories = ['Running', 'Cycling', 'Triathlon', 'Trail', 'Road'];
if (isLoading) {
return <div className="text-center py-8">Loading events...</div>;
}
if (error) {
return (
<div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded">
{error}
</div>
);
}
return (
<div className="space-y-6">
{/* Filters */}
<div className="flex gap-4 mb-6">
<select
value={category}
onChange={(e) => setCategory(e.target.value)}
className="px-3 py-2 border border-gray-300 rounded-md"
>
<option value="">All Categories</option>
{categories.map((cat) => (
<option key={cat} value={cat}>{cat}</option>
))}
</select>
<select
value={status}
onChange={(e) => setStatus(e.target.value)}
className="px-3 py-2 border border-gray-300 rounded-md"
>
<option value="">All Status</option>
<option value="Published">Published</option>
<option value="Draft">Draft</option>
<option value="Cancelled">Cancelled</option>
</select>
</div>
{/* Events Grid */}
{events.length === 0 ? (
<div className="text-center py-8 text-gray-500">No events found</div>
) : (
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{events.map((event) => (
<div key={event.id} className="bg-white rounded-lg shadow-md p-6 hover:shadow-lg transition-shadow">
<div className="flex justify-between items-start mb-2">
<span className={`px-2 py-1 text-xs rounded ${
event.status === 'Published' ? 'bg-green-100 text-green-800' :
event.status === 'Draft' ? 'bg-yellow-100 text-yellow-800' :
'bg-red-100 text-red-800'
}`}>
{event.status}
</span>
{event.category && (
<span className="text-sm text-gray-500">{event.category}</span>
)}
</div>
<h3 className="text-xl font-semibold mb-2">{event.name}</h3>
<p className="text-gray-600 mb-4 line-clamp-2">{event.description}</p>
<div className="space-y-2 text-sm text-gray-500">
<div className="flex items-center gap-2">
<span>📅</span>
{new Date(event.eventDate).toLocaleDateString()}
</div>
<div className="flex items-center gap-2">
<span>📍</span>
{event.location}
</div>
<div className="flex items-center gap-2">
<span>👥</span>
{event.currentRegistrations}
{event.maxParticipants && ` / ${event.maxParticipants}`} registered
</div>
</div>
{event.tags.length > 0 && (
<div className="flex flex-wrap gap-2 mt-4">
{event.tags.map((tag) => (
<span key={tag} className="px-2 py-1 bg-gray-100 text-gray-700 text-xs rounded">
{tag}
</span>
))}
</div>
)}
<a
href={`/events/${event.id}`}
className="mt-4 block text-center py-2 px-4 bg-blue-600 text-white rounded-md hover:bg-blue-700"
>
View Details
</a>
</div>
))}
</div>
)}
</div>
);
}
+85
View File
@@ -0,0 +1,85 @@
'use client';
import { useState } from 'react';
import { useAuth } from '@/lib/auth-context';
import { useRouter } from 'next/navigation';
export function LoginForm() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const { login, error } = useAuth();
const router = useRouter();
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setIsSubmitting(true);
try {
await login(email, password);
router.push('/dashboard');
} catch (err) {
// Error is handled by auth context
} finally {
setIsSubmitting(false);
}
};
return (
<div className="max-w-md mx-auto mt-8 p-6 bg-white rounded-lg shadow-md">
<h2 className="text-2xl font-bold mb-6 text-gray-800">Login</h2>
{error && (
<div className="mb-4 p-3 bg-red-100 border border-red-400 text-red-700 rounded">
{error}
</div>
)}
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-1">
Email
</label>
<input
id="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div>
<label htmlFor="password" className="block text-sm font-medium text-gray-700 mb-1">
Password
</label>
<input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
minLength={8}
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<button
type="submit"
disabled={isSubmitting}
className="w-full py-2 px-4 bg-blue-600 text-white rounded-md hover:bg-blue-700 disabled:bg-gray-400 transition-colors"
>
{isSubmitting ? 'Logging in...' : 'Login'}
</button>
</form>
<p className="mt-4 text-center text-sm text-gray-600">
Don&apos;t have an account?{' '}
<a href="/register" className="text-blue-600 hover:underline">
Register
</a>
</p>
</div>
);
}
+145
View File
@@ -0,0 +1,145 @@
'use client';
import { useState } from 'react';
import { useAuth } from '@/lib/auth-context';
import { useRouter } from 'next/navigation';
export function RegisterForm() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [name, setName] = useState('');
const [role, setRole] = useState<'Organizer' | 'Participant'>('Participant');
const [isSubmitting, setIsSubmitting] = useState(false);
const [validationError, setValidationError] = useState<string | null>(null);
const { register, error } = useAuth();
const router = useRouter();
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setValidationError(null);
if (password !== confirmPassword) {
setValidationError('Passwords do not match');
return;
}
if (password.length < 8) {
setValidationError('Password must be at least 8 characters');
return;
}
setIsSubmitting(true);
try {
await register(email, password, name, role);
router.push('/dashboard');
} catch (err) {
// Error is handled by auth context
} finally {
setIsSubmitting(false);
}
};
return (
<div className="max-w-md mx-auto mt-8 p-6 bg-white rounded-lg shadow-md">
<h2 className="text-2xl font-bold mb-6 text-gray-800">Register</h2>
{(error || validationError) && (
<div className="mb-4 p-3 bg-red-100 border border-red-400 text-red-700 rounded">
{error || validationError}
</div>
)}
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label htmlFor="name" className="block text-sm font-medium text-gray-700 mb-1">
Full Name
</label>
<input
id="name"
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
required
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div>
<label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-1">
Email
</label>
<input
id="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div>
<label htmlFor="password" className="block text-sm font-medium text-gray-700 mb-1">
Password
</label>
<input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
minLength={8}
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div>
<label htmlFor="confirmPassword" className="block text-sm font-medium text-gray-700 mb-1">
Confirm Password
</label>
<input
id="confirmPassword"
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
required
minLength={8}
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div>
<label htmlFor="role" className="block text-sm font-medium text-gray-700 mb-1">
Account Type
</label>
<select
id="role"
value={role}
onChange={(e) => setRole(e.target.value as 'Organizer' | 'Participant')}
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<option value="Participant">Participant</option>
<option value="Organizer">Organizer</option>
</select>
</div>
<button
type="submit"
disabled={isSubmitting}
className="w-full py-2 px-4 bg-blue-600 text-white rounded-md hover:bg-blue-700 disabled:bg-gray-400 transition-colors"
>
{isSubmitting ? 'Registering...' : 'Register'}
</button>
</form>
<p className="mt-4 text-center text-sm text-gray-600">
Already have an account?{' '}
<a href="/login" className="text-blue-600 hover:underline">
Login
</a>
</p>
</div>
);
}