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
@@ -4,12 +4,12 @@ import { ContentContext } from '../context';
4
4
  import { Record } from '../interfaces/Record';
5
5
  import { StorageService } from '../service/Storage';
6
6
 
7
- export type SubscribeType = () => Promise<void | undefined>
8
- export type UnsubscribeType = () => void | undefined;
9
- export type FetchType = () => Promise<void | undefined>
10
- export type CreateType = (record: {}) => Promise<void | undefined>
11
- export type UpdateType = (id: string, record: {}) => Promise<void | undefined>
12
- export type DeleteType = (id: string) => Promise<void | undefined>
7
+ export type SubscribeType = () => Promise<void | undefined>;
8
+ export type UnsubscribeType = () => Promise<void | undefined>;
9
+ export type FetchType = () => Promise<void | undefined>;
10
+ export type CreateType = (record: {}) => Promise<void | undefined>;
11
+ export type UpdateType = (id: string, record: {}) => Promise<void | undefined>;
12
+ export type DeleteType = (id: string) => Promise<void | undefined>;
13
13
 
14
14
  export interface Actions {
15
15
  subscribe: SubscribeType;
@@ -23,43 +23,32 @@ export interface Actions {
23
23
  export function useAppContent<T extends Record>(
24
24
  collectionName: string,
25
25
  initialFetch: boolean = false
26
- ): {records: T[], actions: Actions, isSubscribed: boolean} {
27
- const records = (store.useAppSelector((state) => state.reducer.records[collectionName]) ?? []) as T[];
26
+ ): { 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
30
  const context = useContext(ContentContext);
29
31
 
30
32
  useEffect(() => {
31
33
  if (initialFetch) {
32
- context.actions.fetch(collectionName);
34
+ context.fetch(collectionName);
33
35
  }
34
36
  }, [collectionName, initialFetch]);
35
37
 
36
38
  const [isSubscribed, setIsSubscribed] = useState(false);
37
-
38
- async function refetchSubscribeState() {
39
- const isSubscribed = JSON.parse(await StorageService.get("subscribed") ?? JSON.stringify([])) as string[];
40
- setIsSubscribed(isSubscribed.includes(collectionName));
41
- }
42
39
 
43
40
  useEffect(() => {
44
- (async () => {
45
- await refetchSubscribeState();
46
- })();
47
- }, [])
48
-
41
+ setIsSubscribed(subscriptions.includes(collectionName));
42
+ }, [subscriptions]);
49
43
 
50
44
  const actions: Actions = {
51
- subscribe: async () => {
52
- await context.actions.subscribe(collectionName)
53
- await refetchSubscribeState()
54
- },
55
- unsubscribe: () => {
56
- context.actions.unsubscribe(collectionName)
57
- refetchSubscribeState()
58
- },
59
- fetch: async () => await context.actions.fetch(collectionName),
60
- create: async (record: {}) => await context.actions.create(collectionName, record),
61
- update: async (id: string, record: {}) => await context.actions.update(collectionName, id, record),
62
- delete: async (id: string) => await context.actions.delete(collectionName, id),
45
+ subscribe: async () => await context.subscribe(collectionName),
46
+ unsubscribe: async () => await context.unsubscribe(collectionName),
47
+ fetch: async () => await context.fetch(collectionName),
48
+ create: async (record: {}) => await context.create(collectionName, record),
49
+ update: async (id: string, record: {}) => await context.update(collectionName, id, record),
50
+ delete: async (id: string) => await context.delete(collectionName, id),
63
51
  };
52
+
64
53
  return { records, actions, isSubscribed };
65
54
  }
@@ -7,5 +7,4 @@ const useClientContext = () => {
7
7
  return context;
8
8
  };
9
9
 
10
-
11
- export { useClientContext };
10
+ export { useClientContext };
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from './hooks';
2
2
  export * from './interfaces';
3
3
  export * from './context';
4
- export * from './store';
4
+ export * from './store';
@@ -1 +1 @@
1
- export * from './Record';
1
+ export * from './Record';
@@ -1,9 +1,9 @@
1
- import PocketBase from "@tobicrain/pocketbase";
1
+ import PocketBase from '@tobicrain/pocketbase';
2
2
 
3
3
  export class AuthenticationService {
4
4
  private client: PocketBase;
5
5
  public redirect_url: string;
6
-
6
+
7
7
  constructor(client: PocketBase) {
8
8
  this.client = client;
9
9
  this.redirect_url = '';
@@ -14,37 +14,27 @@ export class AuthenticationService {
14
14
  }
15
15
 
16
16
  async getDataAuth() {
17
- await this.client.users.authViaOAuth2(
18
- 'google',
19
- 'CODE',
20
- 'VERIFIER',
21
- 'REDIRECT_URL'
22
- );
17
+ await this.client.users.authViaOAuth2('google', 'CODE', 'VERIFIER', 'REDIRECT_URL');
23
18
  }
24
19
  async AuthWithOauth(provider: string, code: string, verifier: string) {
25
- await this.client.users.authViaOAuth2(
26
- provider,
27
- code,
28
- verifier,
29
- this.redirect_url
30
- );
20
+ await this.client.users.authViaOAuth2(provider, code, verifier, this.redirect_url);
31
21
  }
32
22
 
33
- async AuthWithEmail(email: string, password: string){
23
+ async AuthWithEmail(email: string, password: string) {
34
24
  await this.client.users.authViaEmail(email, password);
35
25
  }
36
26
 
37
- async RegisterWithEmail(email: string, password: string){
27
+ async RegisterWithEmail(email: string, password: string) {
38
28
  await this.client.users.create({
39
29
  email: email,
40
30
  password: password,
41
31
  passwordConfirm: password,
42
- });
32
+ });
43
33
  }
44
34
 
45
- async getUserData(id:string, token:string) {
46
- const headers = new Headers()
47
- headers.append("Authorization","user "+token)
48
- return await this.client.users.getOne(id)
35
+ async getUserData(id: string, token: string) {
36
+ const headers = new Headers();
37
+ headers.append('Authorization', 'user ' + token);
38
+ return await this.client.users.getOne(id);
49
39
  }
50
- }
40
+ }
@@ -1,23 +1,29 @@
1
- import AsyncStorage from "@react-native-async-storage/async-storage";
1
+ import AsyncStorage from '@react-native-async-storage/async-storage';
2
2
 
3
3
  export class StorageService {
4
- static async get(key: string): Promise<string | null> {
5
- return typeof document !== 'undefined' ? localStorage.getItem(key) : await AsyncStorage.getItem(key);
6
- }
4
+ static Constants = {
5
+ SUBSCRIBED: 'subscribed',
6
+ };
7
+
8
+ static async get(key: string): Promise<string | null> {
9
+ return typeof document !== 'undefined'
10
+ ? localStorage.getItem(key)
11
+ : await AsyncStorage.getItem(key);
12
+ }
7
13
 
8
- static async set(key: string, value: string): Promise<void> {
9
- if (typeof document !== 'undefined') {
10
- return localStorage.setItem(key, value);
11
- } else {
12
- return await AsyncStorage.setItem(key, value);
13
- }
14
+ static async set(key: string, value: string): Promise<void> {
15
+ if (typeof document !== 'undefined') {
16
+ return localStorage.setItem(key, value);
17
+ } else {
18
+ return await AsyncStorage.setItem(key, value);
14
19
  }
20
+ }
15
21
 
16
- static async remove(key: string): Promise<void> {
17
- if (typeof document !== 'undefined') {
18
- return localStorage.removeItem(key);
19
- } else {
20
- return await AsyncStorage.removeItem(key);
21
- }
22
+ static async remove(key: string): Promise<void> {
23
+ if (typeof document !== 'undefined') {
24
+ return localStorage.removeItem(key);
25
+ } else {
26
+ return await AsyncStorage.removeItem(key);
22
27
  }
23
- }
28
+ }
29
+ }
@@ -1,3 +1,4 @@
1
1
  import * as recordsAction from './records';
2
+ import * as subscriptionsAction from './subscriptions';
2
3
 
3
- export { recordsAction };
4
+ export { recordsAction, subscriptionsAction };
@@ -44,11 +44,4 @@ const updateRecord = (key: string, payload: Record) =>
44
44
  payload,
45
45
  } as RecordAction);
46
46
 
47
- export {
48
- setRecords,
49
- addRecord,
50
- addRecords,
51
- deleteRecord,
52
- deleteRecords,
53
- updateRecord,
54
- };
47
+ export { setRecords, addRecord, addRecords, deleteRecord, deleteRecords, updateRecord };
@@ -0,0 +1,22 @@
1
+ import { SubscriptionAction } from '../reducers/subscriptions';
2
+ import * as ReduxType from '../types';
3
+
4
+ const setSubscriptions = (payload: string[]) =>
5
+ ({
6
+ type: ReduxType.SET_SUBSCRIPTIONS,
7
+ payload,
8
+ } as SubscriptionAction);
9
+
10
+ const addSubscription = (payload: string) =>
11
+ ({
12
+ type: ReduxType.ADD_SUBSCRIPTION,
13
+ payload,
14
+ } as SubscriptionAction);
15
+
16
+ const deleteSubscription = (payload: string) =>
17
+ ({
18
+ type: ReduxType.DELETE_SUBSCRIPTION,
19
+ payload,
20
+ } as SubscriptionAction);
21
+
22
+ export { setSubscriptions, addSubscription, deleteSubscription };
@@ -1,4 +1,4 @@
1
1
  export * from './actions';
2
2
  export * from './reducers';
3
3
  export * from './types';
4
- export * from './store';
4
+ export * from './store';
@@ -1,12 +1,14 @@
1
1
  import { combineReducers } from 'redux';
2
2
  import { records } from './records';
3
+ import { subscriptions } from './subscriptions';
3
4
 
4
5
  export const appReducer = combineReducers({
5
6
  records: records,
7
+ subscriptions: subscriptions,
6
8
  });
7
9
 
8
10
  interface AppReducer {
9
11
  records: ReturnType<typeof appReducer>;
10
12
  }
11
13
 
12
- export type State = AppReducer;
14
+ export type State = AppReducer;
@@ -6,7 +6,7 @@ export interface ReduxRecord {
6
6
  }
7
7
 
8
8
  export type RecordAction = {
9
- type: ReduxType.Types;
9
+ type: ReduxType.RecordTypes;
10
10
  key: string;
11
11
  payload: null | Record | Record[];
12
12
  };
@@ -75,4 +75,4 @@ export const records = (state: ReduxRecord = {}, action: RecordAction) => {
75
75
  default:
76
76
  return state;
77
77
  }
78
- };
78
+ };
@@ -0,0 +1,46 @@
1
+ import * as ReduxType from '../types';
2
+
3
+ export interface ReduxSubscriptions {
4
+ subscriptions: string[];
5
+ }
6
+
7
+ export type SubscriptionAction = {
8
+ type: ReduxType.SubscriptionsTypes;
9
+ payload: string | string[];
10
+ };
11
+
12
+ function appendSubscription(subscription: string, subscriptions: string[]): string[] {
13
+ return [...subscriptions, subscription];
14
+ }
15
+
16
+ function deleteSubscription(subscription: string, subscriptions: string[]): string[] {
17
+ return subscriptions.filter((sub) => sub !== subscription);
18
+ }
19
+
20
+ export const subscriptions = (
21
+ state: ReduxSubscriptions = {
22
+ subscriptions: [],
23
+ },
24
+ action: SubscriptionAction
25
+ ) => {
26
+ const list = state.subscriptions;
27
+
28
+ switch (action.type) {
29
+ case ReduxType.SET_SUBSCRIPTIONS:
30
+ if (Array.isArray(action.payload)) {
31
+ return {
32
+ subscriptions: action.payload,
33
+ };
34
+ }
35
+ case ReduxType.ADD_SUBSCRIPTION:
36
+ return {
37
+ subscriptions: appendSubscription(action.payload as string, list),
38
+ };
39
+ case ReduxType.DELETE_SUBSCRIPTION:
40
+ return {
41
+ subscriptions: deleteSubscription(action.payload as string, list),
42
+ };
43
+ default:
44
+ return state;
45
+ }
46
+ };
@@ -4,7 +4,6 @@ import { TypedUseSelectorHook, useSelector } from 'react-redux';
4
4
  import { appReducer } from './reducers';
5
5
  import thunk from 'redux-thunk';
6
6
  import { RecordAction } from './reducers/records';
7
- import AsyncStorage from '@react-native-async-storage/async-storage';
8
7
  import { StorageService } from '../service/Storage';
9
8
 
10
9
  interface Storage {
@@ -13,22 +12,21 @@ interface Storage {
13
12
  removeItem(key: string, ...args: Array<any>): any;
14
13
  }
15
14
 
16
-
17
15
  const CustomStorage: Storage = {
18
16
  getItem: async (key: string, ..._args: Array<any>) => {
19
17
  return await StorageService.get(key);
20
18
  },
21
19
  setItem: async (key: string, value: any, ..._args: Array<any>) => {
22
- return StorageService.set(key, value);
20
+ return await StorageService.set(key, value);
23
21
  },
24
22
  removeItem: async (key: string, ..._args: Array<any>) => {
25
- return StorageService.remove(key);
23
+ return await StorageService.remove(key);
26
24
  },
27
25
  };
28
26
 
29
27
  export const persistConfig = {
30
28
  key: 'root',
31
- storage: CustomStorage
29
+ storage: CustomStorage,
32
30
  };
33
31
 
34
32
  const reducer = combineReducers({
@@ -45,7 +43,6 @@ const useAppDispatch = store.dispatch;
45
43
  type RootState = ReturnType<typeof store.getState>;
46
44
  const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
47
45
 
48
-
49
46
  const persistor = persistStore(store);
50
47
 
51
- export { AppDispatch, RootState, useAppDispatch, useAppSelector, store, persistor };
48
+ export { AppDispatch, RootState, useAppDispatch, useAppSelector, store, persistor };
@@ -5,10 +5,19 @@ export const UPDATE_RECORD = 'UPDATE_RECORD';
5
5
  export const DELETE_RECORD = 'DELETE_RECORD';
6
6
  export const DELETE_RECORDS = 'DELETE_RECORDS';
7
7
 
8
- export type Types =
8
+ export type RecordTypes =
9
9
  | typeof SET_RECORDS
10
10
  | typeof ADD_RECORD
11
11
  | typeof ADD_RECORDS
12
12
  | typeof UPDATE_RECORD
13
13
  | typeof DELETE_RECORD
14
14
  | typeof DELETE_RECORDS;
15
+
16
+ export const SET_SUBSCRIPTIONS = 'SET_SUBSCRIPTIONS';
17
+ export const ADD_SUBSCRIPTION = 'ADD_SUBSCRIPTION';
18
+ export const DELETE_SUBSCRIPTION = 'DELETE_SUBSCRIPTION';
19
+
20
+ export type SubscriptionsTypes =
21
+ | typeof SET_SUBSCRIPTIONS
22
+ | typeof ADD_SUBSCRIPTION
23
+ | typeof DELETE_SUBSCRIPTION;