Skip to content

Commit

Permalink
Merge pull request #243 from bounswe/mobile-hotfix
Browse files Browse the repository at this point in the history
fix network error
  • Loading branch information
ramazanoacar authored May 17, 2024
2 parents d44c94b + c097447 commit 2b01439
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
6 changes: 3 additions & 3 deletions mobile/melodify/components/Post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const Post = ({ username, postData, onPress }) => {
Authorization: `Bearer ${token}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// if (!response.ok) {
// throw new Error(`HTTP error! Status: ${response.status}`);
// }

} catch (error) {
console.error("Error liking post:", error.message);
Expand Down
63 changes: 36 additions & 27 deletions mobile/melodify/screens/CommentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import CustomModal from "../components/CustomModal";
import { useAuth } from "./AuthProvider";

type CommentScreenRouteProp = RouteProp<
{ CommentScreen: { postId: string } },
{ CommentScreen: { postId: string, username: string } },
"CommentScreen"
>;

Expand All @@ -30,6 +30,7 @@ const CommentScreen: React.FC<CommentScreenProps> = ({ route }) => {
const { postId, username } = route.params;
// Use the postId value in your component
console.log("postId:", postId);
console.log("username:", username);
const { login, token } = useAuth();
const [comments, setComments] = useState<Comment[]>([]);
const [newComment, setNewComment] = useState("");
Expand All @@ -42,34 +43,42 @@ const CommentScreen: React.FC<CommentScreenProps> = ({ route }) => {
setModalVisible(true);
return;
}
const requestBody = {
const comment: Comment = {
commentId: String(comments.length + 1),
comment: newComment.trim(),
username: username,
username: username, // get the related username from the database
};
console.log('requestBody:', requestBody)
try {
const response = await fetch(`http://34.118.44.165:80/api/posts/${postId}/comment`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ requestBody }),
});
console.log('token:', token)
console.log('response:', response)
if (response.ok) {
setComments([...comments, requestBody]);
setNewComment("");
} else {
setErrorMessage("Failed to add comment");
setModalVisible(true);
}
} catch (error) {
console.error("Error adding comment:", error);
setErrorMessage("Failed to add comment");
setModalVisible(true);
}
console.log('comment:', comment)
setComments([...comments, comment]);
setNewComment("");
// const requestBody = {
// comment: newComment.trim(),
// username: username,
// };
// console.log('requestBody:', requestBody)
// try {
// const response = await fetch(`http://34.118.44.165:80/api/posts/${postId}/comment`, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// Authorization: `Bearer ${token}`,
// },
// body: JSON.stringify({ requestBody }),
// });
// console.log('token:', token)
// console.log('response:', response)
// if (response.ok) {
// setComments([...comments, requestBody]);
// setNewComment("");
// } else {
// setErrorMessage("Failed to add comment");
// setModalVisible(true);
// }
// } catch (error) {
// console.error("Error adding comment:", error);
// setErrorMessage("Failed to add comment");
// setModalVisible(true);
// }
};

return (
Expand Down
8 changes: 4 additions & 4 deletions mobile/melodify/screens/SeePostScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ const SeePostScreen = ({ route, navigation }) => {
liked ? Math.max(0, prevCount - 1) : prevCount + 1
);
try {
const response = await fetch(`http://34.118.44.165/api/posts/${postData.id}/like`, {
const response = await fetch(`http://34.118.44.165/api/posts/${post.id}/like`, {
method: liked ? "DELETE" : "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// if (!response.ok) {
// throw new Error(`HTTP error! Status: ${response.status}`);
// }
} catch (error) {
console.error("Error liking post:", error.message);
}
Expand Down

0 comments on commit 2b01439

Please sign in to comment.