From 40fdb86e8aba5be4ed165ea8014bf8c3fa36cc83 Mon Sep 17 00:00:00 2001 From: Ken-ichi Ueda Date: Mon, 26 Aug 2024 14:36:09 -0700 Subject: [PATCH] fix: open not chosen when editing synced obs --- ...GeoprivacySheet.js => GeoprivacySheet.tsx} | 38 +++++++++++-------- src/realmModels/Observation.js | 6 ++- 2 files changed, 28 insertions(+), 16 deletions(-) rename src/components/ObsEdit/Sheets/{GeoprivacySheet.js => GeoprivacySheet.tsx} (50%) diff --git a/src/components/ObsEdit/Sheets/GeoprivacySheet.js b/src/components/ObsEdit/Sheets/GeoprivacySheet.tsx similarity index 50% rename from src/components/ObsEdit/Sheets/GeoprivacySheet.js rename to src/components/ObsEdit/Sheets/GeoprivacySheet.tsx index 39d523faa..2e4777cc8 100644 --- a/src/components/ObsEdit/Sheets/GeoprivacySheet.js +++ b/src/components/ObsEdit/Sheets/GeoprivacySheet.tsx @@ -1,53 +1,61 @@ -// @flow - import { RadioButtonSheet } from "components/SharedComponents"; -import type { Node } from "react"; -import React from "react"; +import React, { useMemo } from "react"; +import { + GEOPRIVACY_OBSCURED, + GEOPRIVACY_OPEN, + GEOPRIVACY_PRIVATE +} from "realmModels/Observation"; import useTranslation from "sharedHooks/useTranslation"; +type Geoprivacy = null | GEOPRIVACY_OPEN | GEOPRIVACY_OBSCURED | GEOPRIVACY_PRIVATE; + type Props = { - handleClose: Function, - selectedValue?: boolean, - updateGeoprivacyStatus: Function + handleClose: ( ) => void, + selectedValue?: Geoprivacy, + updateGeoprivacyStatus: ( Geoprivacy ) => void } const GeoprivacySheet = ( { handleClose, selectedValue, updateGeoprivacyStatus -}: Props ): Node => { +}: Props ) => { const { t } = useTranslation( ); - const radioValues = { + const radioValues = useMemo( () => ( { open: { label: t( "Open" ), text: t( "Anyone-using-iNaturalist-can-see" ), - value: "open" + value: GEOPRIVACY_OPEN }, obscured: { label: t( "Obscured" ), text: t( "The-exact-location-will-be-hidden" ), - value: "obscured" + value: GEOPRIVACY_OBSCURED }, private: { label: t( "Private" ), text: t( "The-location-will-not-be-visible" ), - value: "private" + value: GEOPRIVACY_PRIVATE } - }; + } ), [t] ); return ( { - updateGeoprivacyStatus( checkBoxValue ); + // Don't bother changing anything if we're "changing" from null + // to "open" since they're basically the same + if ( checkBoxValue !== GEOPRIVACY_OPEN || selectedValue !== null ) { + updateGeoprivacyStatus( checkBoxValue ); + } handleClose( ); }} handleClose={handleClose} radioValues={radioValues} - selectedValue={selectedValue} + selectedValue={selectedValue || GEOPRIVACY_OPEN} /> ); }; diff --git a/src/realmModels/Observation.js b/src/realmModels/Observation.js index 7eb794210..87f24e9ac 100644 --- a/src/realmModels/Observation.js +++ b/src/realmModels/Observation.js @@ -14,6 +14,10 @@ import Taxon from "./Taxon"; import User from "./User"; import Vote from "./Vote"; +export const GEOPRIVACY_OPEN = "open"; +export const GEOPRIVACY_OBSCURED = "obscured"; +export const GEOPRIVACY_PRIVATE = "private"; + // noting that methods like .toJSON( ) are only accessible when the model // class is extended with Realm.Object per this issue: // https://github.com/realm/realm-js/issues/3600#issuecomment-785828614 @@ -107,7 +111,7 @@ class Observation extends Realm.Object { return { ...obs, captive_flag: false, - geoprivacy: "open", + geoprivacy: GEOPRIVACY_OPEN, owners_identification_from_vision: false, observed_on: obs?.observed_on, observed_on_string: obs