|
@@ -1,4 +1,5 @@
|
|
|
import { useMutation } from '@tanstack/react-query';
|
|
import { useMutation } from '@tanstack/react-query';
|
|
|
|
|
+import { ConfigurableOperationInput } from '@vendure/common/lib/generated-types';
|
|
|
import { CopyIcon } from 'lucide-react';
|
|
import { CopyIcon } from 'lucide-react';
|
|
|
import { useState } from 'react';
|
|
import { useState } from 'react';
|
|
|
import { toast } from 'sonner';
|
|
import { toast } from 'sonner';
|
|
@@ -8,13 +9,13 @@ import { usePaginatedList } from '@/vdb/components/shared/paginated-list-data-ta
|
|
|
import { api } from '@/vdb/graphql/api.js';
|
|
import { api } from '@/vdb/graphql/api.js';
|
|
|
import { duplicateEntityDocument } from '@/vdb/graphql/common-operations.js';
|
|
import { duplicateEntityDocument } from '@/vdb/graphql/common-operations.js';
|
|
|
import { Trans, useLingui } from '@/vdb/lib/trans.js';
|
|
import { Trans, useLingui } from '@/vdb/lib/trans.js';
|
|
|
|
|
+import { DuplicateEntityDialog } from './duplicate-entity-dialog.js';
|
|
|
|
|
|
|
|
interface DuplicateBulkActionProps {
|
|
interface DuplicateBulkActionProps {
|
|
|
entityType: 'Product' | 'Collection' | 'Facet' | 'Promotion';
|
|
entityType: 'Product' | 'Collection' | 'Facet' | 'Promotion';
|
|
|
duplicatorCode: string;
|
|
duplicatorCode: string;
|
|
|
- duplicatorArguments?: Array<{ name: string; value: string }>;
|
|
|
|
|
requiredPermissions: string[];
|
|
requiredPermissions: string[];
|
|
|
- entityName: string; // For display purposes in error messages
|
|
|
|
|
|
|
+ entityName: string;
|
|
|
onSuccess?: () => void;
|
|
onSuccess?: () => void;
|
|
|
selection: any[];
|
|
selection: any[];
|
|
|
table: any;
|
|
table: any;
|
|
@@ -23,23 +24,28 @@ interface DuplicateBulkActionProps {
|
|
|
export function DuplicateBulkAction({
|
|
export function DuplicateBulkAction({
|
|
|
entityType,
|
|
entityType,
|
|
|
duplicatorCode,
|
|
duplicatorCode,
|
|
|
- duplicatorArguments = [],
|
|
|
|
|
requiredPermissions,
|
|
requiredPermissions,
|
|
|
entityName,
|
|
entityName,
|
|
|
onSuccess,
|
|
onSuccess,
|
|
|
selection,
|
|
selection,
|
|
|
table,
|
|
table,
|
|
|
-}: DuplicateBulkActionProps) {
|
|
|
|
|
|
|
+}: Readonly<DuplicateBulkActionProps>) {
|
|
|
const { refetchPaginatedList } = usePaginatedList();
|
|
const { refetchPaginatedList } = usePaginatedList();
|
|
|
const { i18n } = useLingui();
|
|
const { i18n } = useLingui();
|
|
|
const [isDuplicating, setIsDuplicating] = useState(false);
|
|
const [isDuplicating, setIsDuplicating] = useState(false);
|
|
|
const [progress, setProgress] = useState({ completed: 0, total: 0 });
|
|
const [progress, setProgress] = useState({ completed: 0, total: 0 });
|
|
|
|
|
+ const [dialogOpen, setDialogOpen] = useState(false);
|
|
|
|
|
|
|
|
const { mutateAsync } = useMutation({
|
|
const { mutateAsync } = useMutation({
|
|
|
mutationFn: api.mutate(duplicateEntityDocument),
|
|
mutationFn: api.mutate(duplicateEntityDocument),
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- const handleDuplicate = async () => {
|
|
|
|
|
|
|
+ const handleStartDuplication = () => {
|
|
|
|
|
+ if (isDuplicating) return;
|
|
|
|
|
+ setDialogOpen(true);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const handleConfirmDuplication = async (duplicatorInput: ConfigurableOperationInput) => {
|
|
|
if (isDuplicating) return;
|
|
if (isDuplicating) return;
|
|
|
|
|
|
|
|
setIsDuplicating(true);
|
|
setIsDuplicating(true);
|
|
@@ -61,10 +67,7 @@ export function DuplicateBulkAction({
|
|
|
input: {
|
|
input: {
|
|
|
entityName: entityType,
|
|
entityName: entityType,
|
|
|
entityId: entity.id,
|
|
entityId: entity.id,
|
|
|
- duplicatorInput: {
|
|
|
|
|
- code: duplicatorCode,
|
|
|
|
|
- arguments: duplicatorArguments,
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ duplicatorInput,
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -116,19 +119,30 @@ export function DuplicateBulkAction({
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
- <DataTableBulkActionItem
|
|
|
|
|
- requiresPermission={requiredPermissions}
|
|
|
|
|
- onClick={handleDuplicate}
|
|
|
|
|
- label={
|
|
|
|
|
- isDuplicating ? (
|
|
|
|
|
- <Trans>
|
|
|
|
|
- Duplicating... ({progress.completed}/{progress.total})
|
|
|
|
|
- </Trans>
|
|
|
|
|
- ) : (
|
|
|
|
|
- <Trans>Duplicate</Trans>
|
|
|
|
|
- )
|
|
|
|
|
- }
|
|
|
|
|
- icon={CopyIcon}
|
|
|
|
|
- />
|
|
|
|
|
|
|
+ <>
|
|
|
|
|
+ <DataTableBulkActionItem
|
|
|
|
|
+ requiresPermission={requiredPermissions}
|
|
|
|
|
+ onClick={handleStartDuplication}
|
|
|
|
|
+ label={
|
|
|
|
|
+ isDuplicating ? (
|
|
|
|
|
+ <Trans>
|
|
|
|
|
+ Duplicating... ({progress.completed}/{progress.total})
|
|
|
|
|
+ </Trans>
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ <Trans>Duplicate</Trans>
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ icon={CopyIcon}
|
|
|
|
|
+ />
|
|
|
|
|
+ <DuplicateEntityDialog
|
|
|
|
|
+ open={dialogOpen}
|
|
|
|
|
+ onOpenChange={setDialogOpen}
|
|
|
|
|
+ entityType={entityType}
|
|
|
|
|
+ entityName={entityName}
|
|
|
|
|
+ entities={selection}
|
|
|
|
|
+ duplicatorCode={duplicatorCode}
|
|
|
|
|
+ onConfirm={handleConfirmDuplication}
|
|
|
|
|
+ />
|
|
|
|
|
+ </>
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|