mirror of
https://github.com/clucraft/PriceGhost.git
synced 2026-05-15 10:52:36 +02:00
Initial commit: PriceGhost price tracking application
Full-stack application for tracking product prices: - Backend: Node.js + Express + TypeScript - Frontend: React + Vite + TypeScript - Database: PostgreSQL - Price scraping with Cheerio - JWT authentication - Background price checking with node-cron - Price history charts with Recharts - Docker support with docker-compose Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
commit
10660e5626
44 changed files with 3662 additions and 0 deletions
184
frontend/src/components/AuthForm.tsx
Normal file
184
frontend/src/components/AuthForm.tsx
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
import { useState, FormEvent } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
|
||||
interface AuthFormProps {
|
||||
mode: 'login' | 'register';
|
||||
onSubmit: (email: string, password: string) => Promise<void>;
|
||||
}
|
||||
|
||||
export default function AuthForm({ mode, onSubmit }: AuthFormProps) {
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [confirmPassword, setConfirmPassword] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
const handleSubmit = async (e: FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError('');
|
||||
|
||||
if (mode === 'register' && password !== confirmPassword) {
|
||||
setError('Passwords do not match');
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.length < 8) {
|
||||
setError('Password must be at least 8 characters');
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
await onSubmit(email, password);
|
||||
} catch (err) {
|
||||
if (err instanceof Error) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const axiosError = err as any;
|
||||
setError(axiosError.response?.data?.error || 'An error occurred');
|
||||
} else {
|
||||
setError('An error occurred');
|
||||
}
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="auth-form-container">
|
||||
<style>{`
|
||||
.auth-form-container {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.auth-form-card {
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
background: var(--surface);
|
||||
border-radius: 1rem;
|
||||
box-shadow: var(--shadow-lg);
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.auth-form-header {
|
||||
text-align: center;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.auth-form-logo {
|
||||
font-size: 2.5rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.auth-form-title {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 600;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.auth-form-subtitle {
|
||||
color: var(--text-muted);
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
.auth-form-footer {
|
||||
text-align: center;
|
||||
margin-top: 1.5rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.auth-form-footer a {
|
||||
font-weight: 500;
|
||||
}
|
||||
`}</style>
|
||||
|
||||
<div className="auth-form-card">
|
||||
<div className="auth-form-header">
|
||||
<div className="auth-form-logo">👻</div>
|
||||
<h1 className="auth-form-title">PriceGhost</h1>
|
||||
<p className="auth-form-subtitle">
|
||||
{mode === 'login'
|
||||
? 'Sign in to track prices'
|
||||
: 'Create your account'}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{error && <div className="alert alert-error">{error}</div>}
|
||||
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="form-group">
|
||||
<label htmlFor="email">Email</label>
|
||||
<input
|
||||
type="email"
|
||||
id="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
placeholder="you@example.com"
|
||||
required
|
||||
autoComplete="email"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<label htmlFor="password">Password</label>
|
||||
<input
|
||||
type="password"
|
||||
id="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
placeholder="••••••••"
|
||||
required
|
||||
autoComplete={mode === 'login' ? 'current-password' : 'new-password'}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{mode === 'register' && (
|
||||
<div className="form-group">
|
||||
<label htmlFor="confirmPassword">Confirm Password</label>
|
||||
<input
|
||||
type="password"
|
||||
id="confirmPassword"
|
||||
value={confirmPassword}
|
||||
onChange={(e) => setConfirmPassword(e.target.value)}
|
||||
placeholder="••••••••"
|
||||
required
|
||||
autoComplete="new-password"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
className="btn btn-primary"
|
||||
style={{ width: '100%', marginTop: '0.5rem' }}
|
||||
disabled={isLoading}
|
||||
>
|
||||
{isLoading ? (
|
||||
<span className="spinner" />
|
||||
) : mode === 'login' ? (
|
||||
'Sign In'
|
||||
) : (
|
||||
'Create Account'
|
||||
)}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div className="auth-form-footer">
|
||||
{mode === 'login' ? (
|
||||
<>
|
||||
Don't have an account? <Link to="/register">Sign up</Link>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
Already have an account? <Link to="/login">Sign in</Link>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
115
frontend/src/components/Layout.tsx
Normal file
115
frontend/src/components/Layout.tsx
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
import { ReactNode } from 'react';
|
||||
import { Link, useNavigate } from 'react-router-dom';
|
||||
import { useAuth } from '../hooks/useAuth';
|
||||
|
||||
interface LayoutProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export default function Layout({ children }: LayoutProps) {
|
||||
const { user, logout } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const handleLogout = () => {
|
||||
logout();
|
||||
navigate('/login');
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="layout">
|
||||
<style>{`
|
||||
.layout {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.navbar {
|
||||
background: var(--surface);
|
||||
border-bottom: 1px solid var(--border);
|
||||
padding: 0 1rem;
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.navbar-content {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
height: 64px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.navbar-brand {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
color: var(--text);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.navbar-brand:hover {
|
||||
text-decoration: none;
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
.navbar-brand-icon {
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.navbar-user {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.navbar-email {
|
||||
color: var(--text-muted);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.main-content {
|
||||
flex: 1;
|
||||
padding: 2rem 1rem;
|
||||
}
|
||||
|
||||
.main-content-inner {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.navbar-email {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
`}</style>
|
||||
|
||||
<nav className="navbar">
|
||||
<div className="navbar-content">
|
||||
<Link to="/" className="navbar-brand">
|
||||
<span className="navbar-brand-icon">👻</span>
|
||||
<span>PriceGhost</span>
|
||||
</Link>
|
||||
|
||||
{user && (
|
||||
<div className="navbar-user">
|
||||
<span className="navbar-email">{user.email}</span>
|
||||
<button className="btn btn-secondary" onClick={handleLogout}>
|
||||
Logout
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main className="main-content">
|
||||
<div className="main-content-inner">{children}</div>
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
262
frontend/src/components/PriceChart.tsx
Normal file
262
frontend/src/components/PriceChart.tsx
Normal file
|
|
@ -0,0 +1,262 @@
|
|||
import { useState } from 'react';
|
||||
import {
|
||||
LineChart,
|
||||
Line,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
ResponsiveContainer,
|
||||
ReferenceLine,
|
||||
} from 'recharts';
|
||||
import { PriceHistory } from '../api/client';
|
||||
|
||||
interface PriceChartProps {
|
||||
prices: PriceHistory[];
|
||||
currency: string;
|
||||
onRangeChange?: (days: number | undefined) => void;
|
||||
}
|
||||
|
||||
const DATE_RANGES = [
|
||||
{ value: 7, label: '7 days' },
|
||||
{ value: 30, label: '30 days' },
|
||||
{ value: 90, label: '90 days' },
|
||||
{ value: undefined, label: 'All time' },
|
||||
];
|
||||
|
||||
export default function PriceChart({
|
||||
prices,
|
||||
currency,
|
||||
onRangeChange,
|
||||
}: PriceChartProps) {
|
||||
const [selectedRange, setSelectedRange] = useState<number | undefined>(30);
|
||||
|
||||
const handleRangeChange = (days: number | undefined) => {
|
||||
setSelectedRange(days);
|
||||
onRangeChange?.(days);
|
||||
};
|
||||
|
||||
const currencySymbol =
|
||||
currency === 'EUR' ? '€' : currency === 'GBP' ? '£' : '$';
|
||||
|
||||
const chartData = prices.map((p) => ({
|
||||
date: new Date(p.recorded_at).getTime(),
|
||||
price: parseFloat(p.price.toString()),
|
||||
}));
|
||||
|
||||
const minPrice = Math.min(...chartData.map((d) => d.price));
|
||||
const maxPrice = Math.max(...chartData.map((d) => d.price));
|
||||
const avgPrice =
|
||||
chartData.reduce((sum, d) => sum + d.price, 0) / chartData.length;
|
||||
|
||||
const formatDate = (timestamp: number) => {
|
||||
const date = new Date(timestamp);
|
||||
return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
|
||||
};
|
||||
|
||||
const formatPrice = (value: number) => {
|
||||
return `${currencySymbol}${value.toFixed(2)}`;
|
||||
};
|
||||
|
||||
if (prices.length === 0) {
|
||||
return (
|
||||
<div className="price-chart-empty">
|
||||
<style>{`
|
||||
.price-chart-empty {
|
||||
background: var(--surface);
|
||||
border-radius: 0.75rem;
|
||||
padding: 3rem;
|
||||
text-align: center;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
`}</style>
|
||||
<p>No price history available yet.</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="price-chart">
|
||||
<style>{`
|
||||
.price-chart {
|
||||
background: var(--surface);
|
||||
border-radius: 0.75rem;
|
||||
padding: 1.5rem;
|
||||
box-shadow: var(--shadow);
|
||||
}
|
||||
|
||||
.price-chart-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 1.5rem;
|
||||
flex-wrap: wrap;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.price-chart-title {
|
||||
font-size: 1.125rem;
|
||||
font-weight: 600;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.price-chart-range {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.price-chart-range button {
|
||||
padding: 0.375rem 0.75rem;
|
||||
border: 1px solid var(--border);
|
||||
background: var(--surface);
|
||||
border-radius: 0.375rem;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-muted);
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.price-chart-range button:hover {
|
||||
border-color: var(--primary);
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
.price-chart-range button.active {
|
||||
background: var(--primary);
|
||||
border-color: var(--primary);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.price-chart-container {
|
||||
height: 300px;
|
||||
}
|
||||
|
||||
.price-chart-stats {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
gap: 1rem;
|
||||
margin-top: 1.5rem;
|
||||
padding-top: 1.5rem;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.price-stat {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.price-stat-label {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-muted);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
}
|
||||
|
||||
.price-stat-value {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
.price-stat-value.min {
|
||||
color: var(--secondary);
|
||||
}
|
||||
|
||||
.price-stat-value.max {
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
.price-stat-value.avg {
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.price-chart-stats {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
`}</style>
|
||||
|
||||
<div className="price-chart-header">
|
||||
<h3 className="price-chart-title">Price History</h3>
|
||||
<div className="price-chart-range">
|
||||
{DATE_RANGES.map((range) => (
|
||||
<button
|
||||
key={range.label}
|
||||
className={selectedRange === range.value ? 'active' : ''}
|
||||
onClick={() => handleRangeChange(range.value)}
|
||||
>
|
||||
{range.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="price-chart-container">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<LineChart data={chartData}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="#e2e8f0" />
|
||||
<XAxis
|
||||
dataKey="date"
|
||||
tickFormatter={formatDate}
|
||||
stroke="#94a3b8"
|
||||
fontSize={12}
|
||||
/>
|
||||
<YAxis
|
||||
tickFormatter={formatPrice}
|
||||
stroke="#94a3b8"
|
||||
fontSize={12}
|
||||
domain={['auto', 'auto']}
|
||||
/>
|
||||
<Tooltip
|
||||
formatter={(value: number) => [formatPrice(value), 'Price']}
|
||||
labelFormatter={(label) =>
|
||||
new Date(label).toLocaleDateString('en-US', {
|
||||
weekday: 'short',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
})
|
||||
}
|
||||
contentStyle={{
|
||||
background: 'white',
|
||||
border: '1px solid #e2e8f0',
|
||||
borderRadius: '0.5rem',
|
||||
boxShadow: '0 4px 6px -1px rgba(0, 0, 0, 0.1)',
|
||||
}}
|
||||
/>
|
||||
<ReferenceLine
|
||||
y={avgPrice}
|
||||
stroke="#6366f1"
|
||||
strokeDasharray="5 5"
|
||||
label=""
|
||||
/>
|
||||
<Line
|
||||
type="monotone"
|
||||
dataKey="price"
|
||||
stroke="#6366f1"
|
||||
strokeWidth={2}
|
||||
dot={{ fill: '#6366f1', strokeWidth: 0, r: 3 }}
|
||||
activeDot={{ r: 5, stroke: '#6366f1', strokeWidth: 2, fill: 'white' }}
|
||||
/>
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
|
||||
<div className="price-chart-stats">
|
||||
<div className="price-stat">
|
||||
<div className="price-stat-label">Lowest</div>
|
||||
<div className="price-stat-value min">{formatPrice(minPrice)}</div>
|
||||
</div>
|
||||
<div className="price-stat">
|
||||
<div className="price-stat-label">Average</div>
|
||||
<div className="price-stat-value avg">{formatPrice(avgPrice)}</div>
|
||||
</div>
|
||||
<div className="price-stat">
|
||||
<div className="price-stat-label">Highest</div>
|
||||
<div className="price-stat-value max">{formatPrice(maxPrice)}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
155
frontend/src/components/ProductCard.tsx
Normal file
155
frontend/src/components/ProductCard.tsx
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
import { Link } from 'react-router-dom';
|
||||
import { Product } from '../api/client';
|
||||
|
||||
interface ProductCardProps {
|
||||
product: Product;
|
||||
onDelete: (id: number) => void;
|
||||
}
|
||||
|
||||
export default function ProductCard({ product, onDelete }: ProductCardProps) {
|
||||
const formatPrice = (price: number | null, currency: string | null) => {
|
||||
if (price === null) return 'N/A';
|
||||
const currencySymbol =
|
||||
currency === 'EUR' ? '€' : currency === 'GBP' ? '£' : '$';
|
||||
return `${currencySymbol}${price.toFixed(2)}`;
|
||||
};
|
||||
|
||||
const formatDate = (dateStr: string | null) => {
|
||||
if (!dateStr) return 'Never';
|
||||
const date = new Date(dateStr);
|
||||
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
|
||||
};
|
||||
|
||||
const truncateUrl = (url: string, maxLength: number = 50) => {
|
||||
try {
|
||||
const parsed = new URL(url);
|
||||
const display = parsed.hostname + parsed.pathname;
|
||||
return display.length > maxLength
|
||||
? display.slice(0, maxLength) + '...'
|
||||
: display;
|
||||
} catch {
|
||||
return url.length > maxLength ? url.slice(0, maxLength) + '...' : url;
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="product-card">
|
||||
<style>{`
|
||||
.product-card {
|
||||
background: var(--surface);
|
||||
border-radius: 0.75rem;
|
||||
box-shadow: var(--shadow);
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
transition: box-shadow 0.2s;
|
||||
}
|
||||
|
||||
.product-card:hover {
|
||||
box-shadow: var(--shadow-lg);
|
||||
}
|
||||
|
||||
.product-image {
|
||||
width: 100%;
|
||||
height: 160px;
|
||||
object-fit: contain;
|
||||
background: #f8fafc;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.product-image-placeholder {
|
||||
width: 100%;
|
||||
height: 160px;
|
||||
background: linear-gradient(135deg, #e2e8f0 0%, #f1f5f9 100%);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: var(--text-muted);
|
||||
font-size: 2rem;
|
||||
}
|
||||
|
||||
.product-content {
|
||||
padding: 1rem;
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.product-name {
|
||||
font-weight: 600;
|
||||
color: var(--text);
|
||||
margin-bottom: 0.25rem;
|
||||
line-height: 1.3;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.product-url {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-muted);
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.product-price {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 700;
|
||||
color: var(--primary);
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.product-meta {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-muted);
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.product-actions {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
margin-top: auto;
|
||||
}
|
||||
|
||||
.product-actions .btn {
|
||||
flex: 1;
|
||||
padding: 0.5rem;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
`}</style>
|
||||
|
||||
{product.image_url ? (
|
||||
<img
|
||||
src={product.image_url}
|
||||
alt={product.name || 'Product'}
|
||||
className="product-image"
|
||||
/>
|
||||
) : (
|
||||
<div className="product-image-placeholder">📦</div>
|
||||
)}
|
||||
|
||||
<div className="product-content">
|
||||
<h3 className="product-name">{product.name || 'Unknown Product'}</h3>
|
||||
<p className="product-url">{truncateUrl(product.url)}</p>
|
||||
<div className="product-price">
|
||||
{formatPrice(product.current_price, product.currency)}
|
||||
</div>
|
||||
<p className="product-meta">
|
||||
Last checked: {formatDate(product.last_checked)}
|
||||
</p>
|
||||
|
||||
<div className="product-actions">
|
||||
<Link to={`/product/${product.id}`} className="btn btn-primary">
|
||||
View Details
|
||||
</Link>
|
||||
<button
|
||||
className="btn btn-danger"
|
||||
onClick={() => onDelete(product.id)}
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
130
frontend/src/components/ProductForm.tsx
Normal file
130
frontend/src/components/ProductForm.tsx
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
import { useState, FormEvent } from 'react';
|
||||
|
||||
interface ProductFormProps {
|
||||
onSubmit: (url: string, refreshInterval: number) => Promise<void>;
|
||||
}
|
||||
|
||||
const REFRESH_INTERVALS = [
|
||||
{ value: 900, label: '15 minutes' },
|
||||
{ value: 1800, label: '30 minutes' },
|
||||
{ value: 3600, label: '1 hour' },
|
||||
{ value: 7200, label: '2 hours' },
|
||||
{ value: 21600, label: '6 hours' },
|
||||
{ value: 43200, label: '12 hours' },
|
||||
{ value: 86400, label: '24 hours' },
|
||||
];
|
||||
|
||||
export default function ProductForm({ onSubmit }: ProductFormProps) {
|
||||
const [url, setUrl] = useState('');
|
||||
const [refreshInterval, setRefreshInterval] = useState(3600);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const handleSubmit = async (e: FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError('');
|
||||
|
||||
// Validate URL
|
||||
try {
|
||||
new URL(url);
|
||||
} catch {
|
||||
setError('Please enter a valid URL');
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
await onSubmit(url, refreshInterval);
|
||||
setUrl('');
|
||||
setRefreshInterval(3600);
|
||||
} catch (err) {
|
||||
if (err instanceof Error) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const axiosError = err as any;
|
||||
setError(axiosError.response?.data?.error || 'Failed to add product');
|
||||
} else {
|
||||
setError('Failed to add product');
|
||||
}
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="product-form">
|
||||
<style>{`
|
||||
.product-form {
|
||||
background: var(--surface);
|
||||
border-radius: 0.75rem;
|
||||
padding: 1.5rem;
|
||||
box-shadow: var(--shadow);
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.product-form h2 {
|
||||
font-size: 1.25rem;
|
||||
margin-bottom: 1rem;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.product-form-row {
|
||||
display: grid;
|
||||
gap: 1rem;
|
||||
grid-template-columns: 1fr auto auto;
|
||||
align-items: end;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.product-form-row {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
`}</style>
|
||||
|
||||
<h2>Track a New Product</h2>
|
||||
|
||||
{error && <div className="alert alert-error mb-3">{error}</div>}
|
||||
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="product-form-row">
|
||||
<div className="form-group" style={{ margin: 0 }}>
|
||||
<label htmlFor="product-url">Product URL</label>
|
||||
<input
|
||||
type="url"
|
||||
id="product-url"
|
||||
value={url}
|
||||
onChange={(e) => setUrl(e.target.value)}
|
||||
placeholder="https://www.example.com/product"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="form-group" style={{ margin: 0 }}>
|
||||
<label htmlFor="refresh-interval">Check Every</label>
|
||||
<select
|
||||
id="refresh-interval"
|
||||
value={refreshInterval}
|
||||
onChange={(e) => setRefreshInterval(parseInt(e.target.value, 10))}
|
||||
>
|
||||
{REFRESH_INTERVALS.map((interval) => (
|
||||
<option key={interval.value} value={interval.value}>
|
||||
{interval.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
className="btn btn-primary"
|
||||
disabled={isLoading}
|
||||
style={{ height: '46px' }}
|
||||
>
|
||||
{isLoading ? <span className="spinner" /> : 'Add Product'}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue