Skip to content

Commit

Permalink
Add save story to preview card, one home screen viewing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Apr 16, 2024
1 parent 75019e6 commit 9bc23e7
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/app/(tabs)/author/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ function AuthorScreen() {
{authorStoryPreview?.map(story => (
<PreviewCard
key={story.title}
storyId={story.id}
title={story.title}
image={story.featured_media}
author={story.author_name}
Expand Down
1 change: 1 addition & 0 deletions src/app/(tabs)/genre/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ function GenreScreen() {
renderItem={({ item }) => (
<PreviewCard
key={item.id}
storyId={item.id}
tags={item.genre_medium.concat(item.tone).concat(item.topic)}
author={item.author_name}
image={item.featured_media}
Expand Down
1 change: 1 addition & 0 deletions src/app/(tabs)/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function HomeScreen() {
{featuredStories.map(story => (
<PreviewCard
key={story.id}
storyId={story.id}
title={story.title}
image={story.featured_media}
author={story.author_name}
Expand Down
4 changes: 3 additions & 1 deletion src/app/(tabs)/search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ function SearchScreen() {
{recentlyViewed.map(item => (
<PreviewCard
key={item.title}
storyId={item.id}
title={item.title}
image={item.featured_media}
author={item.author_name}
Expand Down Expand Up @@ -395,7 +396,8 @@ function SearchScreen() {
contentContainerStyle={styles.contentCotainerStories}
renderItem={({ item }) => (
<PreviewCard
key={item.title}
key={item.id}
storyId={item.id}
title={item.title}
image={item.featured_media}
author={item.author_name}
Expand Down
22 changes: 19 additions & 3 deletions src/components/ContentCard/ContentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
} from 'react-native';

import styles from './styles';
import { addUserStoryToReadingList } from '../../queries/savedStories';
import { addUserStoryToReadingList, deleteUserStoryToReadingList, isStoryInReadingList } from '../../queries/savedStories';
import globalStyles from '../../styles/globalStyles';
import { useSession } from '../../utils/AuthContext';
import Emoji from 'react-native-emoji';
import { useEffect, useMemo, useState } from 'react';


type ContentCardProps = {
title: string;
Expand All @@ -22,6 +24,9 @@ type ContentCardProps = {
pressFunction: (event: GestureResponderEvent) => void;
};

const saveStoryImage = require('../../../assets/save_story.png');
const savedStoryImage = require('../../../assets/saved_story.png');

function ContentCard({
title,
author,
Expand All @@ -31,9 +36,20 @@ function ContentCard({
pressFunction,
}: ContentCardProps) {
const { user } = useSession();
const [storyIsSaved, setStoryIsSaved] = useState(false);

useEffect(() => {
isStoryInReadingList(storyId, user?.id).then(storyInReadingList => setStoryIsSaved(storyInReadingList))
}, [storyId])


const saveStory = () => {
addUserStoryToReadingList(user?.id, storyId);
if (storyIsSaved) {
deleteUserStoryToReadingList(user?.id, storyId);
} else {
addUserStoryToReadingList(user?.id, storyId);
}
setStoryIsSaved(!storyIsSaved);
};

return (
Expand Down Expand Up @@ -85,7 +101,7 @@ function ContentCard({
<TouchableOpacity onPress={() => saveStory()}>
<Image
style={styles.saveStoryImage}
source={require('../../../assets/save_story.png')}
source={storyIsSaved ? savedStoryImage : saveStoryImage}
/>
</TouchableOpacity>
</View>
Expand Down
26 changes: 24 additions & 2 deletions src/components/PreviewCard/PreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ import Emoji from 'react-native-emoji';

import styles from './styles';
import globalStyles from '../../styles/globalStyles';
import { useEffect, useState } from 'react';
import { addUserStoryToReadingList, deleteUserStoryToReadingList, isStoryInReadingList } from '../../queries/savedStories';
import { useSession } from '../../utils/AuthContext';
import { useIsFocused } from '@react-navigation/native';

const placeholderImage =
'https://gwn-uploads.s3.amazonaws.com/wp-content/uploads/2021/10/10120952/Girls-Write-Now-logo-avatar.png';
const saveStoryImage = require('../../../assets/save_story.png');
const savedStoryImage = require('../../../assets/saved_story.png');

type PreviewCardProps = {
title: string;
image: string;
storyId: number;
author: string;
authorImage: string;
excerpt: { html: string };
Expand All @@ -28,14 +35,29 @@ type PreviewCardProps = {
function PreviewCard({
title,
image,
storyId,
author,
authorImage,
excerpt,
tags,
pressFunction,
}: PreviewCardProps) {
const { user } = useSession();
const isFocused = useIsFocused();
const [storyIsSaved, setStoryIsSaved] = useState(false);

useEffect(() => {
isStoryInReadingList(storyId, user?.id).then(storyInReadingList => setStoryIsSaved(storyInReadingList))
}, [storyId, isFocused])


const saveStory = () => {
console.log("testing '+' icon does something for story " + title);
if (storyIsSaved) {
deleteUserStoryToReadingList(user?.id, storyId);
} else {
addUserStoryToReadingList(user?.id, storyId);
}
setStoryIsSaved(!storyIsSaved);
};

return (
Expand All @@ -48,7 +70,7 @@ function PreviewCard({
<TouchableOpacity onPress={() => saveStory()}>
<Image
style={{ width: 30, height: 30 }}
source={require('../../../assets/save_story.png')}
source={storyIsSaved ? savedStoryImage : saveStoryImage}
/>
</TouchableOpacity>
</View>
Expand Down
2 changes: 1 addition & 1 deletion src/queries/reactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function fetchAllReactionsToStory(
`An error occured when trying to fetch reactions to a story', ${error}`,
);
} else {
return data;
return data as Reactions[];
}
}

Expand Down
48 changes: 41 additions & 7 deletions src/queries/savedStories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import supabase from '../utils/supabase';

const favorites = 'favorites';
const readingList = 'reading list';
enum SavedList {
FAVORITES = 'favorites',
READING_LIST = 'reading list'
}


async function fetchUserStories(
user_id: string | undefined,
Expand Down Expand Up @@ -48,11 +51,11 @@ async function fetchUserStories(
}

export async function fetchUserStoriesFavorites(user_id: string | undefined) {
return await fetchUserStories(user_id, favorites);
return await fetchUserStories(user_id, SavedList.FAVORITES);
}

export async function fetchUserStoriesReadingList(user_id: string | undefined) {
return await fetchUserStories(user_id, readingList);
return await fetchUserStories(user_id, SavedList.READING_LIST);
}

async function addUserStory(
Expand All @@ -78,17 +81,32 @@ export async function addUserStoryToFavorites(
user_id: string | undefined,
story_id: number,
) {
addUserStory(user_id, story_id, favorites);
addUserStory(user_id, story_id, SavedList.FAVORITES);
}

export async function addUserStoryToReadingList(
user_id: string | undefined,
story_id: number,
) {
addUserStory(user_id, story_id, readingList);
addUserStory(user_id, story_id, SavedList.READING_LIST);
}

export async function deleteUserStoryToFavorites(
user_id: string | undefined,
story_id: number,
) {
deleteUserStory(user_id, story_id, SavedList.FAVORITES);
}

export async function deleteUserStories(
export async function deleteUserStoryToReadingList(
user_id: string | undefined,
story_id: number,
) {
deleteUserStory(user_id, story_id, SavedList.READING_LIST);
}


export async function deleteUserStory(
user_id: string | undefined,
story_id: number,
name: string,
Expand All @@ -108,3 +126,19 @@ export async function deleteUserStories(
}
}
}

export async function isStoryInReadingList(storyId: number, userId: string | undefined): Promise<boolean> {
let { data, error } = await supabase
.rpc('is_story_saved_for_user', {
list_name: "reading list",
story_db_id: storyId,
user_uuid: userId,
})

if (error) {
console.error(error)
return false;
}

return data;
}

0 comments on commit 9bc23e7

Please sign in to comment.