Skip to content

Commit

Permalink
Fix return statement in fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Mar 16, 2024
1 parent 6f73709 commit 1d6960a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/app/(tabs)/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { StoryCard, StoryPreview } from '../../../queries/types';
import globalStyles from '../../../styles/globalStyles';
import { useSession } from '../../../utils/AuthContext';
import TestCard from '../../../components/TestCard/TestCard';

function HomeScreen() {
const { user } = useSession();
Expand Down
22 changes: 16 additions & 6 deletions src/components/TestCard/TestCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,34 @@ function TestCard() {
<View style={styles.textContainer}>
<TextInput onChangeText={t => setStoryId(t)} value={storyId} />
<Text
style={[globalStyles.h3, styles.title]}
numberOfLines={1}
style={globalStyles.body1}
onPress={async () =>
await addUserStoryToReadingList(user?.id, parseInt(storyId))
}
>
Add story to favorites
Add story to reading
</Text>
<Text
style={globalStyles.body1}
onPress={async () =>
await addUserStoryToFavorites(user?.id, parseInt(storyId))
}
>
Add story to reading list
Add story to favs list
</Text>
<Button onPress={() => fetchUserStoriesFavorites(user?.id)}>
log
<Button
onPress={async () =>
console.log(await fetchUserStoriesFavorites(user?.id))
}
>
log favs
</Button>
<Button
onPress={async () =>
console.log(await fetchUserStoriesReadingList(user?.id))
}
>
log reading
</Button>
</View>
</View>
Expand Down
62 changes: 29 additions & 33 deletions src/components/UserSelectorInput/UserSelectorInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,35 @@ function UserSelectorInput({
return (
<View style={styles.container}>
<Text style={[globalStyles.subtext, styles.label]}>{label}</Text>
<View style={styles.outer}>
<Dropdown
mode="default"
style={styles.dropdown}
placeholderStyle={[globalStyles.body1, styles.grey]}
selectedTextStyle={globalStyles.body1}
inputSearchStyle={globalStyles.body1}
itemTextStyle={globalStyles.body1}
containerStyle={styles.dropdownContainer}
dropdownPosition="bottom"
itemContainerStyle={styles.itemContainer}
iconStyle={styles.iconStyle}
data={options.map(option => {
return { label: option, value: option };
})}
maxHeight={400}
labelField="label"
valueField="value"
placeholder={'Select Option'}
value={value}
renderItem={(item: Option, selected: boolean | undefined) => (
<Text style={[globalStyles.body1, styles.itemContainer]}>
{item.value}
</Text>
)}
renderRightIcon={() => (
<Icon name="arrow-drop-down" type="material" />
)}
onChange={(item: Option) => {
setValue(item.value);
}}
/>
</View>
<Dropdown
mode="default"
style={styles.dropdown}
placeholderStyle={[globalStyles.body1, styles.grey]}
selectedTextStyle={globalStyles.body1}
inputSearchStyle={globalStyles.body1}
itemTextStyle={globalStyles.body1}
containerStyle={styles.dropdownContainer}
dropdownPosition="bottom"
itemContainerStyle={styles.itemContainer}
iconStyle={styles.iconStyle}
data={options.map(option => {
return { label: option, value: option };
})}
maxHeight={400}
labelField="label"
valueField="value"
placeholder={'Select Option'}
value={value}
renderItem={(item: Option, selected: boolean | undefined) => (
<Text style={[globalStyles.body1, styles.itemContainer]}>
{item.value}
</Text>
)}
renderRightIcon={() => <Icon name="arrow-drop-down" type="material" />}
onChange={(item: Option) => {
setValue(item.value);
}}
/>
</View>
);
}
Expand Down
31 changes: 25 additions & 6 deletions src/queries/savedStories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async function fetchUserStories(
user_id: string | undefined,
name: string | undefined,
) {
const { data, error } = await supabase
const { data: storyIds, error } = await supabase
.from('saved_stories')
.select('story_id')
.eq('user_id', user_id)
Expand All @@ -16,20 +16,39 @@ async function fetchUserStories(
if (error) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
`An error occured when trying to fetch user saved stories: ${error.details}`,
`An error occured when trying to fetch user saved stories: ${JSON.stringify(
error,
)}`,
);
}
} else {
return data;
return [];
}

const { error: storyError, data } = await supabase
.from('stories')
.select('*')
.in('id', storyIds?.map(value => value['story_id']));

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

return data;
}

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

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

async function addUserStory(
Expand Down

0 comments on commit 1d6960a

Please sign in to comment.