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'; import axios from 'axios'; const BASE_URL = "http://localhost:8000"; 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 () => { const token = localStorage.getItem('token'); try { setLoading(true); const response = await axios.get(`${BASE_URL}/refs/`, { headers: { 'Authorization': `Bearer ${token}` } }); const sortedRefs = response.data.sort((a, b) => a.refName.localeCompare(b.refName) ); setRefs(sortedRefs); setError(null); } catch (error) { console.error("Error fetching refs:", error); setError("Failed to load refs. Please try again later."); } finally { setLoading(false); } }; useEffect(() => { fetchRefs(); }, []); const handleSubmit = async (e) => { e.preventDefault(); const token = localStorage.getItem('token'); try { await axios.post(`${BASE_URL}/refs/`, { refName: formData.refName, refUrl: formData.refUrl }, { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` } }); await fetchRefs(); setFormData({ refName: '', refUrl: '' }); setIsCreating(false); } catch (error) { console.error("Error creating ref:", error); setError("Failed to create ref. Please try again."); } }; const handleDelete = async (refId) => { const token = localStorage.getItem('token'); try { await axios.delete(`${BASE_URL}/refs/${refId}`, { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` } }); await fetchRefs(); setShowDeleteConfirm(false); setSelectedRefId(null); } catch (error) { console.error("Error deleting ref:", error); setError("Failed to delete ref. Please try again."); } }; const handleUpdate = async (refId) => { const token = localStorage.getItem('token'); try { await axios.patch(`${BASE_URL}/refs/${refId}`, { refName: formData.refName, refUrl: formData.refUrl }, { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` } }); await fetchRefs(); setShowUpdateForm(false); setFormData({ refName: '', refUrl: '' }); setSelectedRefId(null); } catch (error) { console.error("Error updating ref:", error); setError("Failed to update ref. Please try again."); } }; 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;