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 (
ID | Referans Adı | URL | İşlemler |
---|---|---|---|
{ref.refId} | {ref.refName} |
|
Bu referansı silmek istediğinizden emin misiniz?