mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2026-06-21 05:58:37 -04:00
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:
@@ -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
|
||||
|
||||
@@ -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 => ( {
|
||||
|
||||
Reference in New Issue
Block a user