pocketbase-react 0.1.13 → 0.1.15
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 +15 -3
- package/README.md +56 -16
- package/dist/pocketbase-react.js +97 -49
- package/dist/pocketbase-react.min.js +3 -3
- package/es/context/content.d.ts +2 -5
- package/es/context/content.js +24 -25
- package/es/hooks/useAppContent.d.ts +1 -1
- package/es/hooks/useAppContent.js +10 -23
- package/es/service/Authentication.d.ts +1 -1
- package/es/service/Authentication.js +1 -1
- package/es/service/Storage.d.ts +3 -0
- package/es/service/Storage.js +5 -2
- package/es/store/actions/index.d.ts +2 -1
- package/es/store/actions/index.js +2 -1
- package/es/store/actions/records.d.ts +1 -1
- package/es/store/actions/subscriptions.d.ts +5 -0
- package/es/store/actions/subscriptions.js +14 -0
- package/es/store/reducers/index.d.ts +2 -1
- package/es/store/reducers/index.js +3 -1
- package/es/store/reducers/records.d.ts +1 -1
- package/es/store/reducers/subscriptions.d.ts +9 -0
- package/es/store/reducers/subscriptions.js +30 -0
- package/es/store/store.d.ts +3 -2
- package/es/store/store.js +2 -2
- package/es/store/types/index.d.ts +5 -1
- package/es/store/types/index.js +4 -1
- package/lib/context/content.js +23 -25
- package/lib/hooks/useAppContent.js +10 -23
- package/lib/service/Authentication.js +1 -1
- package/lib/service/Storage.js +4 -1
- package/lib/store/actions/index.js +3 -1
- package/lib/store/actions/subscriptions.js +22 -0
- package/lib/store/reducers/index.js +3 -1
- package/lib/store/reducers/subscriptions.js +37 -0
- package/lib/store/store.js +2 -2
- package/lib/store/types/index.js +8 -2
- package/package.json +1 -1
- package/src/context/Pocketbase.tsx +5 -2
- package/src/context/auth.tsx +15 -10
- package/src/context/content.tsx +51 -54
- package/src/context/index.ts +1 -1
- package/src/hooks/index.ts +1 -1
- package/src/hooks/useAppContent.ts +20 -31
- package/src/hooks/useClientContext.ts +1 -2
- package/src/index.ts +1 -1
- package/src/interfaces/index.ts +1 -1
- package/src/service/Authentication.ts +12 -22
- package/src/service/Storage.ts +23 -17
- package/src/store/actions/index.tsx +2 -1
- package/src/store/actions/records.tsx +1 -8
- package/src/store/actions/subscriptions.tsx +22 -0
- package/src/store/index.ts +1 -1
- package/src/store/reducers/index.tsx +3 -1
- package/src/store/reducers/records.tsx +2 -2
- package/src/store/reducers/subscriptions.tsx +46 -0
- package/src/store/store.tsx +4 -7
- package/src/store/types/index.ts +10 -1
package/es/store/store.js
CHANGED
|
@@ -9,10 +9,10 @@ const CustomStorage = {
|
|
|
9
9
|
return await StorageService.get(key);
|
|
10
10
|
},
|
|
11
11
|
setItem: async (key, value, ..._args) => {
|
|
12
|
-
return StorageService.set(key, value);
|
|
12
|
+
return await StorageService.set(key, value);
|
|
13
13
|
},
|
|
14
14
|
removeItem: async (key, ..._args) => {
|
|
15
|
-
return StorageService.remove(key);
|
|
15
|
+
return await StorageService.remove(key);
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
export const persistConfig = {
|
|
@@ -4,4 +4,8 @@ export declare const ADD_RECORDS = "ADD_RECORDS";
|
|
|
4
4
|
export declare const UPDATE_RECORD = "UPDATE_RECORD";
|
|
5
5
|
export declare const DELETE_RECORD = "DELETE_RECORD";
|
|
6
6
|
export declare const DELETE_RECORDS = "DELETE_RECORDS";
|
|
7
|
-
export declare type
|
|
7
|
+
export declare type RecordTypes = typeof SET_RECORDS | typeof ADD_RECORD | typeof ADD_RECORDS | typeof UPDATE_RECORD | typeof DELETE_RECORD | typeof DELETE_RECORDS;
|
|
8
|
+
export declare const SET_SUBSCRIPTIONS = "SET_SUBSCRIPTIONS";
|
|
9
|
+
export declare const ADD_SUBSCRIPTION = "ADD_SUBSCRIPTION";
|
|
10
|
+
export declare const DELETE_SUBSCRIPTION = "DELETE_SUBSCRIPTION";
|
|
11
|
+
export declare type SubscriptionsTypes = typeof SET_SUBSCRIPTIONS | typeof ADD_SUBSCRIPTION | typeof DELETE_SUBSCRIPTION;
|
package/es/store/types/index.js
CHANGED
|
@@ -3,4 +3,7 @@ export const ADD_RECORD = 'ADD_RECORD';
|
|
|
3
3
|
export const ADD_RECORDS = 'ADD_RECORDS';
|
|
4
4
|
export const UPDATE_RECORD = 'UPDATE_RECORD';
|
|
5
5
|
export const DELETE_RECORD = 'DELETE_RECORD';
|
|
6
|
-
export const DELETE_RECORDS = 'DELETE_RECORDS';
|
|
6
|
+
export const DELETE_RECORDS = 'DELETE_RECORDS';
|
|
7
|
+
export const SET_SUBSCRIPTIONS = 'SET_SUBSCRIPTIONS';
|
|
8
|
+
export const ADD_SUBSCRIPTION = 'ADD_SUBSCRIPTION';
|
|
9
|
+
export const DELETE_SUBSCRIPTION = 'DELETE_SUBSCRIPTION';
|
package/lib/context/content.js
CHANGED
|
@@ -6,7 +6,6 @@ var store = _interopRequireWildcard(require("../store/store"));
|
|
|
6
6
|
var React = _interopRequireWildcard(require("react"));
|
|
7
7
|
var _useClientContext = require("../hooks/useClientContext");
|
|
8
8
|
var _actions = require("../store/actions");
|
|
9
|
-
var _Storage = require("../service/Storage");
|
|
10
9
|
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); }
|
|
11
10
|
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; }
|
|
12
11
|
const ContentContext = (0, React.createContext)({});
|
|
@@ -14,7 +13,6 @@ exports.ContentContext = ContentContext;
|
|
|
14
13
|
const ContentProvider = props => {
|
|
15
14
|
const client = (0, _useClientContext.useClientContext)();
|
|
16
15
|
const dispatch = store.useAppDispatch;
|
|
17
|
-
const [collections, _] = React.useState(props.collections || []);
|
|
18
16
|
const actions = {
|
|
19
17
|
subscribe: async collectionName => {
|
|
20
18
|
await (client == null ? void 0 : client.realtime.subscribe(collectionName, event => {
|
|
@@ -31,23 +29,21 @@ const ContentProvider = props => {
|
|
|
31
29
|
default:
|
|
32
30
|
break;
|
|
33
31
|
}
|
|
34
|
-
}).then(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
})
|
|
32
|
+
}).then(() => {
|
|
33
|
+
dispatch(_actions.subscriptionsAction.addSubscription(collectionName));
|
|
34
|
+
}).catch(_error => {
|
|
35
|
+
dispatch(_actions.subscriptionsAction.deleteSubscription(collectionName));
|
|
36
|
+
}));
|
|
39
37
|
},
|
|
40
|
-
unsubscribe: collectionName => {
|
|
38
|
+
unsubscribe: async collectionName => {
|
|
41
39
|
if (collectionName) {
|
|
42
|
-
client == null ? void 0 : client.realtime.unsubscribe(collectionName).then(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
await _Storage.StorageService.set("subscribed", JSON.stringify(subscribed.filter(name => name !== collectionName)));
|
|
46
|
-
}).catch(_error => {});
|
|
40
|
+
await (client == null ? void 0 : client.realtime.unsubscribe(collectionName).then(() => {
|
|
41
|
+
dispatch(_actions.subscriptionsAction.deleteSubscription(collectionName));
|
|
42
|
+
}).catch(_error => {}));
|
|
47
43
|
} else {
|
|
48
|
-
client == null ? void 0 : client.realtime.unsubscribe().then(
|
|
49
|
-
|
|
50
|
-
}).catch(_error => {});
|
|
44
|
+
await (client == null ? void 0 : client.realtime.unsubscribe().then(() => {
|
|
45
|
+
dispatch(_actions.subscriptionsAction.setSubscriptions([]));
|
|
46
|
+
}).catch(_error => {}));
|
|
51
47
|
}
|
|
52
48
|
},
|
|
53
49
|
fetch: async collectionName => {
|
|
@@ -65,18 +61,20 @@ const ContentProvider = props => {
|
|
|
65
61
|
}
|
|
66
62
|
};
|
|
67
63
|
(0, React.useEffect)(() => {
|
|
68
|
-
if (collections) {
|
|
69
|
-
collections.forEach(collectionName => {
|
|
70
|
-
actions.fetch(collectionName);
|
|
71
|
-
actions.subscribe(collectionName);
|
|
64
|
+
if (props.collections) {
|
|
65
|
+
props.collections.forEach(async collectionName => {
|
|
66
|
+
await actions.fetch(collectionName);
|
|
67
|
+
await actions.subscribe(collectionName);
|
|
72
68
|
});
|
|
73
69
|
}
|
|
74
|
-
return () =>
|
|
75
|
-
|
|
70
|
+
return () => {
|
|
71
|
+
(async () => {
|
|
72
|
+
await actions.unsubscribe();
|
|
73
|
+
})();
|
|
74
|
+
};
|
|
75
|
+
}, [props.collections]);
|
|
76
76
|
return /*#__PURE__*/React.createElement(ContentContext.Provider, {
|
|
77
|
-
value:
|
|
78
|
-
actions
|
|
79
|
-
}
|
|
77
|
+
value: actions
|
|
80
78
|
}, props.children);
|
|
81
79
|
};
|
|
82
80
|
exports.ContentProvider = ContentProvider;
|
|
@@ -5,42 +5,29 @@ exports.useAppContent = useAppContent;
|
|
|
5
5
|
var _react = require("react");
|
|
6
6
|
var store = _interopRequireWildcard(require("../store"));
|
|
7
7
|
var _context = require("../context");
|
|
8
|
-
var _Storage = require("../service/Storage");
|
|
9
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); }
|
|
10
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; }
|
|
11
10
|
function useAppContent(collectionName, initialFetch = false) {
|
|
12
11
|
var _store$useAppSelector;
|
|
13
12
|
const records = (_store$useAppSelector = store.useAppSelector(state => state.reducer.records[collectionName])) != null ? _store$useAppSelector : [];
|
|
13
|
+
const subscriptions = store.useAppSelector(state => state.reducer.subscriptions).subscriptions;
|
|
14
14
|
const context = (0, _react.useContext)(_context.ContentContext);
|
|
15
15
|
(0, _react.useEffect)(() => {
|
|
16
16
|
if (initialFetch) {
|
|
17
|
-
context.
|
|
17
|
+
context.fetch(collectionName);
|
|
18
18
|
}
|
|
19
19
|
}, [collectionName, initialFetch]);
|
|
20
20
|
const [isSubscribed, setIsSubscribed] = (0, _react.useState)(false);
|
|
21
|
-
async function refetchSubscribeState() {
|
|
22
|
-
var _await$StorageService;
|
|
23
|
-
const isSubscribed = JSON.parse((_await$StorageService = await _Storage.StorageService.get("subscribed")) != null ? _await$StorageService : JSON.stringify([]));
|
|
24
|
-
setIsSubscribed(isSubscribed.includes(collectionName));
|
|
25
|
-
}
|
|
26
21
|
(0, _react.useEffect)(() => {
|
|
27
|
-
(
|
|
28
|
-
|
|
29
|
-
})();
|
|
30
|
-
}, []);
|
|
22
|
+
setIsSubscribed(subscriptions.includes(collectionName));
|
|
23
|
+
}, [subscriptions]);
|
|
31
24
|
const actions = {
|
|
32
|
-
subscribe: async () =>
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
refetchSubscribeState();
|
|
39
|
-
},
|
|
40
|
-
fetch: async () => await context.actions.fetch(collectionName),
|
|
41
|
-
create: async record => await context.actions.create(collectionName, record),
|
|
42
|
-
update: async (id, record) => await context.actions.update(collectionName, id, record),
|
|
43
|
-
delete: async id => await context.actions.delete(collectionName, id)
|
|
25
|
+
subscribe: async () => await context.subscribe(collectionName),
|
|
26
|
+
unsubscribe: async () => await context.unsubscribe(collectionName),
|
|
27
|
+
fetch: async () => await context.fetch(collectionName),
|
|
28
|
+
create: async record => await context.create(collectionName, record),
|
|
29
|
+
update: async (id, record) => await context.update(collectionName, id, record),
|
|
30
|
+
delete: async id => await context.delete(collectionName, id)
|
|
44
31
|
};
|
|
45
32
|
return {
|
|
46
33
|
records,
|
|
@@ -30,7 +30,7 @@ class AuthenticationService {
|
|
|
30
30
|
}
|
|
31
31
|
async getUserData(id, token) {
|
|
32
32
|
const headers = new Headers();
|
|
33
|
-
headers.append(
|
|
33
|
+
headers.append('Authorization', 'user ' + token);
|
|
34
34
|
return await this.client.users.getOne(id);
|
|
35
35
|
}
|
|
36
36
|
}
|
package/lib/service/Storage.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
|
-
exports.recordsAction = void 0;
|
|
4
|
+
exports.subscriptionsAction = exports.recordsAction = void 0;
|
|
5
5
|
var recordsAction = _interopRequireWildcard(require("./records"));
|
|
6
6
|
exports.recordsAction = recordsAction;
|
|
7
|
+
var subscriptionsAction = _interopRequireWildcard(require("./subscriptions"));
|
|
8
|
+
exports.subscriptionsAction = subscriptionsAction;
|
|
7
9
|
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); }
|
|
8
10
|
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; }
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.setSubscriptions = exports.deleteSubscription = exports.addSubscription = void 0;
|
|
5
|
+
var ReduxType = _interopRequireWildcard(require("../types"));
|
|
6
|
+
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); }
|
|
7
|
+
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; }
|
|
8
|
+
const setSubscriptions = payload => ({
|
|
9
|
+
type: ReduxType.SET_SUBSCRIPTIONS,
|
|
10
|
+
payload
|
|
11
|
+
});
|
|
12
|
+
exports.setSubscriptions = setSubscriptions;
|
|
13
|
+
const addSubscription = payload => ({
|
|
14
|
+
type: ReduxType.ADD_SUBSCRIPTION,
|
|
15
|
+
payload
|
|
16
|
+
});
|
|
17
|
+
exports.addSubscription = addSubscription;
|
|
18
|
+
const deleteSubscription = payload => ({
|
|
19
|
+
type: ReduxType.DELETE_SUBSCRIPTION,
|
|
20
|
+
payload
|
|
21
|
+
});
|
|
22
|
+
exports.deleteSubscription = deleteSubscription;
|
|
@@ -4,7 +4,9 @@ exports.__esModule = true;
|
|
|
4
4
|
exports.appReducer = void 0;
|
|
5
5
|
var _redux = require("redux");
|
|
6
6
|
var _records = require("./records");
|
|
7
|
+
var _subscriptions = require("./subscriptions");
|
|
7
8
|
const appReducer = (0, _redux.combineReducers)({
|
|
8
|
-
records: _records.records
|
|
9
|
+
records: _records.records,
|
|
10
|
+
subscriptions: _subscriptions.subscriptions
|
|
9
11
|
});
|
|
10
12
|
exports.appReducer = appReducer;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.subscriptions = void 0;
|
|
5
|
+
var ReduxType = _interopRequireWildcard(require("../types"));
|
|
6
|
+
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); }
|
|
7
|
+
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; }
|
|
8
|
+
function appendSubscription(subscription, subscriptions) {
|
|
9
|
+
return [...subscriptions, subscription];
|
|
10
|
+
}
|
|
11
|
+
function deleteSubscription(subscription, subscriptions) {
|
|
12
|
+
return subscriptions.filter(sub => sub !== subscription);
|
|
13
|
+
}
|
|
14
|
+
const subscriptions = (state = {
|
|
15
|
+
subscriptions: []
|
|
16
|
+
}, action) => {
|
|
17
|
+
const list = state.subscriptions;
|
|
18
|
+
switch (action.type) {
|
|
19
|
+
case ReduxType.SET_SUBSCRIPTIONS:
|
|
20
|
+
if (Array.isArray(action.payload)) {
|
|
21
|
+
return {
|
|
22
|
+
subscriptions: action.payload
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
case ReduxType.ADD_SUBSCRIPTION:
|
|
26
|
+
return {
|
|
27
|
+
subscriptions: appendSubscription(action.payload, list)
|
|
28
|
+
};
|
|
29
|
+
case ReduxType.DELETE_SUBSCRIPTION:
|
|
30
|
+
return {
|
|
31
|
+
subscriptions: deleteSubscription(action.payload, list)
|
|
32
|
+
};
|
|
33
|
+
default:
|
|
34
|
+
return state;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
exports.subscriptions = subscriptions;
|
package/lib/store/store.js
CHANGED
|
@@ -14,10 +14,10 @@ const CustomStorage = {
|
|
|
14
14
|
return await _Storage.StorageService.get(key);
|
|
15
15
|
},
|
|
16
16
|
setItem: async (key, value, ..._args) => {
|
|
17
|
-
return _Storage.StorageService.set(key, value);
|
|
17
|
+
return await _Storage.StorageService.set(key, value);
|
|
18
18
|
},
|
|
19
19
|
removeItem: async (key, ..._args) => {
|
|
20
|
-
return _Storage.StorageService.remove(key);
|
|
20
|
+
return await _Storage.StorageService.remove(key);
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
23
|
const persistConfig = {
|
package/lib/store/types/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
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;
|
|
4
|
+
exports.UPDATE_RECORD = exports.SET_SUBSCRIPTIONS = exports.SET_RECORDS = exports.DELETE_SUBSCRIPTION = exports.DELETE_RECORDS = exports.DELETE_RECORD = exports.ADD_SUBSCRIPTION = exports.ADD_RECORDS = exports.ADD_RECORD = void 0;
|
|
5
5
|
const SET_RECORDS = 'SET_RECORDS';
|
|
6
6
|
exports.SET_RECORDS = SET_RECORDS;
|
|
7
7
|
const ADD_RECORD = 'ADD_RECORD';
|
|
@@ -13,4 +13,10 @@ exports.UPDATE_RECORD = UPDATE_RECORD;
|
|
|
13
13
|
const DELETE_RECORD = 'DELETE_RECORD';
|
|
14
14
|
exports.DELETE_RECORD = DELETE_RECORD;
|
|
15
15
|
const DELETE_RECORDS = 'DELETE_RECORDS';
|
|
16
|
-
exports.DELETE_RECORDS = DELETE_RECORDS;
|
|
16
|
+
exports.DELETE_RECORDS = DELETE_RECORDS;
|
|
17
|
+
const SET_SUBSCRIPTIONS = 'SET_SUBSCRIPTIONS';
|
|
18
|
+
exports.SET_SUBSCRIPTIONS = SET_SUBSCRIPTIONS;
|
|
19
|
+
const ADD_SUBSCRIPTION = 'ADD_SUBSCRIPTION';
|
|
20
|
+
exports.ADD_SUBSCRIPTION = ADD_SUBSCRIPTION;
|
|
21
|
+
const DELETE_SUBSCRIPTION = 'DELETE_SUBSCRIPTION';
|
|
22
|
+
exports.DELETE_SUBSCRIPTION = DELETE_SUBSCRIPTION;
|
package/package.json
CHANGED
|
@@ -26,7 +26,10 @@ export const Pocketbase = (props: PocketbaseProviderProps) => {
|
|
|
26
26
|
<ClientProvider client={client}>
|
|
27
27
|
<Provider store={store.store}>
|
|
28
28
|
<PersistGate persistor={store.persistor}>
|
|
29
|
-
<AuthProvider
|
|
29
|
+
<AuthProvider
|
|
30
|
+
webRedirectUrl={props.webRedirectUrl}
|
|
31
|
+
mobileRedirectUrl={props.mobileRedirectUrl}
|
|
32
|
+
openURL={props.openURL}>
|
|
30
33
|
<ContentProvider collections={props.initialCollections}>
|
|
31
34
|
{props.children}
|
|
32
35
|
</ContentProvider>
|
|
@@ -34,5 +37,5 @@ export const Pocketbase = (props: PocketbaseProviderProps) => {
|
|
|
34
37
|
</PersistGate>
|
|
35
38
|
</Provider>
|
|
36
39
|
</ClientProvider>
|
|
37
|
-
): null;
|
|
40
|
+
) : null;
|
|
38
41
|
};
|
package/src/context/auth.tsx
CHANGED
|
@@ -25,7 +25,7 @@ export type UpdateEmailType = (email: string) => Promise<void>;
|
|
|
25
25
|
export type DeleteUserType = (id: string) => Promise<void>;
|
|
26
26
|
|
|
27
27
|
export interface AuthActions {
|
|
28
|
-
registerWithEmail: RegisterWithEmailType
|
|
28
|
+
registerWithEmail: RegisterWithEmailType;
|
|
29
29
|
signInWithEmail: SignInWithEmailType;
|
|
30
30
|
signInWithProvider: SignInWithProviderType;
|
|
31
31
|
submitProviderResult: SubmitProviderResultType;
|
|
@@ -46,11 +46,10 @@ export type AuthProviderProps = {
|
|
|
46
46
|
openURL: (url: string) => Promise<void>;
|
|
47
47
|
};
|
|
48
48
|
|
|
49
|
-
|
|
50
49
|
export const AuthProvider = (props: AuthProviderProps) => {
|
|
51
50
|
const client = useClientContext();
|
|
52
51
|
const [authProviders, setAuthProviders] = React.useState<AuthProviderInfo[]>();
|
|
53
|
-
|
|
52
|
+
|
|
54
53
|
const actions: AuthActions = {
|
|
55
54
|
registerWithEmail: async (email, password) => {
|
|
56
55
|
await client?.users.create({
|
|
@@ -64,7 +63,10 @@ export const AuthProvider = (props: AuthProviderProps) => {
|
|
|
64
63
|
},
|
|
65
64
|
signInWithProvider: async (provider: string) => {
|
|
66
65
|
const authProvider = authProviders?.find((p) => p.name === provider);
|
|
67
|
-
const url =
|
|
66
|
+
const url =
|
|
67
|
+
authProvider?.authUrl + typeof document !== 'undefined'
|
|
68
|
+
? props.webRedirectUrl
|
|
69
|
+
: props.mobileRedirectUrl;
|
|
68
70
|
await props.openURL(url);
|
|
69
71
|
await StorageService.set('provider', JSON.stringify(authProviders));
|
|
70
72
|
},
|
|
@@ -77,7 +79,12 @@ export const AuthProvider = (props: AuthProviderProps) => {
|
|
|
77
79
|
const providers = JSON.parse(providersString) as AuthProviderInfo[];
|
|
78
80
|
const authProvider = providers?.find((p) => p.state === state);
|
|
79
81
|
if (authProvider && code) {
|
|
80
|
-
await client?.users.authViaOAuth2(
|
|
82
|
+
await client?.users.authViaOAuth2(
|
|
83
|
+
authProvider.name,
|
|
84
|
+
code,
|
|
85
|
+
authProvider.codeVerifier,
|
|
86
|
+
typeof document !== 'undefined' ? props.webRedirectUrl : props.mobileRedirectUrl
|
|
87
|
+
);
|
|
81
88
|
}
|
|
82
89
|
}
|
|
83
90
|
},
|
|
@@ -97,8 +104,8 @@ export const AuthProvider = (props: AuthProviderProps) => {
|
|
|
97
104
|
await client?.users.requestEmailChange(email);
|
|
98
105
|
},
|
|
99
106
|
deleteUser: async (id: string) => {
|
|
100
|
-
await client?.users.delete(id)
|
|
101
|
-
}
|
|
107
|
+
await client?.users.delete(id);
|
|
108
|
+
},
|
|
102
109
|
};
|
|
103
110
|
|
|
104
111
|
React.useEffect(() => {
|
|
@@ -108,7 +115,5 @@ export const AuthProvider = (props: AuthProviderProps) => {
|
|
|
108
115
|
})();
|
|
109
116
|
}, [props.webRedirectUrl, props.mobileRedirectUrl]);
|
|
110
117
|
|
|
111
|
-
return
|
|
112
|
-
<AuthContext.Provider value={actions}>{props.children}</AuthContext.Provider>
|
|
113
|
-
);
|
|
118
|
+
return <AuthContext.Provider value={actions}>{props.children}</AuthContext.Provider>;
|
|
114
119
|
};
|
package/src/context/content.tsx
CHANGED
|
@@ -4,10 +4,10 @@ import { createContext, useEffect } from 'react';
|
|
|
4
4
|
import { useClientContext } from '../hooks/useClientContext';
|
|
5
5
|
import { Record } from '../interfaces/Record';
|
|
6
6
|
import { recordsAction } from '../store/actions';
|
|
7
|
-
import {
|
|
7
|
+
import { subscriptionsAction } from '../store/actions';
|
|
8
8
|
|
|
9
9
|
type SubscribeType = (collectionName: string) => Promise<void>;
|
|
10
|
-
type UnsubscribeType = (collectionName?: string) => void
|
|
10
|
+
type UnsubscribeType = (collectionName?: string) => Promise<void>;
|
|
11
11
|
type FetchType = (collectionName: string) => Promise<void>;
|
|
12
12
|
type CreateType = (collectionName: string, record: {}) => Promise<void>;
|
|
13
13
|
type UpdateType = (collectionName: string, recordId: string, record: {}) => Promise<void>;
|
|
@@ -22,11 +22,7 @@ interface ContentActions {
|
|
|
22
22
|
delete: DeleteType;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
actions: ContentActions;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export const ContentContext = createContext<ContentContext>({} as ContentContext);
|
|
25
|
+
export const ContentContext = createContext<ContentActions>({} as ContentActions);
|
|
30
26
|
|
|
31
27
|
export type ContentProviderProps = {
|
|
32
28
|
children: React.ReactNode;
|
|
@@ -34,53 +30,54 @@ export type ContentProviderProps = {
|
|
|
34
30
|
};
|
|
35
31
|
|
|
36
32
|
interface MessageData {
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
action: string;
|
|
34
|
+
record: Record;
|
|
39
35
|
}
|
|
40
36
|
|
|
41
37
|
export const ContentProvider = (props: ContentProviderProps) => {
|
|
42
38
|
const client = useClientContext();
|
|
43
39
|
const dispatch = store.useAppDispatch;
|
|
44
|
-
const [collections, _] = React.useState<string[]>(props.collections || []);
|
|
45
|
-
|
|
46
40
|
|
|
47
41
|
const actions: ContentActions = {
|
|
48
42
|
subscribe: async (collectionName: string) => {
|
|
49
|
-
await client?.realtime
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
43
|
+
await client?.realtime
|
|
44
|
+
.subscribe(collectionName, (event: MessageData) => {
|
|
45
|
+
switch (event.action) {
|
|
46
|
+
case 'create':
|
|
47
|
+
dispatch(recordsAction.addRecord(collectionName, event.record));
|
|
48
|
+
break;
|
|
49
|
+
case 'update':
|
|
50
|
+
dispatch(recordsAction.updateRecord(collectionName, event.record));
|
|
51
|
+
break;
|
|
52
|
+
case 'delete':
|
|
53
|
+
dispatch(recordsAction.deleteRecord(collectionName, event.record));
|
|
54
|
+
break;
|
|
55
|
+
default:
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
.then(() => {
|
|
60
|
+
dispatch(subscriptionsAction.addSubscription(collectionName));
|
|
61
|
+
})
|
|
62
|
+
.catch((_error) => {
|
|
63
|
+
dispatch(subscriptionsAction.deleteSubscription(collectionName));
|
|
64
|
+
});
|
|
69
65
|
},
|
|
70
|
-
unsubscribe: (collectionName?: string) => {
|
|
66
|
+
unsubscribe: async (collectionName?: string) => {
|
|
71
67
|
if (collectionName) {
|
|
72
|
-
client?.realtime
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
68
|
+
await client?.realtime
|
|
69
|
+
.unsubscribe(collectionName)
|
|
70
|
+
.then(() => {
|
|
71
|
+
dispatch(subscriptionsAction.deleteSubscription(collectionName));
|
|
72
|
+
})
|
|
73
|
+
.catch((_error) => {});
|
|
78
74
|
} else {
|
|
79
|
-
client?.realtime
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
75
|
+
await client?.realtime
|
|
76
|
+
.unsubscribe()
|
|
77
|
+
.then(() => {
|
|
78
|
+
dispatch(subscriptionsAction.setSubscriptions([]));
|
|
79
|
+
})
|
|
80
|
+
.catch((_error) => {});
|
|
84
81
|
}
|
|
85
82
|
},
|
|
86
83
|
fetch: async (collectionName: string) => {
|
|
@@ -99,18 +96,18 @@ export const ContentProvider = (props: ContentProviderProps) => {
|
|
|
99
96
|
};
|
|
100
97
|
|
|
101
98
|
useEffect(() => {
|
|
102
|
-
if (collections) {
|
|
103
|
-
collections.forEach((collectionName) => {
|
|
104
|
-
actions.fetch(collectionName);
|
|
105
|
-
actions.subscribe(collectionName);
|
|
99
|
+
if (props.collections) {
|
|
100
|
+
props.collections.forEach(async (collectionName) => {
|
|
101
|
+
await actions.fetch(collectionName);
|
|
102
|
+
await actions.subscribe(collectionName);
|
|
106
103
|
});
|
|
107
104
|
}
|
|
108
|
-
return () =>
|
|
109
|
-
|
|
105
|
+
return () => {
|
|
106
|
+
(async () => {
|
|
107
|
+
await actions.unsubscribe();
|
|
108
|
+
})();
|
|
109
|
+
};
|
|
110
|
+
}, [props.collections]);
|
|
110
111
|
|
|
111
|
-
return
|
|
112
|
-
<ContentContext.Provider value={{
|
|
113
|
-
actions,
|
|
114
|
-
}}>{props.children}</ContentContext.Provider>
|
|
115
|
-
);
|
|
112
|
+
return <ContentContext.Provider value={actions}>{props.children}</ContentContext.Provider>;
|
|
116
113
|
};
|
package/src/context/index.ts
CHANGED
package/src/hooks/index.ts
CHANGED