Skip to content

Commit

Permalink
Fix title preview card overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Apr 18, 2024
1 parent 0900b7e commit f8c41e7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 74 deletions.
102 changes: 61 additions & 41 deletions src/app/(tabs)/library/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
fetchUserStoriesFavorites,
fetchUserStoriesReadingList,
} from '../../../queries/savedStories';
import { FlatList } from 'react-native-gesture-handler';

function LibraryScreen() {
const { user } = useSession();
Expand All @@ -26,22 +27,25 @@ function LibraryScreen() {
const favoritesPressed = () => {
setFavoritesSelected(true);
setReadingSelected(false);
console.log(favoriteStories);
};

const readingPressed = () => {
setFavoritesSelected(false);
setReadingSelected(true);
console.log(readingListStories);
};

useEffect(() => {
(async () => {
const [favoriteStoriesResponse, readingListStoriesResponse] =
await Promise.all([
fetchUserStoriesFavorites(user?.id).catch(() => []),
fetchUserStoriesReadingList(user?.id).catch(() => []),
]);
setFavoriteStories(favoriteStoriesResponse);
setReadingListStories(readingListStoriesResponse);
await Promise.all([
fetchUserStoriesFavorites(user?.id).catch(favorites =>
setFavoriteStories(favorites),
),
fetchUserStoriesReadingList(user?.id).catch(readingList =>
setReadingListStories(readingList),
),
]);
})().finally(() => {});
}, [user]);

Expand Down Expand Up @@ -88,45 +92,61 @@ function LibraryScreen() {
>
{favoritesSelected && favoriteStories.length > 0 && (
<View>
{favoriteStories.map(story => (
<PreviewCard
key={story.title}
title={story.title}
image={story.featured_media}
author={story.author_name}
authorImage={story.author_image}
excerpt={story.excerpt}
tags={story.genre_medium.concat(story.tone).concat(story.topic)}
pressFunction={() =>
router.push({
pathname: '/story',
params: { storyId: story.id.toString() },
})
}
/>
))}
<FlatList
data={favoriteStories}
renderItem={({ item }) => {
return (
<PreviewCard
key={item.title}
title={item.title}
storyId={item.id}
image={item.featured_media}
author={item.author_name}
authorImage={item.author_image}
excerpt={item.excerpt}
tags={item.genre_medium
.concat(item.tone)
.concat(item.topic)}
pressFunction={() =>
router.push({
pathname: '/story',
params: { storyId: item.id.toString() },
})
}
/>
);
}}
/>
</View>
)}

{readingSelected && readingListStories.length > 0 && (
<View>
{readingListStories.map(story => (
<PreviewCard
key={story.title}
title={story.title}
image={story.featured_media}
author={story.author_name}
authorImage={story.author_image}
excerpt={story.excerpt}
tags={story.genre_medium.concat(story.tone).concat(story.topic)}
pressFunction={() =>
router.push({
pathname: '/story',
params: { storyId: story.id.toString() },
})
}
/>
))}
<FlatList
data={readingListStories}
renderItem={({ item }) => {
return (
<PreviewCard
key={item.title}
title={item.title}
storyId={item.id}
image={item.featured_media}
author={item.author_name}
authorImage={item.author_image}
excerpt={item.excerpt}
tags={item.genre_medium
.concat(item.tone)
.concat(item.topic)}
pressFunction={() =>
router.push({
pathname: '/story',
params: { storyId: item.id.toString() },
})
}
/>
);
}}
/>
</View>
)}
</ScrollView>
Expand Down
6 changes: 4 additions & 2 deletions src/components/PreviewCard/PreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ function PreviewCard({
<Text numberOfLines={1} style={[globalStyles.h3, styles.title]}>
{title}
</Text>
<TouchableOpacity>
<SaveStoryButton storyId={storyId} />
<TouchableOpacity style={{ alignSelf: 'flex-end' }}>
<View>
<SaveStoryButton storyId={storyId} />
</View>
</TouchableOpacity>
</View>
<View style={styles.body}>
Expand Down
7 changes: 4 additions & 3 deletions src/components/PreviewCard/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const styles = StyleSheet.create({
marginBottom: 12,
},
title: {
marginBottom: 8,
flex: 1,
alignSelf: 'flex-start',
// marginBottom: 8,
},
titleContainer: {
paddingTop: 16,
Expand All @@ -54,8 +56,7 @@ const styles = StyleSheet.create({
borderBottomColor: '#EBEBEB',
borderBottomWidth: StyleSheet.hairlineWidth,
flexDirection: 'row',
flexGrow: 1,
justifyContent: 'space-between',
flex: 1,
},
tag: {
paddingHorizontal: 8,
Expand Down
36 changes: 8 additions & 28 deletions src/queries/savedStories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import supabase from '../utils/supabase';
import { StoryPreview } from './types';

enum SavedList {
FAVORITES = 'favorites',
Expand All @@ -9,44 +10,23 @@ async function fetchUserStories(
user_id: string | undefined,
name: string | undefined,
) {
const { data: storyObjects, error } = await supabase
.from('saved_stories')
.select('story_id')
.eq('user_id', user_id)
.eq('name', name);
let { data, error } = await supabase.rpc('get_saved_stories_for_user', {
user_id,
saved_list_name: name,
});

if (error) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
`An error occured when trying to fetch user saved stories: ${JSON.stringify(
`An error occured when trying to set user saved stories: ${JSON.stringify(
error,
)}`,
);
}
return [];
}

let storyData = [];
for (const storyObject of storyObjects) {
const storyId = storyObject['story_id'];
const { data, error } = await supabase.rpc('fetch_story', {
input_id: storyId,
});

if (error || data.length == 0) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
`An error occured when trying to use rpc to get story data: ${JSON.stringify(
error,
)}`,
);
}
} else {
storyData.push(data[0]);
}
}

return storyData;
console.log(data[0]);
return data as StoryPreview[];
}

export async function fetchUserStoriesFavorites(user_id: string | undefined) {
Expand Down

0 comments on commit f8c41e7

Please sign in to comment.