Skip to content

Commit 207141c

Browse files
Expose data import wizard to plugin context
1 parent 97aec82 commit 207141c

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed

src/frontend/lib/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export { UserRoles, UserPermissions } from './enums/Roles';
1515
export type {
1616
InvenTreePluginContext,
1717
InvenTreeFormsContext,
18+
InvenTreeWizardsContext,
1819
PluginVersion,
1920
StockAdjustmentFormsContext
2021
} from './types/Plugins';
@@ -35,6 +36,12 @@ export type {
3536
UseModalReturn
3637
} from './types/Modals';
3738

39+
export type {
40+
DataImportWizardProps,
41+
UseWizardProps,
42+
UseWizardReturn
43+
} from './types/Wizards';
44+
3845
// Common utility functions
3946
export { apiUrl } from './functions/Api';
4047
export {

src/frontend/lib/types/Plugins.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { UseModalReturn } from './Modals';
1414
import type { RenderInstanceProps } from './Rendering';
1515
import type { SettingsStateProps } from './Settings';
1616
import type { UserStateProps } from './User';
17+
import type { DataImportWizardProps, UseWizardReturn } from './Wizards';
1718

1819
export interface PluginProps {
1920
name: string;
@@ -48,6 +49,10 @@ export type InvenTreeFormsContext = {
4849
stockActions: StockAdjustmentFormsContext;
4950
};
5051

52+
export type InvenTreeWizardsContext = {
53+
importData: (props: DataImportWizardProps) => UseWizardReturn;
54+
};
55+
5156
/**
5257
* A set of properties which are passed to a plugin,
5358
* for rendering an element in the user interface.
@@ -60,6 +65,8 @@ export type InvenTreeFormsContext = {
6065
* @param navigate - The navigation function (see react-router-dom)
6166
* @param theme - The current Mantine theme
6267
* @param colorScheme - The current Mantine color scheme (e.g. 'light' / 'dark')
68+
* @param forms - A set of functions for rendering common InvenTree forms (see ../lib/types/Plugins.tsx)
69+
* @param wizards - A set of functions for rendering common InvenTree wizards (see ../lib/types/Plugins.tsx)
6370
* @param host - The current host URL
6471
* @param i18n - The i18n instance for translations (from @lingui/core)
6572
* @param locale - The current locale string (e.g. 'en' / 'de')
@@ -87,6 +94,7 @@ export type InvenTreePluginContext = {
8794
navigate: NavigateFunction;
8895
theme: MantineTheme;
8996
forms: InvenTreeFormsContext;
97+
wizards: InvenTreeWizardsContext;
9098
colorScheme: MantineColorScheme;
9199
model?: ModelType | string;
92100
id?: string | number | null;

src/frontend/lib/types/Wizards.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export interface UseWizardProps {
2+
opened: boolean;
3+
onClose: () => void;
4+
}
5+
6+
export interface DataImportWizardProps extends UseWizardProps {
7+
sessionId: number;
8+
}
9+
10+
export interface UseWizardReturn {
11+
wizard: React.ReactElement;
12+
}

src/frontend/src/components/importer/ImporterDrawer.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { IconCheck, IconExclamationCircle } from '@tabler/icons-react';
1414
import { type ReactNode, useMemo } from 'react';
1515

1616
import { ModelType } from '@lib/enums/ModelType';
17+
import type {
18+
DataImportWizardProps,
19+
UseWizardReturn
20+
} from '@lib/types/Wizards';
1721
import { useImportSession } from '../../hooks/UseImportSession';
1822
import useStatusCodes from '../../hooks/UseStatusCodes';
1923
import { StylishText } from '../items/StylishText';
@@ -49,16 +53,10 @@ function ImportDrawerStepper({
4953
);
5054
}
5155

52-
export default function ImporterDrawer({
53-
sessionId,
54-
opened,
55-
onClose
56-
}: Readonly<{
57-
sessionId: number;
58-
opened: boolean;
59-
onClose: () => void;
60-
}>) {
61-
const session = useImportSession({ sessionId: sessionId });
56+
export default function ImporterDrawer(
57+
props: DataImportWizardProps
58+
): React.ReactElement {
59+
const session = useImportSession({ sessionId: props.sessionId });
6260

6361
const importSessionStatus = useStatusCodes({
6462
modelType: ModelType.importsession
@@ -106,13 +104,13 @@ export default function ImporterDrawer({
106104
>
107105
{t`Data has been imported successfully`}
108106
</Alert>
109-
<Button color='blue' onClick={onClose}>{t`Close`}</Button>
107+
<Button color='blue' onClick={props.onClose}>{t`Close`}</Button>
110108
</Stack>
111109
);
112110
default:
113111
return <ImporterStatus session={session} />;
114112
}
115-
}, [session.status, session.sessionQuery]);
113+
}, [session.status, session.sessionQuery, props.onClose]);
116114

117115
const title: ReactNode = useMemo(() => {
118116
return (
@@ -140,8 +138,8 @@ export default function ImporterDrawer({
140138
position='bottom'
141139
size='80%'
142140
title={title}
143-
opened={opened}
144-
onClose={onClose}
141+
opened={props.opened}
142+
onClose={props.onClose}
145143
withCloseButton={true}
146144
closeOnEscape={false}
147145
closeOnClickOutside={false}
@@ -160,3 +158,10 @@ export default function ImporterDrawer({
160158
</Drawer>
161159
);
162160
}
161+
162+
// Hook for using the import wizard in a plugin context
163+
export function useImportWizard(props: DataImportWizardProps): UseWizardReturn {
164+
return {
165+
wizard: <ImporterDrawer {...props} />
166+
};
167+
}

src/frontend/src/components/plugins/PluginContext.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
useEditApiFormModal
3838
} from '../../hooks/UseForm';
3939
import { useServerApiState } from '../../states/ServerApiState';
40+
import { useImportWizard } from '../importer/ImporterDrawer';
4041
import { RenderInstance } from '../render/Instance';
4142

4243
export const useInvenTreeContext = () => {
@@ -86,6 +87,9 @@ export const useInvenTreeContext = () => {
8687
transferStock: useTransferStockItem,
8788
returnStock: useReturnStockItem
8889
}
90+
},
91+
wizards: {
92+
importData: useImportWizard
8993
}
9094
};
9195
}, [

0 commit comments

Comments
 (0)