Skip to content

Commit

Permalink
User stays logged in correctly, error get logged -> TODO: context->co…
Browse files Browse the repository at this point in the history
…ntent:42-43
  • Loading branch information
tobicrain committed Oct 17, 2022
1 parent f6483df commit 6385fd9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pocketbase-react",
"version": "0.1.21",
"version": "0.1.22",
"description": "Unofficial React SDK (React, React Native, Expo) for interacting with the PocketBase JS SDK",
"keywords": [
"pocketbase",
Expand Down
3 changes: 3 additions & 0 deletions src/context/Pocketbase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ export const Pocketbase = (props: PocketbaseProviderProps) => {
const client = new PocketBase(props.serverURL);
client.authStore.onChange(async () => {
await StorageService.set(StorageService.Constants.COOKIE, client.authStore.exportToCookie());
setInitialCollections([]);
setInitialCollections(props.initialCollections);
});
StorageService.get(StorageService.Constants.COOKIE).then((cookie) => {
if (cookie) {
client.authStore.loadFromCookie(cookie);
setInitialCollections([]);
setInitialCollections(props.initialCollections);
}
setClient(client);
});
Expand Down
29 changes: 19 additions & 10 deletions src/context/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export const ContentProvider = (props: ContentProviderProps) => {
const client = useClientContext();
const dispatch = store.useAppDispatch;

function tempErrorHandler(error: any) {
// TODO: Handle error
// IDEA: Create new ErrorContext and Update it with error
if (error?.originalError?.name !== 'AbortError') {
console.log('Error in content provider', JSON.stringify(error));
}
}
const actions: ContentActions = {
subscribe: async (collectionName: string) => {
await client?.realtime
Expand All @@ -59,9 +66,7 @@ export const ContentProvider = (props: ContentProviderProps) => {
.then(() => {
dispatch(subscriptionsAction.addSubscription(collectionName));
})
.catch((_error) => {
dispatch(subscriptionsAction.deleteSubscription(collectionName));
});
.catch(tempErrorHandler);
},
unsubscribe: async (collectionName?: string) => {
if (collectionName) {
Expand All @@ -70,28 +75,32 @@ export const ContentProvider = (props: ContentProviderProps) => {
.then(() => {
dispatch(subscriptionsAction.deleteSubscription(collectionName));
})
.catch((_error) => {});
.catch(tempErrorHandler);
} else {
await client?.realtime
.unsubscribe()
.then(() => {
dispatch(subscriptionsAction.setSubscriptions([]));
})
.catch((_error) => {});
.catch(tempErrorHandler);
}
},
fetch: async (collectionName: string) => {
const records = await client?.records.getFullList(collectionName, 200).catch((_error) => {});
dispatch(recordsAction.setRecords(collectionName, records as Record[]));
await client?.records
.getFullList(collectionName, 200)
.then((records) => {
dispatch(recordsAction.setRecords(collectionName, records as Record[]));
})
.catch(tempErrorHandler);
},
create: async (collectionName: string, record: {}) => {
await client?.records.create(collectionName, record).catch((_error) => {});
await client?.records.create(collectionName, record).catch(tempErrorHandler);
},
update: async (collectionName: string, recordId: string, record: {}) => {
await client?.records.update(collectionName, recordId, record).catch((_error) => {});
await client?.records.update(collectionName, recordId, record).catch(tempErrorHandler);
},
delete: async (collectionName: string, recordId: string) => {
await client?.records.delete(collectionName, recordId).catch((_error) => {});
await client?.records.delete(collectionName, recordId).catch(tempErrorHandler);
},
};

Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useAppContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export function useAppContent<T extends Record>(

useEffect(() => {
if (initialFetch) {
context.fetch(collectionName);
(async () => {
await context.fetch(collectionName);
})();
}
}, [collectionName, initialFetch]);

Expand Down
4 changes: 0 additions & 4 deletions src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ export function useAuth(): AuthContextInterface {
function updateAuth() {
setIsSignedIn(client?.authStore.token !== '');
setUser(client?.authStore.model ?? null);
const cookie = client?.authStore.exportToCookie();
if (cookie) {
StorageService.set(StorageService.Constants.COOKIE, cookie);
}
}

useEffect(() => {
Expand Down

0 comments on commit 6385fd9

Please sign in to comment.