import { GridPagination } from '@components/admin/grid/GridPagination.js'; import { DummyColumnHeader } from '@components/admin/grid/header/Dummy.js'; import { SortableHeader } from '@components/admin/grid/header/Sortable.js'; import { Status } from '@components/admin/Status.js'; import Area from '@components/common/Area.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 } from '@components/common/ui/Card.js'; import { CardAction, CardContent, CardHeader } from '@components/common/ui/Card.js'; import { Checkbox } from '@components/common/ui/Checkbox.js'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@components/common/ui/Table.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import axios from 'axios'; import { Copy, LayoutTemplate } from 'lucide-react'; import React, { useState } from 'react'; import { LandingPageName } from './rows/LandingPageName.js'; interface LandingPageItem { landingPageId: number; uuid: string; name: string; urlKey: string; status: boolean; url: string; editUrl: string; updateApi: string; deleteApi: string; duplicateApi: string; pageBuilderUrl: string; } interface Filter { key: string; operation: string; value: string; } function Actions({ landingPages = [], selectedIds = [] }: { landingPages: LandingPageItem[]; selectedIds: string[]; }) { const { openAlert, closeAlert } = useAlertContext(); const [, setIsLoading] = useState(false); const updateLandingPages = async (status: boolean) => { setIsLoading(true); const promises = landingPages .filter((landingPage) => selectedIds.includes(landingPage.uuid)) .map((landingPage) => axios.patch(landingPage.updateApi, { status })); await Promise.all(promises); setIsLoading(false); // Refresh the page window.location.reload(); }; const deleteLandingPages = async () => { setIsLoading(true); const promises = landingPages .filter((landingPage) => selectedIds.includes(landingPage.uuid)) .map((landingPage) => axios.delete(landingPage.deleteApi)); await Promise.all(promises); setIsLoading(false); // Refresh the page window.location.reload(); }; const actions = [ { name: _('Disable'), onAction: () => { openAlert({ heading: _('Disable ${count} landing pages', { count: String(selectedIds.length) }), content: _('Are you sure?'), primaryAction: { title: _('Cancel'), onAction: closeAlert, variant: 'secondary' }, secondaryAction: { title: _('Disable'), onAction: async () => { await updateLandingPages(false); }, variant: 'destructive' } }); } }, { name: _('Enable'), onAction: () => { openAlert({ heading: _('Enable ${count} landing pages', { count: String(selectedIds.length) }), content: _('Are you sure?'), primaryAction: { title: _('Cancel'), onAction: closeAlert, variant: 'secondary' }, secondaryAction: { title: _('Enable'), onAction: async () => { await updateLandingPages(true); }, variant: 'destructive' } }); } }, { name: _('Delete'), onAction: () => { openAlert({ heading: _('Delete ${count} landing pages', { count: String(selectedIds.length) }), content:
{_("Can't be undone")}
, primaryAction: { title: _('Cancel'), onAction: closeAlert, variant: 'secondary' }, secondaryAction: { title: _('Delete'), onAction: async () => { await deleteLandingPages(); }, variant: 'destructive' } }); } } ]; return ( {selectedIds.length === 0 && null} {selectedIds.length > 0 && ( {actions.map((action, i) => ( ))} )} ); } interface LandingPageGridProps { landingPages: { items: LandingPageItem[]; total: number; currentFilters?: Filter[]; }; } export default function LandingPageGrid({ landingPages: { items: landingPages, total, currentFilters = [] } }: LandingPageGridProps) { const page = currentFilters.find((filter) => filter.key === 'page') ? parseInt( currentFilters.find((filter) => filter.key === 'page')!.value, 10 ) : 1; const limit = currentFilters.find((filter) => filter.key === 'limit') ? parseInt( currentFilters.find((filter) => filter.key === 'limit')!.value, 10 ) : 20; const [selectedRows, setSelectedRows] = useState([]); return (
( f.key === 'name')?.value } onKeyPress={(e) => { // If the user press enter, we should submit the form if (e.key === 'Enter') { const url = new URL(document.location.href); const name = (e.target as HTMLInputElement)?.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; } }} /> ) }, sortOrder: 10 } ]} />
{ if (checked) { setSelectedRows(landingPages.map((l) => l.uuid)); } else { setSelectedRows([]); } }} />
( ) }, sortOrder: 10 }, { component: { default: () => ( ) }, sortOrder: 20 }, { component: { default: () => ( ) }, sortOrder: 30 }, { component: { default: () => }, sortOrder: 40 } ]} />
{landingPages.map((l) => (
{ if (checked) { setSelectedRows(selectedRows.concat([l.uuid])); } else { setSelectedRows( selectedRows.filter((row) => row !== l.uuid) ); } }} />
( ) }, sortOrder: 10 }, { component: { default: () => {l.urlKey} }, sortOrder: 20 }, { component: { default: () => }, sortOrder: 30 }, { component: { default: () => (
) }, sortOrder: 40 } ]} />
))}
{landingPages.length === 0 && (
{_('There is no landing page to display')}
)}
); } export const layout = { areaId: 'content', sortOrder: 20 }; export const query = ` query Query($filters: [FilterInput]) { landingPages(filters: $filters) { items { landingPageId uuid name urlKey status url editUrl updateApi deleteApi duplicateApi pageBuilderUrl } total currentFilters { key operation value } } } `; export const variables = ` { filters: getContextValue('filtersFromUrl') }`;