Skip to content

Commit ece1354

Browse files
Merge pull request #19 from gritzkoo/master
Adjust build steps to complience to npm
2 parents 33161eb + d6bb139 commit ece1354

File tree

13 files changed

+8794
-3144
lines changed

13 files changed

+8794
-3144
lines changed

package-lock.json

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

package.json

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
{
22
"name": "@penseapp/uselocalstoragereducer",
3-
"version": "1.1.8",
3+
"version": "2.0.0",
44
"description": "A react hooks to use useReducer with localStorage persist",
5-
"main": "dist/index.cjs.js",
6-
"module": "dist/index.esm.js",
5+
"main": "dist/index.js",
6+
"module": "dist/index.js",
7+
"types": "dist/index.d.ts",
8+
"private": false,
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/penseapp/useLocalStorageReducer.git"
12+
},
13+
"homepage": "https://penseapp-uselocalstoragereduce.web.app",
14+
"keywords": [
15+
"react",
16+
"hooks",
17+
"localStorage",
18+
"useReducer"
19+
],
20+
"files": [
21+
"dist/**/*"
22+
],
23+
"author": "@joaomantovani",
24+
"license": "MIT",
725
"scripts": {
826
"test": "echo \"Error: no test specified\" && exit 1",
927
"lint": "FORCE_COLOR=true eslint ./**/*.{ts,tsx} --max-warnings=0 --format=codeframe",
1028
"prettier": "FORCE_COLOR=true prettier --write './**/*.{jsx,tsx,ts,js,md}' && prettier --write './README.md'",
1129
"i-all": "npm i && cd playground && npm i",
1230
"build": "rollup -c",
31+
"build2": "tsc",
1332
"build-watch": "rollup -c -w",
14-
"build:all": "npm-run-all build build:playground",
33+
"build:all": "npm-run-all build build:playground build2",
1534
"start-playground": "cd playground && npm run start",
1635
"build:playground": "cd playground && npm run build",
1736
"install:playground": "cd playground && npm i-all",
@@ -20,19 +39,6 @@
2039
"clean": "rm -rf ./node_modules && rm package-lock.json",
2140
"clean:all": "npm-run-all --parallel clean clean:playground"
2241
},
23-
"repository": {
24-
"type": "git",
25-
"url": "git+https://github.com/penseapp/useLocalStorageReducer.git"
26-
},
27-
"homepage": "https://penseapp-uselocalstoragereduce.web.app",
28-
"keywords": [
29-
"react",
30-
"hooks",
31-
"localStorage",
32-
"useReducer"
33-
],
34-
"author": "@joaomantovani",
35-
"license": "MIT",
3642
"devDependencies": {
3743
"@commitlint/cli": "^11.0.0",
3844
"@commitlint/config-conventional": "^11.0.0",
@@ -54,17 +60,13 @@
5460
"rollup-plugin-delete": "^2.0.0",
5561
"rollup-plugin-prettier": "^2.1.0",
5662
"rollup-plugin-typescript2": "^0.30.0",
57-
"rollup-plugin-uglify": "^6.0.4",
63+
"rollup-plugin-uglify": "^5.0.2",
5864
"typescript": "^4.2.2"
5965
},
6066
"peerDependencies": {
6167
"react": "^17.0.1",
6268
"react-dom": "^17.0.1"
6369
},
64-
"files": [
65-
"dist",
66-
"package.json"
67-
],
6870
"dependencies": {
6971
"expired-storage": "^1.0.2"
7072
},
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
export { useLocalStorageReducer } from "./useLocalStorageReducer";
2-
//# sourceMappingURL=index.d.ts.map
1+
import { Reducer, ReducerState, Dispatch, ReducerAction } from "react";
2+
export declare function useLocalStorageReducer<I, A>(
3+
key: string,
4+
reducer: Reducer<I, A>,
5+
initialState: I,
6+
expire?: number | boolean
7+
): [ReducerState<Reducer<I, A>>, Dispatch<ReducerAction<Reducer<I, A>>>];
8+
//# sourceMappingURL=index.d.ts.map

playground/src/component-lib/index.d.ts.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.

playground/src/component-lib/useLocalStorageReducer/index.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

playground/src/component-lib/useLocalStorageReducer/index.d.ts.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

playground/src/component-lib/useLocalStorageReducer/useLocalStorageReducer.d.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

playground/src/component-lib/useLocalStorageReducer/useLocalStorageReducer.d.ts.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/index.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
1-
export { useLocalStorageReducer } from "./useLocalStorageReducer";
1+
import {
2+
useEffect,
3+
useReducer,
4+
Reducer,
5+
ReducerState,
6+
Dispatch,
7+
ReducerAction,
8+
} from "react";
9+
import ExpiredStorage from "expired-storage";
10+
11+
export function useLocalStorageReducer<I, A>(
12+
key: string,
13+
reducer: Reducer<I, A>,
14+
initialState: I,
15+
expire: number | boolean = 60 * 30
16+
): [ReducerState<Reducer<I, A>>, Dispatch<ReducerAction<Reducer<I, A>>>] {
17+
const [state, dispatch] = useReducer(
18+
reducer,
19+
initialState,
20+
(initialState) => {
21+
try {
22+
const expiredStorage = new ExpiredStorage();
23+
24+
// Get from local storage by key
25+
const item = expiredStorage.getItem(key);
26+
27+
const parsedInitialValue = JSON.stringify(initialState);
28+
29+
// Parse stored json or if none return initialValue
30+
return item ? JSON.parse(item) : JSON.parse(parsedInitialValue);
31+
} catch (error) {
32+
// If error also return initialValue
33+
console.error(error);
34+
return initialState;
35+
}
36+
}
37+
);
38+
39+
useEffect(() => {
40+
try {
41+
const expiredStorage = new ExpiredStorage();
42+
43+
expire !== false && typeof expire === "number"
44+
? expiredStorage.setItem(key, JSON.stringify(state), expire)
45+
: window.localStorage.setItem(key, JSON.stringify(state));
46+
} catch (error) {
47+
// TODO: A more advanced implementation would handle the error case
48+
console.error(error);
49+
}
50+
51+
localStorage.setItem(key, JSON.stringify(state));
52+
}, [expire, key, state]);
53+
54+
return [state, dispatch];
55+
}

src/useLocalStorageReducer/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)