mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2026-06-21 22:18:32 -04:00
* Add test for UserProfile species button from MyObs screen * Finish initial tests for navigating to Explore from MyObs * Check API calls are made with correct params * Use RootExplore screen in CustomTabBar and write test for navigating cyclical Explore -> TaxonDetails * Add navigation check for UserProfile from root explore screen * Simplify RootExplore screen; show nearby data for species view * Mock location permissions for Explore navigation tests * Update snapshot * Merge from main * Fix tests for Explore navigation
134 lines
3.5 KiB
JavaScript
134 lines
3.5 KiB
JavaScript
// @flow
|
|
|
|
import LocationPermissionGate from "components/SharedComponents/LocationPermissionGate";
|
|
import {
|
|
EXPLORE_ACTION,
|
|
ExploreProvider,
|
|
useExplore
|
|
} from "providers/ExploreContext.tsx";
|
|
import type { Node } from "react";
|
|
import React, { useState } from "react";
|
|
import fetchUserLocation from "sharedHelpers/fetchUserLocation";
|
|
import { useCurrentUser, useIsConnected, useTranslation } from "sharedHooks";
|
|
|
|
import Explore from "./Explore";
|
|
import mapParamsToAPI from "./helpers/mapParamsToAPI";
|
|
import useHeaderCount from "./hooks/useHeaderCount";
|
|
|
|
const RootExploreContainerWithContext = ( ): Node => {
|
|
const { t } = useTranslation( );
|
|
const isOnline = useIsConnected( );
|
|
const currentUser = useCurrentUser( );
|
|
|
|
const { state, dispatch, makeSnapshot } = useExplore( );
|
|
|
|
const [showFiltersModal, setShowFiltersModal] = useState( false );
|
|
const [exploreView, setExploreView] = useState( "species" );
|
|
|
|
const changeExploreView = newView => {
|
|
setExploreView( newView );
|
|
};
|
|
|
|
const updateTaxon = ( taxon: Object ) => {
|
|
dispatch( {
|
|
type: EXPLORE_ACTION.CHANGE_TAXON,
|
|
taxon,
|
|
taxonId: taxon?.id,
|
|
taxonName: taxon?.preferred_common_name || taxon?.name
|
|
} );
|
|
};
|
|
|
|
const filteredParams = mapParamsToAPI(
|
|
state,
|
|
currentUser
|
|
);
|
|
|
|
const queryParams = {
|
|
...filteredParams,
|
|
per_page: 20
|
|
};
|
|
if ( exploreView === "observers" ) {
|
|
queryParams.order_by = "observation_count";
|
|
}
|
|
|
|
// need this hook to be top-level enough that HeaderCount rerenders
|
|
const { count, loadingStatus, updateCount } = useHeaderCount( );
|
|
|
|
const closeFiltersModal = ( ) => setShowFiltersModal( false );
|
|
|
|
const openFiltersModal = ( ) => {
|
|
setShowFiltersModal( true );
|
|
makeSnapshot( );
|
|
};
|
|
|
|
const onPermissionGranted = async ( ) => {
|
|
if ( state.place_guess ) { return; }
|
|
const location = await fetchUserLocation( );
|
|
console.log( location, "location in onPermissionGranted" );
|
|
if ( !location || !location.latitude ) {
|
|
dispatch( {
|
|
type: EXPLORE_ACTION.SET_PLACE,
|
|
placeName: t( "Worldwide" )
|
|
} );
|
|
} else {
|
|
dispatch( {
|
|
type: EXPLORE_ACTION.SET_PLACE,
|
|
placeName: t( "Nearby" ),
|
|
lat: location?.latitude,
|
|
lng: location?.longitude,
|
|
radius: 50
|
|
} );
|
|
}
|
|
};
|
|
|
|
const onPermissionDenied = ( ) => {
|
|
if ( state.place_guess ) { return; }
|
|
dispatch( {
|
|
type: EXPLORE_ACTION.SET_PLACE,
|
|
placeName: t( "Worldwide" )
|
|
} );
|
|
};
|
|
|
|
const onPermissionBlocked = ( ) => {
|
|
if ( state.place_guess ) { return; }
|
|
dispatch( {
|
|
type: EXPLORE_ACTION.SET_PLACE,
|
|
placeName: t( "Worldwide" )
|
|
} );
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<Explore
|
|
changeExploreView={changeExploreView}
|
|
closeFiltersModal={closeFiltersModal}
|
|
count={count}
|
|
exploreView={exploreView}
|
|
hideBackButton
|
|
isOnline={isOnline}
|
|
loadingStatus={loadingStatus}
|
|
openFiltersModal={openFiltersModal}
|
|
queryParams={queryParams}
|
|
showFiltersModal={showFiltersModal}
|
|
updateCount={updateCount}
|
|
updateTaxon={updateTaxon}
|
|
/>
|
|
<LocationPermissionGate
|
|
permissionNeeded
|
|
onPermissionGranted={onPermissionGranted}
|
|
onPermissionDenied={onPermissionDenied}
|
|
onPermissionBlocked={onPermissionBlocked}
|
|
withoutNavigation
|
|
/>
|
|
</>
|
|
);
|
|
};
|
|
|
|
const ExploreContainer = (): Node => (
|
|
<ExploreProvider>
|
|
<RootExploreContainerWithContext />
|
|
</ExploreProvider>
|
|
);
|
|
|
|
export default ExploreContainer;
|