@myparcel-dev/pdk-admin 2.2.7 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/dist/actions/composables/mutations/orders/useExportOrdersMutation.d.ts +1 -1
- package/dist/actions/print/resolvePrintParameters.d.ts +1 -0
- package/dist/composables/language/useLanguage.d.ts +6 -0
- package/dist/pdk/PdkAdmin.d.ts +26 -2
- package/dist/pdk/boot.d.ts +5 -0
- package/dist/sdk/index.d.ts +1 -0
- package/dist/sdk/requestHeaders.d.ts +10 -0
- package/dist/services/mutations/getModalMutationOptions.d.ts +7 -1
- package/dist/services/mutations/index.d.ts +1 -0
- package/dist/services/mutations/modalMutationToken.d.ts +16 -0
- package/dist/services/print/createPdfObjectUrl.d.ts +2 -0
- package/dist/services/print/index.d.ts +3 -0
- package/dist/services/print/labelsNotification.d.ts +5 -0
- package/dist/services/print/pollLabelPdf.d.ts +9 -0
- package/dist/stores/useModalStore.d.ts +9 -1
- package/dist/stores/useNotificationStore.d.ts +15 -0
- package/dist/types/actions/endpoints.types.d.ts +1 -1
- package/dist/types/actions/parameters.types.d.ts +3 -0
- package/dist/types/admin.types.d.ts +10 -0
- package/dist/types/common.types.d.ts +7 -0
- package/dist/types/configuration.types.d.ts +12 -0
- package/dist/types/sdk.types.d.ts +14 -1
- package/dist/utils/encodeArrayParameter.d.ts +1 -0
- package/package.json +2 -2
- package/src/actions/composables/mutations/orders/useExportOrdersMutation.spec.ts +75 -0
- package/src/actions/composables/mutations/orders/useExportOrdersMutation.ts +17 -6
- package/src/actions/print/openOrPrintPdf.spec.ts +119 -0
- package/src/actions/print/openOrPrintPdf.ts +48 -3
- package/src/actions/print/resolvePrintParameters.ts +13 -3
- package/src/components/common/TabNavigation.spec.ts +78 -0
- package/src/components/common/TabNavigation.vue +7 -5
- package/src/composables/language/resetLanguageCache.spec.ts +24 -0
- package/src/composables/language/useLanguage.ts +13 -0
- package/src/pdk/PdkAdmin.spec.ts +97 -0
- package/src/pdk/PdkAdmin.ts +63 -7
- package/src/pdk/boot.ts +13 -3
- package/src/pdk/createPdkAdmin.spec.ts +75 -0
- package/src/pdk/createPdkAdmin.ts +2 -0
- package/src/pdk/instance/plugins/createStorePlugin.spec.ts +44 -0
- package/src/pdk/instance/plugins/createStorePlugin.ts +12 -7
- package/src/pdk/instance/plugins/createVueQueryPlugin.spec.ts +29 -0
- package/src/pdk/instance/plugins/createVueQueryPlugin.ts +7 -7
- package/src/pdk/setupAdminApp.ts +3 -0
- package/src/sdk/composables/usePdkAdminApi.ts +3 -0
- package/src/sdk/index.ts +1 -0
- package/src/sdk/requestHeaders.spec.ts +54 -0
- package/src/sdk/requestHeaders.ts +36 -0
- package/src/services/mutations/getModalMutationOptions.spec.ts +59 -0
- package/src/services/mutations/getModalMutationOptions.ts +23 -15
- package/src/services/mutations/index.ts +1 -0
- package/src/services/mutations/modalMutationToken.ts +27 -0
- package/src/services/print/createPdfObjectUrl.ts +6 -0
- package/src/services/print/index.ts +3 -0
- package/src/services/print/labelsNotification.ts +52 -0
- package/src/services/print/pollLabelPdf.spec.ts +58 -0
- package/src/services/print/pollLabelPdf.ts +47 -0
- package/src/stores/useModalStore.spec.ts +33 -0
- package/src/stores/useModalStore.ts +13 -1
- package/src/stores/useNotificationStore.spec.ts +30 -0
- package/src/stores/useNotificationStore.ts +1 -1
- package/src/types/actions/endpoints.types.ts +1 -0
- package/src/types/actions/parameters.types.ts +2 -0
- package/src/types/admin.types.ts +12 -0
- package/src/types/common.types.ts +7 -0
- package/src/types/configuration.types.ts +14 -0
- package/src/types/sdk.types.ts +16 -0
- package/src/utils/encodeArrayParameter.spec.ts +26 -0
- package/src/utils/encodeArrayParameter.ts +2 -1
|
@@ -1,13 +1,58 @@
|
|
|
1
1
|
import {isOfType} from '@myparcel-dev/ts-utils';
|
|
2
2
|
import {type ActionContextWithResponse} from '../executors';
|
|
3
|
-
import {generateLabelFilename} from '../../utils';
|
|
4
|
-
import {type PdfDataResponse, type PrintAction} from '../../types';
|
|
5
|
-
import {
|
|
3
|
+
import {downloadFileFromUrl, generateLabelFilename, openUrlInNewTab} from '../../utils';
|
|
4
|
+
import {type PdfDataResponse, type PdfUrlResponse, type PrintAction} from '../../types';
|
|
5
|
+
import {
|
|
6
|
+
createPdfObjectUrl,
|
|
7
|
+
downloadFile,
|
|
8
|
+
hideLabelsNotification,
|
|
9
|
+
openPdfInNewWindow,
|
|
10
|
+
pollLabelPdf,
|
|
11
|
+
showLabelsFailed,
|
|
12
|
+
showLabelsReady,
|
|
13
|
+
} from '../../services';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A link means the API prepares the pdf in the background. Wait for it, then offer a button to
|
|
17
|
+
* open it, because browsers block a new tab that does not come directly from a click.
|
|
18
|
+
*/
|
|
19
|
+
const openPreparedPdf = async <A extends PrintAction>(
|
|
20
|
+
labelId: string,
|
|
21
|
+
parameters: ActionContextWithResponse<A>['parameters'],
|
|
22
|
+
): Promise<void> => {
|
|
23
|
+
let url: string;
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
url = createPdfObjectUrl(await pollLabelPdf(labelId));
|
|
27
|
+
} catch (error) {
|
|
28
|
+
showLabelsFailed();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (parameters?.output === 'download') {
|
|
33
|
+
hideLabelsNotification();
|
|
34
|
+
downloadFileFromUrl(url, generateLabelFilename(parameters));
|
|
35
|
+
URL.revokeObjectURL(url);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
showLabelsReady(() => openUrlInNewTab(url));
|
|
40
|
+
};
|
|
6
41
|
|
|
7
42
|
export const openOrPrintPdf = async <A extends PrintAction>({
|
|
8
43
|
response,
|
|
9
44
|
parameters,
|
|
10
45
|
}: ActionContextWithResponse<A>): Promise<void> => {
|
|
46
|
+
if (isOfType<PdfUrlResponse>(response, 'labelId') && response.labelId) {
|
|
47
|
+
return openPreparedPdf(response.labelId, parameters);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
hideLabelsNotification();
|
|
51
|
+
|
|
52
|
+
if (!response) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
11
56
|
if (isOfType<PdfDataResponse>(response, 'data')) {
|
|
12
57
|
return openPdfInNewWindow(response.data);
|
|
13
58
|
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import {toArray} from '@myparcel-dev/ts-utils';
|
|
2
2
|
import {type ActionContext} from '../executors';
|
|
3
3
|
import {type ActionParameters, type PrintAction} from '../../types';
|
|
4
|
+
import {showLabelsLoading} from '../../services';
|
|
4
5
|
import {usePluginSettings} from '../../composables';
|
|
5
6
|
import {waitForLabelPrompt} from './waitForLabelPrompt';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
context: ActionContext<A>,
|
|
9
|
-
): Promise<ActionParameters<A>> => {
|
|
8
|
+
const resolveParameters = <A extends PrintAction>(context: ActionContext<A>): Promise<ActionParameters<A>> => {
|
|
10
9
|
const pluginSettings = usePluginSettings();
|
|
11
10
|
|
|
12
11
|
if (!pluginSettings.label.prompt) {
|
|
@@ -22,3 +21,14 @@ export const resolvePrintParameters = <A extends PrintAction>(
|
|
|
22
21
|
|
|
23
22
|
return waitForLabelPrompt(context);
|
|
24
23
|
};
|
|
24
|
+
|
|
25
|
+
/** Resolves the label options, then shows the loading notification that openOrPrintPdf closes. */
|
|
26
|
+
export const resolvePrintParameters = async <A extends PrintAction>(
|
|
27
|
+
context: ActionContext<A>,
|
|
28
|
+
): Promise<ActionParameters<A>> => {
|
|
29
|
+
const parameters = await resolveParameters(context);
|
|
30
|
+
|
|
31
|
+
showLabelsLoading();
|
|
32
|
+
|
|
33
|
+
return parameters;
|
|
34
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import {defineComponent, h} from 'vue';
|
|
4
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
|
5
|
+
import {mount} from '@vue/test-utils';
|
|
6
|
+
import {useAdminConfig} from '../../composables';
|
|
7
|
+
import TabNavigation from './TabNavigation.vue';
|
|
8
|
+
|
|
9
|
+
vi.mock('../../composables', () => ({
|
|
10
|
+
useAdminConfig: vi.fn(),
|
|
11
|
+
useLanguage: vi.fn(() => ({translate: (key: string) => key})),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
const createTab = (name: string) => ({
|
|
15
|
+
name,
|
|
16
|
+
label: name,
|
|
17
|
+
component: defineComponent({name: `Tab-${name}`, render: () => h('p', name)}),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
21
|
+
const PdkTabNavContentWrapper = defineComponent({
|
|
22
|
+
render() {
|
|
23
|
+
return h('div', this.$slots.default?.());
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const mountTabs = () =>
|
|
28
|
+
mount(TabNavigation, {
|
|
29
|
+
props: {tabs: [createTab('general'), createTab('label')], button: 'button', buttonWrapper: 'div'},
|
|
30
|
+
global: {stubs: {PdkTabNavContentWrapper}},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const clickTab = async (wrapper: ReturnType<typeof mountTabs>, name: string) => {
|
|
34
|
+
await wrapper
|
|
35
|
+
.findAll('button')
|
|
36
|
+
.find((button) => button.text() === name)
|
|
37
|
+
?.trigger('click');
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
describe('TabNavigation', () => {
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
window.location.hash = '';
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
afterEach(() => {
|
|
46
|
+
window.location.hash = '';
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('writes the active tab to the location hash by default', async () => {
|
|
50
|
+
vi.mocked(useAdminConfig).mockReturnValue({} as never);
|
|
51
|
+
const wrapper = mountTabs();
|
|
52
|
+
|
|
53
|
+
await clickTab(wrapper, 'label');
|
|
54
|
+
|
|
55
|
+
expect(window.location.hash).toBe('#label');
|
|
56
|
+
expect(wrapper.text()).toContain('label');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('opens the tab from the location hash by default', () => {
|
|
60
|
+
vi.mocked(useAdminConfig).mockReturnValue({} as never);
|
|
61
|
+
window.location.hash = '#label';
|
|
62
|
+
|
|
63
|
+
expect(mountTabs().find('p').text()).toBe('label');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('leaves the location hash alone when the host app routes with it', async () => {
|
|
67
|
+
vi.mocked(useAdminConfig).mockReturnValue({useLocationHash: false} as never);
|
|
68
|
+
window.location.hash = '#/myparcel/settings/index';
|
|
69
|
+
const wrapper = mountTabs();
|
|
70
|
+
|
|
71
|
+
expect(wrapper.find('p').text()).toBe('general');
|
|
72
|
+
|
|
73
|
+
await clickTab(wrapper, 'label');
|
|
74
|
+
|
|
75
|
+
expect(window.location.hash).toBe('#/myparcel/settings/index');
|
|
76
|
+
expect(wrapper.find('p').text()).toBe('label');
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -79,8 +79,13 @@ const props = defineProps({
|
|
|
79
79
|
|
|
80
80
|
const activeTab = ref<string | null>(null);
|
|
81
81
|
|
|
82
|
+
const config = useAdminConfig();
|
|
83
|
+
const {translate} = useLanguage();
|
|
84
|
+
|
|
85
|
+
const useLocationHash = config.useLocationHash !== false;
|
|
86
|
+
|
|
82
87
|
const handleClick = (tab: TabDefinition) => {
|
|
83
|
-
if (props.initialTab !== false) {
|
|
88
|
+
if (useLocationHash && props.initialTab !== false) {
|
|
84
89
|
window.location.hash = props.hashPrefix + tab.name;
|
|
85
90
|
}
|
|
86
91
|
|
|
@@ -96,9 +101,6 @@ const activeTabContents = computed(() => {
|
|
|
96
101
|
return (props.tabs as unknown as TabDefinition[])?.find((tab) => tab.name === activeTab.value);
|
|
97
102
|
});
|
|
98
103
|
|
|
99
|
-
const config = useAdminConfig();
|
|
100
|
-
const {translate} = useLanguage();
|
|
101
|
-
|
|
102
104
|
watch(
|
|
103
105
|
() => [props.tabs, props.initialTab],
|
|
104
106
|
() => {
|
|
@@ -113,7 +115,7 @@ watch(
|
|
|
113
115
|
|
|
114
116
|
let initialTab = props.initialTab === true ? null : props.initialTab;
|
|
115
117
|
|
|
116
|
-
if (!props.initialTab) {
|
|
118
|
+
if (!props.initialTab && useLocationHash) {
|
|
117
119
|
const hash = window.location.hash.replace('#', '').replace(props.hashPrefix ?? '', '');
|
|
118
120
|
const tabName = hash.split(HASH_SEPARATOR)[0];
|
|
119
121
|
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import {describe, expect, it, vi} from 'vitest';
|
|
4
|
+
import {useGlobalContext} from '../context';
|
|
5
|
+
import {resetLanguageCache, useLanguage} from './useLanguage';
|
|
6
|
+
|
|
7
|
+
vi.mock('../context', () => ({useGlobalContext: vi.fn()}));
|
|
8
|
+
|
|
9
|
+
const useTranslations = (translations: Record<string, string>) => {
|
|
10
|
+
vi.mocked(useGlobalContext).mockReturnValue({translations} as never);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
describe('resetLanguageCache', () => {
|
|
14
|
+
it('translates with the translations of the next admin instance after a reset', () => {
|
|
15
|
+
useTranslations({settings_title: 'Settings'});
|
|
16
|
+
expect(useLanguage().translate('settings_title')).toBe('Settings');
|
|
17
|
+
|
|
18
|
+
useTranslations({settings_title: 'Einstellungen'});
|
|
19
|
+
resetLanguageCache();
|
|
20
|
+
|
|
21
|
+
expect(useLanguage().translate('settings_title')).toBe('Einstellungen');
|
|
22
|
+
expect(useLanguage().has('settings_title')).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -104,6 +104,19 @@ const memoizedAll = useMemoize(all);
|
|
|
104
104
|
const memoizedHas = useMemoize(has);
|
|
105
105
|
const memoizedTranslate = useMemoize(translate);
|
|
106
106
|
|
|
107
|
+
/**
|
|
108
|
+
* Clears the cached translations. createPdkAdmin() calls it, so a new admin
|
|
109
|
+
* instance, for example after a language change in a single-page app, uses its
|
|
110
|
+
* own translations.
|
|
111
|
+
*/
|
|
112
|
+
export const resetLanguageCache = (): void => {
|
|
113
|
+
memoizedAll.clear();
|
|
114
|
+
memoizedHas.clear();
|
|
115
|
+
memoizedTranslate.clear();
|
|
116
|
+
useCache().clear();
|
|
117
|
+
useMissingKeys().value = [];
|
|
118
|
+
};
|
|
119
|
+
|
|
107
120
|
export const useLanguage = (): UseLanguage => {
|
|
108
121
|
const missingKeys = useMissingKeys();
|
|
109
122
|
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {defineComponent, h} from 'vue';
|
|
2
|
+
import {afterEach, describe, expect, it, vi} from 'vitest';
|
|
3
|
+
import {AdminView} from '../data';
|
|
4
|
+
import {setupAdminApp} from './setupAdminApp';
|
|
5
|
+
import {createAdminConfig} from './createAdminConfig';
|
|
6
|
+
import {PdkAdmin} from './PdkAdmin';
|
|
7
|
+
|
|
8
|
+
vi.mock('./renderMap', () => ({
|
|
9
|
+
renderViewComponent: vi.fn(() => Promise.resolve(defineComponent({render: () => h('p', 'rendered')}))),
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
vi.mock('./setupAdminApp', () => ({setupAdminApp: vi.fn()}));
|
|
13
|
+
|
|
14
|
+
const createPdkAdmin = (): PdkAdmin => new PdkAdmin(createAdminConfig({components: {}} as never), {} as never);
|
|
15
|
+
|
|
16
|
+
describe('PdkAdmin.render', () => {
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
document.body.innerHTML = '';
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('returns the mounted app, so the caller can unmount it', async () => {
|
|
22
|
+
document.body.innerHTML = '<div id="target"></div>';
|
|
23
|
+
|
|
24
|
+
const app = await createPdkAdmin().render(AdminView.PluginSettings, '#target');
|
|
25
|
+
|
|
26
|
+
expect(document.querySelector('#target')?.textContent).toBe('rendered');
|
|
27
|
+
|
|
28
|
+
app?.unmount();
|
|
29
|
+
|
|
30
|
+
expect(document.querySelector('#target')?.textContent).toBe('');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('returns undefined when the element does not exist', async () => {
|
|
34
|
+
const app = await createPdkAdmin().render(AdminView.PluginSettings, '#missing');
|
|
35
|
+
|
|
36
|
+
expect(app).toBeUndefined();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('shares one store and query client between the apps of one instance', async () => {
|
|
40
|
+
document.body.innerHTML = '<div id="one"></div><div id="two"></div>';
|
|
41
|
+
vi.mocked(setupAdminApp).mockClear();
|
|
42
|
+
const pdkAdmin = createPdkAdmin();
|
|
43
|
+
|
|
44
|
+
await pdkAdmin.render(AdminView.PluginSettings, '#one');
|
|
45
|
+
await pdkAdmin.render(AdminView.PluginSettings, '#two');
|
|
46
|
+
|
|
47
|
+
const [first, second] = vi.mocked(setupAdminApp).mock.calls.map(([, appConfig]) => appConfig);
|
|
48
|
+
|
|
49
|
+
expect(first.store).toBeDefined();
|
|
50
|
+
expect(first.queryClient).toBeDefined();
|
|
51
|
+
expect(second.store).toBe(first.store);
|
|
52
|
+
expect(second.queryClient).toBe(first.queryClient);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('gives a new instance its own store and query client, so a second visit starts clean', async () => {
|
|
56
|
+
document.body.innerHTML = '<div id="one"></div><div id="two"></div>';
|
|
57
|
+
vi.mocked(setupAdminApp).mockClear();
|
|
58
|
+
|
|
59
|
+
await createPdkAdmin().render(AdminView.PluginSettings, '#one');
|
|
60
|
+
await createPdkAdmin().render(AdminView.PluginSettings, '#two');
|
|
61
|
+
|
|
62
|
+
const [first, second] = vi.mocked(setupAdminApp).mock.calls.map(([, appConfig]) => appConfig);
|
|
63
|
+
|
|
64
|
+
expect(second.store).not.toBe(first.store);
|
|
65
|
+
expect(second.queryClient).not.toBe(first.queryClient);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('removes the cached queries on dispose', async () => {
|
|
69
|
+
document.body.innerHTML = '<div id="target"></div>';
|
|
70
|
+
vi.mocked(setupAdminApp).mockClear();
|
|
71
|
+
const pdkAdmin = createPdkAdmin();
|
|
72
|
+
|
|
73
|
+
await pdkAdmin.render(AdminView.PluginSettings, '#target');
|
|
74
|
+
|
|
75
|
+
const [[, {queryClient}]] = vi.mocked(setupAdminApp).mock.calls;
|
|
76
|
+
|
|
77
|
+
queryClient.setQueryData(['orders'], ['order']);
|
|
78
|
+
pdkAdmin.dispose();
|
|
79
|
+
|
|
80
|
+
expect(queryClient.getQueryCache().getAll()).toHaveLength(0);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('returns the mounted app when the onRendered hook fails, so the caller can still unmount it', async () => {
|
|
84
|
+
document.body.innerHTML = '<div id="target"></div>';
|
|
85
|
+
const config = createAdminConfig({
|
|
86
|
+
components: {},
|
|
87
|
+
onRendered: () => {
|
|
88
|
+
throw new Error('hook failed');
|
|
89
|
+
},
|
|
90
|
+
} as never);
|
|
91
|
+
|
|
92
|
+
const app = await new PdkAdmin(config, {} as never).render(AdminView.PluginSettings, '#target');
|
|
93
|
+
|
|
94
|
+
expect(app).toBeDefined();
|
|
95
|
+
expect(document.querySelector('#target')?.textContent).toBe('rendered');
|
|
96
|
+
});
|
|
97
|
+
});
|
package/src/pdk/PdkAdmin.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
2
|
|
|
3
3
|
import {type App, createApp} from 'vue';
|
|
4
|
+
import {createPinia} from 'pinia';
|
|
4
5
|
import {type AdminAppConfig, type AdminConfiguration, type AdminContextObject} from '../types';
|
|
5
6
|
import {INJECT_GLOBAL_PDK_ADMIN} from '../symbols';
|
|
6
7
|
import {createLogger, getElementContext} from '../services';
|
|
7
8
|
import {AdminView} from '../data';
|
|
8
9
|
import {setupAdminApp} from './setupAdminApp';
|
|
9
10
|
import {renderViewComponent} from './renderMap';
|
|
11
|
+
import {createQueryClient} from './instance/createQueryClient';
|
|
10
12
|
|
|
11
13
|
export class PdkAdmin {
|
|
12
14
|
public readonly config: AdminConfiguration;
|
|
@@ -14,6 +16,16 @@ export class PdkAdmin {
|
|
|
14
16
|
|
|
15
17
|
public readonly renderedComponents: string[] = [];
|
|
16
18
|
|
|
19
|
+
/**
|
|
20
|
+
* One store and one query client per instance, shared by its apps. A page that
|
|
21
|
+
* creates a new instance after unmounting the apps of the previous one (for
|
|
22
|
+
* example a single-page admin) then starts without stale queries.
|
|
23
|
+
*/
|
|
24
|
+
protected readonly store = createPinia();
|
|
25
|
+
protected readonly queryClient = createQueryClient();
|
|
26
|
+
|
|
27
|
+
private readonly disposers: (() => void)[] = [];
|
|
28
|
+
|
|
17
29
|
public constructor(config: AdminConfiguration, context: AdminContextObject) {
|
|
18
30
|
config.beforeInitialize?.(config, context);
|
|
19
31
|
|
|
@@ -24,9 +36,32 @@ export class PdkAdmin {
|
|
|
24
36
|
}
|
|
25
37
|
|
|
26
38
|
/**
|
|
27
|
-
*
|
|
39
|
+
* Register cleanup work for dispose().
|
|
28
40
|
*/
|
|
29
|
-
public
|
|
41
|
+
public onDispose(disposer: () => void): void {
|
|
42
|
+
this.disposers.push(disposer);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Stop this instance, for example when a single-page app leaves the page.
|
|
47
|
+
* Unmount the apps that render() returned first. Safe to call twice.
|
|
48
|
+
*
|
|
49
|
+
* createPdkAdmin() reads the context from the boot element once. To create
|
|
50
|
+
* the next instance, render a new boot element.
|
|
51
|
+
*
|
|
52
|
+
* The cached queries go at once. A request that is still running reports to
|
|
53
|
+
* the admin on screen when it finishes.
|
|
54
|
+
*/
|
|
55
|
+
public dispose(): void {
|
|
56
|
+
this.disposers.splice(0).forEach((disposer) => disposer());
|
|
57
|
+
this.queryClient.clear();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Render a view in the given selector. Returns the app, so the caller can
|
|
62
|
+
* unmount it, or undefined when mounting failed.
|
|
63
|
+
*/
|
|
64
|
+
public async render(view: AdminView, selector: string): Promise<App | undefined> {
|
|
30
65
|
const config: AdminConfiguration = {...this.config};
|
|
31
66
|
const context: AdminContextObject = {...this.context, ...getElementContext(selector)};
|
|
32
67
|
|
|
@@ -35,16 +70,37 @@ export class PdkAdmin {
|
|
|
35
70
|
|
|
36
71
|
logger.debug(`Rendering "${view}" in "${selector}"`);
|
|
37
72
|
|
|
38
|
-
const app = await this.createApp(view, {
|
|
73
|
+
const app = await this.createApp(view, {
|
|
74
|
+
appName,
|
|
75
|
+
config,
|
|
76
|
+
context,
|
|
77
|
+
logger,
|
|
78
|
+
view,
|
|
79
|
+
store: this.store,
|
|
80
|
+
queryClient: this.queryClient,
|
|
81
|
+
});
|
|
39
82
|
|
|
40
83
|
try {
|
|
41
|
-
app.mount(selector)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
84
|
+
if (!app.mount(selector)) {
|
|
85
|
+
logger.error(`Element "${selector}" not found`);
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
45
88
|
} catch (e) {
|
|
46
89
|
logger.error('Error mounting app', e);
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
this.renderedComponents.push(view);
|
|
94
|
+
logger.debug(`Rendered in ${selector}`);
|
|
95
|
+
|
|
96
|
+
// The app is mounted now, so return it even when the hook fails: the caller needs it to unmount.
|
|
97
|
+
try {
|
|
98
|
+
config?.onRendered?.(config);
|
|
99
|
+
} catch (e) {
|
|
100
|
+
logger.error('Error in the onRendered hook', e);
|
|
47
101
|
}
|
|
102
|
+
|
|
103
|
+
return app;
|
|
48
104
|
}
|
|
49
105
|
|
|
50
106
|
protected async createApp(view: AdminView, appConfig: AdminAppConfig): Promise<App> {
|
package/src/pdk/boot.ts
CHANGED
|
@@ -3,12 +3,22 @@ import {globalLogger} from '../services';
|
|
|
3
3
|
import {sendBootEvent} from './sendBootEvent';
|
|
4
4
|
import {type PdkAdmin} from './PdkAdmin';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Sends the boot event now and again on every ping, until the instance is
|
|
8
|
+
* disposed. The listener and its cleanup exist before the first event, so a
|
|
9
|
+
* handler of that event can already dispose the instance.
|
|
10
|
+
*/
|
|
6
11
|
export const boot = (pdkAdmin: PdkAdmin, context: AdminContextObject): void => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
document.addEventListener(context.global.eventPing, () => {
|
|
12
|
+
const onPing = () => {
|
|
10
13
|
sendBootEvent(pdkAdmin, context);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
document.addEventListener(context.global.eventPing, onPing);
|
|
17
|
+
pdkAdmin.onDispose(() => {
|
|
18
|
+
document.removeEventListener(context.global.eventPing, onPing);
|
|
11
19
|
});
|
|
12
20
|
|
|
21
|
+
sendBootEvent(pdkAdmin, context);
|
|
22
|
+
|
|
13
23
|
globalLogger.debug('Created PDK admin!', {context});
|
|
14
24
|
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import {afterEach, describe, expect, it, vi} from 'vitest';
|
|
4
|
+
import {useLanguage} from '../composables/language/useLanguage';
|
|
5
|
+
import {createPdkAdmin} from './createPdkAdmin';
|
|
6
|
+
|
|
7
|
+
const PING = 'test_pdk_ping';
|
|
8
|
+
const PONG = 'test_pdk_pong';
|
|
9
|
+
|
|
10
|
+
const addBootElement = () => {
|
|
11
|
+
const element = document.createElement('span');
|
|
12
|
+
|
|
13
|
+
element.id = 'myparcel-pdk-boot';
|
|
14
|
+
element.dataset.pdkContext = JSON.stringify({global: {eventPing: PING, eventPong: PONG, mode: 'production'}});
|
|
15
|
+
document.body.append(element);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
describe('createPdkAdmin', () => {
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
document.body.innerHTML = '';
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('answers a ping with its instance until it is disposed', () => {
|
|
24
|
+
addBootElement();
|
|
25
|
+
const pong = vi.fn();
|
|
26
|
+
document.addEventListener(PONG, pong);
|
|
27
|
+
|
|
28
|
+
const pdkAdmin = createPdkAdmin({components: {}} as never);
|
|
29
|
+
pong.mockClear();
|
|
30
|
+
|
|
31
|
+
document.dispatchEvent(new Event(PING));
|
|
32
|
+
expect(pong).toHaveBeenCalledTimes(1);
|
|
33
|
+
|
|
34
|
+
pdkAdmin?.dispose();
|
|
35
|
+
document.dispatchEvent(new Event(PING));
|
|
36
|
+
expect(pong).toHaveBeenCalledTimes(1);
|
|
37
|
+
|
|
38
|
+
document.removeEventListener(PONG, pong);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('can be disposed twice', () => {
|
|
42
|
+
addBootElement();
|
|
43
|
+
const pdkAdmin = createPdkAdmin({components: {}} as never);
|
|
44
|
+
|
|
45
|
+
pdkAdmin?.dispose();
|
|
46
|
+
|
|
47
|
+
expect(() => pdkAdmin?.dispose()).not.toThrow();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('stops answering pings when a pong handler disposes it right away', () => {
|
|
51
|
+
addBootElement();
|
|
52
|
+
const disposeOnPong = (event: Event) => (event as CustomEvent).detail.dispose();
|
|
53
|
+
document.addEventListener(PONG, disposeOnPong, {once: true});
|
|
54
|
+
|
|
55
|
+
createPdkAdmin({components: {}} as never);
|
|
56
|
+
|
|
57
|
+
const pong = vi.fn();
|
|
58
|
+
document.addEventListener(PONG, pong);
|
|
59
|
+
document.dispatchEvent(new Event(PING));
|
|
60
|
+
|
|
61
|
+
expect(pong).not.toHaveBeenCalled();
|
|
62
|
+
|
|
63
|
+
document.removeEventListener(PONG, pong);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('starts without the translation cache of a previous instance', () => {
|
|
67
|
+
addBootElement();
|
|
68
|
+
const {missingKeys} = useLanguage();
|
|
69
|
+
missingKeys.value = ['key_from_the_previous_instance'];
|
|
70
|
+
|
|
71
|
+
createPdkAdmin({components: {}} as never);
|
|
72
|
+
|
|
73
|
+
expect(missingKeys.value).toEqual([]);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {type InputAdminConfiguration} from '../types';
|
|
2
2
|
import {getElementContext, globalLogger} from '../services';
|
|
3
|
+
import {resetLanguageCache} from '../composables/language/useLanguage';
|
|
3
4
|
import {createAdminConfig} from './createAdminConfig';
|
|
4
5
|
import {boot} from './boot';
|
|
5
6
|
import {PdkAdmin} from './PdkAdmin';
|
|
@@ -22,6 +23,7 @@ export const createPdkAdmin: CreatePdkAdmin = (configuration?) => {
|
|
|
22
23
|
const config = createAdminConfig(configuration);
|
|
23
24
|
|
|
24
25
|
globalLogger.level = config.logLevel;
|
|
26
|
+
resetLanguageCache();
|
|
25
27
|
|
|
26
28
|
const context = getElementContext(BOOTSTRAP_CONTAINER_SELECTOR);
|
|
27
29
|
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {createApp} from 'vue';
|
|
2
|
+
import {describe, expect, it, vi} from 'vitest';
|
|
3
|
+
import {createPinia, defineStore, type Pinia} from 'pinia';
|
|
4
|
+
import {type AdminAppConfig} from '../../../types';
|
|
5
|
+
import {createStorePlugin} from './createStorePlugin';
|
|
6
|
+
|
|
7
|
+
const createAppConfig = (overrides: Partial<AdminAppConfig> = {}): AdminAppConfig =>
|
|
8
|
+
({
|
|
9
|
+
appName: 'test',
|
|
10
|
+
config: {},
|
|
11
|
+
context: {},
|
|
12
|
+
logger: {debug: () => undefined},
|
|
13
|
+
...overrides,
|
|
14
|
+
} as unknown as AdminAppConfig);
|
|
15
|
+
|
|
16
|
+
describe('createStorePlugin', () => {
|
|
17
|
+
it('uses the store of the pdk admin instance', () => {
|
|
18
|
+
const store = createPinia();
|
|
19
|
+
|
|
20
|
+
expect(createStorePlugin(createAppConfig({store}))).toBe(store);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('shares one store between apps without an instance store', () => {
|
|
24
|
+
expect(createStorePlugin(createAppConfig())).toBe(createStorePlugin(createAppConfig()));
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('calls onCreateStore once for every pdk admin instance store', () => {
|
|
28
|
+
const onCreateStore = vi.fn();
|
|
29
|
+
const useTestStore = defineStore('test', {state: () => ({value: 1})});
|
|
30
|
+
|
|
31
|
+
const useStoreIn = (store: Pinia) => {
|
|
32
|
+
const app = createApp({render: () => null});
|
|
33
|
+
|
|
34
|
+
app.use(createStorePlugin(createAppConfig({store, config: {onCreateStore} as never})));
|
|
35
|
+
app.use(createStorePlugin(createAppConfig({store, config: {onCreateStore} as never})));
|
|
36
|
+
app.runWithContext(() => useTestStore(store));
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
useStoreIn(createPinia());
|
|
40
|
+
useStoreIn(createPinia());
|
|
41
|
+
|
|
42
|
+
expect(onCreateStore).toHaveBeenCalledTimes(2);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -2,22 +2,27 @@ import {createPinia, type Pinia} from 'pinia';
|
|
|
2
2
|
import {type PdkAppPlugin} from './plugins.types';
|
|
3
3
|
|
|
4
4
|
let store: Pinia;
|
|
5
|
-
let initialized = false;
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
/**
|
|
7
|
+
* The stores that ran the onCreateStore hook. Per store, because every PdkAdmin
|
|
8
|
+
* instance has its own.
|
|
9
|
+
*/
|
|
10
|
+
const initializedStores = new WeakSet<Pinia>();
|
|
11
|
+
|
|
12
|
+
export const createStorePlugin: PdkAppPlugin = ({config, logger, store: instanceStore}) => {
|
|
13
|
+
const pinia = instanceStore ?? (store ??= createPinia());
|
|
9
14
|
|
|
10
15
|
logger.debug('Preparing store plugin');
|
|
11
16
|
|
|
12
|
-
|
|
13
|
-
if (
|
|
17
|
+
pinia.use((storeContext) => {
|
|
18
|
+
if (initializedStores.has(pinia) || !config.onCreateStore) {
|
|
14
19
|
return;
|
|
15
20
|
}
|
|
16
21
|
|
|
17
|
-
|
|
22
|
+
initializedStores.add(pinia);
|
|
18
23
|
logger.debug('Calling store renderer hook');
|
|
19
24
|
config.onCreateStore(storeContext);
|
|
20
25
|
});
|
|
21
26
|
|
|
22
|
-
return
|
|
27
|
+
return pinia;
|
|
23
28
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {createApp} from 'vue';
|
|
2
|
+
import {describe, expect, it} from 'vitest';
|
|
3
|
+
import {QueryClient, useQueryClient} from '@tanstack/vue-query';
|
|
4
|
+
import {AdminContextKey, BackendEndpoint} from '@myparcel-dev/pdk-common';
|
|
5
|
+
import {type AdminAppConfig} from '../../../types';
|
|
6
|
+
import {createVueQueryPlugin} from './createVueQueryPlugin';
|
|
7
|
+
|
|
8
|
+
const createAppConfig = (overrides: Partial<AdminAppConfig> = {}): AdminAppConfig =>
|
|
9
|
+
({
|
|
10
|
+
appName: 'test',
|
|
11
|
+
config: {},
|
|
12
|
+
context: {[AdminContextKey.Global]: {mode: 'production'}},
|
|
13
|
+
logger: {debug: () => undefined},
|
|
14
|
+
...overrides,
|
|
15
|
+
} as unknown as AdminAppConfig);
|
|
16
|
+
|
|
17
|
+
describe('createVueQueryPlugin', () => {
|
|
18
|
+
it('provides the query client of the pdk admin instance, filled with the context', () => {
|
|
19
|
+
const queryClient = new QueryClient();
|
|
20
|
+
const app = createApp({render: () => null});
|
|
21
|
+
|
|
22
|
+
app.use(createVueQueryPlugin(createAppConfig({queryClient})));
|
|
23
|
+
|
|
24
|
+
expect(app.runWithContext(() => useQueryClient())).toBe(queryClient);
|
|
25
|
+
expect(queryClient.getQueryData([BackendEndpoint.FetchContext, AdminContextKey.Global])).toEqual({
|
|
26
|
+
mode: 'production',
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
});
|