Skip to content

Commit

Permalink
Refactor some filter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Apr 5, 2024
1 parent 7d089c5 commit 352eb49
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions src/app/(tabs)/genre/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,15 @@ function GenreScreen() {
// console.log('passing in genreId params:', genreId);
// console.log('testing passing in genreType', genreType);
// console.log('testing genreName', genreName);
//

useEffect(() => {
const checkTopic = (preview: StoryPreview): boolean => {
if (preview == null) return false;
if (preview.topic == null) return false;
if (preview == null || preview.topic == null) return false;
if (currentTopics.length == 0) return true;
else return currentTopics.every(t => preview.topic.includes(t));
}
const checkTone = (preview: StoryPreview): boolean => {
if (preview == null) return false;
if (preview.tone == null) return false;
if (preview == null || preview.tone == null) return false;
if (currentTones.length == 0) return true;
else return currentTones.every(t => preview.tone.includes(t));
}
Expand All @@ -67,28 +65,16 @@ function GenreScreen() {
setFilteredStoryPreviews(filteredPreviews);
}, [currentTopics, currentTones])

async function getAllStoryIds(
genreStories: GenreStories[],
): Promise<string[]> {
const allStoryIds: string[] = [];
for (const subgenre of genreStories) {
if (subgenre.genre_story_previews[0] != null) {
subgenre.genre_story_previews.forEach(id => {
allStoryIds.push(id);
});
}
}
return allStoryIds;
function getAllStoryIds(genreStories: GenreStories[]): string[] {
return genreStories.map(story => story.genre_story_previews).flat().filter(story => story !== null);
}

async function findStoryIdsByName(
subgenre_name: string,
genreStories: GenreStories[],
): Promise<string[]> {
function findStoryIdsByName(subgenreName: string, genreStories: GenreStories[]): string[] {
const filteredStoryIds: string[] = [];
const matchingGenreStory = genreStories.find(
subgenre => subgenre.subgenre_name === subgenre_name,
subgenre => subgenre.subgenre_name === subgenreName,
);

if (matchingGenreStory?.genre_story_previews[0] != null) {
matchingGenreStory.genre_story_previews.forEach(id => {
filteredStoryIds.push(id);
Expand All @@ -103,12 +89,8 @@ function GenreScreen() {
}

async function getSubgenres(genreStories: GenreStories[]): Promise<string[]> {
const subGenres: string[] = [];
subGenres.push('All');
for (const subgenre of genreStories) {
subGenres.push(subgenre.subgenre_name);
}
return subGenres;
const subgenres = genreStories.map(subgenre => subgenre.subgenre_name)
return ['All', ...subgenres];
}

async function filterBySubgenre(filterName: string) {
Expand Down

0 comments on commit 352eb49

Please sign in to comment.