Skip to content

Commit

Permalink
Reactions implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Apr 22, 2024
1 parent 3c15f86 commit 89a796d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
4 changes: 1 addition & 3 deletions src/app/(tabs)/story/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { faHeart as farHeart } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
import { useLocalSearchParams, router } from 'expo-router';
import React, { useEffect, useState } from 'react';
import {
Expand Down Expand Up @@ -205,7 +203,7 @@ function StoryScreen() {
<Text style={globalStyles.bodyBoldUnderline}>Back To Top</Text>
</Button>
<View style={styles.bottomReactionContainer}>
<ReactionPicker />
<ReactionPicker storyId={parseInt(storyId as string, 10)} />
</View>
</ScrollView>
)}
Expand Down
4 changes: 2 additions & 2 deletions src/components/ReactionDisplay/ReactionDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type ReactionDisplayProps = {
};

function ReactionDisplay({ reactions }: ReactionDisplayProps) {
const cleanedReactions = reactions.filter(reaction => reaction !== null);
const cleanedReactions = reactions.filter(reaction => reaction != null);
const reactionColors: Record<string, string> = {
heart: '#FFCCCB',
clap: '#FFD580',
Expand All @@ -32,7 +32,7 @@ function ReactionDisplay({ reactions }: ReactionDisplayProps) {
}}
>
{reactionDisplay.map(reaction => {
if (reaction === null) return;
if (reaction == null) return;

return (
<View
Expand Down
45 changes: 20 additions & 25 deletions src/components/ReactionPicker/ReactionPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import { faFaceSmile } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
import { useState } from 'react';
import { View, TouchableOpacity, StyleSheet, Text } from 'react-native';
import { View, TouchableOpacity } from 'react-native';

import styles from './styles';
import Emoji from 'react-native-emoji';
import { addReactionToStory } from '../../queries/reactions';
import { useSession } from '../../utils/AuthContext';

const ReactionPicker = () => {
const [showReactions, setShowReactions] = useState(false);
type ReactionPickerProps = {
storyId: number
}

const ReactionPicker = ({ storyId }: ReactionPickerProps) => {
const { user } = useSession();
const [showReactions, setShowReactions] = useState(false);
const toggleReactions = () => setShowReactions(!showReactions);
const reactionMapping: Record<string, number> = { "heart": 2, "clap": 3, "muscle": 4, "cry": 5, "hugging_face": 6 };

// Dummy onPress functions
const onClapPress = () => console.log('Smile pressed');
const onHeartPress = () => console.log('Heart pressed');
const onMusclePress = () => console.log('Thumbs up pressed');
const onCryPress = () => console.log('Laugh pressed');
const onHuggingFacePress = () => console.log('Laugh pressed');
const addReaction = (reactionName: string) => {
const reactionId = reactionMapping[reactionName];
addReactionToStory(user?.id, storyId, reactionId);
}

// <View style={styles.container}>
return (
<TouchableOpacity style={styles.reactionView} onPress={toggleReactions}>
<View style={styles.reactionsContainer}>
Expand All @@ -27,21 +31,12 @@ const ReactionPicker = () => {
{showReactions && (
<>
<View style={{ marginLeft: 12 }} />
<TouchableOpacity onPress={onHeartPress}>
<Emoji name="heart" />
</TouchableOpacity>
<TouchableOpacity onPress={onClapPress}>
<Emoji name="clap" />
</TouchableOpacity>
<TouchableOpacity onPress={onMusclePress}>
<Emoji name="muscle" />
</TouchableOpacity>
<TouchableOpacity onPress={onCryPress}>
<Emoji name="cry" />
</TouchableOpacity>
<TouchableOpacity onPress={onHuggingFacePress}>
<Emoji name="hugging_face" />
</TouchableOpacity>
{Object.keys(reactionMapping).map((reaction, i) => (
<TouchableOpacity key={i} onPress={() => addReaction(reaction)}>
<Emoji name={reaction} />
</TouchableOpacity>
)
)}
</>
)}
</View>
Expand Down
11 changes: 6 additions & 5 deletions src/queries/reactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Reactions } from './types';
import supabase from '../utils/supabase';

export async function addReactionToStory(
input_profile_id: number,
input_profile_id: string | undefined,
input_story_id: number,
input_reaction_id: number,
): Promise<void> {
Expand All @@ -22,7 +22,7 @@ export async function addReactionToStory(
}

export async function deleteReactionToStory(
input_profile_id: number,
input_profile_id: string | undefined,
input_story_id: number,
input_reaction_id: number,
): Promise<void> {
Expand All @@ -43,17 +43,18 @@ export async function deleteReactionToStory(

export async function fetchAllReactionsToStory(
storyId: number,
): Promise<Reactions[]> {
): Promise<string[]> {
const { data, error } = await supabase.rpc('curr_get_reactions_for_story', {
input_story_id: storyId,
story_id: storyId,
});

if (error) {
console.log(error);
throw new Error(
`An error occured when trying to fetch reactions to a story', ${error}`,
);
} else {
return data as Reactions[];
return data;
}
}

Expand Down

0 comments on commit 89a796d

Please sign in to comment.