@myparcel-dev/pdk-admin 2.3.0 → 2.4.1
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 +22 -0
- package/dist/actions/composables/mutations/orders/useExportOrdersMutation.d.ts +1 -1
- 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/stores/useModalStore.d.ts +9 -1
- package/dist/types/admin.types.d.ts +10 -0
- package/dist/types/configuration.types.d.ts +12 -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/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/forms/helpers/useFormCapabilities.spec.ts +15 -9
- package/src/forms/helpers/useFormCapabilities.ts +27 -13
- 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/stores/useModalStore.spec.ts +33 -0
- package/src/stores/useModalStore.ts +13 -1
- package/src/types/admin.types.ts +12 -0
- package/src/types/configuration.types.ts +14 -0
|
@@ -3,22 +3,30 @@ import {type MutationOptions} from '@tanstack/vue-query';
|
|
|
3
3
|
import {addErrorToNotifications} from '../addErrorToNotifications';
|
|
4
4
|
import {useModalStore} from '../../stores';
|
|
5
5
|
import {NotificationCategory} from '../../data';
|
|
6
|
+
import {isSameModal, keepModalToken, type ModalMutationContext} from './modalMutationToken';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Options for a mutation that a modal starts. The callbacks only change the
|
|
10
|
+
* modal when it is still the modal that started the mutation. Pass the default
|
|
11
|
+
* mutation options, so their onMutate still runs.
|
|
12
|
+
*/
|
|
13
|
+
export const getModalMutationOptions = <TData = unknown, TError = unknown, TVariables = void>(
|
|
14
|
+
defaultOptions: MutationOptions<TData, TError, TVariables> = {},
|
|
15
|
+
): MutationOptions<TData, TError, TVariables, ModalMutationContext> => ({
|
|
16
|
+
onMutate: keepModalToken(defaultOptions.onMutate),
|
|
17
|
+
onSuccess(data, variables, context) {
|
|
18
|
+
if (isSameModal(context)) {
|
|
15
19
|
useModalStore().close();
|
|
16
|
-
}
|
|
17
|
-
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
onSettled(data, error, variables, context) {
|
|
23
|
+
if (isSameModal(context)) {
|
|
18
24
|
useModalStore().loading = false;
|
|
19
|
-
}
|
|
20
|
-
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
onError(error, variables, context) {
|
|
28
|
+
if (isSameModal(context)) {
|
|
21
29
|
addErrorToNotifications(error, NotificationCategory.Modal);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
};
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {type ModalToken, useModalStore} from '../../stores';
|
|
2
|
+
|
|
3
|
+
export type ModalMutationContext = {modalToken: ModalToken | null};
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates an onMutate that keeps the token of the modal that is open when the
|
|
7
|
+
* mutation starts. Pass the onMutate it replaces, for example the default one
|
|
8
|
+
* of the query client, so that one still runs.
|
|
9
|
+
*/
|
|
10
|
+
export const keepModalToken =
|
|
11
|
+
<TVariables>(onMutate?: (variables: TVariables) => unknown) =>
|
|
12
|
+
(variables: TVariables): ModalMutationContext => {
|
|
13
|
+
void onMutate?.(variables);
|
|
14
|
+
|
|
15
|
+
return {modalToken: useModalStore().token};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Tells if the modal that was open when the mutation started is still open.
|
|
20
|
+
* Use it in late mutation callbacks, so they do not change a modal that the
|
|
21
|
+
* user opened in the meantime.
|
|
22
|
+
*/
|
|
23
|
+
export const isSameModal = (context: ModalMutationContext | undefined): boolean => {
|
|
24
|
+
const modalToken = context?.modalToken;
|
|
25
|
+
|
|
26
|
+
return Boolean(modalToken) && modalToken === useModalStore().token;
|
|
27
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import {beforeEach, describe, expect, it} from 'vitest';
|
|
4
|
+
import {createPinia, setActivePinia} from 'pinia';
|
|
5
|
+
import {AdminModalKey} from '../data';
|
|
6
|
+
import {useModalStore} from './useModalStore';
|
|
7
|
+
|
|
8
|
+
describe('useModalStore', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
setActivePinia(createPinia());
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('gives every opened modal a new token', () => {
|
|
14
|
+
const modalStore = useModalStore();
|
|
15
|
+
|
|
16
|
+
modalStore.open(AdminModalKey.ShipmentOptions);
|
|
17
|
+
const first = modalStore.token;
|
|
18
|
+
|
|
19
|
+
modalStore.open(AdminModalKey.ShipmentOptions);
|
|
20
|
+
|
|
21
|
+
expect(first).toEqual({modal: AdminModalKey.ShipmentOptions});
|
|
22
|
+
expect(modalStore.token).not.toBe(first);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('removes the token when the modal closes', () => {
|
|
26
|
+
const modalStore = useModalStore();
|
|
27
|
+
|
|
28
|
+
modalStore.open(AdminModalKey.PrintOptions);
|
|
29
|
+
modalStore.close();
|
|
30
|
+
|
|
31
|
+
expect(modalStore.token).toBeNull();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {ref, type Ref, type UnwrapNestedRefs} from 'vue';
|
|
1
|
+
import {ref, type Ref, shallowRef, type UnwrapNestedRefs} from 'vue';
|
|
2
2
|
import {defineStore} from 'pinia';
|
|
3
3
|
import {type AdminModalContext} from '../types';
|
|
4
4
|
import {type AdminModalKey, NotificationCategory} from '../data';
|
|
@@ -9,10 +9,17 @@ type ModalOpenFn = <K extends AdminModalKey>(modal: K, context?: AdminModalConte
|
|
|
9
9
|
|
|
10
10
|
type ModalCloseFn = <K extends AdminModalKey>(modal: K) => void;
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Identifies one opened modal. Every open() creates a new object, so a token
|
|
14
|
+
* never matches another modal, also not a modal in another admin instance.
|
|
15
|
+
*/
|
|
16
|
+
export type ModalToken = Readonly<{modal: AdminModalKey}>;
|
|
17
|
+
|
|
12
18
|
type ModalStore = {
|
|
13
19
|
loading: Ref<boolean>;
|
|
14
20
|
opened: Ref<AdminModalKey | null>;
|
|
15
21
|
context: Ref<UnwrapNestedRefs<AdminModalContext>>;
|
|
22
|
+
token: Ref<ModalToken | null>;
|
|
16
23
|
onOpen(callback: ModalOpenFn): void;
|
|
17
24
|
onClose(callback: ModalCloseFn): void;
|
|
18
25
|
open<K extends AdminModalKey>(modal: K, newContext?: AdminModalContext<K>): void;
|
|
@@ -24,6 +31,7 @@ export const useModalStore = defineStore('modal', (): ModalStore => {
|
|
|
24
31
|
|
|
25
32
|
const opened = ref<AdminModalKey | null>(null);
|
|
26
33
|
const context = ref<AdminModalContext>(null);
|
|
34
|
+
const token = shallowRef<ModalToken | null>(null);
|
|
27
35
|
|
|
28
36
|
const openHooks = ref<ModalOpenFn[]>([]);
|
|
29
37
|
const closeHooks = ref<ModalCloseFn[]>([]);
|
|
@@ -35,6 +43,8 @@ export const useModalStore = defineStore('modal', (): ModalStore => {
|
|
|
35
43
|
|
|
36
44
|
context,
|
|
37
45
|
|
|
46
|
+
token,
|
|
47
|
+
|
|
38
48
|
onOpen(callback) {
|
|
39
49
|
openHooks.value.push(callback);
|
|
40
50
|
},
|
|
@@ -49,6 +59,7 @@ export const useModalStore = defineStore('modal', (): ModalStore => {
|
|
|
49
59
|
// @ts-ignore
|
|
50
60
|
context.value = newContext;
|
|
51
61
|
opened.value = modal;
|
|
62
|
+
token.value = {modal};
|
|
52
63
|
|
|
53
64
|
openHooks.value.forEach((hook) => hook(modal, newContext));
|
|
54
65
|
},
|
|
@@ -59,6 +70,7 @@ export const useModalStore = defineStore('modal', (): ModalStore => {
|
|
|
59
70
|
|
|
60
71
|
opened.value = null;
|
|
61
72
|
context.value = null;
|
|
73
|
+
token.value = null;
|
|
62
74
|
closeHooks.value.forEach((hook) => hook(modal));
|
|
63
75
|
},
|
|
64
76
|
};
|
package/src/types/admin.types.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import {type Component} from 'vue';
|
|
2
|
+
import {type Pinia} from 'pinia';
|
|
3
|
+
import {type QueryClient} from '@tanstack/vue-query';
|
|
2
4
|
import {type PdkLogger} from '../services';
|
|
3
5
|
import {
|
|
4
6
|
type ADMIN_COMPONENT_PREFIX,
|
|
@@ -40,6 +42,16 @@ export interface AdminAppConfig {
|
|
|
40
42
|
context: AdminContextObject;
|
|
41
43
|
logger: PdkLogger;
|
|
42
44
|
view?: AdminView;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Store of the PdkAdmin instance, shared by its apps. Without it, all apps share one store.
|
|
48
|
+
*/
|
|
49
|
+
store?: Pinia;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Query client of the PdkAdmin instance, shared by its apps. Without it, all apps share one client.
|
|
53
|
+
*/
|
|
54
|
+
queryClient?: QueryClient;
|
|
43
55
|
}
|
|
44
56
|
|
|
45
57
|
export interface AdminInstance extends Omit<AdminAppConfig, 'context'> {
|
|
@@ -2,6 +2,7 @@ import {type PiniaPluginContext} from 'pinia';
|
|
|
2
2
|
import {type FormConfiguration, type InteractiveElementInstance} from '@myparcel-dev/vue-form-builder';
|
|
3
3
|
import {type MakeOptional} from '@myparcel-dev/ts-utils';
|
|
4
4
|
import {type LogLevel} from '../services';
|
|
5
|
+
import {type RequestHeadersProvider} from '../sdk/requestHeaders';
|
|
5
6
|
import {type FORM_KEYS} from '../forms';
|
|
6
7
|
import {type FormatterObject} from '../composables';
|
|
7
8
|
import {type AdminContextObject} from './context.types';
|
|
@@ -109,6 +110,19 @@ export type AdminConfiguration = {
|
|
|
109
110
|
* Callback to generate a field id.
|
|
110
111
|
*/
|
|
111
112
|
generateFieldId(element: InteractiveElementInstance): string;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Gives extra headers for every request to the PDK backend endpoint, for
|
|
116
|
+
* example a short-lived bearer token. Called once per request.
|
|
117
|
+
*/
|
|
118
|
+
getRequestHeaders?: RequestHeadersProvider;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Read and write the active tab in window.location.hash. Turn it off when the
|
|
122
|
+
* host app routes with the hash, such as the Shopware administration.
|
|
123
|
+
* Defaults to true.
|
|
124
|
+
*/
|
|
125
|
+
useLocationHash?: boolean;
|
|
112
126
|
};
|
|
113
127
|
|
|
114
128
|
export type AdminConfigurationPreset = Omit<Partial<AdminConfiguration>, 'components'>;
|