'use client';
import { useState, useEffect } from "react";
import Card from "@/app/components/Card";
import Navbar from "@/app/components/Navbar";
import { fetchWithAuth } from "@/utils/api";
export default function Quotes() {
const [pendingQuotes, setPendingQuotes] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [message, setMessage] = useState(null);
const [rejectReason, setRejectReason] = useState('');
const [quoteToReject, setQuoteToReject] = useState(null);
const [formData, setFormData] = useState({
author: '',
authorImage: '',
quote: '',
year: '',
submitterID: ''
});
const [silentReject, setSilentReject] = useState(false);
useEffect(() => {
fetchPendingQuotes();
}, []);
const fetchPendingQuotes = async () => {
try {
setLoading(true);
const response = await fetchWithAuth('/api/quotes/pending');
if (!response.ok) {
throw new Error('Failed to fetch pending quotes');
}
const data = await response.json();
setPendingQuotes(data);
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
const handleInputChange = (e) => {
const { name, value } = e.target;
setFormData({
...formData,
[name]: value
});
};
const handleSubmit = async (e) => {
e.preventDefault();
setMessage(null);
try {
const response = await fetchWithAuth('/api/quotes/add', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formData)
});
if (!response.ok) {
throw new Error('Failed to submit quote');
}
setMessage({
type: 'success',
text: 'Quote submitted successfully'
});
// Reset form
setFormData({
author: '',
authorImage: '',
quote: '',
year: '',
submitterID: ''
});
} catch (err) {
setMessage({
type: 'error',
text: err.message
});
}
};
const handleApproveQuote = async (id) => {
try {
const response = await fetchWithAuth('/api/quotes/approve', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id })
});
if (!response.ok) {
throw new Error('Failed to approve quote');
}
setMessage({
type: 'success',
text: 'Quote approved successfully'
});
// Refresh quotes
fetchPendingQuotes();
} catch (err) {
setMessage({
type: 'error',
text: err.message
});
}
};
const openRejectModal = (id) => {
setQuoteToReject(id);
setRejectReason('');
};
const closeRejectModal = () => {
setQuoteToReject(null);
setRejectReason('');
};
const handleRejectQuote = async () => {
try {
const response = await fetchWithAuth('/api/quotes/reject', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: quoteToReject,
reason: rejectReason,
silent: silentReject
})
});
if (!response.ok) {
throw new Error('Failed to reject quote');
}
setMessage({
type: 'success',
text: 'Quote rejected successfully'
});
// Close modal
closeRejectModal();
// Refresh quotes
fetchPendingQuotes();
} catch (err) {
setMessage({
type: 'error',
text: err.message
});
}
};
return (
<>
Loading quotes...
} {error &&Error: {error}
} {!loading && pendingQuotes.length === 0 && (No pending quotes found.
)}Author URL: {quote.authorImage}
{quote.quote}
- {quote.year}Submitted by: {quote.submitterAuthor || quote.submitterID}