ROWY-795 change useFirestoreDocWithAtom createIfNonExistent behavior to prevent existing documents being reset

This commit is contained in:
Sidney Alcantara
2022-11-30 15:37:43 +11:00
parent d7065ad8bb
commit 9d146d7478

View File

@@ -88,15 +88,12 @@ export function useFirestoreDocWithAtom<T = TableRow>(
{ includeMetadataChanges: true },
(docSnapshot) => {
try {
// Create doc if it doesnt exist and were online
// WARNING: If offline and we doc doesnt exist in cache, it will
// ovewrite with default values when we go online
if (
!docSnapshot.exists() &&
!!createIfNonExistent &&
!docSnapshot.metadata.fromCache
) {
setDoc(docSnapshot.ref, createIfNonExistent);
// If doc doesnt exist, set data atom to default value
// But dont create a new document in db, since this has previously
// caused documents to be reset, and the bug is hard to reproduce.
// Instead, when the user updates the document, it will be created.
if (!docSnapshot.exists() && !!createIfNonExistent) {
// Temporarily set the data atom to the default data
setDataAtom({ ...createIfNonExistent, _rowy_ref: docSnapshot.ref });
} else {
setDataAtom({