Skip to content

Commit

Permalink
Merge pull request #317 from bounswe/delete-post-and-connect-progress
Browse files Browse the repository at this point in the history
Delete post and connect progress
  • Loading branch information
aasimdag authored Dec 15, 2024
2 parents b3bb74c + 27988a9 commit 9275394
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 24 deletions.
4 changes: 2 additions & 2 deletions mobile/components/CreatePost.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ const {
Toast.show({
type: 'error',
position: 'bottom',
text1: 'Login Error',
text2: 'There was an error creating post. Please try again.',
text1: 'Create Post Error',
text2: 'There was an error while creating post. Please try again.',
visibilityTime: 2000,
autoHide: true,
topOffset: 30,
Expand Down
43 changes: 41 additions & 2 deletions mobile/components/JoinedExercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import {
ScrollView,
} from 'react-native';

import Toast from 'react-native-toast-message';
import { useQuery } from "@tanstack/react-query"
import { useSelector } from 'react-redux';
import { userPassword, userProfile, userSessionToken } from '../user.js';
import apiInstance from '../Api'

const ExerciseDetail = ({ route }) => {
const {programId,weekId,weekNumber,workoutId,workoutNumber,exerciseNumber,exerciseId,exercise,sets,repetitions} =route.params;

Expand All @@ -28,14 +34,47 @@ const ExerciseDetail = ({ route }) => {
setRepInputs(updatedInputs);
};

const sessionToken = useSelector(userSessionToken);

// Function to handle form submission
const handleSubmit = () => {
const handleSubmit = async () => {
if(repInputs.some((rep)=>{return rep.length==0})){
ToastAndroid.show('Please fill all set fields!', ToastAndroid.SHORT);
return;
}

var newInputs = repInputs.map(i=>parseInt(i,10));
console.log(repInputs);
const response = await apiInstance(sessionToken).post(`/api/training-programs/${programId}/workout-exercises/${exerciseId}/complete`, newInputs);
console.log(response);

if (response.status === 200) {
setIsSubmitted(true);
ToastAndroid.show('Successfully submitted!', ToastAndroid.SHORT);
Toast.show({
type: 'success',
position: 'bottom',
text1: 'Progress Submitted',
text2: 'Your progress has been submitted successfully.',
visibilityTime: 3000,
autoHide: true,
topOffset: 30,
bottomOffset: 40
});
//goHome();

//Cookies.set("username", username)
} else {
Toast.show({
type: 'error',
position: 'bottom',
text1: 'Submit Error',
text2: 'There was an error submitting progress. Please try again.',
visibilityTime: 2000,
autoHide: true,
topOffset: 30,
bottomOffset: 40
});
}
};

return (
Expand Down
2 changes: 2 additions & 0 deletions mobile/components/JoinedWeek.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {

const JoinedWeek = ({ route }) => {
const {programId,programTitle,weekId,weekNumber,workouts,navigation} = route.params;
console.log("Response for JoinedWeek:");
console.log(workouts);
const {expandedState,setExpandedState} = useState(0);
const renderWorkout = ({ item, index, navigation }) => {
return (
Expand Down
6 changes: 4 additions & 2 deletions mobile/components/JoinedWorkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import {

const JoinedWorkout = ({ route }) => {
const {programId,weekId,weekNumber,workoutId,name,workoutNumber,workoutExercises, navigation} = route.params;
console.log("Response for JoinedWorkout:");
console.log(workoutExercises);
const {expandedState,setExpandedState} = useState(0);
const renderExercise = ({ item, index, navigation }) => {

return (
<View style={styles.exerciseContainer}>
<View style={styles.exerciseNameContainer}>
Expand All @@ -26,8 +29,7 @@ const JoinedWorkout = ({ route }) => {
<TouchableOpacity style={styles.startButton} onPress={()=>navigation.navigate('JoinedExercise',{programId,weekId,weekNumber,workoutId,workoutNumber,exerciseNumber:item.exerciseNumber,exerciseId:item.id,exercise:item.exercise,sets:item.sets,repetitions:item.repetitions})}>
<Text style={styles.startButtonText}>Start</Text>
</TouchableOpacity>
<Text style={styles.completionText}>0%</Text>

<Text style={styles.completionText}>%{ (100 *( item.completedSets?(item.completedSets.reduce((accumulator, currentValue) => accumulator + currentValue, 0))/((item.sets) * (item.repetitions)):0))}</Text>
</View>
);
};
Expand Down
100 changes: 82 additions & 18 deletions mobile/components/ProfilePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { UserContext } from "../UserContext";
import { useSelector } from 'react-redux';
import { userName, userProfile, userSessionToken } from '../user.js';
import { useQuery } from "@tanstack/react-query"
import Toast from 'react-native-toast-message';
import apiInstance from '../Api';

const ProfilePage = ({ darkMode }) => {
Expand All @@ -19,15 +20,57 @@ const ProfilePage = ({ darkMode }) => {
const profile = useSelector(userProfile);
const [user, setUser] = useState({})
const [posts, setPosts] = useState([]);
const [isDeleted, setIsDeleted] = useState(false);
const [programs, setPrograms] = useState([]);
const [followers, setFollowers] = useState([]);

const [following, setFollowing] = useState([]);

const deletePost = async(id) =>{
try {
const response = await apiInstance(sessionToken).delete(`api/posts/${id}`);
if (Math.floor(response.status/100) == 2) {
setIsDeleted(true);
Toast.show({
type: 'success',
position: 'bottom',
text1: 'Post Deleted',
text2: 'Your post has been deleted successfully.',
visibilityTime: 3000,
autoHide: true,
topOffset: 30,
bottomOffset: 40
});
}
else{
Toast.show({
type: 'error',
position: 'bottom',
text1: 'Delete Post Error',
text2: 'There was an error while deleting this post. Please try again.',
visibilityTime: 2000,
autoHide: true,
topOffset: 30,
bottomOffset: 40
});
}
} catch (e) {
Toast.show({
type: 'error',
position: 'bottom',
text1: 'Delete Post Error',
text2: 'There was an error while deleting this post. Please try again.',
visibilityTime: 2000,
autoHide: true,
topOffset: 30,
bottomOffset: 40
});
console.log(e);
}
}
const fetchJoinedPrograms = async (userJoinedData) => {
const programArr = [];

console.log(userJoinedData);
//console.log(userJoinedData);

for (const item of userJoinedData) {
try {
Expand Down Expand Up @@ -66,7 +109,7 @@ const ProfilePage = ({ darkMode }) => {
refetchOnWindowFocus: false,
refetchInterval:60000
});
const {
/*const {
data: joinedData,
isFetching: programsIsFetching,
isLoading: programsIsLoading,
Expand All @@ -84,7 +127,7 @@ const {
programArr = fetchJoinedPrograms(joinedData);
setPrograms(programArr);
}
}, [joinedData, programsIsFetching])
}, [joinedData, programsIsFetching])*/
const {
data: postsData,
isFetching: postsIsFetching,
Expand All @@ -102,8 +145,8 @@ const {
}

},
refetchOnWindowFocus: false,
refetchInterval:60000
refetchOnWindowFocus: true,
refetchInterval:1000
});
const {
data: followersData,
Expand Down Expand Up @@ -142,28 +185,30 @@ const {
refetchOnWindowFocus: false,
refetchInterval:60000
})
/*const {
const {
data: programsData,
isFetching: programsIsFetching,
isLoading: programsIsLoading,
} = useQuery({
queryKey: ['programs'],
queryFn: async () => {
try {
const response = await apiInstance(token).get(`api/training-programs/trainer/${username}`)
const response = await apiInstance(sessionToken).get(`api/training-programs/joined/${username}`);
console.log("Joined programs response:")
console.log(response.data);
//response.data.weeks.forEach((workout)=>console.log(workout.workoutExercises));
return response.data
} catch (error) {
return []
}
},
refetchOnWindowFocus: false,
})*/
/*useEffect(() => {
})
useEffect(() => {
if (programsData && !programsIsFetching) {
setPrograms(programsData)
}
}, [programsData, programsIsFetching])*/
}, [programsData, programsIsFetching])

useEffect(() => {
if (profileData && !profileIsFetching) {
Expand All @@ -174,10 +219,12 @@ useEffect(() => {
}
}, [profileData, profileIsFetching])
useEffect(() => {
if (postsData && !postsIsFetching) setPosts(postsData);
if (postsData && !postsIsFetching){ setPosts(postsData);
setIsDeleted(false);
}
//console.log(posts);

}, [postsData, postsIsFetching]);
}, [postsData, isDeleted, postsIsFetching]);
useEffect(() => {
if (followersData && !followersIsFetching) setFollowers(followersData);
//console.log(followers);
Expand All @@ -190,8 +237,8 @@ useEffect(() => {
}, [followingData, followingIsFetching]);


console.log(username);
console.log(profile);
//console.log(username);
//console.log(profile);



Expand Down Expand Up @@ -247,7 +294,8 @@ useEffect(() => {
data={posts}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => (
<PostCard
<View style={styles.header}>
<PostCard
program_id={item.trainingProgram.id}
description={item.content}
owner={item.username}
Expand All @@ -258,7 +306,11 @@ useEffect(() => {
commentList={forumPosts[0].commentList}
date={item.createdAt}
navigation={navigation}
/>
/>
<TouchableOpacity style={styles.deleteButton} onPress={()=>deletePost(item.id)}>
<Text>Delete Post</Text>
</TouchableOpacity>
</View>
)}
style={styles.postList}
showsVerticalScrollIndicator={false}
Expand Down Expand Up @@ -473,6 +525,12 @@ const lightStyles = StyleSheet.create({
postList: {
marginTop: 10,
},
deleteButton: {
backgroundColor: '#FF0000',
paddingVertical: 10,
paddingHorizontal: 40,
borderRadius: 20,
}
});

const darkStyles = StyleSheet.create({
Expand Down Expand Up @@ -566,6 +624,12 @@ const darkStyles = StyleSheet.create({
postList: {
marginTop: 10,
},
deleteButton: {
backgroundColor: '#FF0000',
paddingVertical: 10,
paddingHorizontal: 40,
borderRadius: 20,
},
});

export default ProfilePage;

0 comments on commit 9275394

Please sign in to comment.