Skip to content

Commit 0f7f6e2

Browse files
authored
Merge pull request #4 from react-gx/dev
Version 1.2.1
2 parents 5224154 + 32dc50a commit 0f7f6e2

27 files changed

+613
-581
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
[![GitHub license](https://img.shields.io/github/license/react-gx/gx)](https://github.com/react-gx/gx/blob/main/LICENSE)
88

99

10-
![logo](./assets/logo.png)
10+
![logo](https://lh4.googleusercontent.com/k2V9Oh-tfABeDjwovtMUqE-lt6cULH0c1EFgb-XNTFh1lt5DVGTGhl3Ty3fMF3xhCBY=w2400)
11+
1112

1213
This library aims to provide you an `easy` and `fast` way to set up and manage the global state of your **`react`** application.
1314

@@ -123,7 +124,7 @@ For structuring your code very well you have to follow these steps.
123124

124125
Here is the result.
125126

126-
![structure](./assets/structure.png)
127+
![structure](https://lh3.googleusercontent.com/_z_JTStNFHyXTmjz4GrcphAN6BC_CeKYxN1zwyxWGC-ujpIcVTqthesXT6Lfe8b4t1M=w2400)
127128

128129
### Second step: Creating your signals.
129130

@@ -184,19 +185,19 @@ export default App;
184185

185186
### Fifth step: Using your signals.
186187

187-
Create a component called `Counter` inside the Counter.js file. Then import two hooks from `gx` called `useSignal` and `useAction` like follow.
188+
Create a component called `Counter` inside the Counter.js file. Then import two hooks from `gx` called `useSignal` and `useActions` like follow.
188189

189190

190191
```js
191192
import React from "react";
192-
import { useSignal, useAction } from "@dilane3/gx";
193+
import { useSignal, useActions } from "@dilane3/gx";
193194

194195
function Counter() {
195196
// State
196197
const counter = useSignal("counter");
197198

198199
// Actions
199-
const { increment, decrement } = useAction("counter");
200+
const { increment, decrement } = useActions("counter");
200201

201202
return (
202203
<div>
@@ -278,30 +279,29 @@ This hook takes the name of the signal as a parameter and returns the state cont
278279
const counter = useSignal("counter");
279280
```
280281

281-
### `useAction`
282+
### `useActions`
282283

283284
This hook takes the name of the signal as a the first parameter and returns an object that contains all the actions of this signal.
284285

285286
```js
286-
const { increment, decrement } = useAction("counter");
287+
const { increment, decrement } = useActions("counter");
287288
```
288289

289-
**`New in version 1.1.0`**
290-
291-
Note that, the `useAction` hook can accept a second parameter which is the list of actions that you want to use. If you don't specify the second parameter, all the actions of the signal will be returned.
290+
### `useAction`
292291

293-
There is another thing that you have to know.
294-
If you provide only one action as a second parameter, the hook will return only the action itself and not an object that contains the action.
292+
This hook takes the name of the signal as the first parameter and the name of the action as the second one and then return that action.
295293

296294
```js
297295
const increment = useAction("counter", "increment");
298296
```
299297

300-
And if you provide more than one action, the hook will return an object that contains all the actions provided.
298+
See more on the [documentation](https://gx.dilane3.com/docs/guide/hooks/useAction)
301299

302-
```js
303-
const { increment, decrement } = useAction("counter", "increment", "decrement");
304-
```
300+
## TypeScript Support
301+
302+
`GX` support TypeScript, so that you can use it directly into your application.
303+
304+
See how to integrate it on the [documentation](https://gx.dilane3.com/docs/typescript) website
305305

306306
## License
307307

assets/logo.png

-51.1 KB
Binary file not shown.

assets/structure.png

-28.6 KB
Binary file not shown.

dist/hooks/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export type Action = {
1+
export type Actions = {
22
[key: string]: (payload: any) => void;
33
};

dist/hooks/useAction.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
declare const useAction: (signalName: string, ...actions: string[]) => ((payload: any) => void) | {
2-
[key: string]: (payload: any) => void;
3-
};
1+
declare const useAction: (signalName: string, action: string) => (payload: any) => void;
42
export default useAction;

dist/hooks/useAction.js

Lines changed: 8 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/hooks/useAction.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/hooks/useActions.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Actions } from "./types";
2+
declare const useActions: (signalName: string, ...actions: string[]) => Actions;
3+
export default useActions;

dist/hooks/useActions.js

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/hooks/useActions.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/hooks/useSignal.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
declare const useSignal: (signalName: string) => any;
1+
declare const useSignal: <T = any>(signalName: string) => T;
22
export default useSignal;

dist/hooks/useSignal.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import createSignal from "./helpers/createSignal";
22
import createStore from "./helpers/createStore";
33
import GXProvider from "./providers";
44
import useAction from "./hooks/useAction";
5+
import useActions from "./hooks/useActions";
56
import useSignal from "./hooks/useSignal";
67
export default GXProvider;
7-
export { createSignal, createStore, useAction, useSignal, };
8+
export { createSignal, createStore, useAction, useActions, useSignal, };

dist/index.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dilane3/gx",
3-
"version": "1.1.1",
3+
"version": "1.2.1",
44
"private": false,
55
"license": "MIT",
66
"main": "dist/index.js",
@@ -23,6 +23,7 @@
2323
"@types/jest": "^27.0.1",
2424
"@types/node": "^16.7.13",
2525
"@types/react": "^18.0.0",
26+
"react-dom": "^18.0.0",
2627
"react-scripts": "^5.0.1",
2728
"typescript": "^4.4.2"
2829
},

src/hooks/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export type Action = {
1+
export type Actions = {
22
[key: string]: (payload: any) => void
33
}

src/hooks/useAction.ts

Lines changed: 8 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,15 @@
1-
import { useContext, useState, useEffect } from "react";
2-
import GXContext from "../contexts";
3-
import { GXActionType } from "../contexts/types";
1+
import useActions from "./useActions";
42

5-
const useAction = (signalName: string, ...actions: string[]) => {
6-
// Get Global Context
7-
const { signals, dispatch } = useContext(GXContext);
3+
const useAction = (signalName: string, action: string) => {
4+
if (!signalName || typeof signalName !== "string")
5+
throw new Error("Provide a signalName as a first argument of useAction");
86

9-
// Some handlers
7+
if (!action || typeof action !== "string")
8+
throw new Error("Provide an action as second argument of useAction");
109

11-
/**
12-
* Get actions of a signal
13-
* @param signalName
14-
* @returns
15-
*/
16-
const handleGetActions = (signalName: string) => {
17-
const signal = signals.find((signal) => signal.name === signalName);
10+
const actions = useActions(signalName, action);
1811

19-
if (signal) {
20-
if (!actions || actions.length === 0) return signal.actions;
21-
22-
const filteredActions : GXActionType<any>[] = [];
23-
24-
for (let action of actions) {
25-
const actionName = `${signalName}/${action}`;
26-
27-
const retrievedAction = signal.actions.find((act) => act.type === actionName);
28-
29-
if (retrievedAction) filteredActions.push(retrievedAction);
30-
else throw new Error(`Action ${actionName} not found`);
31-
}
32-
33-
return filteredActions;
34-
} else throw new Error(`Signal ${signalName} not found`);
35-
};
36-
37-
const handleFormatActions = () => {
38-
// Get actions
39-
const nonFormattedActions = handleGetActions(signalName);
40-
41-
// Get number of actions
42-
const numberOfActions = nonFormattedActions.length;
43-
44-
// Check if actions are only one
45-
if (numberOfActions === 1) {
46-
const action = nonFormattedActions[0];
47-
48-
// Return action as a function
49-
return (payload: any) => {
50-
dispatch({
51-
type: action.type,
52-
payload,
53-
});
54-
};
55-
}
56-
57-
// If actions are more than one
58-
59-
// Formatted actions
60-
const formattedActions: { [key: string]: (payload: any) => void } = {};
61-
62-
for (const action of nonFormattedActions) {
63-
// Get action name
64-
const actionName = action.type.split("/")[1];
65-
66-
formattedActions[actionName] = (payload: any) => {
67-
dispatch({
68-
type: action.type,
69-
payload,
70-
});
71-
};
72-
}
73-
74-
return formattedActions;
75-
};
76-
77-
return handleFormatActions();
12+
return Object.values(actions)[0];
7813
};
7914

8015
export default useAction;

0 commit comments

Comments
 (0)