From 279ea8a11838890e0bca97657ba76595bd3a9fa2 Mon Sep 17 00:00:00 2001 From: joergmlpts <66563749+joergmlpts@users.noreply.github.com> Date: Tue, 16 Jan 2024 18:36:32 -0800 Subject: [PATCH] comprehensive fix for Realm objects in observations; fix for issue #1031 (#1033) I agree this is a better solution, though I wish neither solution was really necessary. We still need to watch out for nested Realm.Objects, I think. --- src/realmModels/Observation.js | 4 +--- src/stores/useStore.js | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/realmModels/Observation.js b/src/realmModels/Observation.js index 004b207b2..30258268b 100644 --- a/src/realmModels/Observation.js +++ b/src/realmModels/Observation.js @@ -270,9 +270,7 @@ class Observation extends Realm.Object { }; static appendObsPhotos = ( obsPhotos, currentObservation ) => { - const updatedObs = ( currentObservation instanceof Realm.Object - ? currentObservation.toJSON( ) - : currentObservation ); + const updatedObs = currentObservation; // need empty case for when a user creates an observation with no photos, // then tries to add photos to observation later diff --git a/src/stores/useStore.js b/src/stores/useStore.js index f6087b742..4006dd759 100644 --- a/src/stores/useStore.js +++ b/src/stores/useStore.js @@ -1,4 +1,5 @@ // eslint-disable-next-line +import { Realm } from "@realm/react"; import { create } from "zustand"; import _ from "lodash"; @@ -19,6 +20,10 @@ const removeObsPhotoFromObservation = ( currentObservation, uri ) => { return []; }; +const observationToJSON = observation => ( observation instanceof Realm.Object + ? observation.toJSON( ) + : observation ); + const updateObservationKeysWithState = ( keysAndValues, state ) => { const { observations, @@ -27,9 +32,7 @@ const updateObservationKeysWithState = ( keysAndValues, state ) => { } = state; const updatedObservations = observations; const updatedObservation = { - ...( currentObservation.toJSON - ? currentObservation.toJSON( ) - : currentObservation ), + ...observationToJSON( currentObservation ), ...keysAndValues }; updatedObservations[currentObservationIndex] = updatedObservation; @@ -91,14 +94,14 @@ const useStore = create( set => ( { } ) ), setCurrentObservationIndex: index => set( state => ( { currentObservationIndex: index, - currentObservation: state.observations[index] + currentObservation: observationToJSON( state.observations[index] ) } ) ), setGroupedPhotos: photos => set( { groupedPhotos: photos } ), setObservations: updatedObservations => set( state => ( { observations: updatedObservations, - currentObservation: updatedObservations[state.currentObservationIndex] + currentObservation: observationToJSON( updatedObservations[state.currentObservationIndex] ) } ) ), setPhotoEvidenceUris: uris => set( { photoEvidenceUris: uris @@ -109,13 +112,13 @@ const useStore = create( set => ( { evidenceToAdd: options?.evidenceToAdd || state.evidenceToAdd, groupedPhotos: options?.groupedPhotos || state.groupedPhotos, observations: options?.observations || state.observations, - currentObservation: options?.observations?.[state.currentObservationIndex] - || state.observations?.[state.currentObservationIndex] + currentObservation: observationToJSON( options?.observations?.[state.currentObservationIndex] + || state.observations?.[state.currentObservationIndex] ) } ) ), updateComment: newComment => set( { comment: newComment } ), updateObservations: updatedObservations => set( state => ( { observations: updatedObservations, - currentObservation: updatedObservations[state.currentObservationIndex], + currentObservation: observationToJSON( updatedObservations[state.currentObservationIndex] ), unsavedChanges: true } ) ), updateObservationKeys: keysAndValues => set( state => ( {