import { GridPagination } from '@components/admin/grid/GridPagination.js'; import { Form } from '@components/common/form/Form.js'; import { InputField } from '@components/common/form/InputField.js'; import { useAlertContext } from '@components/common/modal/Alert.js'; import { Button } from '@components/common/ui/Button.js'; import { ButtonGroup } from '@components/common/ui/ButtonGroup.js'; import { Card, CardAction, CardContent, CardHeader } from '@components/common/ui/Card.js'; import { Checkbox } from '@components/common/ui/Checkbox.js'; import { Table, TableBody, TableCell, TableHeader, TableRow } from '@components/common/ui/Table.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import axios from 'axios'; import React, { useState } from 'react'; interface CommentRow { uuid: string; name: string; comment: string; status: string; postName?: string; createdAt?: string; moderateApi: string; deleteApi: string; } interface Filter { key: string; operation: string; value: string; } const STATUS_STYLES: Record = { approved: 'bg-green-100 text-green-800', pending: 'bg-yellow-100 text-yellow-800', spam: 'bg-red-100 text-red-800' }; function StatusBadge({ status }: { status: string }) { return ( {status} ); } async function moderate(api: string, status: string) { await axios.patch(api, { status }); window.location.reload(); } async function remove(api: string) { await axios.delete(api); window.location.reload(); } function BulkActions({ comments, selectedIds }: { comments: CommentRow[]; selectedIds: string[]; }) { const { openAlert, closeAlert } = useAlertContext(); if (selectedIds.length === 0) { return null; } const selected = comments.filter((c) => selectedIds.includes(c.uuid)); const bulk = async (fn: (c: CommentRow) => Promise) => { await Promise.all(selected.map(fn)); window.location.reload(); }; return ( ); } export default function BlogCommentGrid({ blogComments: { items: comments, total, currentFilters = [] } }: { blogComments: { items: CommentRow[]; total: number; currentFilters: Filter[]; }; }) { const page = currentFilters.find((f) => f.key === 'page') ? parseInt(currentFilters.find((f) => f.key === 'page')!.value, 10) : 1; const limit = currentFilters.find((f) => f.key === 'limit') ? parseInt(currentFilters.find((f) => f.key === 'limit')!.value, 10) : 20; const statusFilter = currentFilters.find((f) => f.key === 'status')?.value; const [selectedRows, setSelectedRows] = useState([]); const setStatus = (status: string | null) => { const url = new URL(document.location.href); if (status) { url.searchParams.set('status[operation]', 'eq'); url.searchParams.set('status[value]', status); } else { url.searchParams.delete('status[operation]'); url.searchParams.delete('status[value]'); } window.location.href = url.href; }; return (
{[ { label: _('All'), value: null }, { label: _('Pending'), value: 'pending' }, { label: _('Approved'), value: 'approved' }, { label: _('Spam'), value: 'spam' } ].map((tab) => ( ))}
f.key === 'keyword')?.value } onKeyPress={(e: any) => { if (e.key === 'Enter') { const url = new URL(document.location.href); const kw = e.target?.value; if (kw) { url.searchParams.set('keyword[operation]', 'like'); url.searchParams.set('keyword[value]', kw); } else { url.searchParams.delete('keyword[operation]'); url.searchParams.delete('keyword[value]'); } window.location.href = url.href; } }} />
setSelectedRows(checked ? comments.map((c) => c.uuid) : []) } /> {_('Comment')} {_('Status')} {_('Actions')} {comments.map((c, i) => ( setSelectedRows( checked ? selectedRows.concat([c.uuid]) : selectedRows.filter((row) => row !== c.uuid) ) } />
{c.comment}
{c.name} {c.postName ? ` · ${c.postName}` : ''} {c.createdAt ? ` · ${new Date(c.createdAt).toLocaleString()}` : ''}
{c.status !== 'approved' && ( )} {c.status !== 'spam' && ( )}
))}
{comments.length === 0 && (
{_('There is no comment to display')}
)}
); } export const layout = { areaId: 'content', sortOrder: 20 }; export const query = ` query Query($filters: [FilterInput]) { blogComments(filters: $filters) { items { uuid name comment status postName createdAt moderateApi deleteApi } total currentFilters { key operation value } } } `; export const variables = ` { filters: getContextValue('filtersFromUrl') }`;