diff --git a/mailpoet/assets/js/src/automation/editor/store/actions.ts b/mailpoet/assets/js/src/automation/editor/store/actions.ts new file mode 100644 index 0000000000..5577c32af1 --- /dev/null +++ b/mailpoet/assets/js/src/automation/editor/store/actions.ts @@ -0,0 +1,8 @@ +import { store as preferencesStore } from '@wordpress/preferences'; +import { storeName } from './constants'; +import { Feature } from './types'; + +export const toggleFeature = + (feature: Feature) => + ({ registry }) => + registry.dispatch(preferencesStore).toggle(storeName, feature); diff --git a/mailpoet/assets/js/src/automation/editor/store/constants.ts b/mailpoet/assets/js/src/automation/editor/store/constants.ts new file mode 100644 index 0000000000..cbf753e12b --- /dev/null +++ b/mailpoet/assets/js/src/automation/editor/store/constants.ts @@ -0,0 +1 @@ +export const storeName = 'mailpoet/automation-editor'; diff --git a/mailpoet/assets/js/src/automation/editor/store/index.ts b/mailpoet/assets/js/src/automation/editor/store/index.ts new file mode 100644 index 0000000000..35c50dff77 --- /dev/null +++ b/mailpoet/assets/js/src/automation/editor/store/index.ts @@ -0,0 +1,29 @@ +import { createReduxStore, register, StoreDescriptor } from '@wordpress/data'; +import * as actions from './actions'; +import { storeName } from './constants'; +import { reducer } from './reducer'; +import * as selectors from './selectors'; +import { State } from './types'; +import { OmitFirstArgs } from '../../../types'; + +export * from './constants'; + +type StoreType = Omit & { + name: typeof storeName; +}; + +export const store = createReduxStore(storeName, { + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- the "Action" type is missing thunks with "dispatch" + actions: actions as any, + selectors, + reducer, +}) as StoreType; + +type StoreKey = typeof storeName | StoreType; + +declare module '@wordpress/data' { + function select(key: StoreKey): OmitFirstArgs; + function dispatch(key: StoreKey): typeof actions; +} + +register(store); diff --git a/mailpoet/assets/js/src/automation/editor/store/reducer.ts b/mailpoet/assets/js/src/automation/editor/store/reducer.ts new file mode 100644 index 0000000000..f2a427f533 --- /dev/null +++ b/mailpoet/assets/js/src/automation/editor/store/reducer.ts @@ -0,0 +1,9 @@ +import { Action } from '@wordpress/data'; +import { State } from './types'; + +export function reducer(state: State, action: Action): State { + switch (action.type) { + default: + return state; + } +} diff --git a/mailpoet/assets/js/src/automation/editor/store/selectors.ts b/mailpoet/assets/js/src/automation/editor/store/selectors.ts new file mode 100644 index 0000000000..623fd7a577 --- /dev/null +++ b/mailpoet/assets/js/src/automation/editor/store/selectors.ts @@ -0,0 +1,10 @@ +import { createRegistrySelector } from '@wordpress/data'; +import { store as preferencesStore } from '@wordpress/preferences'; +import { storeName } from './constants'; +import { Feature } from './types'; + +export const isFeatureActive = createRegistrySelector( + (select) => + (_, feature: Feature): boolean => + select(preferencesStore).get(storeName, feature), +); diff --git a/mailpoet/assets/js/src/automation/editor/store/types.ts b/mailpoet/assets/js/src/automation/editor/store/types.ts new file mode 100644 index 0000000000..d50e6723f7 --- /dev/null +++ b/mailpoet/assets/js/src/automation/editor/store/types.ts @@ -0,0 +1 @@ +export type Feature = 'fullscreenMode'; diff --git a/mailpoet/assets/js/src/types/index.ts b/mailpoet/assets/js/src/types/index.ts index 2f15aecd3a..e0faa0c496 100644 --- a/mailpoet/assets/js/src/types/index.ts +++ b/mailpoet/assets/js/src/types/index.ts @@ -1,7 +1,12 @@ import { ColorPalette, FontSizePicker } from '@wordpress/components'; +import { store as preferencesStore } from '@wordpress/preferences'; import './wordpress_modules'; +/* eslint-disable @typescript-eslint/no-explicit-any -- some general types in this file need to use "any" */ +/* eslint-disable @typescript-eslint/naming-convention -- we have no control over 3rd-party naming conventions */ +/* eslint-disable no-underscore-dangle -- we have no control over 3rd-party naming conventions */ + export * from '../segments/dynamic/types'; // Inspired by: https://neliosoftware.com/blog/adding-typescript-to-wordpress-data-stores/ @@ -17,7 +22,6 @@ export type OmitFirstArgs = { }; declare module '@wordpress/block-editor' { - // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/naming-convention,no-underscore-dangle export const __experimentalLibrary: any; // types for 'useSetting' are missing in @types/wordpress__block-editor @@ -36,3 +40,18 @@ declare module '@wordpress/block-editor' { }[]; } } + +declare module '@wordpress/data' { + type PreferencesStore = 'core/preferences' | typeof preferencesStore; + + // there are no @types/wordpress__preferences yet + function select(key: PreferencesStore): { + get: (scope: string, name: string) => T; + }; + + // types for "createRegistrySelector" are not correct + export function createRegistrySelector< + S extends typeof select, + T extends (state: any, ...args: any) => any, + >(registrySelector: (select: S) => T): T; +} diff --git a/mailpoet/assets/js/src/types/wordpress_modules.ts b/mailpoet/assets/js/src/types/wordpress_modules.ts index 0f36e9307a..cda104fc83 100644 --- a/mailpoet/assets/js/src/types/wordpress_modules.ts +++ b/mailpoet/assets/js/src/types/wordpress_modules.ts @@ -4,3 +4,10 @@ declare module '@wordpress/interface' { export const InterfaceSkeleton: any; } + +// there are no @types/wordpress__preferences yet +declare module '@wordpress/preferences' { + import { StoreDescriptor } from '@wordpress/data'; + + export const store: { name: 'core/preferences' } & StoreDescriptor; +}