pocketbase-react 0.1.20 → 0.1.22

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 (47) hide show
  1. package/dist/pocketbase-react.js +214 -302
  2. package/dist/pocketbase-react.min.js +3 -3
  3. package/es/context/Pocketbase.js +21 -3
  4. package/es/context/auth.js +0 -17
  5. package/es/context/content.js +17 -10
  6. package/es/hooks/useAppContent.js +6 -5
  7. package/es/hooks/useAuth.js +0 -5
  8. package/es/store/actions/index.d.ts +1 -2
  9. package/es/store/actions/index.js +1 -2
  10. package/es/store/reducers/index.d.ts +3 -8
  11. package/es/store/reducers/index.js +2 -4
  12. package/es/store/reducers/subscriptions.d.ts +1 -4
  13. package/es/store/reducers/subscriptions.js +5 -14
  14. package/es/store/store.d.ts +3 -4
  15. package/es/store/types/index.d.ts +0 -7
  16. package/es/store/types/index.js +1 -7
  17. package/lib/context/Pocketbase.js +20 -2
  18. package/lib/context/auth.js +0 -17
  19. package/lib/context/content.js +17 -10
  20. package/lib/hooks/useAppContent.js +6 -7
  21. package/lib/hooks/useAuth.js +0 -5
  22. package/lib/store/actions/index.js +1 -3
  23. package/lib/store/reducers/index.js +1 -3
  24. package/lib/store/reducers/subscriptions.js +5 -14
  25. package/lib/store/types/index.js +2 -14
  26. package/package.json +1 -1
  27. package/src/context/Pocketbase.tsx +19 -5
  28. package/src/context/auth.tsx +1 -23
  29. package/src/context/content.tsx +19 -10
  30. package/src/hooks/useAppContent.ts +6 -4
  31. package/src/hooks/useAuth.ts +0 -4
  32. package/src/store/actions/index.tsx +1 -2
  33. package/src/store/reducers/index.tsx +3 -9
  34. package/src/store/reducers/subscriptions.tsx +6 -23
  35. package/src/store/types/index.ts +0 -15
  36. package/es/service/Authentication.d.ts +0 -23
  37. package/es/service/Authentication.js +0 -32
  38. package/es/store/actions/auth.d.ts +0 -9
  39. package/es/store/actions/auth.js +0 -26
  40. package/es/store/reducers/auth.d.ts +0 -12
  41. package/es/store/reducers/auth.js +0 -36
  42. package/lib/service/Authentication.js +0 -37
  43. package/lib/store/actions/auth.js +0 -37
  44. package/lib/store/reducers/auth.js +0 -44
  45. package/src/service/Authentication.ts +0 -40
  46. package/src/store/actions/auth.tsx +0 -41
  47. package/src/store/reducers/auth.tsx +0 -57
@@ -11,12 +11,30 @@ var _store = require("../store/store");
11
11
  var _client = require("./client");
12
12
  var _content = require("./content");
13
13
  var _auth = require("./auth");
14
+ var _Storage = require("../service/Storage");
14
15
  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); }
15
16
  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; }
16
17
  const PocketbaseContext = (0, React.createContext)(null);
17
18
  exports.PocketbaseContext = PocketbaseContext;
18
19
  const Pocketbase = props => {
19
- const client = new _pocketbase.default(props.serverURL);
20
+ const [client, setClient] = React.useState(null);
21
+ const [initialCollections, setInitialCollections] = React.useState();
22
+ (0, React.useEffect)(() => {
23
+ const client = new _pocketbase.default(props.serverURL);
24
+ client.authStore.onChange(async () => {
25
+ await _Storage.StorageService.set(_Storage.StorageService.Constants.COOKIE, client.authStore.exportToCookie());
26
+ setInitialCollections([]);
27
+ setInitialCollections(props.initialCollections);
28
+ });
29
+ _Storage.StorageService.get(_Storage.StorageService.Constants.COOKIE).then(cookie => {
30
+ if (cookie) {
31
+ client.authStore.loadFromCookie(cookie);
32
+ setInitialCollections([]);
33
+ setInitialCollections(props.initialCollections);
34
+ }
35
+ setClient(client);
36
+ });
37
+ }, [props.serverURL]);
20
38
  return client ? /*#__PURE__*/React.createElement(_client.ClientProvider, {
21
39
  client: client
22
40
  }, /*#__PURE__*/React.createElement(_reactRedux.Provider, {
@@ -28,7 +46,7 @@ const Pocketbase = props => {
28
46
  mobileRedirectUrl: props.mobileRedirectUrl,
29
47
  openURL: props.openURL
30
48
  }, /*#__PURE__*/React.createElement(_content.ContentProvider, {
31
- collections: props.initialCollections
49
+ collections: initialCollections
32
50
  }, props.children))))) : null;
33
51
  };
34
52
  exports.Pocketbase = Pocketbase;
@@ -5,7 +5,6 @@ exports.AuthProvider = exports.AuthContext = void 0;
5
5
  var React = _interopRequireWildcard(require("react"));
6
6
  var _useClientContext = require("../hooks/useClientContext");
7
7
  var _Storage = require("../service/Storage");
8
- var _store = require("../store");
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
  const AuthContext = (0, React.createContext)({});
@@ -62,28 +61,12 @@ const AuthProvider = props => {
62
61
  await (client == null ? void 0 : client.users.delete(id));
63
62
  }
64
63
  };
65
- const dispatch = _store.useAppDispatch;
66
- const selector = (0, _store.useAppSelector)(state => state.reducer.auth);
67
64
  React.useEffect(() => {
68
- const listener = client == null ? void 0 : client.authStore.onChange(() => {
69
- const cookie = client == null ? void 0 : client.authStore.exportToCookie();
70
- if (cookie) dispatch(_store.authAction.setCookie(cookie));
71
- });
72
65
  (async () => {
73
66
  const methods = await (client == null ? void 0 : client.users.listAuthMethods());
74
67
  setAuthProviders(methods == null ? void 0 : methods.authProviders);
75
68
  })();
76
- return () => {
77
- if (listener) listener();
78
- };
79
69
  }, [props.webRedirectUrl, props.mobileRedirectUrl]);
80
- React.useEffect(() => {
81
- const cookie = selector.cookie;
82
- if (cookie) {
83
- console.log('cookie', cookie);
84
- client == null ? void 0 : client.authStore.loadFromCookie(cookie);
85
- }
86
- }, [selector.cookie]);
87
70
  return /*#__PURE__*/React.createElement(AuthContext.Provider, {
88
71
  value: actions
89
72
  }, props.children);
@@ -13,6 +13,14 @@ exports.ContentContext = ContentContext;
13
13
  const ContentProvider = props => {
14
14
  const client = (0, _useClientContext.useClientContext)();
15
15
  const dispatch = store.useAppDispatch;
16
+ function tempErrorHandler(error) {
17
+ var _error$originalError;
18
+ // TODO: Handle error
19
+ // IDEA: Create new ErrorContext and Update it with error
20
+ if ((error == null ? void 0 : (_error$originalError = error.originalError) == null ? void 0 : _error$originalError.name) !== 'AbortError') {
21
+ console.log('Error in content provider', JSON.stringify(error));
22
+ }
23
+ }
16
24
  const actions = {
17
25
  subscribe: async collectionName => {
18
26
  await (client == null ? void 0 : client.realtime.subscribe(collectionName, event => {
@@ -31,33 +39,32 @@ const ContentProvider = props => {
31
39
  }
32
40
  }).then(() => {
33
41
  dispatch(_actions.subscriptionsAction.addSubscription(collectionName));
34
- }).catch(_error => {
35
- dispatch(_actions.subscriptionsAction.deleteSubscription(collectionName));
36
- }));
42
+ }).catch(tempErrorHandler));
37
43
  },
38
44
  unsubscribe: async collectionName => {
39
45
  if (collectionName) {
40
46
  await (client == null ? void 0 : client.realtime.unsubscribe(collectionName).then(() => {
41
47
  dispatch(_actions.subscriptionsAction.deleteSubscription(collectionName));
42
- }).catch(_error => {}));
48
+ }).catch(tempErrorHandler));
43
49
  } else {
44
50
  await (client == null ? void 0 : client.realtime.unsubscribe().then(() => {
45
51
  dispatch(_actions.subscriptionsAction.setSubscriptions([]));
46
- }).catch(_error => {}));
52
+ }).catch(tempErrorHandler));
47
53
  }
48
54
  },
49
55
  fetch: async collectionName => {
50
- const records = await (client == null ? void 0 : client.records.getFullList(collectionName, 200).catch(_error => {}));
51
- dispatch(_actions.recordsAction.setRecords(collectionName, records));
56
+ await (client == null ? void 0 : client.records.getFullList(collectionName, 200).then(records => {
57
+ dispatch(_actions.recordsAction.setRecords(collectionName, records));
58
+ }).catch(tempErrorHandler));
52
59
  },
53
60
  create: async (collectionName, record) => {
54
- await (client == null ? void 0 : client.records.create(collectionName, record).catch(_error => {}));
61
+ await (client == null ? void 0 : client.records.create(collectionName, record).catch(tempErrorHandler));
55
62
  },
56
63
  update: async (collectionName, recordId, record) => {
57
- await (client == null ? void 0 : client.records.update(collectionName, recordId, record).catch(_error => {}));
64
+ await (client == null ? void 0 : client.records.update(collectionName, recordId, record).catch(tempErrorHandler));
58
65
  },
59
66
  delete: async (collectionName, recordId) => {
60
- await (client == null ? void 0 : client.records.delete(collectionName, recordId).catch(_error => {}));
67
+ await (client == null ? void 0 : client.records.delete(collectionName, recordId).catch(tempErrorHandler));
61
68
  }
62
69
  };
63
70
  (0, React.useEffect)(() => {
@@ -3,18 +3,17 @@
3
3
  exports.__esModule = true;
4
4
  exports.useAppContent = useAppContent;
5
5
  var _react = require("react");
6
- var store = _interopRequireWildcard(require("../store"));
7
6
  var _context = require("../context");
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; }
7
+ var _store = require("../store");
10
8
  function useAppContent(collectionName, initialFetch = false) {
11
- var _store$useAppSelector;
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;
9
+ const records = (0, _store.useAppSelector)(state => state.reducer.records[collectionName]);
10
+ const subscriptions = (0, _store.useAppSelector)(state => state.reducer.subscriptions);
14
11
  const context = (0, _react.useContext)(_context.ContentContext);
15
12
  (0, _react.useEffect)(() => {
16
13
  if (initialFetch) {
17
- context.fetch(collectionName);
14
+ (async () => {
15
+ await context.fetch(collectionName);
16
+ })();
18
17
  }
19
18
  }, [collectionName, initialFetch]);
20
19
  const [isSubscribed, setIsSubscribed] = (0, _react.useState)(false);
@@ -4,7 +4,6 @@ exports.__esModule = true;
4
4
  exports.useAuth = useAuth;
5
5
  var _react = require("react");
6
6
  var _auth = require("../context/auth");
7
- var _Storage = require("../service/Storage");
8
7
  var _useClientContext = require("./useClientContext");
9
8
  function useAuth() {
10
9
  const client = (0, _useClientContext.useClientContext)();
@@ -15,10 +14,6 @@ function useAuth() {
15
14
  var _client$authStore$mod;
16
15
  setIsSignedIn((client == null ? void 0 : client.authStore.token) !== '');
17
16
  setUser((_client$authStore$mod = client == null ? void 0 : client.authStore.model) != null ? _client$authStore$mod : null);
18
- const cookie = client == null ? void 0 : client.authStore.exportToCookie();
19
- if (cookie) {
20
- _Storage.StorageService.set(_Storage.StorageService.Constants.COOKIE, cookie);
21
- }
22
17
  }
23
18
  (0, _react.useEffect)(() => {
24
19
  updateAuth();
@@ -1,12 +1,10 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.subscriptionsAction = exports.recordsAction = exports.authAction = void 0;
4
+ exports.subscriptionsAction = exports.recordsAction = void 0;
5
5
  var recordsAction = _interopRequireWildcard(require("./records"));
6
6
  exports.recordsAction = recordsAction;
7
7
  var subscriptionsAction = _interopRequireWildcard(require("./subscriptions"));
8
8
  exports.subscriptionsAction = subscriptionsAction;
9
- var authAction = _interopRequireWildcard(require("./auth"));
10
- exports.authAction = authAction;
11
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); }
12
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; }
@@ -5,10 +5,8 @@ exports.appReducer = void 0;
5
5
  var _redux = require("redux");
6
6
  var _records = require("./records");
7
7
  var _subscriptions = require("./subscriptions");
8
- var _auth = require("./auth");
9
8
  const appReducer = (0, _redux.combineReducers)({
10
9
  records: _records.records,
11
- subscriptions: _subscriptions.subscriptions,
12
- auth: _auth.auth
10
+ subscriptions: _subscriptions.subscriptions
13
11
  });
14
12
  exports.appReducer = appReducer;
@@ -6,30 +6,21 @@ var ReduxType = _interopRequireWildcard(require("../types"));
6
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
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
8
  function appendSubscription(subscription, subscriptions) {
9
- return [...subscriptions, subscription];
9
+ return subscriptions.includes(subscription) ? subscriptions : [...subscriptions, subscription];
10
10
  }
11
11
  function deleteSubscription(subscription, subscriptions) {
12
12
  return subscriptions.filter(sub => sub !== subscription);
13
13
  }
14
- const subscriptions = (state = {
15
- subscriptions: []
16
- }, action) => {
17
- const list = state.subscriptions;
14
+ const subscriptions = (state = [], action) => {
18
15
  switch (action.type) {
19
16
  case ReduxType.SET_SUBSCRIPTIONS:
20
17
  if (Array.isArray(action.payload)) {
21
- return {
22
- subscriptions: action.payload
23
- };
18
+ return action.payload;
24
19
  }
25
20
  case ReduxType.ADD_SUBSCRIPTION:
26
- return {
27
- subscriptions: appendSubscription(action.payload, list)
28
- };
21
+ return appendSubscription(action.payload, state);
29
22
  case ReduxType.DELETE_SUBSCRIPTION:
30
- return {
31
- subscriptions: deleteSubscription(action.payload, list)
32
- };
23
+ return deleteSubscription(action.payload, state);
33
24
  default:
34
25
  return state;
35
26
  }
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.UPDATE_RECORD = exports.SET_TOKEN = exports.SET_SUBSCRIPTIONS = exports.SET_RECORDS = exports.SET_MODEL = exports.SET_COOKIE = exports.DELETE_TOKEN = exports.DELETE_SUBSCRIPTION = exports.DELETE_RECORDS = exports.DELETE_RECORD = exports.DELETE_MODEL = exports.DELETE_COOKIE = exports.ADD_SUBSCRIPTION = 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';
@@ -19,16 +19,4 @@ exports.SET_SUBSCRIPTIONS = SET_SUBSCRIPTIONS;
19
19
  const ADD_SUBSCRIPTION = 'ADD_SUBSCRIPTION';
20
20
  exports.ADD_SUBSCRIPTION = ADD_SUBSCRIPTION;
21
21
  const DELETE_SUBSCRIPTION = 'DELETE_SUBSCRIPTION';
22
- exports.DELETE_SUBSCRIPTION = DELETE_SUBSCRIPTION;
23
- const SET_MODEL = 'SET_MODEL';
24
- exports.SET_MODEL = SET_MODEL;
25
- const SET_TOKEN = 'SET_TOKEN';
26
- exports.SET_TOKEN = SET_TOKEN;
27
- const SET_COOKIE = 'SET_COOKIE';
28
- exports.SET_COOKIE = SET_COOKIE;
29
- const DELETE_MODEL = 'DELETE_MODEL';
30
- exports.DELETE_MODEL = DELETE_MODEL;
31
- const DELETE_TOKEN = 'DELETE_TOKEN';
32
- exports.DELETE_TOKEN = DELETE_TOKEN;
33
- const DELETE_COOKIE = 'DELETE_COOKIE';
34
- exports.DELETE_COOKIE = DELETE_COOKIE;
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.20",
3
+ "version": "0.1.22",
4
4
  "description": "Unofficial React SDK (React, React Native, Expo) for interacting with the PocketBase JS SDK",
5
5
  "keywords": [
6
6
  "pocketbase",
@@ -8,7 +8,6 @@ import { ClientProvider } from './client';
8
8
  import { ContentProvider } from './content';
9
9
  import { AuthProvider } from './auth';
10
10
  import { StorageService } from '../service/Storage';
11
- import { authAction } from '../store';
12
11
 
13
12
  export const PocketbaseContext = createContext<PocketBase | null>(null);
14
13
 
@@ -22,7 +21,24 @@ export type PocketbaseProviderProps = {
22
21
  };
23
22
 
24
23
  export const Pocketbase = (props: PocketbaseProviderProps) => {
25
- const client = new PocketBase(props.serverURL);
24
+ const [client, setClient] = React.useState<PocketBase | null>(null);
25
+ const [initialCollections, setInitialCollections] = React.useState<string[]>();
26
+ useEffect(() => {
27
+ const client = new PocketBase(props.serverURL);
28
+ client.authStore.onChange(async () => {
29
+ await StorageService.set(StorageService.Constants.COOKIE, client.authStore.exportToCookie());
30
+ setInitialCollections([]);
31
+ setInitialCollections(props.initialCollections);
32
+ });
33
+ StorageService.get(StorageService.Constants.COOKIE).then((cookie) => {
34
+ if (cookie) {
35
+ client.authStore.loadFromCookie(cookie);
36
+ setInitialCollections([]);
37
+ setInitialCollections(props.initialCollections);
38
+ }
39
+ setClient(client);
40
+ });
41
+ }, [props.serverURL]);
26
42
 
27
43
  return client ? (
28
44
  <ClientProvider client={client}>
@@ -32,9 +48,7 @@ export const Pocketbase = (props: PocketbaseProviderProps) => {
32
48
  webRedirectUrl={props.webRedirectUrl}
33
49
  mobileRedirectUrl={props.mobileRedirectUrl}
34
50
  openURL={props.openURL}>
35
- <ContentProvider collections={props.initialCollections}>
36
- {props.children}
37
- </ContentProvider>
51
+ <ContentProvider collections={initialCollections}>{props.children}</ContentProvider>
38
52
  </AuthProvider>
39
53
  </PersistGate>
40
54
  </Provider>
@@ -1,10 +1,7 @@
1
- import AsyncStorage from '@react-native-async-storage/async-storage';
2
- import { Admin, User } from '@tobicrain/pocketbase';
3
1
  import * as React from 'react';
4
- import { createContext, useEffect } from 'react';
2
+ import { createContext } from 'react';
5
3
  import { useClientContext } from '../hooks/useClientContext';
6
4
  import { StorageService } from '../service/Storage';
7
- import { authAction, store, useAppDispatch, useAppSelector } from '../store';
8
5
 
9
6
  export type AuthProviderInfo = {
10
7
  name: string;
@@ -109,32 +106,13 @@ export const AuthProvider = (props: AuthProviderProps) => {
109
106
  await client?.users.delete(id);
110
107
  },
111
108
  };
112
- const dispatch = useAppDispatch;
113
- const selector = useAppSelector((state) => state.reducer.auth);
114
109
 
115
110
  React.useEffect(() => {
116
- const listener = client?.authStore.onChange(() => {
117
- const cookie = client?.authStore.exportToCookie();
118
- if (cookie) dispatch(authAction.setCookie(cookie));
119
- });
120
-
121
111
  (async () => {
122
112
  const methods = await client?.users.listAuthMethods();
123
113
  setAuthProviders(methods?.authProviders);
124
114
  })();
125
-
126
- return () => {
127
- if (listener) listener();
128
- };
129
115
  }, [props.webRedirectUrl, props.mobileRedirectUrl]);
130
116
 
131
- React.useEffect(() => {
132
- const cookie = selector.cookie;
133
- if (cookie) {
134
- console.log('cookie', cookie);
135
- client?.authStore.loadFromCookie(cookie);
136
- }
137
- }, [selector.cookie]);
138
-
139
117
  return <AuthContext.Provider value={actions}>{props.children}</AuthContext.Provider>;
140
118
  };
@@ -38,6 +38,13 @@ export const ContentProvider = (props: ContentProviderProps) => {
38
38
  const client = useClientContext();
39
39
  const dispatch = store.useAppDispatch;
40
40
 
41
+ function tempErrorHandler(error: any) {
42
+ // TODO: Handle error
43
+ // IDEA: Create new ErrorContext and Update it with error
44
+ if (error?.originalError?.name !== 'AbortError') {
45
+ console.log('Error in content provider', JSON.stringify(error));
46
+ }
47
+ }
41
48
  const actions: ContentActions = {
42
49
  subscribe: async (collectionName: string) => {
43
50
  await client?.realtime
@@ -59,9 +66,7 @@ export const ContentProvider = (props: ContentProviderProps) => {
59
66
  .then(() => {
60
67
  dispatch(subscriptionsAction.addSubscription(collectionName));
61
68
  })
62
- .catch((_error) => {
63
- dispatch(subscriptionsAction.deleteSubscription(collectionName));
64
- });
69
+ .catch(tempErrorHandler);
65
70
  },
66
71
  unsubscribe: async (collectionName?: string) => {
67
72
  if (collectionName) {
@@ -70,28 +75,32 @@ export const ContentProvider = (props: ContentProviderProps) => {
70
75
  .then(() => {
71
76
  dispatch(subscriptionsAction.deleteSubscription(collectionName));
72
77
  })
73
- .catch((_error) => {});
78
+ .catch(tempErrorHandler);
74
79
  } else {
75
80
  await client?.realtime
76
81
  .unsubscribe()
77
82
  .then(() => {
78
83
  dispatch(subscriptionsAction.setSubscriptions([]));
79
84
  })
80
- .catch((_error) => {});
85
+ .catch(tempErrorHandler);
81
86
  }
82
87
  },
83
88
  fetch: async (collectionName: string) => {
84
- const records = await client?.records.getFullList(collectionName, 200).catch((_error) => {});
85
- dispatch(recordsAction.setRecords(collectionName, records as Record[]));
89
+ await client?.records
90
+ .getFullList(collectionName, 200)
91
+ .then((records) => {
92
+ dispatch(recordsAction.setRecords(collectionName, records as Record[]));
93
+ })
94
+ .catch(tempErrorHandler);
86
95
  },
87
96
  create: async (collectionName: string, record: {}) => {
88
- await client?.records.create(collectionName, record).catch((_error) => {});
97
+ await client?.records.create(collectionName, record).catch(tempErrorHandler);
89
98
  },
90
99
  update: async (collectionName: string, recordId: string, record: {}) => {
91
- await client?.records.update(collectionName, recordId, record).catch((_error) => {});
100
+ await client?.records.update(collectionName, recordId, record).catch(tempErrorHandler);
92
101
  },
93
102
  delete: async (collectionName: string, recordId: string) => {
94
- await client?.records.delete(collectionName, recordId).catch((_error) => {});
103
+ await client?.records.delete(collectionName, recordId).catch(tempErrorHandler);
95
104
  },
96
105
  };
97
106
 
@@ -3,6 +3,7 @@ import * as store from '../store';
3
3
  import { ContentContext } from '../context';
4
4
  import { Record } from '../interfaces/Record';
5
5
  import { StorageService } from '../service/Storage';
6
+ import { useAppSelector } from '../store';
6
7
 
7
8
  export type SubscribeType = () => Promise<void | undefined>;
8
9
  export type UnsubscribeType = () => Promise<void | undefined>;
@@ -24,14 +25,15 @@ export function useAppContent<T extends Record>(
24
25
  collectionName: string,
25
26
  initialFetch: boolean = false
26
27
  ): { records: T[]; actions: Actions; isSubscribed: boolean } {
27
- const records = (store.useAppSelector((state) => state.reducer.records[collectionName]) ??
28
- []) as T[];
29
- const subscriptions = store.useAppSelector((state) => state.reducer.subscriptions).subscriptions;
28
+ const records = useAppSelector((state) => state.reducer.records[collectionName]) as T[];
29
+ const subscriptions = useAppSelector((state) => state.reducer.subscriptions);
30
30
  const context = useContext(ContentContext);
31
31
 
32
32
  useEffect(() => {
33
33
  if (initialFetch) {
34
- context.fetch(collectionName);
34
+ (async () => {
35
+ await context.fetch(collectionName);
36
+ })();
35
37
  }
36
38
  }, [collectionName, initialFetch]);
37
39
 
@@ -19,10 +19,6 @@ export function useAuth(): AuthContextInterface {
19
19
  function updateAuth() {
20
20
  setIsSignedIn(client?.authStore.token !== '');
21
21
  setUser(client?.authStore.model ?? null);
22
- const cookie = client?.authStore.exportToCookie();
23
- if (cookie) {
24
- StorageService.set(StorageService.Constants.COOKIE, cookie);
25
- }
26
22
  }
27
23
 
28
24
  useEffect(() => {
@@ -1,5 +1,4 @@
1
1
  import * as recordsAction from './records';
2
2
  import * as subscriptionsAction from './subscriptions';
3
- import * as authAction from './auth';
4
3
 
5
- export { recordsAction, subscriptionsAction, authAction };
4
+ export { recordsAction, subscriptionsAction };
@@ -1,16 +1,10 @@
1
1
  import { combineReducers } from 'redux';
2
2
  import { records } from './records';
3
3
  import { subscriptions } from './subscriptions';
4
- import { auth } from './auth';
5
4
 
6
5
  export const appReducer = combineReducers({
7
- records: records,
8
- subscriptions: subscriptions,
9
- auth: auth,
6
+ records,
7
+ subscriptions,
10
8
  });
11
9
 
12
- interface AppReducer {
13
- records: ReturnType<typeof appReducer>;
14
- }
15
-
16
- export type State = AppReducer;
10
+ export type State = ReturnType<typeof appReducer>;
@@ -1,46 +1,29 @@
1
1
  import * as ReduxType from '../types';
2
2
 
3
- export interface ReduxSubscriptions {
4
- subscriptions: string[];
5
- }
6
-
7
3
  export type SubscriptionAction = {
8
4
  type: ReduxType.SubscriptionsTypes;
9
5
  payload: string | string[];
10
6
  };
11
7
 
12
8
  function appendSubscription(subscription: string, subscriptions: string[]): string[] {
13
- return [...subscriptions, subscription];
9
+ return subscriptions.includes(subscription) ? subscriptions : [...subscriptions, subscription];
14
10
  }
15
11
 
16
12
  function deleteSubscription(subscription: string, subscriptions: string[]): string[] {
17
13
  return subscriptions.filter((sub) => sub !== subscription);
18
14
  }
19
15
 
20
- export const subscriptions = (
21
- state: ReduxSubscriptions = {
22
- subscriptions: [],
23
- },
24
- action: SubscriptionAction
25
- ) => {
26
- const list = state.subscriptions;
27
-
16
+ export const subscriptions = (state: string[] = [], action: SubscriptionAction) => {
28
17
  switch (action.type) {
29
18
  case ReduxType.SET_SUBSCRIPTIONS:
30
19
  if (Array.isArray(action.payload)) {
31
- return {
32
- subscriptions: action.payload,
33
- };
20
+ return action.payload as string[];
34
21
  }
35
22
  case ReduxType.ADD_SUBSCRIPTION:
36
- return {
37
- subscriptions: appendSubscription(action.payload as string, list),
38
- };
23
+ return appendSubscription(action.payload as string, state) as string[];
39
24
  case ReduxType.DELETE_SUBSCRIPTION:
40
- return {
41
- subscriptions: deleteSubscription(action.payload as string, list),
42
- };
25
+ return deleteSubscription(action.payload as string, state) as string[];
43
26
  default:
44
- return state;
27
+ return state as string[];
45
28
  }
46
29
  };
@@ -21,18 +21,3 @@ export type SubscriptionsTypes =
21
21
  | typeof SET_SUBSCRIPTIONS
22
22
  | typeof ADD_SUBSCRIPTION
23
23
  | typeof DELETE_SUBSCRIPTION;
24
-
25
- export const SET_MODEL = 'SET_MODEL';
26
- export const SET_TOKEN = 'SET_TOKEN';
27
- export const SET_COOKIE = 'SET_COOKIE';
28
- export const DELETE_MODEL = 'DELETE_MODEL';
29
- export const DELETE_TOKEN = 'DELETE_TOKEN';
30
- export const DELETE_COOKIE = 'DELETE_COOKIE';
31
-
32
- export type AuthTypes =
33
- | typeof SET_MODEL
34
- | typeof SET_TOKEN
35
- | typeof DELETE_MODEL
36
- | typeof DELETE_TOKEN
37
- | typeof SET_COOKIE
38
- | typeof DELETE_COOKIE;
@@ -1,23 +0,0 @@
1
- import PocketBase from '@tobicrain/pocketbase';
2
- export declare class AuthenticationService {
3
- private client;
4
- redirect_url: string;
5
- constructor(client: PocketBase);
6
- getListAuthMethods(): Promise<{
7
- [key: string]: any;
8
- emailPassword: boolean;
9
- authProviders: {
10
- name: string;
11
- state: string;
12
- codeVerifier: string;
13
- codeChallenge: string;
14
- codeChallengeMethod: string;
15
- authUrl: string;
16
- }[];
17
- }>;
18
- getDataAuth(): Promise<void>;
19
- AuthWithOauth(provider: string, code: string, verifier: string): Promise<void>;
20
- AuthWithEmail(email: string, password: string): Promise<void>;
21
- RegisterWithEmail(email: string, password: string): Promise<void>;
22
- getUserData(id: string, token: string): Promise<import("@tobicrain/pocketbase").User>;
23
- }
@@ -1,32 +0,0 @@
1
- export class AuthenticationService {
2
- constructor(client) {
3
- this.client = void 0;
4
- this.redirect_url = void 0;
5
- this.client = client;
6
- this.redirect_url = '';
7
- }
8
- async getListAuthMethods() {
9
- return await this.client.users.listAuthMethods();
10
- }
11
- async getDataAuth() {
12
- await this.client.users.authViaOAuth2('google', 'CODE', 'VERIFIER', 'REDIRECT_URL');
13
- }
14
- async AuthWithOauth(provider, code, verifier) {
15
- await this.client.users.authViaOAuth2(provider, code, verifier, this.redirect_url);
16
- }
17
- async AuthWithEmail(email, password) {
18
- await this.client.users.authViaEmail(email, password);
19
- }
20
- async RegisterWithEmail(email, password) {
21
- await this.client.users.create({
22
- email: email,
23
- password: password,
24
- passwordConfirm: password
25
- });
26
- }
27
- async getUserData(id, token) {
28
- const headers = new Headers();
29
- headers.append('Authorization', 'user ' + token);
30
- return await this.client.users.getOne(id);
31
- }
32
- }