Skip to content

Commit

Permalink
fix(home): get the correct channel activities count
Browse files Browse the repository at this point in the history
  • Loading branch information
AXeL-dev committed Oct 15, 2022
1 parent de32a0e commit 6f1effd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/providers/ChannelVideosProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { GetChannelVideosResponse } from 'store/services/youtube';
import { Channel, Video, HomeView } from 'types';

export interface ChannelData extends GetChannelVideosResponse {
export interface ChannelData extends Omit<GetChannelVideosResponse, 'count'> {
channel: Channel;
}

Expand Down
16 changes: 12 additions & 4 deletions src/store/services/youtube/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type GetChannelActivitiesArgs = {

type GetChannelActivitiesResponse = {
items: ChannelActivities[];
count: number;
total: number;
};

Expand All @@ -56,7 +57,9 @@ export interface PersistVideosOptions {
flags?: VideoFlags;
}

export type GetChannelVideosResponse = GetVideosByIdResponse;
export type GetChannelVideosResponse = GetVideosByIdResponse & {
count: number;
};

const queries = {
// Channel search query
Expand Down Expand Up @@ -129,6 +132,7 @@ const queries = {
.map((item) => ({
videoId: item.contentDetails.upload.videoId,
})),
count: response.items.length,
total: response.pageInfo.totalResults,
}),
},
Expand Down Expand Up @@ -234,21 +238,24 @@ export const extendedApi = youtubeApi.injectEndpoints({
return {
data: {
items: [],
count: 0,
total: 0,
},
};
}
return { error: activities.error as FetchBaseQueryError };
}
const { items, total } = queries.getChannelActivities.transformResponse(
activities.data as Response,
);
const { items, count, total } =
queries.getChannelActivities.transformResponse(
activities.data as Response,
);
// Fetch channel videos
const ids = items.map(({ videoId }) => videoId);
if (ids.length === 0) {
return {
data: {
items: [],
count: 0,
total: 0,
},
};
Expand Down Expand Up @@ -276,6 +283,7 @@ export const extendedApi = youtubeApi.injectEndpoints({
return {
data: {
...videosData,
count,
total,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ChannelRendererProps {
view: HomeView;
channel: Channel;
videos: Video[];
count?: number;
total: number;
isLoading: boolean;
itemsPerRow: number;
Expand All @@ -17,10 +18,12 @@ export interface ChannelRendererProps {
}

function ChannelRenderer(props: ChannelRendererProps) {
const { channel, videos, total, isLoading, maxResults, ...rest } = props;
const { channel, videos, count, total, isLoading, maxResults, ...rest } =
props;
const videosCount = count || videos.length;
const hasVideos = isLoading || videos.length > 0;
const hasMore =
videos.length > 0 && videos.length >= maxResults && total > maxResults;
videosCount > 0 && videosCount >= maxResults && total > maxResults;

return hasVideos ? (
<Box
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function DefaultRenderer(props: DefaultRendererProps) {
},
);
const videos = (data?.items || []).filter(filter);
const count = data?.count || 0;
const total = data?.total || 0;

const handleLoadMore = () => {
Expand All @@ -72,6 +73,7 @@ function DefaultRenderer(props: DefaultRendererProps) {
view={view}
channel={channel}
videos={videos}
count={count}
total={total}
isLoading={isLoading || isFetching}
itemsPerRow={itemsPerRow}
Expand Down

0 comments on commit 6f1effd

Please sign in to comment.