mirror of
https://github.com/R0m1k3/podcastic.git
synced 2026-10-22 14:38:03 +02:00
feat: implement React components - dashboard, auth pages, audio player UI
This commit is contained in:
1 parent
8c00a1b0f5
commit
068f4e7ef7
7 files changed
+791
-20
No files matched your search
@@ -0,0 +1,120 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { episodeService, Episode } from '../services/episodeService';
|
||||
import { authService } from '../services/authService';
|
||||
import Header from '../components/Header';
|
||||
import EpisodeCard from '../components/EpisodeCard';
|
||||
import AudioPlayer from '../components/AudioPlayer';
|
||||
|
||||
export default function Dashboard() {
|
||||
const [user, setUser] = useState<any>(null);
|
||||
const [selectedEpisode, setSelectedEpisode] = useState<Episode | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
// Load user
|
||||
useEffect(() => {
|
||||
const loadUser = async () => {
|
||||
try {
|
||||
const response = await authService.getMe();
|
||||
setUser(response.user);
|
||||
} catch (error) {
|
||||
console.error('Failed to load user:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
loadUser();
|
||||
}, []);
|
||||
|
||||
// Fetch latest episodes
|
||||
const { data: episodesData, isLoading: episodesLoading } = useQuery({
|
||||
queryKey: ['episodes', 'latest'],
|
||||
queryFn: () => episodeService.getLatestEpisodes(30),
|
||||
enabled: !!user,
|
||||
staleTime: 5 * 60 * 1000, // 5 minutes
|
||||
});
|
||||
|
||||
const handleLogout = async () => {
|
||||
try {
|
||||
await authService.logout();
|
||||
authService.clearTokens();
|
||||
window.location.href = '/login';
|
||||
} catch (error) {
|
||||
console.error('Logout failed:', error);
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-gradient-light">
|
||||
<div className="text-center">
|
||||
<div className="w-16 h-16 rounded-full border-4 border-light-200 border-t-blue-500 animate-spin mx-auto mb-4"></div>
|
||||
<p className="text-light-600">Loading your podcasts...</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-light-50">
|
||||
<Header
|
||||
title="Podcastic"
|
||||
subtitle="Your podcast companion"
|
||||
user={user}
|
||||
onLogout={handleLogout}
|
||||
/>
|
||||
|
||||
<main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
|
||||
{/* Welcome Section */}
|
||||
<div className="mb-12">
|
||||
<h2 className="text-3xl font-bold text-light-900 mb-2">
|
||||
Welcome back, {user?.username}! 👋
|
||||
</h2>
|
||||
<p className="text-light-600">
|
||||
{episodesData?.count || 0} new episodes from your subscriptions
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Latest Episodes Grid */}
|
||||
{episodesLoading ? (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<div className="text-center">
|
||||
<div className="w-12 h-12 rounded-full border-4 border-light-200 border-t-blue-500 animate-spin mx-auto mb-3"></div>
|
||||
<p className="text-light-600">Loading episodes...</p>
|
||||
</div>
|
||||
</div>
|
||||
) : episodesData?.episodes && episodesData.episodes.length > 0 ? (
|
||||
<div>
|
||||
<h3 className="text-2xl font-bold text-light-900 mb-6">Latest Episodes</h3>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
|
||||
{episodesData.episodes.map((episode) => (
|
||||
<EpisodeCard
|
||||
key={episode._id}
|
||||
episode={episode}
|
||||
onPlay={setSelectedEpisode}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-center py-12">
|
||||
<p className="text-light-600 text-lg mb-4">No episodes yet</p>
|
||||
<p className="text-light-500">
|
||||
Subscribe to podcasts to see episodes in your dashboard
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</main>
|
||||
|
||||
{/* Audio Player */}
|
||||
{selectedEpisode && (
|
||||
<AudioPlayer
|
||||
episode={selectedEpisode}
|
||||
onClose={() => setSelectedEpisode(null)}
|
||||
userId={user?._id}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
import { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { authService } from '../services/authService';
|
||||
|
||||
export default function Login() {
|
||||
const navigate = useNavigate();
|
||||
const [isLogin, setIsLogin] = useState(true);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const [formData, setFormData] = useState({
|
||||
email: '',
|
||||
password: '',
|
||||
username: '',
|
||||
});
|
||||
|
||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData((prev) => ({ ...prev, [name]: value }));
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
if (isLogin) {
|
||||
const response = await authService.login(formData.email, formData.password);
|
||||
authService.setTokens(response.tokens.accessToken, response.tokens.refreshToken);
|
||||
navigate('/dashboard');
|
||||
} else {
|
||||
const response = await authService.register(
|
||||
formData.email,
|
||||
formData.password,
|
||||
formData.username
|
||||
);
|
||||
authService.setTokens(response.tokens.accessToken, response.tokens.refreshToken);
|
||||
navigate('/dashboard');
|
||||
}
|
||||
} catch (err: any) {
|
||||
setError(err.response?.data?.message || 'An error occurred');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gradient-light flex items-center justify-center px-4">
|
||||
<div className="w-full max-w-md">
|
||||
{/* Header */}
|
||||
<div className="text-center mb-8">
|
||||
<div className="w-16 h-16 rounded-2xl bg-gradient-primary flex items-center justify-center text-white text-4xl mx-auto mb-4">
|
||||
🎙️
|
||||
</div>
|
||||
<h1 className="text-4xl font-bold text-light-900 mb-2">Podcastic</h1>
|
||||
<p className="text-light-600">Your podcast streaming companion</p>
|
||||
</div>
|
||||
|
||||
{/* Form Card */}
|
||||
<div className="card mb-6">
|
||||
<div className="mb-6">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setIsLogin(!isLogin);
|
||||
setError(null);
|
||||
setFormData({ email: '', password: '', username: '' });
|
||||
}}
|
||||
className="w-full mb-4"
|
||||
>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
className={`flex-1 py-2 rounded-lg font-medium transition-colors ${
|
||||
isLogin
|
||||
? 'bg-blue-500 text-white'
|
||||
: 'bg-light-100 text-light-700 hover:bg-light-200'
|
||||
}`}
|
||||
onClick={() => setIsLogin(true)}
|
||||
>
|
||||
Sign In
|
||||
</button>
|
||||
<button
|
||||
className={`flex-1 py-2 rounded-lg font-medium transition-colors ${
|
||||
!isLogin
|
||||
? 'bg-blue-500 text-white'
|
||||
: 'bg-light-100 text-light-700 hover:bg-light-200'
|
||||
}`}
|
||||
onClick={() => setIsLogin(false)}
|
||||
>
|
||||
Create Account
|
||||
</button>
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
{!isLogin && (
|
||||
<input
|
||||
type="text"
|
||||
name="username"
|
||||
placeholder="Username"
|
||||
value={formData.username}
|
||||
onChange={handleChange}
|
||||
required={!isLogin}
|
||||
className="input"
|
||||
/>
|
||||
)}
|
||||
|
||||
<input
|
||||
type="email"
|
||||
name="email"
|
||||
placeholder="Email address"
|
||||
value={formData.email}
|
||||
onChange={handleChange}
|
||||
required
|
||||
className="input"
|
||||
/>
|
||||
|
||||
<input
|
||||
type="password"
|
||||
name="password"
|
||||
placeholder="Password"
|
||||
value={formData.password}
|
||||
onChange={handleChange}
|
||||
required
|
||||
className="input"
|
||||
/>
|
||||
|
||||
{error && (
|
||||
<div className="p-4 rounded-lg bg-red-50 text-red-700 text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="btn-primary w-full"
|
||||
>
|
||||
{loading ? (
|
||||
<div className="flex items-center justify-center gap-2">
|
||||
<div className="spinner border-white"></div>
|
||||
{isLogin ? 'Signing in...' : 'Creating account...'}
|
||||
</div>
|
||||
) : isLogin ? (
|
||||
'Sign In'
|
||||
) : (
|
||||
'Create Account'
|
||||
)}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{/* Footer */}
|
||||
<p className="text-center text-sm text-light-600">
|
||||
{isLogin
|
||||
? "Don't have an account? Click Create Account above"
|
||||
: 'Already have an account? Click Sign In above'}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user