import { GridPagination } from '@components/admin/grid/GridPagination.js'; import { SortableHeader } from '@components/admin/grid/header/Sortable.js'; import { Status } from '@components/admin/Status.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'; import './General.scss'; interface PostRow { uuid: string; name: string; status: number; category?: { name: string } | null; editUrl: string; updateApi: string; deleteApi: string; } interface Filter { key: string; operation: string; value: string; } function Actions({ posts = [], selectedIds = [] }: { posts: PostRow[]; selectedIds: string[]; }) { const { openAlert, closeAlert } = useAlertContext(); const updatePosts = async (status: number) => { await Promise.all( posts .filter((post) => selectedIds.includes(post.uuid)) .map((post) => axios.patch(post.updateApi, { status })) ); window.location.reload(); }; const deletePosts = async () => { await Promise.all( posts .filter((post) => selectedIds.includes(post.uuid)) .map((post) => axios.delete(post.deleteApi)) ); window.location.reload(); }; const confirm = ( heading: string, label: string, onAction: () => Promise ) => openAlert({ heading, content: _('Are you sure?'), primaryAction: { title: _('Cancel'), onAction: closeAlert, variant: 'secondary' }, secondaryAction: { title: label, onAction, variant: 'destructive' } }); const actions = [ { name: _('Publish'), onAction: () => confirm( _('Publish ${count} posts', { count: `${selectedIds.length}` }), _('Publish'), () => updatePosts(1) ) }, { name: _('Unpublish'), onAction: () => confirm( _('Unpublish ${count} posts', { count: `${selectedIds.length}` }), _('Unpublish'), () => updatePosts(0) ) }, { name: _('Delete'), onAction: () => confirm( _('Delete ${count} posts', { count: `${selectedIds.length}` }), _('Delete'), deletePosts ) } ]; if (selectedIds.length === 0) { return null; } return ( {actions.map((action, i) => ( ))} ); } export default function BlogPostGrid({ blogPosts: { items: posts, total, currentFilters = [] }, categories }: { blogPosts: { items: PostRow[]; total: number; currentFilters: Filter[] }; categories?: { items?: Array<{ blogCategoryId: number; name: string }> }; }) { const currentCategory = currentFilters.find((f) => f.key === 'category')?.value ?? ''; 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 [selectedRows, setSelectedRows] = useState([]); return (
f.key === 'name')?.value} onKeyPress={(e: any) => { if (e.key === 'Enter') { const url = new URL(document.location.href); const name = e.target?.value; if (name) { url.searchParams.set('name[operation]', 'like'); url.searchParams.set('name[value]', name); } else { url.searchParams.delete('name[operation]'); url.searchParams.delete('name[value]'); } window.location.href = url.href; } }} />
setSelectedRows(checked ? posts.map((p) => p.uuid) : []) } /> {_('Category')} {posts.map((p, i) => ( setSelectedRows( checked ? selectedRows.concat([p.uuid]) : selectedRows.filter((row) => row !== p.uuid) ) } /> {p.name} {p.category?.name || '—'} ))}
{posts.length === 0 && (
{_('There is no post to display')}
)}
); } export const layout = { areaId: 'content', sortOrder: 20 }; export const query = ` query Query($filters: [FilterInput]) { blogPosts (filters: $filters) { items { blogPostId uuid name status category { name } editUrl updateApi deleteApi } total currentFilters { key operation value } } categories: blogCategories( filters: [{ key: "limit", operation: eq, value: "1000" }] ) { items { blogCategoryId name } } } `; export const variables = ` { filters: getContextValue('filtersFromUrl') }`;