From c09744792cedb6623e2815729645a7cf5214c5fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebrar=20K=C4=B1z=C4=B1lo=C4=9Flu?= Date: Fri, 17 May 2024 19:46:07 +0300 Subject: [PATCH] fix network error --- mobile/melodify/components/Post.tsx | 6 +-- mobile/melodify/screens/CommentScreen.tsx | 63 +++++++++++++---------- mobile/melodify/screens/SeePostScreen.tsx | 8 +-- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/mobile/melodify/components/Post.tsx b/mobile/melodify/components/Post.tsx index f8bd3287..934151e2 100644 --- a/mobile/melodify/components/Post.tsx +++ b/mobile/melodify/components/Post.tsx @@ -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); diff --git a/mobile/melodify/screens/CommentScreen.tsx b/mobile/melodify/screens/CommentScreen.tsx index 212ab92a..3a6003e3 100644 --- a/mobile/melodify/screens/CommentScreen.tsx +++ b/mobile/melodify/screens/CommentScreen.tsx @@ -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" >; @@ -30,6 +30,7 @@ const CommentScreen: React.FC = ({ 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([]); const [newComment, setNewComment] = useState(""); @@ -42,34 +43,42 @@ const CommentScreen: React.FC = ({ 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 ( diff --git a/mobile/melodify/screens/SeePostScreen.tsx b/mobile/melodify/screens/SeePostScreen.tsx index 75424f85..01b2907b 100644 --- a/mobile/melodify/screens/SeePostScreen.tsx +++ b/mobile/melodify/screens/SeePostScreen.tsx @@ -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); }