Skip to content

Commit 3c64268

Browse files
committed
fix subscribers empty issue
1 parent 20e24f4 commit 3c64268

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

client/my-sites/stats/hooks/use-subscribers-totals-query.tsx

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { parseAvatar } from 'calypso/state/stats/lists/utils';
55
import getDefaultQueryParams from './default-query-params';
66

77
const MAX_SUBSCRIBERS_TO_RETURN = 10;
8-
const isJetpackApi = config.isEnabled( 'is_running_in_jetpack' );
8+
const isJetpackApi = config.isEnabled( 'is_running_in_jetpack_site' );
9+
const sortByDateDesc = ( a: { date_subscribed: string }, b: { date_subscribed: string } ) => {
10+
return new Date( b.date_subscribed ).getTime() - new Date( a.date_subscribed ).getTime();
11+
};
912

1013
const querySubscribersTotals = ( siteId: number | null, filterAdmin?: boolean ): Promise< any > => {
1114
return wpcom.req
@@ -117,8 +120,6 @@ export function useSubscribersTotalsWithoutAdminQueries( siteId: number | null )
117120
}
118121

119122
function useSubscribersTotalsQueries( siteId: number | null, filterAdmin?: boolean ) {
120-
const isJetpackApi = config.isEnabled( 'is_running_in_jetpack' );
121-
122123
const results = useQueries( {
123124
queries: [
124125
{
@@ -152,54 +153,50 @@ function useSubscribersTotalsQueries( siteId: number | null, filterAdmin?: boole
152153
],
153154
} );
154155

155-
if ( ! isJetpackApi ) {
156-
// Use `subscribers_by_user_type` endpoint in Calypso Stats.
156+
if ( isJetpackApi ) {
157+
// `subscribers_by_user_type` endpoint is not available for Odyssey Stats yet.
157158
return {
158159
data: {
159-
total_email: results[ 3 ]?.data?.total,
160-
total_wpcom: results[ 2 ]?.data?.total,
161-
total: results[ 1 ].data?.email_subscribers,
160+
total_email: results[ 0 ]?.data?.total_email,
161+
total_wpcom: results[ 0 ]?.data?.total_wpcom,
162+
total: results[ 0 ]?.data?.total,
162163
paid_subscribers: results[ 1 ]?.data?.paid_subscribers,
163164
free_subscribers:
164165
results[ 1 ]?.data?.email_subscribers !== undefined &&
165166
results[ 1 ]?.data?.paid_subscribers !== undefined
166167
? results[ 1 ].data.email_subscribers - results[ 1 ].data.paid_subscribers
167168
: null,
168169
social_followers: results[ 1 ]?.data?.social_followers,
169-
is_owner_subscribing: results[ 2 ]?.data?.is_owner_subscribing,
170-
// Merge email and wpcom subscribers and sort by date_subscribed, and only shows the most recent 10 subscribers.
171-
subscribers:
172-
[
173-
...( results[ 3 ]?.data?.subscribers ?? [] ),
174-
...( results[ 2 ]?.data?.subscribers ?? [] ),
175-
]
176-
.sort( ( a, b ) => {
177-
return (
178-
new Date( b.date_subscribed ).getTime() - new Date( a.date_subscribed ).getTime()
179-
);
180-
} )
181-
.slice( 0, MAX_SUBSCRIBERS_TO_RETURN ) ?? [],
170+
is_owner_subscribing: results[ 0 ]?.data?.is_owner_subscribing,
171+
subscribers: ( results[ 0 ]?.data?.subscribers ?? [] ).sort( sortByDateDesc ),
182172
},
183-
isLoading: results.some( ( result ) => result.isLoading ),
173+
isLoading: results.some( ( result ) => result.isPending ),
184174
isError: results.some( ( result ) => result.isError ),
185175
};
186176
}
187177

188-
// `subscribers_by_user_type` endpoint is not available for Odyssey Stats yet.
178+
// Use `subscribers_by_user_type` endpoint in Calypso Stats.
189179
return {
190180
data: {
191-
total_email: results[ 0 ]?.data?.total_email,
192-
total_wpcom: results[ 0 ]?.data?.total_wpcom,
193-
total: results[ 0 ]?.data?.total,
181+
total_email: results[ 3 ]?.data?.total,
182+
total_wpcom: results[ 2 ]?.data?.total,
183+
total: results[ 1 ].data?.email_subscribers,
194184
paid_subscribers: results[ 1 ]?.data?.paid_subscribers,
195185
free_subscribers:
196186
results[ 1 ]?.data?.email_subscribers !== undefined &&
197187
results[ 1 ]?.data?.paid_subscribers !== undefined
198188
? results[ 1 ].data.email_subscribers - results[ 1 ].data.paid_subscribers
199189
: null,
200190
social_followers: results[ 1 ]?.data?.social_followers,
201-
is_owner_subscribing: results[ 0 ]?.data?.is_owner_subscribing,
202-
subscribers: results[ 0 ]?.data?.subscribers,
191+
is_owner_subscribing: results[ 2 ]?.data?.is_owner_subscribing,
192+
// Merge email and wpcom subscribers and sort by date_subscribed, and only shows the most recent 10 subscribers.
193+
subscribers:
194+
[
195+
...( results[ 3 ]?.data?.subscribers ?? [] ),
196+
...( results[ 2 ]?.data?.subscribers ?? [] ),
197+
]
198+
.sort( sortByDateDesc )
199+
.slice( 0, MAX_SUBSCRIBERS_TO_RETURN ) ?? [],
203200
},
204201
isLoading: results.some( ( result ) => result.isLoading ),
205202
isError: results.some( ( result ) => result.isError ),

client/my-sites/stats/stats-followers/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const StatModuleFollowers = ( { className } ) => {
3232
const difference = now.getTime() - value.getTime();
3333

3434
const seconds = Math.floor( difference / 1000 );
35-
const minutes = Math.floor( seconds / 60 );
35+
const minutes = Math.ceil( seconds / 60 );
3636
const hours = Math.floor( minutes / 60 );
3737
const days = Math.floor( hours / 24 );
3838

0 commit comments

Comments
 (0)