Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Store #1

Merged
merged 18 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"ignore": [
"src/helpers/**",
"src/pages/**",
"src/utils/testing/**",
"src/routes/**",
"src/store/*.js",
"src/App.js",
"src/App.test.js",
"src/index.js",
"src/initialize.js",
"src/setupTests.js",
"src/*.css"
]
}
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
REACT_APP_API_RETRY_TIMEOUT=1500
REACT_APP_API_RETRY_COUNT=10
REACT_APP_API_DEFAULT_MAX_AGE=60000
3 changes: 3 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
REACT_APP_API_RETRY_TIMEOUT=10
REACT_APP_API_RETRY_COUNT=10
REACT_APP_API_DEFAULT_MAX_AGE=60000
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/public
/src/helpers
/src/utils
/src/utils/testing
/src/App.js
/src/App.test.js
/src/App.css
Expand Down
55 changes: 43 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ In the `sagas.js` file:
```js
import { apiSaga, initializeAPIStore } from "api-calls-store/src/package-index";

const clockCount = 15;
initializeAPIStore({
getAPI,
clockCount, // clockCount is the amount of times an API_CALL will be skipped - default is 10
});

export default function* rootSaga() {
Expand All @@ -44,31 +46,60 @@ export default function* rootSaga() {
}
```

### Add the dispatch clock

In the `App.js` add this `useEffect` to dispatch the clock every Xms.
This clock will dispatch an action in the saga every Xms and that action will call the API.

```js
useEffect(() => {
const interval = setInterval(() => {
dispatch({ type: "API_DISPATCH_CLOCK" });
}, 1000);
return () => clearInterval(interval);
}, [dispatch]);
```

# Actions

```js
/* API */
/**
* {type: API_CALL, apiName: "apy", args: [], method: method || "GET" }
*/
export const API_CALL = "API_CALL";
export const API_CALL_SUCCESS = "API_CALL_SUCCESS";
export const API_CALL_FAIL = "API_CALL_FAIL";

/*
* Sample actions
* {type: API_CALL, apiName: "apy", args: [], method: method || "GET" }
/**
* {type: API_CALL_SUCCESS, call_key: "0x...", value: 221, code: 200, timestamp: new Date().getTime() }
*/
export const API_CALL_SUCCESS = "API_CALL_SUCCESS";

/**
* {type: API_CALL_FAIL, call_key: "0x...", value: 221, code: 200, timestamp: new Date().getTime() }
*/
export const API_CALL_FAIL = "API_CALL_FAIL";

/**
* {type: API_ADD_SUBSCRIPTION, key: "positions", componentApiCalls: [{ apiName: "apy", args: [etkAddress] }] }
*/
export const API_ADD_SUBSCRIPTION = "API_ADD_SUBSCRIPTION";
export const API_REMOVE_SUBSCRIPTION = "API_REMOVE_SUBSCRIPTION";

/*
* Sample actions
* {type: API_ADD_SUBSCRIPTION, key: "positions", componentApiCalls: [{ apiName: "apy", args: [etkAddress] }] }
/**
* {type: API_REMOVE_SUBSCRIPTION, key: "positions" }
*/
export const API_REMOVE_SUBSCRIPTION = "API_REMOVE_SUBSCRIPTION";

/**
* API_DISPATCH_CLOCK -> This clock will dispatch an action in the saga every Xms and that action will call the API.
*/
export const API_DISPATCH_CLOCK = "API_DISPATCH_CLOCK";
export const API_SET_NEXT_CLOCK = "API_SET_NEXT_CLOCK";
export const API_INCREASE_COUNT = "API_INCREASE_COUNT";

/**
* API_SUBSCRIPTION_INCREASE_CLOCK -> This action sets when to call each subscription again
*/
export const API_SUBSCRIPTION_INCREASE_CLOCK = "API_SUBSCRIPTION_INCREASE_CLOCK";

/**
* API_INCREASE_CLOCK -> Increase the general clock to check if the saga should call the API again
*/
nicodelatorre7 marked this conversation as resolved.
Show resolved Hide resolved
export const API_INCREASE_CLOCK = "API_INCREASE_CLOCK";
```
199 changes: 196 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading