mirror of
https://github.com/inventree/InvenTree.git
synced 2025-10-15 05:32:21 +00:00
add duplicate part action to parts table (#10481)
Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
@@ -4,12 +4,17 @@ import { IconShoppingCart } from '@tabler/icons-react';
|
|||||||
import { type ReactNode, useCallback, useMemo, useState } from 'react';
|
import { type ReactNode, useCallback, useMemo, useState } from 'react';
|
||||||
|
|
||||||
import { AddItemButton } from '@lib/components/AddItemButton';
|
import { AddItemButton } from '@lib/components/AddItemButton';
|
||||||
import { type RowAction, RowEditAction } from '@lib/components/RowActions';
|
import {
|
||||||
|
type RowAction,
|
||||||
|
RowDuplicateAction,
|
||||||
|
RowEditAction
|
||||||
|
} from '@lib/components/RowActions';
|
||||||
import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
|
import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
|
||||||
import { ModelType } from '@lib/enums/ModelType';
|
import { ModelType } from '@lib/enums/ModelType';
|
||||||
import { UserRoles } from '@lib/enums/Roles';
|
import { UserRoles } from '@lib/enums/Roles';
|
||||||
import { apiUrl } from '@lib/functions/Api';
|
import { apiUrl } from '@lib/functions/Api';
|
||||||
import type { TableFilter } from '@lib/types/Filters';
|
import type { TableFilter } from '@lib/types/Filters';
|
||||||
|
import type { ApiFormFieldSet } from '@lib/types/Forms';
|
||||||
import type { TableColumn } from '@lib/types/Tables';
|
import type { TableColumn } from '@lib/types/Tables';
|
||||||
import type { InvenTreeTableProps } from '@lib/types/Tables';
|
import type { InvenTreeTableProps } from '@lib/types/Tables';
|
||||||
import { ActionDropdown } from '../../components/items/ActionDropdown';
|
import { ActionDropdown } from '../../components/items/ActionDropdown';
|
||||||
@@ -23,6 +28,7 @@ import {
|
|||||||
useEditApiFormModal
|
useEditApiFormModal
|
||||||
} from '../../hooks/UseForm';
|
} from '../../hooks/UseForm';
|
||||||
import { useTable } from '../../hooks/UseTable';
|
import { useTable } from '../../hooks/UseTable';
|
||||||
|
import { useGlobalSettingsState } from '../../states/SettingsStates';
|
||||||
import { useUserState } from '../../states/UserState';
|
import { useUserState } from '../../states/UserState';
|
||||||
import {
|
import {
|
||||||
CategoryColumn,
|
CategoryColumn,
|
||||||
@@ -333,6 +339,7 @@ export function PartListTable({
|
|||||||
|
|
||||||
const table = useTable('part-list');
|
const table = useTable('part-list');
|
||||||
const user = useUserState();
|
const user = useUserState();
|
||||||
|
const globalSettings = useGlobalSettingsState();
|
||||||
|
|
||||||
const initialPartData = useMemo(() => {
|
const initialPartData = useMemo(() => {
|
||||||
return defaultPartData ?? props.params ?? {};
|
return defaultPartData ?? props.params ?? {};
|
||||||
@@ -347,16 +354,64 @@ export function PartListTable({
|
|||||||
modelType: ModelType.part
|
modelType: ModelType.part
|
||||||
});
|
});
|
||||||
|
|
||||||
const [selectedPart, setSelectedPart] = useState<number>(-1);
|
const [selectedPart, setSelectedPart] = useState<any>({});
|
||||||
|
|
||||||
const editPart = useEditApiFormModal({
|
const editPart = useEditApiFormModal({
|
||||||
url: ApiEndpoints.part_list,
|
url: ApiEndpoints.part_list,
|
||||||
pk: selectedPart,
|
pk: selectedPart.pk,
|
||||||
title: t`Edit Part`,
|
title: t`Edit Part`,
|
||||||
fields: usePartFields({ create: false }),
|
fields: usePartFields({ create: false }),
|
||||||
onFormSuccess: table.refreshTable
|
onFormSuccess: table.refreshTable
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const createPartFields = usePartFields({ create: true });
|
||||||
|
|
||||||
|
const duplicatePartFields: ApiFormFieldSet = useMemo(() => {
|
||||||
|
return {
|
||||||
|
...createPartFields,
|
||||||
|
duplicate: {
|
||||||
|
children: {
|
||||||
|
part: {
|
||||||
|
value: selectedPart.pk,
|
||||||
|
hidden: true
|
||||||
|
},
|
||||||
|
copy_image: {
|
||||||
|
value: true
|
||||||
|
},
|
||||||
|
copy_bom: {
|
||||||
|
value:
|
||||||
|
selectedPart.assembly && globalSettings.isSet('PART_COPY_BOM'),
|
||||||
|
hidden: !selectedPart.assembly
|
||||||
|
},
|
||||||
|
copy_notes: {
|
||||||
|
value: true
|
||||||
|
},
|
||||||
|
copy_parameters: {
|
||||||
|
value: globalSettings.isSet('PART_COPY_PARAMETERS')
|
||||||
|
},
|
||||||
|
copy_tests: {
|
||||||
|
value: selectedPart.testable,
|
||||||
|
hidden: !selectedPart.testable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [createPartFields, globalSettings, selectedPart]);
|
||||||
|
|
||||||
|
const duplicatePart = useCreateApiFormModal({
|
||||||
|
url: ApiEndpoints.part_list,
|
||||||
|
title: t`Add Part`,
|
||||||
|
fields: duplicatePartFields,
|
||||||
|
initialData: {
|
||||||
|
...selectedPart,
|
||||||
|
active: true,
|
||||||
|
locked: false
|
||||||
|
},
|
||||||
|
follow: false,
|
||||||
|
modelType: ModelType.part,
|
||||||
|
onFormSuccess: table.refreshTable
|
||||||
|
});
|
||||||
|
|
||||||
const setCategory = useBulkEditApiFormModal({
|
const setCategory = useBulkEditApiFormModal({
|
||||||
url: ApiEndpoints.part_list,
|
url: ApiEndpoints.part_list,
|
||||||
items: table.selectedIds,
|
items: table.selectedIds,
|
||||||
@@ -372,18 +427,26 @@ export function PartListTable({
|
|||||||
const rowActions = useCallback(
|
const rowActions = useCallback(
|
||||||
(record: any): RowAction[] => {
|
(record: any): RowAction[] => {
|
||||||
const can_edit = user.hasChangePermission(ModelType.part);
|
const can_edit = user.hasChangePermission(ModelType.part);
|
||||||
|
const can_add = user.hasAddPermission(ModelType.part);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
RowEditAction({
|
RowEditAction({
|
||||||
hidden: !can_edit,
|
hidden: !can_edit,
|
||||||
onClick: () => {
|
onClick: () => {
|
||||||
setSelectedPart(record.pk);
|
setSelectedPart(record);
|
||||||
editPart.open();
|
editPart.open();
|
||||||
}
|
}
|
||||||
|
}),
|
||||||
|
RowDuplicateAction({
|
||||||
|
hidden: !can_add,
|
||||||
|
onClick: () => {
|
||||||
|
setSelectedPart(record);
|
||||||
|
duplicatePart.open();
|
||||||
|
}
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
[user, editPart]
|
[user, editPart, duplicatePart]
|
||||||
);
|
);
|
||||||
|
|
||||||
const tableActions = useMemo(() => {
|
const tableActions = useMemo(() => {
|
||||||
@@ -426,6 +489,7 @@ export function PartListTable({
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{newPart.modal}
|
{newPart.modal}
|
||||||
|
{duplicatePart.modal}
|
||||||
{editPart.modal}
|
{editPart.modal}
|
||||||
{setCategory.modal}
|
{setCategory.modal}
|
||||||
{orderPartsWizard.wizard}
|
{orderPartsWizard.wizard}
|
||||||
|
Reference in New Issue
Block a user