mirror of
https://github.com/clucraft/PriceGhost.git
synced 2026-05-09 15:52:41 +02:00
Add refresh controls and notification support
- Add refresh button to product list items with spinning animation - Add editable refresh interval dropdown on product detail page - Add user profile dropdown with settings link in navbar - Create Settings page for Telegram and Discord configuration - Add per-product notification options (price drop threshold, back in stock) - Integrate notifications into scheduler for automatic alerts - Add notification service supporting Telegram Bot API and Discord webhooks Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
8c5d20707d
commit
a6928a0c17
13 changed files with 1373 additions and 21 deletions
|
|
@ -5,6 +5,7 @@ import Login from './pages/Login';
|
|||
import Register from './pages/Register';
|
||||
import Dashboard from './pages/Dashboard';
|
||||
import ProductDetail from './pages/ProductDetail';
|
||||
import Settings from './pages/Settings';
|
||||
|
||||
function ThemeInitializer({ children }: { children: React.ReactNode }) {
|
||||
useEffect(() => {
|
||||
|
|
@ -101,6 +102,14 @@ function AppRoutes() {
|
|||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/settings"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<Settings />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route path="*" element={<Navigate to="/" replace />} />
|
||||
</Routes>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -57,6 +57,8 @@ export interface Product {
|
|||
refresh_interval: number;
|
||||
last_checked: string | null;
|
||||
stock_status: StockStatus;
|
||||
price_drop_threshold: number | null;
|
||||
notify_back_in_stock: boolean;
|
||||
created_at: string;
|
||||
current_price: number | null;
|
||||
currency: string | null;
|
||||
|
|
@ -89,8 +91,12 @@ export const productsApi = {
|
|||
create: (url: string, refreshInterval?: number) =>
|
||||
api.post<Product>('/products', { url, refresh_interval: refreshInterval }),
|
||||
|
||||
update: (id: number, data: { name?: string; refresh_interval?: number }) =>
|
||||
api.put<Product>(`/products/${id}`, data),
|
||||
update: (id: number, data: {
|
||||
name?: string;
|
||||
refresh_interval?: number;
|
||||
price_drop_threshold?: number | null;
|
||||
notify_back_in_stock?: boolean;
|
||||
}) => api.put<Product>(`/products/${id}`, data),
|
||||
|
||||
delete: (id: number) => api.delete(`/products/${id}`),
|
||||
};
|
||||
|
|
@ -109,4 +115,28 @@ export const pricesApi = {
|
|||
),
|
||||
};
|
||||
|
||||
// Settings API
|
||||
export interface NotificationSettings {
|
||||
telegram_configured: boolean;
|
||||
telegram_chat_id: string | null;
|
||||
discord_configured: boolean;
|
||||
}
|
||||
|
||||
export const settingsApi = {
|
||||
getNotifications: () =>
|
||||
api.get<NotificationSettings>('/settings/notifications'),
|
||||
|
||||
updateNotifications: (data: {
|
||||
telegram_bot_token?: string | null;
|
||||
telegram_chat_id?: string | null;
|
||||
discord_webhook_url?: string | null;
|
||||
}) => api.put<NotificationSettings & { message: string }>('/settings/notifications', data),
|
||||
|
||||
testTelegram: () =>
|
||||
api.post<{ message: string }>('/settings/notifications/test/telegram'),
|
||||
|
||||
testDiscord: () =>
|
||||
api.post<{ message: string }>('/settings/notifications/test/discord'),
|
||||
};
|
||||
|
||||
export default api;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { ReactNode, useState, useEffect } from 'react';
|
||||
import { ReactNode, useState, useEffect, useRef } from 'react';
|
||||
import { Link, useNavigate } from 'react-router-dom';
|
||||
import { useAuth } from '../hooks/useAuth';
|
||||
|
||||
|
|
@ -13,12 +13,25 @@ export default function Layout({ children }: LayoutProps) {
|
|||
const saved = localStorage.getItem('theme');
|
||||
return (saved as 'light' | 'dark') || 'light';
|
||||
});
|
||||
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
|
||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
localStorage.setItem('theme', theme);
|
||||
}, [theme]);
|
||||
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
|
||||
setIsDropdownOpen(false);
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside);
|
||||
}, []);
|
||||
|
||||
const toggleTheme = () => {
|
||||
setTheme((prev) => (prev === 'light' ? 'dark' : 'light'));
|
||||
};
|
||||
|
|
@ -110,8 +123,113 @@ export default function Layout({ children }: LayoutProps) {
|
|||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.user-dropdown {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.user-dropdown-trigger {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.5rem 0.75rem;
|
||||
background: var(--background);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 0.5rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.user-dropdown-trigger:hover {
|
||||
border-color: var(--primary);
|
||||
}
|
||||
|
||||
.user-dropdown-avatar {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
border-radius: 50%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.user-dropdown-email {
|
||||
color: var(--text);
|
||||
font-size: 0.875rem;
|
||||
max-width: 150px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.user-dropdown-arrow {
|
||||
color: var(--text-muted);
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
.user-dropdown-trigger.open .user-dropdown-arrow {
|
||||
transform: rotate(180deg);
|
||||
}
|
||||
|
||||
.user-dropdown-menu {
|
||||
position: absolute;
|
||||
top: calc(100% + 0.5rem);
|
||||
right: 0;
|
||||
min-width: 200px;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 0.5rem;
|
||||
box-shadow: var(--shadow-lg);
|
||||
overflow: hidden;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.user-dropdown-menu-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
padding: 0.75rem 1rem;
|
||||
color: var(--text);
|
||||
text-decoration: none;
|
||||
transition: background 0.2s;
|
||||
border: none;
|
||||
background: none;
|
||||
width: 100%;
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.user-dropdown-menu-item:hover {
|
||||
background: var(--background);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.user-dropdown-menu-item svg {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.user-dropdown-divider {
|
||||
height: 1px;
|
||||
background: var(--border);
|
||||
margin: 0.25rem 0;
|
||||
}
|
||||
|
||||
.user-dropdown-menu-item.danger {
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
.user-dropdown-menu-item.danger svg {
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.navbar-email {
|
||||
.navbar-email, .user-dropdown-email {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
|
@ -133,12 +251,60 @@ export default function Layout({ children }: LayoutProps) {
|
|||
{theme === 'light' ? '🌙' : '☀️'}
|
||||
</button>
|
||||
{user && (
|
||||
<>
|
||||
<span className="navbar-email">{user.email}</span>
|
||||
<button className="btn btn-secondary" onClick={handleLogout}>
|
||||
Logout
|
||||
<div className="user-dropdown" ref={dropdownRef}>
|
||||
<button
|
||||
className={`user-dropdown-trigger ${isDropdownOpen ? 'open' : ''}`}
|
||||
onClick={() => setIsDropdownOpen(!isDropdownOpen)}
|
||||
>
|
||||
<span className="user-dropdown-avatar">
|
||||
{user.email.charAt(0).toUpperCase()}
|
||||
</span>
|
||||
<span className="user-dropdown-email">{user.email}</span>
|
||||
<svg
|
||||
className="user-dropdown-arrow"
|
||||
width="12"
|
||||
height="12"
|
||||
viewBox="0 0 12 12"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<path d="M3 4.5L6 7.5L9 4.5" />
|
||||
</svg>
|
||||
</button>
|
||||
</>
|
||||
{isDropdownOpen && (
|
||||
<div className="user-dropdown-menu">
|
||||
<Link
|
||||
to="/settings"
|
||||
className="user-dropdown-menu-item"
|
||||
onClick={() => setIsDropdownOpen(false)}
|
||||
>
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
||||
<circle cx="12" cy="12" r="3" />
|
||||
<path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z" />
|
||||
</svg>
|
||||
Settings
|
||||
</Link>
|
||||
<div className="user-dropdown-divider" />
|
||||
<button
|
||||
className="user-dropdown-menu-item danger"
|
||||
onClick={() => {
|
||||
setIsDropdownOpen(false);
|
||||
handleLogout();
|
||||
}}
|
||||
>
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
||||
<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4" />
|
||||
<polyline points="16 17 21 12 16 7" />
|
||||
<line x1="21" y1="12" x2="9" y2="12" />
|
||||
</svg>
|
||||
Logout
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { Product } from '../api/client';
|
||||
import Sparkline from './Sparkline';
|
||||
|
|
@ -5,9 +6,20 @@ import Sparkline from './Sparkline';
|
|||
interface ProductCardProps {
|
||||
product: Product;
|
||||
onDelete: (id: number) => void;
|
||||
onRefresh: (id: number) => Promise<void>;
|
||||
}
|
||||
|
||||
export default function ProductCard({ product, onDelete }: ProductCardProps) {
|
||||
export default function ProductCard({ product, onDelete, onRefresh }: ProductCardProps) {
|
||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||
|
||||
const handleRefresh = async () => {
|
||||
setIsRefreshing(true);
|
||||
try {
|
||||
await onRefresh(product.id);
|
||||
} finally {
|
||||
setIsRefreshing(false);
|
||||
}
|
||||
};
|
||||
const formatPrice = (price: number | string | null, currency: string | null) => {
|
||||
if (price === null || price === undefined) return 'N/A';
|
||||
const numPrice = typeof price === 'string' ? parseFloat(price) : price;
|
||||
|
|
@ -185,6 +197,28 @@ export default function ProductCard({ product, onDelete }: ProductCardProps) {
|
|||
font-size: 0.8125rem;
|
||||
}
|
||||
|
||||
.product-actions .btn-icon {
|
||||
padding: 0.5rem;
|
||||
min-width: unset;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.product-actions .btn-icon svg {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
}
|
||||
|
||||
.product-actions .btn-icon.refreshing svg {
|
||||
animation: spin 1s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
from { transform: rotate(0deg); }
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.product-list-item {
|
||||
flex-wrap: wrap;
|
||||
|
|
@ -269,15 +303,30 @@ export default function ProductCard({ product, onDelete }: ProductCardProps) {
|
|||
</div>
|
||||
|
||||
<div className="product-actions">
|
||||
<button
|
||||
className={`btn btn-secondary btn-icon ${isRefreshing ? 'refreshing' : ''}`}
|
||||
onClick={handleRefresh}
|
||||
disabled={isRefreshing}
|
||||
title="Refresh price"
|
||||
>
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
||||
<path d="M21 12a9 9 0 0 0-9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" />
|
||||
<path d="M3 3v5h5" />
|
||||
<path d="M3 12a9 9 0 0 0 9 9 9.75 9.75 0 0 0 6.74-2.74L21 16" />
|
||||
<path d="M16 21h5v-5" />
|
||||
</svg>
|
||||
</button>
|
||||
<Link to={`/product/${product.id}`} className="btn btn-primary">
|
||||
View
|
||||
</Link>
|
||||
<button
|
||||
className="btn btn-danger"
|
||||
className="btn btn-danger btn-icon"
|
||||
onClick={() => onDelete(product.id)}
|
||||
title="Delete"
|
||||
>
|
||||
✕
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
||||
<path d="M18 6L6 18M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { useState, useEffect, useMemo } from 'react';
|
|||
import Layout from '../components/Layout';
|
||||
import ProductCard from '../components/ProductCard';
|
||||
import ProductForm from '../components/ProductForm';
|
||||
import { productsApi, Product } from '../api/client';
|
||||
import { productsApi, pricesApi, Product } from '../api/client';
|
||||
|
||||
export default function Dashboard() {
|
||||
const [products, setProducts] = useState<Product[]>([]);
|
||||
|
|
@ -43,6 +43,16 @@ export default function Dashboard() {
|
|||
}
|
||||
};
|
||||
|
||||
const handleRefreshProduct = async (id: number) => {
|
||||
try {
|
||||
await pricesApi.refresh(id);
|
||||
// Refresh the products list to get updated data
|
||||
await fetchProducts();
|
||||
} catch {
|
||||
alert('Failed to refresh price');
|
||||
}
|
||||
};
|
||||
|
||||
const filteredProducts = useMemo(() => {
|
||||
if (!searchQuery.trim()) return products;
|
||||
const query = searchQuery.toLowerCase();
|
||||
|
|
@ -256,6 +266,7 @@ export default function Dashboard() {
|
|||
key={product.id}
|
||||
product={product}
|
||||
onDelete={handleDeleteProduct}
|
||||
onRefresh={handleRefreshProduct}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -5,8 +5,10 @@ import PriceChart from '../components/PriceChart';
|
|||
import {
|
||||
productsApi,
|
||||
pricesApi,
|
||||
settingsApi,
|
||||
ProductWithStats,
|
||||
PriceHistory,
|
||||
NotificationSettings,
|
||||
} from '../api/client';
|
||||
|
||||
export default function ProductDetail() {
|
||||
|
|
@ -17,7 +19,22 @@ export default function ProductDetail() {
|
|||
const [prices, setPrices] = useState<PriceHistory[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
const [isSavingNotifications, setIsSavingNotifications] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [notificationSettings, setNotificationSettings] = useState<NotificationSettings | null>(null);
|
||||
const [priceDropThreshold, setPriceDropThreshold] = useState<string>('');
|
||||
const [notifyBackInStock, setNotifyBackInStock] = useState(false);
|
||||
|
||||
const REFRESH_INTERVALS = [
|
||||
{ value: 1800, label: '30 minutes' },
|
||||
{ value: 3600, label: '1 hour' },
|
||||
{ value: 7200, label: '2 hours' },
|
||||
{ value: 14400, label: '4 hours' },
|
||||
{ value: 21600, label: '6 hours' },
|
||||
{ value: 43200, label: '12 hours' },
|
||||
{ value: 86400, label: '24 hours' },
|
||||
];
|
||||
|
||||
const productId = parseInt(id || '0', 10);
|
||||
|
||||
|
|
@ -29,6 +46,11 @@ export default function ProductDetail() {
|
|||
]);
|
||||
setProduct(productRes.data);
|
||||
setPrices(pricesRes.data.prices);
|
||||
// Initialize notification form fields from product data
|
||||
if (productRes.data.price_drop_threshold !== null && productRes.data.price_drop_threshold !== undefined) {
|
||||
setPriceDropThreshold(productRes.data.price_drop_threshold.toString());
|
||||
}
|
||||
setNotifyBackInStock(productRes.data.notify_back_in_stock || false);
|
||||
} catch {
|
||||
setError('Failed to load product details');
|
||||
} finally {
|
||||
|
|
@ -36,9 +58,19 @@ export default function ProductDetail() {
|
|||
}
|
||||
};
|
||||
|
||||
const fetchNotificationSettings = async () => {
|
||||
try {
|
||||
const response = await settingsApi.getNotifications();
|
||||
setNotificationSettings(response.data);
|
||||
} catch {
|
||||
// Silently fail - notifications just won't be shown
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (productId) {
|
||||
fetchData(30);
|
||||
fetchNotificationSettings();
|
||||
}
|
||||
}, [productId]);
|
||||
|
||||
|
|
@ -71,6 +103,40 @@ export default function ProductDetail() {
|
|||
fetchData(days);
|
||||
};
|
||||
|
||||
const handleRefreshIntervalChange = async (newInterval: number) => {
|
||||
if (!product) return;
|
||||
setIsSaving(true);
|
||||
try {
|
||||
await productsApi.update(productId, { refresh_interval: newInterval });
|
||||
setProduct({ ...product, refresh_interval: newInterval });
|
||||
} catch {
|
||||
alert('Failed to update refresh interval');
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSaveNotifications = async () => {
|
||||
if (!product) return;
|
||||
setIsSavingNotifications(true);
|
||||
try {
|
||||
const threshold = priceDropThreshold ? parseFloat(priceDropThreshold) : null;
|
||||
await productsApi.update(productId, {
|
||||
price_drop_threshold: threshold,
|
||||
notify_back_in_stock: notifyBackInStock,
|
||||
});
|
||||
setProduct({
|
||||
...product,
|
||||
price_drop_threshold: threshold,
|
||||
notify_back_in_stock: notifyBackInStock,
|
||||
});
|
||||
} catch {
|
||||
alert('Failed to save notification settings');
|
||||
} finally {
|
||||
setIsSavingNotifications(false);
|
||||
}
|
||||
};
|
||||
|
||||
const formatPrice = (price: number | string | null, currency: string | null) => {
|
||||
if (price === null || price === undefined) return 'N/A';
|
||||
const numPrice = typeof price === 'string' ? parseFloat(price) : price;
|
||||
|
|
@ -285,6 +351,33 @@ export default function ProductDetail() {
|
|||
gap: 0.75rem;
|
||||
margin-top: 1.5rem;
|
||||
}
|
||||
|
||||
.product-detail-meta-select {
|
||||
padding: 0.375rem 0.5rem;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 0.375rem;
|
||||
background: var(--surface);
|
||||
color: var(--text);
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: border-color 0.2s;
|
||||
}
|
||||
|
||||
.product-detail-meta-select:hover {
|
||||
border-color: var(--primary);
|
||||
}
|
||||
|
||||
.product-detail-meta-select:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary);
|
||||
box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1);
|
||||
}
|
||||
|
||||
.product-detail-meta-select:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
`}</style>
|
||||
|
||||
<div className="product-detail-header">
|
||||
|
|
@ -355,11 +448,18 @@ export default function ProductDetail() {
|
|||
</div>
|
||||
<div className="product-detail-meta-item">
|
||||
<span className="product-detail-meta-label">Check Interval</span>
|
||||
<span className="product-detail-meta-value">
|
||||
{product.refresh_interval < 3600
|
||||
? `${product.refresh_interval / 60} minutes`
|
||||
: `${product.refresh_interval / 3600} hour(s)`}
|
||||
</span>
|
||||
<select
|
||||
className="product-detail-meta-select"
|
||||
value={product.refresh_interval}
|
||||
onChange={(e) => handleRefreshIntervalChange(parseInt(e.target.value, 10))}
|
||||
disabled={isSaving}
|
||||
>
|
||||
{REFRESH_INTERVALS.map((interval) => (
|
||||
<option key={interval.value} value={interval.value}>
|
||||
{interval.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="product-detail-meta-item">
|
||||
<span className="product-detail-meta-label">Tracking Since</span>
|
||||
|
|
@ -400,6 +500,212 @@ export default function ProductDetail() {
|
|||
currency={product.currency || 'USD'}
|
||||
onRangeChange={handleRangeChange}
|
||||
/>
|
||||
|
||||
{notificationSettings && (notificationSettings.telegram_configured || notificationSettings.discord_configured) && (
|
||||
<>
|
||||
<style>{`
|
||||
.notification-settings-card {
|
||||
background: var(--surface);
|
||||
border-radius: 0.75rem;
|
||||
box-shadow: var(--shadow);
|
||||
padding: 1.5rem;
|
||||
margin-top: 2rem;
|
||||
}
|
||||
|
||||
.notification-settings-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.notification-settings-icon {
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.notification-settings-title {
|
||||
font-size: 1.125rem;
|
||||
font-weight: 600;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.notification-settings-channels {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.notification-channel-badge {
|
||||
padding: 0.25rem 0.5rem;
|
||||
border-radius: 0.25rem;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
background: #f0fdf4;
|
||||
color: #16a34a;
|
||||
}
|
||||
|
||||
[data-theme="dark"] .notification-channel-badge {
|
||||
background: rgba(22, 163, 74, 0.2);
|
||||
color: #4ade80;
|
||||
}
|
||||
|
||||
.notification-settings-description {
|
||||
color: var(--text-muted);
|
||||
font-size: 0.875rem;
|
||||
margin-bottom: 1.5rem;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.notification-form-row {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 1.5rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.notification-form-row {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
.notification-form-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.375rem;
|
||||
}
|
||||
|
||||
.notification-form-group label {
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.notification-form-group input[type="number"] {
|
||||
padding: 0.625rem 0.75rem;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 0.375rem;
|
||||
background: var(--background);
|
||||
color: var(--text);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.notification-form-group input[type="number"]:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary);
|
||||
box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1);
|
||||
}
|
||||
|
||||
.notification-form-group .hint {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.notification-checkbox-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
padding: 0.75rem;
|
||||
background: var(--background);
|
||||
border-radius: 0.375rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.notification-checkbox-group:hover {
|
||||
background: var(--border);
|
||||
}
|
||||
|
||||
.notification-checkbox-group input[type="checkbox"] {
|
||||
width: 1.125rem;
|
||||
height: 1.125rem;
|
||||
accent-color: var(--primary);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.notification-checkbox-label {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.125rem;
|
||||
}
|
||||
|
||||
.notification-checkbox-label span:first-child {
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.notification-checkbox-label span:last-child {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.notification-form-actions {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
`}</style>
|
||||
|
||||
<div className="notification-settings-card">
|
||||
<div className="notification-settings-header">
|
||||
<span className="notification-settings-icon">🔔</span>
|
||||
<h2 className="notification-settings-title">Notification Settings</h2>
|
||||
<div className="notification-settings-channels">
|
||||
{notificationSettings.telegram_configured && (
|
||||
<span className="notification-channel-badge">Telegram</span>
|
||||
)}
|
||||
{notificationSettings.discord_configured && (
|
||||
<span className="notification-channel-badge">Discord</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<p className="notification-settings-description">
|
||||
Get notified when this product's price drops or comes back in stock.
|
||||
Notifications will be sent to your configured channels.
|
||||
</p>
|
||||
|
||||
<div className="notification-form-row">
|
||||
<div className="notification-form-group">
|
||||
<label>Price Drop Threshold</label>
|
||||
<input
|
||||
type="number"
|
||||
min="0"
|
||||
step="0.01"
|
||||
value={priceDropThreshold}
|
||||
onChange={(e) => setPriceDropThreshold(e.target.value)}
|
||||
placeholder="Enter amount (e.g., 5.00)"
|
||||
/>
|
||||
<span className="hint">
|
||||
Notify when price drops by at least this amount ({product.currency || 'USD'})
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="notification-form-group">
|
||||
<label>Back in Stock Alert</label>
|
||||
<label className="notification-checkbox-group">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={notifyBackInStock}
|
||||
onChange={(e) => setNotifyBackInStock(e.target.checked)}
|
||||
/>
|
||||
<div className="notification-checkbox-label">
|
||||
<span>Enable back-in-stock notifications</span>
|
||||
<span>Get notified when this item becomes available</span>
|
||||
</div>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="notification-form-actions">
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
onClick={handleSaveNotifications}
|
||||
disabled={isSavingNotifications}
|
||||
>
|
||||
{isSavingNotifications ? 'Saving...' : 'Save Notification Settings'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
365
frontend/src/pages/Settings.tsx
Normal file
365
frontend/src/pages/Settings.tsx
Normal file
|
|
@ -0,0 +1,365 @@
|
|||
import { useState, useEffect } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import Layout from '../components/Layout';
|
||||
import { settingsApi, NotificationSettings } from '../api/client';
|
||||
|
||||
export default function Settings() {
|
||||
const [settings, setSettings] = useState<NotificationSettings | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
const [isTesting, setIsTesting] = useState<'telegram' | 'discord' | null>(null);
|
||||
const [error, setError] = useState('');
|
||||
const [success, setSuccess] = useState('');
|
||||
|
||||
// Form state
|
||||
const [telegramBotToken, setTelegramBotToken] = useState('');
|
||||
const [telegramChatId, setTelegramChatId] = useState('');
|
||||
const [discordWebhookUrl, setDiscordWebhookUrl] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
fetchSettings();
|
||||
}, []);
|
||||
|
||||
const fetchSettings = async () => {
|
||||
try {
|
||||
const response = await settingsApi.getNotifications();
|
||||
setSettings(response.data);
|
||||
if (response.data.telegram_chat_id) {
|
||||
setTelegramChatId(response.data.telegram_chat_id);
|
||||
}
|
||||
} catch {
|
||||
setError('Failed to load settings');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSaveTelegram = async () => {
|
||||
setIsSaving(true);
|
||||
setError('');
|
||||
setSuccess('');
|
||||
try {
|
||||
const response = await settingsApi.updateNotifications({
|
||||
telegram_bot_token: telegramBotToken || null,
|
||||
telegram_chat_id: telegramChatId || null,
|
||||
});
|
||||
setSettings(response.data);
|
||||
setTelegramBotToken('');
|
||||
setSuccess('Telegram settings saved successfully');
|
||||
} catch {
|
||||
setError('Failed to save Telegram settings');
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSaveDiscord = async () => {
|
||||
setIsSaving(true);
|
||||
setError('');
|
||||
setSuccess('');
|
||||
try {
|
||||
const response = await settingsApi.updateNotifications({
|
||||
discord_webhook_url: discordWebhookUrl || null,
|
||||
});
|
||||
setSettings(response.data);
|
||||
setDiscordWebhookUrl('');
|
||||
setSuccess('Discord settings saved successfully');
|
||||
} catch {
|
||||
setError('Failed to save Discord settings');
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleTestTelegram = async () => {
|
||||
setIsTesting('telegram');
|
||||
setError('');
|
||||
setSuccess('');
|
||||
try {
|
||||
await settingsApi.testTelegram();
|
||||
setSuccess('Test notification sent to Telegram!');
|
||||
} catch {
|
||||
setError('Failed to send test notification. Check your settings.');
|
||||
} finally {
|
||||
setIsTesting(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleTestDiscord = async () => {
|
||||
setIsTesting('discord');
|
||||
setError('');
|
||||
setSuccess('');
|
||||
try {
|
||||
await settingsApi.testDiscord();
|
||||
setSuccess('Test notification sent to Discord!');
|
||||
} catch {
|
||||
setError('Failed to send test notification. Check your webhook URL.');
|
||||
} finally {
|
||||
setIsTesting(null);
|
||||
}
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<Layout>
|
||||
<div style={{ display: 'flex', justifyContent: 'center', padding: '4rem' }}>
|
||||
<span className="spinner" style={{ width: '3rem', height: '3rem' }} />
|
||||
</div>
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Layout>
|
||||
<style>{`
|
||||
.settings-header {
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.settings-back {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
color: var(--text-muted);
|
||||
margin-bottom: 1rem;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.settings-back:hover {
|
||||
color: var(--primary);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.settings-title {
|
||||
font-size: 1.75rem;
|
||||
font-weight: 700;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.settings-subtitle {
|
||||
color: var(--text-muted);
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
.settings-section {
|
||||
background: var(--surface);
|
||||
border-radius: 0.75rem;
|
||||
box-shadow: var(--shadow);
|
||||
padding: 1.5rem;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.settings-section-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.settings-section-icon {
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.settings-section-title {
|
||||
font-size: 1.125rem;
|
||||
font-weight: 600;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.settings-section-status {
|
||||
margin-left: auto;
|
||||
padding: 0.25rem 0.75rem;
|
||||
border-radius: 1rem;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.settings-section-status.configured {
|
||||
background: #f0fdf4;
|
||||
color: #16a34a;
|
||||
}
|
||||
|
||||
[data-theme="dark"] .settings-section-status.configured {
|
||||
background: rgba(22, 163, 74, 0.2);
|
||||
color: #4ade80;
|
||||
}
|
||||
|
||||
.settings-section-status.not-configured {
|
||||
background: #fef3c7;
|
||||
color: #d97706;
|
||||
}
|
||||
|
||||
[data-theme="dark"] .settings-section-status.not-configured {
|
||||
background: rgba(217, 119, 6, 0.2);
|
||||
color: #fbbf24;
|
||||
}
|
||||
|
||||
.settings-section-description {
|
||||
color: var(--text-muted);
|
||||
font-size: 0.875rem;
|
||||
margin-bottom: 1.5rem;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.settings-form-group {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.settings-form-group label {
|
||||
display: block;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
color: var(--text);
|
||||
margin-bottom: 0.375rem;
|
||||
}
|
||||
|
||||
.settings-form-group input {
|
||||
width: 100%;
|
||||
padding: 0.625rem 0.75rem;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 0.375rem;
|
||||
background: var(--background);
|
||||
color: var(--text);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.settings-form-group input:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary);
|
||||
box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1);
|
||||
}
|
||||
|
||||
.settings-form-group .hint {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-muted);
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
.settings-form-actions {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.settings-help-link {
|
||||
color: var(--primary);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.settings-help-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
`}</style>
|
||||
|
||||
<div className="settings-header">
|
||||
<Link to="/" className="settings-back">
|
||||
← Back to Dashboard
|
||||
</Link>
|
||||
<h1 className="settings-title">Settings</h1>
|
||||
<p className="settings-subtitle">Configure notifications and preferences</p>
|
||||
</div>
|
||||
|
||||
{error && <div className="alert alert-error" style={{ marginBottom: '1rem' }}>{error}</div>}
|
||||
{success && <div className="alert alert-success" style={{ marginBottom: '1rem', background: '#f0fdf4', color: '#16a34a', padding: '0.75rem 1rem', borderRadius: '0.5rem' }}>{success}</div>}
|
||||
|
||||
<div className="settings-section">
|
||||
<div className="settings-section-header">
|
||||
<span className="settings-section-icon">📱</span>
|
||||
<h2 className="settings-section-title">Telegram Notifications</h2>
|
||||
<span className={`settings-section-status ${settings?.telegram_configured ? 'configured' : 'not-configured'}`}>
|
||||
{settings?.telegram_configured ? 'Configured' : 'Not configured'}
|
||||
</span>
|
||||
</div>
|
||||
<p className="settings-section-description">
|
||||
Receive price drop and back-in-stock alerts via Telegram. You'll need to create a Telegram bot
|
||||
and get your chat ID.
|
||||
</p>
|
||||
|
||||
<div className="settings-form-group">
|
||||
<label>Bot Token</label>
|
||||
<input
|
||||
type="password"
|
||||
value={telegramBotToken}
|
||||
onChange={(e) => setTelegramBotToken(e.target.value)}
|
||||
placeholder={settings?.telegram_configured ? '••••••••••••••••' : 'Enter your bot token'}
|
||||
/>
|
||||
<p className="hint">Create a bot via @BotFather on Telegram to get a token</p>
|
||||
</div>
|
||||
|
||||
<div className="settings-form-group">
|
||||
<label>Chat ID</label>
|
||||
<input
|
||||
type="text"
|
||||
value={telegramChatId}
|
||||
onChange={(e) => setTelegramChatId(e.target.value)}
|
||||
placeholder="Enter your chat ID"
|
||||
/>
|
||||
<p className="hint">Send /start to @userinfobot to get your chat ID</p>
|
||||
</div>
|
||||
|
||||
<div className="settings-form-actions">
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
onClick={handleSaveTelegram}
|
||||
disabled={isSaving}
|
||||
>
|
||||
{isSaving ? 'Saving...' : 'Save Telegram Settings'}
|
||||
</button>
|
||||
{settings?.telegram_configured && (
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
onClick={handleTestTelegram}
|
||||
disabled={isTesting === 'telegram'}
|
||||
>
|
||||
{isTesting === 'telegram' ? 'Sending...' : 'Send Test'}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="settings-section">
|
||||
<div className="settings-section-header">
|
||||
<span className="settings-section-icon">💬</span>
|
||||
<h2 className="settings-section-title">Discord Notifications</h2>
|
||||
<span className={`settings-section-status ${settings?.discord_configured ? 'configured' : 'not-configured'}`}>
|
||||
{settings?.discord_configured ? 'Configured' : 'Not configured'}
|
||||
</span>
|
||||
</div>
|
||||
<p className="settings-section-description">
|
||||
Receive price drop and back-in-stock alerts in a Discord channel. Create a webhook in your
|
||||
Discord server settings.
|
||||
</p>
|
||||
|
||||
<div className="settings-form-group">
|
||||
<label>Webhook URL</label>
|
||||
<input
|
||||
type="password"
|
||||
value={discordWebhookUrl}
|
||||
onChange={(e) => setDiscordWebhookUrl(e.target.value)}
|
||||
placeholder={settings?.discord_configured ? '••••••••••••••••' : 'https://discord.com/api/webhooks/...'}
|
||||
/>
|
||||
<p className="hint">Server Settings → Integrations → Webhooks → New Webhook</p>
|
||||
</div>
|
||||
|
||||
<div className="settings-form-actions">
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
onClick={handleSaveDiscord}
|
||||
disabled={isSaving}
|
||||
>
|
||||
{isSaving ? 'Saving...' : 'Save Discord Settings'}
|
||||
</button>
|
||||
{settings?.discord_configured && (
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
onClick={handleTestDiscord}
|
||||
disabled={isTesting === 'discord'}
|
||||
>
|
||||
{isTesting === 'discord' ? 'Sending...' : 'Send Test'}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue