import React, { useState, useEffect } from 'react'; import { AlertCircle, Link as LinkIcon, Loader2 } from 'lucide-react'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Card } from '@/components/ui/card'; const RefsTable = () => { const [refs, setRefs] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [searchTerm, setSearchTerm] = useState(''); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [selectedRefId, setSelectedRefId] = useState(null); const [showUpdateForm, setShowUpdateForm] = useState(false); const [isCreating, setIsCreating] = useState(false); const [formData, setFormData] = useState({ refName: '', refUrl: '' }); const fetchRefs = async () => { try { setLoading(true); const token = localStorage.getItem('token'); const response = await fetch('http://localhost:8000/refs/', { headers: { 'Authorization': `Bearer ${token}` } }); if (!response.ok) throw new Error('Failed to fetch refs'); const data = await response.json(); setRefs(data); setError(null); } catch (err) { setError(err.message); } finally { setLoading(false); } }; useEffect(() => { fetchRefs(); }, []); const handleSubmit = async (e) => { e.preventDefault(); const token = localStorage.getItem('token'); try { const response = await fetch('http://localhost:8000/refs/', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify(formData) }); if (!response.ok) throw new Error('Failed to create ref'); await fetchRefs(); setFormData({ refName: '', refUrl: '' }); setIsCreating(false); } catch (err) { setError(err.message); } }; const handleDelete = async (refId) => { const token = localStorage.getItem('token'); try { const response = await fetch(`http://localhost:8000/refs/${refId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }); if (!response.ok) throw new Error('Failed to delete ref'); await fetchRefs(); setShowDeleteConfirm(false); } catch (err) { setError(err.message); } }; const handleUpdate = async (refId) => { const token = localStorage.getItem('token'); try { const response = await fetch(`http://localhost:8000/refs/${refId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify(formData) }); if (!response.ok) throw new Error('Failed to update ref'); await fetchRefs(); setShowUpdateForm(false); setFormData({ refName: '', refUrl: '' }); } catch (err) { setError(err.message); } }; const filteredRefs = refs.filter(ref => ref.refName.toLowerCase().includes(searchTerm.toLowerCase()) || ref.refUrl.toLowerCase().includes(searchTerm.toLowerCase()) ); if (loading) { return (
); } return (
{/* Header and Search */}
setSearchTerm(e.target.value)} />
{/* Error Alert */} {error && ( {error} )} {/* Table */}
{filteredRefs.map((ref) => ( ))}
ID Referans Adı URL İşlemler
{ref.refId} {ref.refName}
{/* No Results Message */} {filteredRefs.length === 0 && (
Aradığınız kriterlere uygun referans bulunamadı.
)}
{/* Create/Update Modal */} {(isCreating || showUpdateForm) && (

{isCreating ? 'Yeni Referans Ekle' : 'Referans Düzenle'}

{ e.preventDefault(); if (isCreating) { handleSubmit(e); } else { handleUpdate(selectedRefId); } }}>
setFormData({ ...formData, refName: e.target.value })} className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-emerald-500 focus:ring-emerald-500" required />
setFormData({ ...formData, refUrl: e.target.value })} className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-emerald-500 focus:ring-emerald-500" required />
)} {/* Delete Confirmation Modal */} {showDeleteConfirm && (

Referansı Sil

Bu referansı silmek istediğinizden emin misiniz?

)}
); }; export default RefsTable;