aboutsummaryrefslogtreecommitdiff
path: root/web/src/app/quotes/page.js
blob: 3ba4ea31a9e9f683f1415c2859a44ed604ccf9e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
'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 (
        <>
            <Navbar />
            <div className="flex flex-col gap-4 p-12">
                <h1 className="text-3xl">Submit New Quote</h1>
                <form className="flex flex-col gap-4 px-20" onSubmit={handleSubmit}>
                    <input
                        name="author"
                        type="text"
                        placeholder="Author"
                        value={formData.author}
                        onChange={handleInputChange}
                        required
                    />
                    <input
                        name="authorImage"
                        type="url"
                        placeholder="Author URL"
                        value={formData.authorImage}
                        onChange={handleInputChange}
                        required
                    />
                    <textarea
                        name="quote"
                        placeholder="Quote"
                        value={formData.quote}
                        onChange={handleInputChange}
                        required
                    />
                    <input
                        name="year"
                        type="number"
                        placeholder="Year"
                        value={formData.year}
                        onChange={handleInputChange}
                        required
                    />
                    <input
                        name="submitterID"
                        type="text"
                        placeholder="Submitter ID"
                        value={formData.submitterID}
                        onChange={handleInputChange}
                        required
                    />
                    <button
                        type="submit"
                        className="bg-blue-500 hover:bg-blue-700 text-white py-2 px-4 rounded"
                    >
                        Submit
                    </button>
                </form>

                {message && (
                    <div className={`p-4 rounded ${message.type === 'success' ? 'bg-green-800 text-green-200' : 'bg-red-800 text-red-200'}`}>
                        {message.text}
                    </div>
                )}

                <h1 className="text-3xl">Pending Quotes</h1>

                {loading && <p>Loading quotes...</p>}
                {error && <p className="text-red-500">Error: {error}</p>}

                {!loading && pendingQuotes.length === 0 && (
                    <p>No pending quotes found.</p>
                )}

                <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
                    {pendingQuotes.map(quote => (
                        <Card key={quote.id}>
                            <h2 className="text-xl font-medium">{quote.author}</h2>
                            <p>Author URL: {quote.authorImage}</p>
                            <p>{quote.quote}</p>
                            <small className="block mb-3">- {quote.year}</small>
                            <p className="text-sm text-gray-400 mb-2">Submitted by: {quote.submitterAuthor || quote.submitterID}</p>
                            <div className="flex gap-3 mt-2">
                                <button
                                    onClick={() => handleApproveQuote(quote.id)}
                                    className="bg-green-600 hover:bg-green-500 text-white py-1 px-3 rounded"
                                >
                                    Approve
                                </button>
                                <button
                                    onClick={() => openRejectModal(quote.id)}
                                    className="bg-red-600 hover:bg-red-500 text-white py-1 px-3 rounded"
                                >
                                    Reject
                                </button>
                            </div>
                        </Card>
                    ))}
                </div>

                {/* Rejection Modal */}
                {quoteToReject && (
                    <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center p-4">
                        <div className="bg-gray-800 p-6 rounded-lg w-full max-w-md">
                            <h2 className="text-xl mb-4">Reject Quote</h2>
                            <textarea
                                className="w-full p-2 bg-gray-700 rounded mb-4"
                                placeholder="Reason for rejection (optional)"
                                value={rejectReason}
                                onChange={(e) => setRejectReason(e.target.value)}
                                rows="4"
                            />
                            <div className="flex items-center mb-4">
                                <input
                                    type="checkbox"
                                    id="silentReject"
                                    checked={silentReject}
                                    onChange={(e) => setSilentReject(e.target.checked)}
                                    className="mr-2"
                                />
                                <label htmlFor="silentReject">Reject silently (don&#39;t notify user)</label>
                            </div>
                            <div className="flex justify-end gap-3">
                                <button
                                    onClick={closeRejectModal}
                                    className="bg-gray-600 hover:bg-gray-500 text-white py-2 px-4 rounded"
                                >
                                    Cancel
                                </button>
                                <button
                                    onClick={handleRejectQuote}
                                    className="bg-red-600 hover:bg-red-500 text-white py-2 px-4 rounded"
                                >
                                    Reject
                                </button>
                            </div>
                        </div>
                    </div>
                )}
            </div>
        </>
    );
}