Skip to content

Commit

Permalink
Fixed pagination - save and clear all cursors for resource
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamFilipek92 committed May 25, 2020
1 parent 3a297aa commit 2cff86d
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/providers/database/FirebaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class FirebaseClient implements IFirebaseClient {

if (snapshots.docs.length === 0) {
log("apiGetListLazy - result", { message: 'There are not records for given query' });
return { data: [], total: 100000 };
return { data: [], total: 0 };
}

const data = snapshots.docs.map(doc => parseFireStoreDocument(doc));
Expand Down Expand Up @@ -171,6 +171,7 @@ export class FirebaseClient implements IFirebaseClient {
await this.addUpdatedByFields(docObj);
log("apiCreate", { docObj });
await r.collection.doc(id).set(docObj, { merge: false });
this.clearQueryCursors(resourceName);
return {
data: {
...data,
Expand Down Expand Up @@ -199,6 +200,7 @@ export class FirebaseClient implements IFirebaseClient {
.catch(error => {
logError("apiUpdate error", { error });
});
this.clearQueryCursors(resourceName);
return {
data: {
...data,
Expand Down Expand Up @@ -229,6 +231,7 @@ export class FirebaseClient implements IFirebaseClient {
};
})
);
this.clearQueryCursors(resourceName);
return {
data: returnData
};
Expand All @@ -248,6 +251,7 @@ export class FirebaseClient implements IFirebaseClient {
.catch(error => {
logError("apiSoftDelete error", { error });
});
this.clearQueryCursors(resourceName);
return {
data: {
...params.previousData,
Expand Down Expand Up @@ -278,6 +282,7 @@ export class FirebaseClient implements IFirebaseClient {
};
})
);
this.clearQueryCursors(resourceName);
return {
data: returnData
};
Expand All @@ -295,6 +300,7 @@ export class FirebaseClient implements IFirebaseClient {
} catch (error) {
throw new Error(error)
}
this.clearQueryCursors(resourceName);
return {
data: params.previousData
};
Expand All @@ -316,6 +322,7 @@ export class FirebaseClient implements IFirebaseClient {
} catch (error) {
throw new Error(error)
}
this.clearQueryCursors(resourceName);
return { data: returnData };
}
public async apiGetMany(
Expand Down Expand Up @@ -416,13 +423,14 @@ export class FirebaseClient implements IFirebaseClient {
const key = btoa(JSON.stringify({ ...params, resourceName }));
localStorage.setItem(key, doc.id);

const localCursorKeys = localStorage.getItem('ra-firebase-cursor-keys');
const allCursorsKey = `ra-firebase-cursor-keys_${resourceName}`;
const localCursorKeys = localStorage.getItem(allCursorsKey);
if (!localCursorKeys) {
localStorage.setItem('ra-firebase-cursor-keys', JSON.stringify([key]));
localStorage.setItem(allCursorsKey, JSON.stringify([key]));
} else {
const cursors: string[] = JSON.parse(localCursorKeys);
const newCursors = cursors.concat(key);
localStorage.setItem('ra-firebase-cursor-keys', JSON.stringify(newCursors));
localStorage.setItem(allCursorsKey, JSON.stringify(newCursors));
}
}

Expand All @@ -441,12 +449,15 @@ export class FirebaseClient implements IFirebaseClient {
return doc.exists && doc;
}

private clearQueryCursors() {
const localCursorKeys = localStorage.getItem('ra-firebase-cursor-keys');
if (localCursorKeys) {
const cursors: string[] = JSON.parse(localCursorKeys);
cursors.forEach(cursor => localStorage.removeItem(cursor));
localStorage.removeItem('ra-firebase-cursor-keys');
private clearQueryCursors(resourceName: string) {
if (this.options.lazyLoading) {
const allCursorsKey = `ra-firebase-cursor-keys_${resourceName}`;
const localCursorKeys = localStorage.getItem(allCursorsKey);
if (localCursorKeys) {
const cursors: string[] = JSON.parse(localCursorKeys);
cursors.forEach(cursor => localStorage.removeItem(cursor));
localStorage.removeItem(allCursorsKey);
}
}
}

Expand Down

0 comments on commit 2cff86d

Please sign in to comment.