Skip to content

Commit

Permalink
Update to prevent fetch location API if tab is gated
Browse files Browse the repository at this point in the history
  • Loading branch information
Initsogar committed Jan 17, 2025
1 parent 2aaec76 commit 03fafd0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ const StatsLocations: React.FC< StatsModuleLocationsProps > = ( { query, summary
data = [],
isLoading: isRequestingData,
isError,
} = useLocationViewsQuery< StatsLocationViewsData >( siteId, geoMode, query );
} = useLocationViewsQuery< StatsLocationViewsData >( siteId, geoMode, query, {
enabled: ! shouldGate,
} );

const changeViewButton = ( selection: SelectOptionType ) => {
const filter = selection.value;
Expand Down Expand Up @@ -163,7 +165,7 @@ const StatsLocations: React.FC< StatsModuleLocationsProps > = ( { query, summary
{ ! shouldGateStatsModule && siteId && statType && (
<QuerySiteStats statType={ statType } siteId={ siteId } query={ query } />
) }
{ isRequestingData && (
{ isRequestingData && ! shouldGate && (
<StatsCardSkeleton isLoading={ isRequestingData } title={ title } type={ 3 } withHero />
) }
{ ( ( ! isRequestingData && ! isError && hasLocationData ) || shouldGate ) && (
Expand Down
26 changes: 18 additions & 8 deletions client/my-sites/stats/hooks/use-location-views-query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from '@tanstack/react-query';
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp';
import { normalizers } from 'calypso/state/stats/lists/utils';
import getDefaultQueryParams from './default-query-params';
Expand All @@ -19,25 +19,35 @@ function queryStatsLocationViews(
return wpcom.req.get( `/sites/${ siteId }/stats/location-views/${ geoMode }`, query );
}

// For now, we are only allowing the enabled option to be passed.
// We can add more options later if needed.
type CustomQueryOptions< TData = unknown, TError = Error > = Pick<
UseQueryOptions< TData, TError >,
'enabled'
>;

const useLocationViewsQuery = < T = StatsLocationViewsData >(
siteId: number,
geoMode: 'country' | 'region' | 'city',
query: QueryStatsParams
query: QueryStatsParams,
options?: CustomQueryOptions< T, Error >
) => {
return useQuery( {
...getDefaultQueryParams(),
queryKey: [ 'stats', 'location-views', siteId, geoMode, query ],
queryFn: () =>
queryStatsLocationViews( siteId, geoMode, processQueryParams( query ) ) as Promise< T >,
...options,
queryKey: [ 'stats', 'location-views', siteId, geoMode, JSON.stringify( query ) ],
queryFn: () => queryStatsLocationViews( siteId, geoMode, processQueryParams( query ) ),
select: ( data ) => {
const normalizedStats = normalizers.statsCountryViews(
data as StatsLocationViewsData,
query
);

return Array.isArray( normalizedStats ) && normalizedStats?.length && query?.max
? normalizedStats.slice( 0, query.max )
: normalizedStats;
if ( ! Array.isArray( normalizedStats ) ) {
return [];
}

return query?.max ? normalizedStats.slice( 0, query.max ) : normalizedStats;
},
staleTime: 1000 * 60 * 5, // Cache for 5 minutes
} );
Expand Down

0 comments on commit 03fafd0

Please sign in to comment.