Skip to content

Commit

Permalink
(fix) Bug where store actions don't receive correct arguments; get ri…
Browse files Browse the repository at this point in the history
…d of appState (#747)
  • Loading branch information
brandones authored Aug 11, 2023
1 parent 68c5b67 commit 33f4a16
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 148 deletions.
103 changes: 62 additions & 41 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@

- [createGlobalStore](API.md#createglobalstore)
- [createUseStore](API.md#createusestore)
- [getAppState](API.md#getappstate)
- [getGlobalStore](API.md#getglobalstore)
- [subscribeTo](API.md#subscribeto)
- [useStore](API.md#usestore)
Expand Down Expand Up @@ -561,13 +560,50 @@ ___

## Store Type Aliases

### ActionFunction

Ƭ **ActionFunction**<`T`\>: (`state`: `T`, ...`args`: `any`[]) => `Partial`<`T`\>

#### Type parameters

| Name |
| :------ |
| `T` |

#### Type declaration

▸ (`state`, ...`args`): `Partial`<`T`\>

##### Parameters

| Name | Type |
| :------ | :------ |
| `state` | `T` |
| `...args` | `any`[] |

##### Returns

`Partial`<`T`\>

#### Defined in

[packages/framework/esm-react-utils/src/useStore.ts:6](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L6)

___

### Actions

Ƭ **Actions**: `Function` \| `Record`<`string`, `Function`\>
Ƭ **Actions**<`T`\>: (`store`: `StoreApi`<`T`\>) => `Record`<`string`, [`ActionFunction`](API.md#actionfunction)<`T`\>\> \| `Record`<`string`, [`ActionFunction`](API.md#actionfunction)<`T`\>\>

#### Type parameters

| Name |
| :------ |
| `T` |

#### Defined in

[packages/framework/esm-react-utils/src/createUseStore.ts:5](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/createUseStore.ts#L5)
[packages/framework/esm-react-utils/src/useStore.ts:7](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L7)

___

Expand All @@ -581,7 +617,7 @@ ___

#### Defined in

[packages/framework/esm-react-utils/src/createUseStore.ts:6](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/createUseStore.ts#L6)
[packages/framework/esm-react-utils/src/useStore.ts:10](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L10)

___

Expand Down Expand Up @@ -2567,7 +2603,7 @@ ___

| Name | Type |
| :------ | :------ |
| `actions` | [`Actions`](API.md#actions) |
| `actions` | [`Actions`](API.md#actions)<[`ExtensionStore`](interfaces/ExtensionStore.md)\> |

#### Returns

Expand All @@ -2583,7 +2619,7 @@ ___

| Name | Type |
| :------ | :------ |
| `actions?` | [`Actions`](API.md#actions) |
| `actions?` | [`Actions`](API.md#actions)<[`ExtensionStore`](interfaces/ExtensionStore.md)\> |

#### Returns

Expand Down Expand Up @@ -3738,15 +3774,16 @@ The newly created store.

#### Defined in

[packages/framework/esm-state/src/state.ts:19](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-state/src/state.ts#L19)
[packages/framework/esm-state/src/state.ts:28](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-state/src/state.ts#L28)

___

### createUseStore

**createUseStore**<`T`\>(`store`): () => `T`(`actions`: [`Actions`](API.md#actions)) => `T` & [`BoundActions`](API.md#boundactions)(`actions?`: [`Actions`](API.md#actions)) => `T` & [`BoundActions`](API.md#boundactions)
**createUseStore**<`T`\>(`store`): () => `T`(`actions`: [`Actions`](API.md#actions)<`T`\>) => `T` & [`BoundActions`](API.md#boundactions)(`actions?`: [`Actions`](API.md#actions)<`T`\>) => `T` & [`BoundActions`](API.md#boundactions)

Avoid this; generally prefer to have clients use `useStore(yourStore)`
Whenever possible, use `useStore(yourStore)` instead. This function is for creating a
custom hook for a specific store.

#### Type parameters

Expand Down Expand Up @@ -3776,7 +3813,7 @@ Avoid this; generally prefer to have clients use `useStore(yourStore)`

| Name | Type |
| :------ | :------ |
| `actions` | [`Actions`](API.md#actions) |
| `actions` | [`Actions`](API.md#actions)<`T`\> |

##### Returns

Expand All @@ -3788,31 +3825,15 @@ Avoid this; generally prefer to have clients use `useStore(yourStore)`

| Name | Type |
| :------ | :------ |
| `actions?` | [`Actions`](API.md#actions) |
| `actions?` | [`Actions`](API.md#actions)<`T`\> |

##### Returns

`T` & [`BoundActions`](API.md#boundactions)

#### Defined in

[packages/framework/esm-react-utils/src/createUseStore.ts:33](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/createUseStore.ts#L33)

___

### getAppState

**getAppState**(): `StoreApi`<[`AppState`](interfaces/AppState.md)\>

#### Returns

`StoreApi`<[`AppState`](interfaces/AppState.md)\>

The [store](https://github.com/developit/unistore#store) named `app`.

#### Defined in

[packages/framework/esm-state/src/state.ts:86](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-state/src/state.ts#L86)
[packages/framework/esm-react-utils/src/useStore.ts:83](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L83)

___

Expand Down Expand Up @@ -3844,7 +3865,7 @@ The found or newly created store.

#### Defined in

[packages/framework/esm-state/src/state.ts:56](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-state/src/state.ts#L56)
[packages/framework/esm-state/src/state.ts:65](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-state/src/state.ts#L65)

___

Expand Down Expand Up @@ -3879,7 +3900,7 @@ ___

#### Defined in

[packages/framework/esm-state/src/state.ts:90](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-state/src/state.ts#L90)
[packages/framework/esm-state/src/state.ts:85](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-state/src/state.ts#L85)

___

Expand All @@ -3906,7 +3927,7 @@ ___

#### Defined in

[packages/framework/esm-react-utils/src/useStore.ts:33](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L33)
[packages/framework/esm-react-utils/src/useStore.ts:38](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L38)

**useStore**<`T`, `U`\>(`store`, `select`): `U`

Expand All @@ -3930,7 +3951,7 @@ ___

#### Defined in

[packages/framework/esm-react-utils/src/useStore.ts:34](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L34)
[packages/framework/esm-react-utils/src/useStore.ts:39](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L39)

**useStore**<`T`, `U`\>(`store`, `select`, `actions`): `T` & [`BoundActions`](API.md#boundactions)

Expand All @@ -3947,15 +3968,15 @@ ___
| :------ | :------ |
| `store` | `StoreApi`<`T`\> |
| `select` | `undefined` |
| `actions` | [`Actions`](API.md#actions) |
| `actions` | [`Actions`](API.md#actions)<`T`\> |

#### Returns

`T` & [`BoundActions`](API.md#boundactions)

#### Defined in

[packages/framework/esm-react-utils/src/useStore.ts:35](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L35)
[packages/framework/esm-react-utils/src/useStore.ts:40](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L40)

**useStore**<`T`, `U`\>(`store`, `select`, `actions`): `U` & [`BoundActions`](API.md#boundactions)

Expand All @@ -3972,15 +3993,15 @@ ___
| :------ | :------ |
| `store` | `StoreApi`<`T`\> |
| `select` | (`state`: `T`) => `U` |
| `actions` | [`Actions`](API.md#actions) |
| `actions` | [`Actions`](API.md#actions)<`T`\> |

#### Returns

`U` & [`BoundActions`](API.md#boundactions)

#### Defined in

[packages/framework/esm-react-utils/src/useStore.ts:40](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L40)
[packages/framework/esm-react-utils/src/useStore.ts:45](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L45)

___

Expand All @@ -3996,18 +4017,18 @@ ___

#### Parameters

| Name | Type |
| :------ | :------ |
| `store` | `StoreApi`<`T`\> |
| `actions` | [`Actions`](API.md#actions) |
| Name | Type | Description |
| :------ | :------ | :------ |
| `store` | `StoreApi`<`T`\> | A zustand store |
| `actions` | [`Actions`](API.md#actions)<`T`\> | |

#### Returns

`T` & [`BoundActions`](API.md#boundactions)

#### Defined in

[packages/framework/esm-react-utils/src/useStore.ts:61](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L61)
[packages/framework/esm-react-utils/src/useStore.ts:72](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useStore.ts#L72)

___

Expand Down
30 changes: 6 additions & 24 deletions packages/framework/esm-framework/mock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export {
validators,
validator,
} from "@openmrs/esm-config";
export { isDesktop } from "@openmrs/esm-react-utils";

window.i18next = { ...window.i18next, language: "en" };

Expand Down Expand Up @@ -284,29 +283,12 @@ export const useExtensionStore = getExtensionStore();

export const useFeatureFlag = jest.fn().mockReturnValue(true);

const defaultSelect = (x) => x;
export function useStore<T = any>(
store: StoreApi<T>,
select = defaultSelect,
actions = {}
) {
const state = select(store.getState());
return { ...state, ...actions };
}

export function useStoreWithActions<T>(
store: StoreApi<T>,
actions: Function | { [key: string]: Function }
): T & { [key: string]: (...args: any[]) => void } {
return useStore(store, defaultSelect, actions);
}

export function createUseStore<T = any>(store: StoreApi<T>) {
return (actions: Function | Record<string, Function>) => {
const state = store.getState();
return { ...state, ...actions };
};
}
export {
isDesktop,
useStore,
useStoreWithActions,
createUseStore,
} from "@openmrs/esm-react-utils";

export const usePagination = jest.fn().mockImplementation(() => ({
currentPage: 1,
Expand Down
8 changes: 4 additions & 4 deletions packages/framework/esm-globals/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ declare global {
*/
initializeSpa(config: SpaConfig): void;
/**
* Gets the API base path.
* Gets the API base path, e.g. /openmrs
*/
openmrsBase: string;
/**
* Gets the SPA base path.
* Gets the SPA base path, e.g. /openmrs/spa
*/
spaBase: string;
/**
* Gets the determined SPA environment.
* Set by the app shell. Indicates whether the app shell is running in production, development, or test mode.
*/
spaEnv: SpaEnvironment;
/**
* Gets the published SPA version.
* The build number of the app shell. Set when the app shell is built by webpack.
*/
spaVersion?: string;
/**
Expand Down
49 changes: 0 additions & 49 deletions packages/framework/esm-react-utils/src/createUseStore.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/framework/esm-react-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from "./ComponentContext";
export * from "./ConfigurableLink";
export * from "./createUseStore";
export * from "./Extension";
export * from "./ExtensionSlot";
export * from "./getLifecycle";
Expand Down
1 change: 0 additions & 1 deletion packages/framework/esm-react-utils/src/public.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export { type ExtensionData } from "./ComponentContext";
export * from "./ConfigurableLink";
export * from "./createUseStore";
export * from "./Extension";
export * from "./ExtensionSlot";
export * from "./getLifecycle";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ExtensionInternalStore,
getExtensionInternalStore,
} from "@openmrs/esm-extensions";
import { createUseStore } from "./createUseStore";
import { createUseStore } from "./useStore";

/** @internal
* @deprecated Use `useStore(getExtensionInternalStore())`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @module @category Extension */
import { ExtensionStore, getExtensionStore } from "@openmrs/esm-extensions";
import { createUseStore } from "./createUseStore";
import { createUseStore } from "./useStore";

export const useExtensionStore = createUseStore<ExtensionStore>(
getExtensionStore()
Expand Down
Loading

0 comments on commit 33f4a16

Please sign in to comment.