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