pocketbase-react 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -1
- package/README.md +15 -0
- package/dist/pocketbase-react.js +464 -0
- package/dist/pocketbase-react.min.js +1 -0
- package/{dist/esm → es}/context/Pocketbase.d.ts +1 -3
- package/es/context/Pocketbase.js +27 -0
- package/{dist/cjs → es}/context/client.d.ts +1 -1
- package/es/context/client.js +8 -0
- package/{dist/cjs → es}/context/content.d.ts +0 -0
- package/es/context/content.js +63 -0
- package/{dist/cjs → es}/context/index.d.ts +0 -0
- package/es/context/index.js +3 -0
- package/{dist/cjs → es}/hooks/index.d.ts +0 -0
- package/es/hooks/index.js +2 -0
- package/{dist/cjs → es}/hooks/useAppContent.d.ts +1 -1
- package/es/hooks/useAppContent.js +25 -0
- package/es/hooks/useClientContext.d.ts +2 -0
- package/es/hooks/useClientContext.js +7 -0
- package/{dist/cjs → es}/index.d.ts +0 -0
- package/{dist/esm/index.d.ts → es/index.js} +1 -1
- package/{dist/cjs → es}/interfaces/Record.d.ts +1 -1
- package/es/interfaces/Record.js +1 -0
- package/{dist/cjs → es}/interfaces/index.d.ts +0 -0
- package/es/interfaces/index.js +1 -0
- package/es/store/actions/index.d.ts +2 -0
- package/es/store/actions/index.js +2 -0
- package/es/store/actions/records.d.ts +9 -0
- package/es/store/actions/records.js +32 -0
- package/{dist/cjs → es}/store/index.d.ts +0 -0
- package/{dist/esm/store/index.d.ts → es/store/index.js} +1 -1
- package/{dist/cjs → es}/store/reducers/index.d.ts +2 -2
- package/es/store/reducers/index.js +5 -0
- package/{dist/cjs → es}/store/reducers/records.d.ts +2 -3
- package/es/store/reducers/records.js +56 -0
- package/{dist/cjs → es}/store/store.d.ts +0 -0
- package/es/store/store.js +44 -0
- package/{dist/cjs → es}/store/types/index.d.ts +0 -0
- package/es/store/types/index.js +6 -0
- package/lib/context/Pocketbase.js +35 -0
- package/lib/context/client.js +15 -0
- package/lib/context/content.js +70 -0
- package/lib/context/index.js +36 -0
- package/lib/hooks/index.js +25 -0
- package/lib/hooks/useAppContent.js +31 -0
- package/lib/hooks/useClientContext.js +11 -0
- package/lib/index.js +47 -0
- package/lib/interfaces/Record.js +1 -0
- package/lib/interfaces/index.js +14 -0
- package/lib/store/actions/index.js +8 -0
- package/lib/store/actions/records.js +43 -0
- package/lib/store/index.js +47 -0
- package/lib/store/reducers/index.js +10 -0
- package/lib/store/reducers/records.js +64 -0
- package/lib/store/store.js +53 -0
- package/lib/store/types/index.js +16 -0
- package/package.json +94 -35
- package/src/context/Pocketbase.tsx +42 -0
- package/src/context/client.tsx +14 -0
- package/src/context/content.tsx +88 -0
- package/{dist/esm/context/index.d.ts → src/context/index.ts} +0 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useAppContent.ts +28 -0
- package/src/hooks/useClientContext.ts +11 -0
- package/src/index.ts +4 -0
- package/src/interfaces/Record.ts +10 -0
- package/src/interfaces/index.ts +1 -0
- package/src/store/actions/index.tsx +3 -0
- package/src/store/actions/records.tsx +54 -0
- package/src/store/index.ts +4 -0
- package/src/store/reducers/index.tsx +12 -0
- package/src/store/reducers/records.tsx +78 -0
- package/src/store/store.tsx +65 -0
- package/src/store/types/index.ts +14 -0
- package/dist/cjs/EventSource.d.ts +0 -1
- package/dist/cjs/context/Pocketbase.d.ts +0 -15
- package/dist/cjs/hooks/useClientContext.d.ts +0 -2
- package/dist/cjs/index.js +0 -3478
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/store/actions/index.d.ts +0 -2
- package/dist/cjs/store/actions/records.d.ts +0 -11
- package/dist/esm/EventSource.d.ts +0 -1
- package/dist/esm/context/client.d.ts +0 -8
- package/dist/esm/context/content.d.ts +0 -16
- package/dist/esm/hooks/index.d.ts +0 -2
- package/dist/esm/hooks/useAppContent.d.ts +0 -12
- package/dist/esm/hooks/useClientContext.d.ts +0 -2
- package/dist/esm/index.js +0 -3433
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/interfaces/Record.d.ts +0 -10
- package/dist/esm/interfaces/index.d.ts +0 -1
- package/dist/esm/store/actions/index.d.ts +0 -2
- package/dist/esm/store/actions/records.d.ts +0 -11
- package/dist/esm/store/reducers/index.d.ts +0 -8
- package/dist/esm/store/reducers/records.d.ts +0 -12
- package/dist/esm/store/store.d.ts +0 -27
- package/dist/esm/store/types/index.d.ts +0 -7
- package/dist/index.d.ts +0 -128
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
var _actions = require("./actions");
|
|
5
|
+
Object.keys(_actions).forEach(function (key) {
|
|
6
|
+
if (key === "default" || key === "__esModule") return;
|
|
7
|
+
if (key in exports && exports[key] === _actions[key]) return;
|
|
8
|
+
Object.defineProperty(exports, key, {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () {
|
|
11
|
+
return _actions[key];
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
var _reducers = require("./reducers");
|
|
16
|
+
Object.keys(_reducers).forEach(function (key) {
|
|
17
|
+
if (key === "default" || key === "__esModule") return;
|
|
18
|
+
if (key in exports && exports[key] === _reducers[key]) return;
|
|
19
|
+
Object.defineProperty(exports, key, {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () {
|
|
22
|
+
return _reducers[key];
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
var _types = require("./types");
|
|
27
|
+
Object.keys(_types).forEach(function (key) {
|
|
28
|
+
if (key === "default" || key === "__esModule") return;
|
|
29
|
+
if (key in exports && exports[key] === _types[key]) return;
|
|
30
|
+
Object.defineProperty(exports, key, {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () {
|
|
33
|
+
return _types[key];
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
var _store = require("./store");
|
|
38
|
+
Object.keys(_store).forEach(function (key) {
|
|
39
|
+
if (key === "default" || key === "__esModule") return;
|
|
40
|
+
if (key in exports && exports[key] === _store[key]) return;
|
|
41
|
+
Object.defineProperty(exports, key, {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
get: function () {
|
|
44
|
+
return _store[key];
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.appReducer = void 0;
|
|
5
|
+
var _redux = require("redux");
|
|
6
|
+
var _records = require("./records");
|
|
7
|
+
const appReducer = (0, _redux.combineReducers)({
|
|
8
|
+
records: _records.records
|
|
9
|
+
});
|
|
10
|
+
exports.appReducer = appReducer;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.records = void 0;
|
|
6
|
+
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
7
|
+
var ReduxType = _interopRequireWildcard(require("../types"));
|
|
8
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
9
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
10
|
+
function appendRecord(record, records) {
|
|
11
|
+
return [...records, record];
|
|
12
|
+
}
|
|
13
|
+
function appendRecords(recordsToAppend, records) {
|
|
14
|
+
return [...records, ...recordsToAppend];
|
|
15
|
+
}
|
|
16
|
+
function updateRecord(record, records) {
|
|
17
|
+
return records.map(r => {
|
|
18
|
+
if (r.id === record.id) {
|
|
19
|
+
return record;
|
|
20
|
+
}
|
|
21
|
+
return r;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function deleteRecord(record, records) {
|
|
25
|
+
return records.filter(r => r.id !== record.id);
|
|
26
|
+
}
|
|
27
|
+
function deleteRecords(recordsToDelete, records) {
|
|
28
|
+
return records.filter(r => !recordsToDelete.includes(r));
|
|
29
|
+
}
|
|
30
|
+
const records = (state = {}, action) => {
|
|
31
|
+
var _state$action$key;
|
|
32
|
+
const list = (_state$action$key = state[action.key]) != null ? _state$action$key : [];
|
|
33
|
+
switch (action.type) {
|
|
34
|
+
case ReduxType.SET_RECORDS:
|
|
35
|
+
if (Array.isArray(action.payload)) {
|
|
36
|
+
return (0, _extends2.default)({}, state, {
|
|
37
|
+
[action.key]: action.payload
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
case ReduxType.ADD_RECORD:
|
|
41
|
+
return (0, _extends2.default)({}, state, {
|
|
42
|
+
[action.key]: appendRecord(action.payload, list)
|
|
43
|
+
});
|
|
44
|
+
case ReduxType.ADD_RECORDS:
|
|
45
|
+
return (0, _extends2.default)({}, state, {
|
|
46
|
+
[action.key]: appendRecords(action.payload, list)
|
|
47
|
+
});
|
|
48
|
+
case ReduxType.DELETE_RECORD:
|
|
49
|
+
return (0, _extends2.default)({}, state, {
|
|
50
|
+
[action.key]: deleteRecord(action.payload, list)
|
|
51
|
+
});
|
|
52
|
+
case ReduxType.DELETE_RECORDS:
|
|
53
|
+
return (0, _extends2.default)({}, state, {
|
|
54
|
+
[action.key]: deleteRecords(action.payload, list)
|
|
55
|
+
});
|
|
56
|
+
case ReduxType.UPDATE_RECORD:
|
|
57
|
+
return (0, _extends2.default)({}, state, {
|
|
58
|
+
[action.key]: updateRecord(action.payload, list)
|
|
59
|
+
});
|
|
60
|
+
default:
|
|
61
|
+
return state;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
exports.records = records;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.useAppSelector = exports.useAppDispatch = exports.store = exports.persistor = exports.persistConfig = void 0;
|
|
6
|
+
var _toolkit = require("@reduxjs/toolkit");
|
|
7
|
+
var _reduxPersist = require("redux-persist");
|
|
8
|
+
var _reactRedux = require("react-redux");
|
|
9
|
+
var _reducers = require("./reducers");
|
|
10
|
+
var _reduxThunk = _interopRequireDefault(require("redux-thunk"));
|
|
11
|
+
var _asyncStorage = _interopRequireDefault(require("@react-native-async-storage/async-storage"));
|
|
12
|
+
const CustomStorage = {
|
|
13
|
+
getItem: async (_key, ..._args) => {
|
|
14
|
+
if (typeof document !== 'undefined') {
|
|
15
|
+
return localStorage.getItem(_key);
|
|
16
|
+
} else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
|
17
|
+
return await _asyncStorage.default.getItem(_key);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
setItem: async (_key, _value, ..._args) => {
|
|
21
|
+
if (typeof document !== 'undefined') {
|
|
22
|
+
return localStorage.setItem(_key, _value);
|
|
23
|
+
} else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
|
24
|
+
return await _asyncStorage.default.setItem(_key, _value);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
removeItem: async (_key, ..._args) => {
|
|
28
|
+
if (typeof document !== 'undefined') {
|
|
29
|
+
return localStorage.removeItem(_key);
|
|
30
|
+
} else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
|
31
|
+
return await _asyncStorage.default.removeItem(_key);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const persistConfig = {
|
|
36
|
+
key: 'root',
|
|
37
|
+
storage: CustomStorage
|
|
38
|
+
};
|
|
39
|
+
exports.persistConfig = persistConfig;
|
|
40
|
+
const reducer = (0, _toolkit.combineReducers)({
|
|
41
|
+
reducer: (0, _reduxPersist.persistReducer)(persistConfig, _reducers.appReducer)
|
|
42
|
+
});
|
|
43
|
+
const store = (0, _toolkit.configureStore)({
|
|
44
|
+
reducer,
|
|
45
|
+
middleware: [_reduxThunk.default]
|
|
46
|
+
});
|
|
47
|
+
exports.store = store;
|
|
48
|
+
const useAppDispatch = store.dispatch;
|
|
49
|
+
exports.useAppDispatch = useAppDispatch;
|
|
50
|
+
const useAppSelector = _reactRedux.useSelector;
|
|
51
|
+
exports.useAppSelector = useAppSelector;
|
|
52
|
+
const persistor = (0, _reduxPersist.persistStore)(store);
|
|
53
|
+
exports.persistor = persistor;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.UPDATE_RECORD = exports.SET_RECORDS = exports.DELETE_RECORDS = exports.DELETE_RECORD = exports.ADD_RECORDS = exports.ADD_RECORD = void 0;
|
|
5
|
+
const SET_RECORDS = 'SET_RECORDS';
|
|
6
|
+
exports.SET_RECORDS = SET_RECORDS;
|
|
7
|
+
const ADD_RECORD = 'ADD_RECORD';
|
|
8
|
+
exports.ADD_RECORD = ADD_RECORD;
|
|
9
|
+
const ADD_RECORDS = 'ADD_RECORDS';
|
|
10
|
+
exports.ADD_RECORDS = ADD_RECORDS;
|
|
11
|
+
const UPDATE_RECORD = 'UPDATE_RECORD';
|
|
12
|
+
exports.UPDATE_RECORD = UPDATE_RECORD;
|
|
13
|
+
const DELETE_RECORD = 'DELETE_RECORD';
|
|
14
|
+
exports.DELETE_RECORD = DELETE_RECORD;
|
|
15
|
+
const DELETE_RECORDS = 'DELETE_RECORDS';
|
|
16
|
+
exports.DELETE_RECORDS = DELETE_RECORDS;
|
package/package.json
CHANGED
|
@@ -1,19 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pocketbase-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Unofficial React SDK (React, React Native, Expo) for interacting with the PocketBase JS SDK",
|
|
5
|
-
"main": "dist/cjs/index.js",
|
|
6
|
-
"module": "dist/esm/index.js",
|
|
7
|
-
"files": [
|
|
8
|
-
"dist"
|
|
9
|
-
],
|
|
10
|
-
"types": "dist/index.d.ts",
|
|
11
|
-
"scripts": {
|
|
12
|
-
"build": "rm -rf dist && rollup -c",
|
|
13
|
-
"dev": "rollup -c -w",
|
|
14
|
-
"rollup": "rollup -c",
|
|
15
|
-
"prepublishOnly": "npm run build"
|
|
16
|
-
},
|
|
17
5
|
"keywords": [
|
|
18
6
|
"pocketbase",
|
|
19
7
|
"react",
|
|
@@ -24,38 +12,109 @@
|
|
|
24
12
|
"typescript",
|
|
25
13
|
"javascript-sdk"
|
|
26
14
|
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Tobias Krainhöfner",
|
|
17
|
+
"homepage": "https://github.com/tobicrain/pocketbase-react#readme",
|
|
27
18
|
"repository": {
|
|
28
19
|
"type": "git",
|
|
29
20
|
"url": "git+https://github.com/tobicrain/pocketbase-react.git"
|
|
30
21
|
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/tobicrain/pocketbase-react/issues"
|
|
24
|
+
},
|
|
25
|
+
"main": "lib/index.js",
|
|
26
|
+
"types": "es/index.d.ts",
|
|
27
|
+
"unpkg": "dist/pocketbase-react.js",
|
|
28
|
+
"module": "es/index.js",
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"lib",
|
|
32
|
+
"src",
|
|
33
|
+
"es"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --extensions \".js,.ts,.tsx\" --out-dir lib",
|
|
37
|
+
"build:es": "babel src --extensions \".js,.ts,.tsx\" --out-dir es",
|
|
38
|
+
"build:umd": "cross-env NODE_ENV=development rollup -c -o dist/pocketbase-react.js",
|
|
39
|
+
"build:umd:min": "cross-env NODE_ENV=production rollup -c -o dist/pocketbase-react.min.js",
|
|
40
|
+
"build:types": "tsc",
|
|
41
|
+
"build": "yarn build:types && yarn build:commonjs && yarn build:es && yarn build:umd && yarn build:umd:min",
|
|
42
|
+
"clean": "rimraf lib dist es coverage",
|
|
43
|
+
"dev": "rollup -c -w",
|
|
44
|
+
"rollup": "rollup -c",
|
|
45
|
+
"prepublishOnly": "npm run build"
|
|
46
|
+
},
|
|
31
47
|
"devDependencies": {
|
|
32
|
-
"@babel/
|
|
48
|
+
"@babel/cli": "^7.12.1",
|
|
49
|
+
"@babel/core": "^7.12.3",
|
|
50
|
+
"@babel/plugin-proposal-decorators": "^7.12.1",
|
|
51
|
+
"@babel/plugin-proposal-object-rest-spread": "^7.12.1",
|
|
52
|
+
"@babel/plugin-transform-react-display-name": "^7.12.1",
|
|
53
|
+
"@babel/plugin-transform-react-jsx": "^7.12.1",
|
|
54
|
+
"@babel/plugin-transform-runtime": "^7.12.1",
|
|
55
|
+
"@babel/preset-env": "^7.12.1",
|
|
56
|
+
"@babel/preset-typescript": "^7.14.5",
|
|
57
|
+
"@microsoft/api-extractor": "^7.18.1",
|
|
58
|
+
"@rollup/plugin-babel": "^5.2.1",
|
|
59
|
+
"@rollup/plugin-commonjs": "^15.1.0",
|
|
60
|
+
"@rollup/plugin-node-resolve": "^9.0.0",
|
|
61
|
+
"@rollup/plugin-replace": "^2.3.3",
|
|
62
|
+
"@testing-library/jest-dom": "^5.11.5",
|
|
63
|
+
"@testing-library/jest-native": "^3.4.3",
|
|
64
|
+
"@testing-library/react": "13.0.0",
|
|
65
|
+
"@testing-library/react-12": "npm:@testing-library/react@^12",
|
|
66
|
+
"@testing-library/react-hooks": "^3.4.2",
|
|
67
|
+
"@testing-library/react-native": "^7.1.0",
|
|
68
|
+
"@types/object-assign": "^4.0.30",
|
|
69
|
+
"@types/react": "^18",
|
|
70
|
+
"@types/react-dom": "^18",
|
|
71
|
+
"@types/react-is": "^17",
|
|
72
|
+
"@types/react-native": "^0.67.4",
|
|
73
|
+
"@typescript-eslint/eslint-plugin": "^4.28.0",
|
|
74
|
+
"@typescript-eslint/parser": "^4.28.0",
|
|
75
|
+
"babel-eslint": "^10.1.0",
|
|
76
|
+
"babel-jest": "^26.6.1",
|
|
77
|
+
"codecov": "^3.8.0",
|
|
78
|
+
"eslint": "^7.12.0",
|
|
79
|
+
"eslint-config-prettier": "^6.14.0",
|
|
80
|
+
"eslint-plugin-import": "^2.22.1",
|
|
81
|
+
"eslint-plugin-prettier": "^3.1.4",
|
|
82
|
+
"eslint-plugin-react": "^7.21.5",
|
|
83
|
+
"glob": "^7.1.6",
|
|
84
|
+
"jest": "^26.6.1",
|
|
85
|
+
"prettier": "^2.1.2",
|
|
86
|
+
"react": "18.0.0",
|
|
87
|
+
"react-17": "npm:react@^17",
|
|
88
|
+
"react-dom": "18.0.0",
|
|
89
|
+
"react-dom-17": "npm:react-dom@^17",
|
|
90
|
+
"react-native": "^0.64.1",
|
|
91
|
+
"react-test-renderer": "18.0.0",
|
|
92
|
+
"react-test-renderer-17": "npm:react-test-renderer@^17",
|
|
93
|
+
"cross-env": "^7.0.3",
|
|
94
|
+
"rollup": "^2.32.1",
|
|
95
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
96
|
+
"rimraf": "^3.0.2",
|
|
97
|
+
"ts-jest": "26.5.6",
|
|
98
|
+
"typescript": "^4.3.4"
|
|
99
|
+
},
|
|
100
|
+
"peerDependencies": {
|
|
101
|
+
"pocketbase": "^0.7.1",
|
|
102
|
+
"react": "^16.12.0",
|
|
33
103
|
"@react-native-async-storage/async-storage": "^1.17.10",
|
|
34
104
|
"@reduxjs/toolkit": "^1.8.5",
|
|
35
|
-
"@rollup/plugin-babel": "^5.3.1",
|
|
36
|
-
"@rollup/plugin-commonjs": "^22.0.2",
|
|
37
|
-
"@rollup/plugin-node-resolve": "^14.1.0",
|
|
38
|
-
"@rollup/plugin-typescript": "^8.5.0",
|
|
39
105
|
"node-localstorage": "^2.2.1",
|
|
40
106
|
"react-redux": "^8.0.4",
|
|
41
107
|
"redux": "^4.2.0",
|
|
42
|
-
"redux-persist": "^6.0.0"
|
|
43
|
-
"rollup": "^2.79.1",
|
|
44
|
-
"rollup-plugin-dts": "^4.2.2",
|
|
45
|
-
"tslib": "^2.4.0",
|
|
46
|
-
"typescript": "^4.8.4"
|
|
47
|
-
},
|
|
48
|
-
"peerDependencies": {
|
|
49
|
-
"pocketbase": "^0.7.1",
|
|
50
|
-
"react": "^16.12.0"
|
|
108
|
+
"redux-persist": "^6.0.0"
|
|
51
109
|
},
|
|
52
110
|
"dependencies": {
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
"
|
|
59
|
-
|
|
60
|
-
|
|
111
|
+
"@babel/runtime": "^7.12.1",
|
|
112
|
+
"pocketbase": "^0.7.1",
|
|
113
|
+
"@react-native-async-storage/async-storage": "^1.17.10",
|
|
114
|
+
"@reduxjs/toolkit": "^1.8.5",
|
|
115
|
+
"node-localstorage": "^2.2.1",
|
|
116
|
+
"react-redux": "^8.0.4",
|
|
117
|
+
"redux": "^4.2.0",
|
|
118
|
+
"redux-persist": "^6.0.0"
|
|
119
|
+
}
|
|
61
120
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { createContext, useEffect } from 'react';
|
|
3
|
+
import PocketBase from 'pocketbase';
|
|
4
|
+
import { Provider } from 'react-redux';
|
|
5
|
+
import { PersistGate } from 'redux-persist/integration/react';
|
|
6
|
+
import * as store from '../store/store';
|
|
7
|
+
import { ClientProvider } from './client';
|
|
8
|
+
import { ContentProvider } from './content';
|
|
9
|
+
|
|
10
|
+
export const PocketbaseContext = createContext<PocketBase | null>(null);
|
|
11
|
+
|
|
12
|
+
export type PocketbaseProviderProps = {
|
|
13
|
+
children: React.ReactNode;
|
|
14
|
+
serverURL: string;
|
|
15
|
+
credentials: {
|
|
16
|
+
username: string;
|
|
17
|
+
password: string;
|
|
18
|
+
};
|
|
19
|
+
initialCollections?: string[];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const Pocketbase = (props: PocketbaseProviderProps) => {
|
|
23
|
+
const [client, setClient] = React.useState<PocketBase | null>(null);
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
const client = new PocketBase(props.serverURL);
|
|
26
|
+
client.admins.authViaEmail(props.credentials.username, props.credentials.password).then(() => {
|
|
27
|
+
setClient(client);
|
|
28
|
+
});
|
|
29
|
+
}, [props.serverURL]);
|
|
30
|
+
|
|
31
|
+
return client ? (
|
|
32
|
+
<ClientProvider client={client}>
|
|
33
|
+
<Provider store={store.store}>
|
|
34
|
+
<PersistGate persistor={store.persistor}>
|
|
35
|
+
<ContentProvider collections={props.initialCollections}>
|
|
36
|
+
{props.children}
|
|
37
|
+
</ContentProvider>
|
|
38
|
+
</PersistGate>
|
|
39
|
+
</Provider>
|
|
40
|
+
</ClientProvider>
|
|
41
|
+
): null;
|
|
42
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { createContext } from 'react';
|
|
3
|
+
import PocketBase from 'pocketbase';
|
|
4
|
+
|
|
5
|
+
export const ClientContext = createContext<PocketBase | null>(null);
|
|
6
|
+
|
|
7
|
+
export type ClientProviderProps = {
|
|
8
|
+
children: React.ReactNode;
|
|
9
|
+
client: PocketBase;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const ClientProvider = (props: ClientProviderProps) => {
|
|
13
|
+
return <ClientContext.Provider value={props.client}>{props.children}</ClientContext.Provider>;
|
|
14
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import * as store from '../store/store';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { createContext, useEffect } from 'react';
|
|
4
|
+
import { useClientContext } from '../hooks/useClientContext';
|
|
5
|
+
import { Record } from '../interfaces/Record';
|
|
6
|
+
import { recordsAction } from '../store/actions';
|
|
7
|
+
|
|
8
|
+
interface ContentActions {
|
|
9
|
+
subscribe: (collectionName: string) => Promise<void>;
|
|
10
|
+
unsubscribe: (collectionName?: string) => void;
|
|
11
|
+
fetch: (collectionName: string) => Promise<void>;
|
|
12
|
+
create: (collectionName: string, record: {}) => Promise<void>;
|
|
13
|
+
update: (collectionName: string, recordId: string, record: {}) => Promise<void>;
|
|
14
|
+
delete: (collectionName: string, recordId: string) => Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const ContentContext = createContext<ContentActions | null>(null);
|
|
18
|
+
|
|
19
|
+
export type ContentProviderProps = {
|
|
20
|
+
children: React.ReactNode;
|
|
21
|
+
collections?: string[];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
interface MessageData {
|
|
25
|
+
action: string;
|
|
26
|
+
record: Record;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const ContentProvider = (props: ContentProviderProps) => {
|
|
30
|
+
const client = useClientContext();
|
|
31
|
+
const dispatch = store.useAppDispatch;
|
|
32
|
+
const [collections, _] = React.useState<string[]>(props.collections || []);
|
|
33
|
+
|
|
34
|
+
const actions: ContentActions = {
|
|
35
|
+
subscribe: async (collectionName: string) => {
|
|
36
|
+
// save local that collectionName should be subscribed
|
|
37
|
+
await client?.realtime.subscribe(collectionName, (event: MessageData) => {
|
|
38
|
+
switch (event.action) {
|
|
39
|
+
case 'create':
|
|
40
|
+
dispatch(recordsAction.addRecord(collectionName, event.record));
|
|
41
|
+
break;
|
|
42
|
+
case 'update':
|
|
43
|
+
dispatch(recordsAction.updateRecord(collectionName, event.record));
|
|
44
|
+
break;
|
|
45
|
+
case 'delete':
|
|
46
|
+
dispatch(recordsAction.deleteRecord(collectionName, event.record));
|
|
47
|
+
break;
|
|
48
|
+
default:
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
unsubscribe: (collectionName?: string) => {
|
|
54
|
+
if (collectionName) {
|
|
55
|
+
client?.realtime.unsubscribe(collectionName);
|
|
56
|
+
} else {
|
|
57
|
+
client?.realtime.unsubscribe();
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
fetch: async (collectionName: string) => {
|
|
61
|
+
const records = await client?.records.getFullList(collectionName, 200);
|
|
62
|
+
dispatch(recordsAction.setRecords(collectionName, records as Record[]));
|
|
63
|
+
},
|
|
64
|
+
create: async (collectionName: string, record: {}) => {
|
|
65
|
+
await client?.records.create(collectionName, record);
|
|
66
|
+
},
|
|
67
|
+
update: async (collectionName: string, recordId: string, record: {}) => {
|
|
68
|
+
await client?.records.update(collectionName, recordId, record);
|
|
69
|
+
},
|
|
70
|
+
delete: async (collectionName: string, recordId: string) => {
|
|
71
|
+
await client?.records.delete(collectionName, recordId);
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
if (collections) {
|
|
77
|
+
collections.forEach((collectionName) => {
|
|
78
|
+
actions.fetch(collectionName);
|
|
79
|
+
actions.subscribe(collectionName);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return () => actions.unsubscribe();
|
|
83
|
+
}, [collections]);
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<ContentContext.Provider value={actions}>{props.children}</ContentContext.Provider>
|
|
87
|
+
);
|
|
88
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useContext, useEffect } from 'react';
|
|
2
|
+
import * as store from '../store';
|
|
3
|
+
import { ContentContext } from '../context';
|
|
4
|
+
import { Record } from '../interfaces/Record';
|
|
5
|
+
|
|
6
|
+
export function useAppContent<T extends Record>(
|
|
7
|
+
collectionName: string,
|
|
8
|
+
initialFetch: boolean = false
|
|
9
|
+
) {
|
|
10
|
+
const records = (store.useAppSelector((state) => state.reducer.records[collectionName]) ?? []) as T[];
|
|
11
|
+
const context = useContext(ContentContext);
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (initialFetch) {
|
|
15
|
+
context?.fetch(collectionName);
|
|
16
|
+
}
|
|
17
|
+
}, [collectionName, initialFetch]);
|
|
18
|
+
|
|
19
|
+
const actions = {
|
|
20
|
+
subscribe: async () => await context?.subscribe(collectionName),
|
|
21
|
+
unsubscribe: () => context?.unsubscribe(collectionName),
|
|
22
|
+
refetch: async () => await context?.fetch(collectionName),
|
|
23
|
+
create: async (record: {}) => await context?.create(collectionName, record),
|
|
24
|
+
update: async (id: string, record: {}) => await context?.update(collectionName, id, record),
|
|
25
|
+
delete: async (id: string) => await context?.delete(collectionName, id),
|
|
26
|
+
};
|
|
27
|
+
return { records, actions };
|
|
28
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Record';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Record } from '../../interfaces/Record';
|
|
2
|
+
import { RecordAction } from '../reducers/records';
|
|
3
|
+
import * as ReduxType from '../types';
|
|
4
|
+
|
|
5
|
+
const setRecords = (key: string, payload: Record[]) =>
|
|
6
|
+
({
|
|
7
|
+
type: ReduxType.SET_RECORDS,
|
|
8
|
+
key,
|
|
9
|
+
payload,
|
|
10
|
+
} as RecordAction);
|
|
11
|
+
|
|
12
|
+
const addRecord = (key: string, payload: Record) =>
|
|
13
|
+
({
|
|
14
|
+
type: ReduxType.ADD_RECORD,
|
|
15
|
+
key,
|
|
16
|
+
payload,
|
|
17
|
+
} as RecordAction);
|
|
18
|
+
|
|
19
|
+
const addRecords = (key: string, payload: Record[]) =>
|
|
20
|
+
({
|
|
21
|
+
type: ReduxType.ADD_RECORDS,
|
|
22
|
+
key,
|
|
23
|
+
payload,
|
|
24
|
+
} as RecordAction);
|
|
25
|
+
|
|
26
|
+
const deleteRecord = (key: string, payload: Record) =>
|
|
27
|
+
({
|
|
28
|
+
type: ReduxType.DELETE_RECORD,
|
|
29
|
+
key,
|
|
30
|
+
payload,
|
|
31
|
+
} as RecordAction);
|
|
32
|
+
|
|
33
|
+
const deleteRecords = (key: string, payload: Record[]) =>
|
|
34
|
+
({
|
|
35
|
+
type: ReduxType.DELETE_RECORDS,
|
|
36
|
+
key,
|
|
37
|
+
payload,
|
|
38
|
+
} as RecordAction);
|
|
39
|
+
|
|
40
|
+
const updateRecord = (key: string, payload: Record) =>
|
|
41
|
+
({
|
|
42
|
+
type: ReduxType.UPDATE_RECORD,
|
|
43
|
+
key,
|
|
44
|
+
payload,
|
|
45
|
+
} as RecordAction);
|
|
46
|
+
|
|
47
|
+
export {
|
|
48
|
+
setRecords,
|
|
49
|
+
addRecord,
|
|
50
|
+
addRecords,
|
|
51
|
+
deleteRecord,
|
|
52
|
+
deleteRecords,
|
|
53
|
+
updateRecord,
|
|
54
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { combineReducers } from 'redux';
|
|
2
|
+
import { records } from './records';
|
|
3
|
+
|
|
4
|
+
export const appReducer = combineReducers({
|
|
5
|
+
records: records,
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
interface AppReducer {
|
|
9
|
+
records: ReturnType<typeof appReducer>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type State = AppReducer;
|