Skip to content

Commit

Permalink
feat(search): add useSearchPlaylist hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Jun 10, 2021
1 parent 5fdfd3e commit b30ce66
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
1 change: 1 addition & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
'home',
'playlist',
'videodetail',
'search',
],
],
},
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ The allowed scopes are:
- home
- playlist
- videodetail
- search

### Subject

Expand Down
25 changes: 4 additions & 21 deletions src/hooks/usePlaylist.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import { UseBaseQueryResult, useQuery } from 'react-query';
import type { Playlist, PlaylistItem } from 'types/playlist';
import type { Playlist } from 'types/playlist';

const baseUrl = 'https://content.jwplatform.com';
import { generatePlaylistPlaceholder } from '../utils/collection';

const placeholderData: Playlist = {
title: '',
playlist: new Array(30).fill({
description: '',
duration: 0,
feedid: '',
image: '',
images: [],
link: '',
genre: '',
mediaid: '',
pubdate: 0,
rating: '',
sources: [],
tags: '',
title: '',
tracks: [],
} as PlaylistItem),
};
const baseUrl = 'https://content.jwplatform.com';
const placeholderData = generatePlaylistPlaceholder(30);

const getPlaylistById = (playlistId: string, relatedMediaId?: string) => {
const relatedQuery = relatedMediaId ? `?related_media_id=${relatedMediaId}` : '';
Expand Down
23 changes: 23 additions & 0 deletions src/hooks/useSearchPlaylist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { UseBaseQueryResult, useQuery } from 'react-query';
import type { Playlist } from 'types/playlist';

import { generatePlaylistPlaceholder } from '../utils/collection';

const baseUrl = 'https://content.jwplatform.com';
const placeholderData = generatePlaylistPlaceholder();

const getSearchPlaylist = (playlistId: string, query: string) => {
return fetch(`${baseUrl}/v2/playlists/${playlistId}?search=${encodeURIComponent(query)}`).then((res) => res.json());
};

export type UseSearchPlaylistResult<TData = Playlist, TError = unknown> = UseBaseQueryResult<TData, TError>;

export default function useSearchPlaylist(playlistId: string, query: string, usePlaceholderData: boolean = true): UseSearchPlaylistResult {
return useQuery(['playlist', playlistId, query], () => {
return getSearchPlaylist(playlistId, query)
}, {
enabled: !!playlistId && !!query,
placeholderData: usePlaceholderData ? placeholderData : undefined,
keepPreviousData: true,
});
}
24 changes: 23 additions & 1 deletion src/utils/collection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Config } from 'types/Config';
import type { PlaylistItem } from 'types/playlist';
import type { Playlist } from 'types/playlist';

const getFiltersFromConfig = (config: Config, playlistId: string): string[] => {
const menuItem = config.menu.find((item) => item.playlistId === playlistId);
Expand All @@ -23,4 +24,25 @@ const chunk = <T>(input: T[], size: number) => {
const findPlaylistImageForWidth = (playlistItem: PlaylistItem, width: number): string =>
playlistItem.images.find((img) => img.width === width)?.src || playlistItem.image;

export { getFiltersFromConfig, filterPlaylist, chunk, findPlaylistImageForWidth };
const generatePlaylistPlaceholder = (playlistLength: number = 15) : Playlist => ({
title: '',
playlist: new Array(playlistLength).fill({}).map((_value, index) => ({
description: '',
duration: 0,
feedid: '',
image: '',
images: [],
link: '',
genre: '',
mediaid: `placeholder_${index}`,
pubdate: 0,
rating: '',
sources: [],
tags: '',
title: '',
tracks: [],
}) as PlaylistItem),
});


export { getFiltersFromConfig, filterPlaylist, chunk, findPlaylistImageForWidth, generatePlaylistPlaceholder };

0 comments on commit b30ce66

Please sign in to comment.