|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useMemo, useState } from 'react'; |
| 4 | +import { Table, Button, Popconfirm, Switch, Tag, message, Select, Input, Space } from 'antd'; |
| 5 | + |
| 6 | +interface ContentRow { |
| 7 | + _id: string; |
| 8 | + title: string; |
| 9 | + body?: string; |
| 10 | + userId?: string; |
| 11 | + status: 'draft' | 'pending' | 'approved' | 'rejected'; |
| 12 | + featured?: boolean; |
| 13 | + createdAt?: string; |
| 14 | +} |
| 15 | + |
| 16 | +export default function AdminContentPage() { |
| 17 | + const [rows, setRows] = useState<ContentRow[]>([]); |
| 18 | + const [loading, setLoading] = useState(true); |
| 19 | + const [status, setStatus] = useState<'all' | 'pending' | 'approved' | 'rejected'>('pending'); |
| 20 | + const [q, setQ] = useState(''); |
| 21 | + const [onlyFeatured, setOnlyFeatured] = useState(false); |
| 22 | + const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]); |
| 23 | + |
| 24 | + const fetchQueue = async () => { |
| 25 | + setLoading(true); |
| 26 | + const params = new URLSearchParams(); |
| 27 | + if (status) params.set('status', status); |
| 28 | + if (q) params.set('q', q); |
| 29 | + if (onlyFeatured) params.set('featured', 'true'); |
| 30 | + const res = await fetch(`/api/admin/content?${params.toString()}`); |
| 31 | + if (!res.ok) { |
| 32 | + message.error('Failed to load content'); |
| 33 | + setRows([]); |
| 34 | + setLoading(false); |
| 35 | + return; |
| 36 | + } |
| 37 | + const data = await res.json(); |
| 38 | + setRows(data); |
| 39 | + setLoading(false); |
| 40 | + }; |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + fetchQueue(); |
| 44 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 45 | + }, [status, q, onlyFeatured]); |
| 46 | + |
| 47 | + const approve = async (row: ContentRow) => { |
| 48 | + const res = await fetch(`/api/admin/content?id=${row._id}`, { |
| 49 | + method: 'PUT', |
| 50 | + headers: { 'Content-Type': 'application/json' }, |
| 51 | + body: JSON.stringify({ action: 'approve' }), |
| 52 | + }); |
| 53 | + if (res.ok) { |
| 54 | + message.success('Approved'); |
| 55 | + fetchQueue(); |
| 56 | + } else { |
| 57 | + message.error('Failed to approve'); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + const reject = async (row: ContentRow) => { |
| 62 | + const res = await fetch(`/api/admin/content?id=${row._id}`, { |
| 63 | + method: 'PUT', |
| 64 | + headers: { 'Content-Type': 'application/json' }, |
| 65 | + body: JSON.stringify({ action: 'reject' }), |
| 66 | + }); |
| 67 | + if (res.ok) { |
| 68 | + message.success('Rejected'); |
| 69 | + fetchQueue(); |
| 70 | + } else { |
| 71 | + message.error('Failed to reject'); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + const bulkAction = async (action: 'approve' | 'reject') => { |
| 76 | + if (selectedRowKeys.length === 0) return; |
| 77 | + const res = await fetch('/api/admin/content', { |
| 78 | + method: 'PUT', |
| 79 | + headers: { 'Content-Type': 'application/json' }, |
| 80 | + body: JSON.stringify({ action, ids: selectedRowKeys }), |
| 81 | + }); |
| 82 | + if (res.ok) { |
| 83 | + message.success(`Bulk ${action} complete`); |
| 84 | + setSelectedRowKeys([]); |
| 85 | + fetchQueue(); |
| 86 | + } else { |
| 87 | + message.error(`Bulk ${action} failed`); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + const toggleFeatured = async (row: ContentRow, featured: boolean) => { |
| 92 | + const res = await fetch(`/api/admin/content/${row._id}/featured`, { |
| 93 | + method: 'PUT', |
| 94 | + headers: { 'Content-Type': 'application/json' }, |
| 95 | + body: JSON.stringify({ featured }), |
| 96 | + }); |
| 97 | + if (res.ok) { |
| 98 | + message.success('Updated featured'); |
| 99 | + fetchQueue(); |
| 100 | + } else { |
| 101 | + message.error('Failed to update featured'); |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + const columns = [ |
| 106 | + { title: 'Title', dataIndex: 'title' }, |
| 107 | + { title: 'Status', dataIndex: 'status', render: (s: string) => <Tag color={s === 'pending' ? 'orange' : s === 'approved' ? 'green' : 'red'}>{s}</Tag> }, |
| 108 | + { title: 'Featured', dataIndex: 'featured', render: (_: any, row: ContentRow) => ( |
| 109 | + <Switch checked={!!row.featured} onChange={(checked) => toggleFeatured(row, checked)} /> |
| 110 | + ) }, |
| 111 | + { title: 'Actions', render: (_: any, row: ContentRow) => ( |
| 112 | + <> |
| 113 | + <Button type="primary" onClick={() => approve(row)} style={{ marginRight: 8 }}>Approve</Button> |
| 114 | + <Popconfirm title="Reject this item?" onConfirm={() => reject(row)}> |
| 115 | + <Button danger>Reject</Button> |
| 116 | + </Popconfirm> |
| 117 | + </> |
| 118 | + ) }, |
| 119 | + ]; |
| 120 | + |
| 121 | + const rowSelection = { |
| 122 | + selectedRowKeys, |
| 123 | + onChange: (keys: React.Key[]) => setSelectedRowKeys(keys), |
| 124 | + }; |
| 125 | + |
| 126 | + return ( |
| 127 | + <div> |
| 128 | + <h2>Content Moderation</h2> |
| 129 | + <Space style={{ marginBottom: 12 }} wrap> |
| 130 | + <Select |
| 131 | + value={status} |
| 132 | + onChange={setStatus as any} |
| 133 | + options={[ |
| 134 | + { value: 'pending', label: 'Pending' }, |
| 135 | + { value: 'approved', label: 'Approved' }, |
| 136 | + { value: 'rejected', label: 'Rejected' }, |
| 137 | + { value: 'all', label: 'All' }, |
| 138 | + ]} |
| 139 | + style={{ width: 140 }} |
| 140 | + /> |
| 141 | + <Input.Search placeholder="Search title" allowClear value={q} onChange={(e) => setQ(e.target.value)} style={{ width: 220 }} /> |
| 142 | + <Button type={onlyFeatured ? 'primary' : 'default'} onClick={() => setOnlyFeatured((v) => !v)}> |
| 143 | + {onlyFeatured ? 'Featured: On' : 'Featured: Off'} |
| 144 | + </Button> |
| 145 | + <Button onClick={() => bulkAction('approve')} disabled={selectedRowKeys.length === 0} type="primary"> |
| 146 | + Bulk Approve |
| 147 | + </Button> |
| 148 | + <Button onClick={() => bulkAction('reject')} disabled={selectedRowKeys.length === 0} danger> |
| 149 | + Bulk Reject |
| 150 | + </Button> |
| 151 | + </Space> |
| 152 | + <Table |
| 153 | + loading={loading} |
| 154 | + dataSource={rows} |
| 155 | + columns={columns as any} |
| 156 | + rowKey="_id" |
| 157 | + rowSelection={rowSelection} |
| 158 | + /> |
| 159 | + </div> |
| 160 | + ); |
| 161 | +} |
0 commit comments