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.
This commit is contained in:
joergmlpts
2024-01-16 18:36:32 -08:00
committed by GitHub
parent 6b37ca1d2b
commit 279ea8a118
2 changed files with 12 additions and 11 deletions

View File

@@ -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

View File

@@ -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 => ( {