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.
Files changed (57) hide show
  1. package/CHANGELOG.md +15 -3
  2. package/README.md +56 -16
  3. package/dist/pocketbase-react.js +97 -49
  4. package/dist/pocketbase-react.min.js +3 -3
  5. package/es/context/content.d.ts +2 -5
  6. package/es/context/content.js +24 -25
  7. package/es/hooks/useAppContent.d.ts +1 -1
  8. package/es/hooks/useAppContent.js +10 -23
  9. package/es/service/Authentication.d.ts +1 -1
  10. package/es/service/Authentication.js +1 -1
  11. package/es/service/Storage.d.ts +3 -0
  12. package/es/service/Storage.js +5 -2
  13. package/es/store/actions/index.d.ts +2 -1
  14. package/es/store/actions/index.js +2 -1
  15. package/es/store/actions/records.d.ts +1 -1
  16. package/es/store/actions/subscriptions.d.ts +5 -0
  17. package/es/store/actions/subscriptions.js +14 -0
  18. package/es/store/reducers/index.d.ts +2 -1
  19. package/es/store/reducers/index.js +3 -1
  20. package/es/store/reducers/records.d.ts +1 -1
  21. package/es/store/reducers/subscriptions.d.ts +9 -0
  22. package/es/store/reducers/subscriptions.js +30 -0
  23. package/es/store/store.d.ts +3 -2
  24. package/es/store/store.js +2 -2
  25. package/es/store/types/index.d.ts +5 -1
  26. package/es/store/types/index.js +4 -1
  27. package/lib/context/content.js +23 -25
  28. package/lib/hooks/useAppContent.js +10 -23
  29. package/lib/service/Authentication.js +1 -1
  30. package/lib/service/Storage.js +4 -1
  31. package/lib/store/actions/index.js +3 -1
  32. package/lib/store/actions/subscriptions.js +22 -0
  33. package/lib/store/reducers/index.js +3 -1
  34. package/lib/store/reducers/subscriptions.js +37 -0
  35. package/lib/store/store.js +2 -2
  36. package/lib/store/types/index.js +8 -2
  37. package/package.json +1 -1
  38. package/src/context/Pocketbase.tsx +5 -2
  39. package/src/context/auth.tsx +15 -10
  40. package/src/context/content.tsx +51 -54
  41. package/src/context/index.ts +1 -1
  42. package/src/hooks/index.ts +1 -1
  43. package/src/hooks/useAppContent.ts +20 -31
  44. package/src/hooks/useClientContext.ts +1 -2
  45. package/src/index.ts +1 -1
  46. package/src/interfaces/index.ts +1 -1
  47. package/src/service/Authentication.ts +12 -22
  48. package/src/service/Storage.ts +23 -17
  49. package/src/store/actions/index.tsx +2 -1
  50. package/src/store/actions/records.tsx +1 -8
  51. package/src/store/actions/subscriptions.tsx +22 -0
  52. package/src/store/index.ts +1 -1
  53. package/src/store/reducers/index.tsx +3 -1
  54. package/src/store/reducers/records.tsx +2 -2
  55. package/src/store/reducers/subscriptions.tsx +46 -0
  56. package/src/store/store.tsx +4 -7
  57. 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 Types = typeof SET_RECORDS | typeof ADD_RECORD | typeof ADD_RECORDS | typeof UPDATE_RECORD | typeof DELETE_RECORD | typeof DELETE_RECORDS;
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;
@@ -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';
@@ -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(async () => {
35
- var _await$StorageService;
36
- const subscribed = JSON.parse((_await$StorageService = await _Storage.StorageService.get("subscribed")) != null ? _await$StorageService : JSON.stringify([]));
37
- await _Storage.StorageService.set("subscribed", JSON.stringify([...subscribed, collectionName]));
38
- }).catch(_error => {}));
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(async () => {
43
- var _await$StorageService2;
44
- const subscribed = JSON.parse((_await$StorageService2 = await _Storage.StorageService.get("subscribed")) != null ? _await$StorageService2 : JSON.stringify([]));
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(async () => {
49
- await _Storage.StorageService.set("subscribed", JSON.stringify([]));
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 () => actions.unsubscribe();
75
- }, [collections]);
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.actions.fetch(collectionName);
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
- (async () => {
28
- await refetchSubscribeState();
29
- })();
30
- }, []);
22
+ setIsSubscribed(subscriptions.includes(collectionName));
23
+ }, [subscriptions]);
31
24
  const actions = {
32
- subscribe: async () => {
33
- await context.actions.subscribe(collectionName);
34
- await refetchSubscribeState();
35
- },
36
- unsubscribe: () => {
37
- context.actions.unsubscribe(collectionName);
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("Authorization", "user " + token);
33
+ headers.append('Authorization', 'user ' + token);
34
34
  return await this.client.users.getOne(id);
35
35
  }
36
36
  }
@@ -23,4 +23,7 @@ class StorageService {
23
23
  }
24
24
  }
25
25
  }
26
- exports.StorageService = StorageService;
26
+ exports.StorageService = StorageService;
27
+ StorageService.Constants = {
28
+ SUBSCRIBED: 'subscribed'
29
+ };
@@ -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;
@@ -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 = {
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pocketbase-react",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "Unofficial React SDK (React, React Native, Expo) for interacting with the PocketBase JS SDK",
5
5
  "keywords": [
6
6
  "pocketbase",
@@ -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 webRedirectUrl={props.webRedirectUrl} mobileRedirectUrl={props.mobileRedirectUrl} openURL={props.openURL} >
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
  };
@@ -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 = authProvider?.authUrl + typeof document !== 'undefined' ? props.webRedirectUrl: props.mobileRedirectUrl;
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(authProvider.name, code, authProvider.codeVerifier, typeof document !== 'undefined' ? props.webRedirectUrl: props.mobileRedirectUrl);
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
  };
@@ -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 { StorageService } from '../service/Storage';
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
- interface ContentContext {
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
- action: string;
38
- record: Record;
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.subscribe(collectionName, (event: MessageData) => {
50
- switch (event.action) {
51
- case 'create':
52
- dispatch(recordsAction.addRecord(collectionName, event.record));
53
- break;
54
- case 'update':
55
- dispatch(recordsAction.updateRecord(collectionName, event.record));
56
- break;
57
- case 'delete':
58
- dispatch(recordsAction.deleteRecord(collectionName, event.record));
59
- break;
60
- default:
61
- break;
62
- }
63
- })
64
- .then(async () => {
65
- const subscribed = JSON.parse(await StorageService.get("subscribed") ?? JSON.stringify([])) as string[];
66
- await StorageService.set("subscribed", JSON.stringify([...subscribed, collectionName]));
67
- })
68
- .catch((_error) => {});
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.unsubscribe(collectionName)
73
- .then(async () => {
74
- const subscribed = JSON.parse(await StorageService.get("subscribed") ?? JSON.stringify([])) as string[];
75
- await StorageService.set("subscribed", JSON.stringify(subscribed.filter((name) => name !== collectionName)));
76
- })
77
- .catch((_error) => {});
68
+ await client?.realtime
69
+ .unsubscribe(collectionName)
70
+ .then(() => {
71
+ dispatch(subscriptionsAction.deleteSubscription(collectionName));
72
+ })
73
+ .catch((_error) => {});
78
74
  } else {
79
- client?.realtime.unsubscribe()
80
- .then(async () => {
81
- await StorageService.set("subscribed", JSON.stringify([]));
82
- })
83
- .catch((_error) => {});
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 () => actions.unsubscribe();
109
- }, [collections]);
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
  };
@@ -1,4 +1,4 @@
1
1
  export * from './content';
2
2
  export * from './client';
3
3
  export * from './auth';
4
- export * from './Pocketbase';
4
+ export * from './Pocketbase';
@@ -1,3 +1,3 @@
1
1
  export * from './useAppContent';
2
2
  export * from './useClientContext';
3
- export * from './useAuth';
3
+ export * from './useAuth';