Skip to content

Commit

Permalink
allow replies and fix zipcode lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
angelp03 committed Nov 8, 2024
1 parent 13f6919 commit cd0b602
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
52 changes: 46 additions & 6 deletions src/pages/CafePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useState, useEffect } from 'react';
import { findZipcode } from '../utilities/findZipcode';
import { findCafes } from '../utilities/findCafes';
import { useAuthState } from '../utilities/firebase';
import { findCafePosts } from '../utilities/posts';
import { findCafePosts, addReplyToPost } from '../utilities/posts';
import Banner from '../components/Banner';

const CafePage = () => {
Expand All @@ -13,6 +13,7 @@ const CafePage = () => {
const [zipcode, setZipcode] = useState(null);
const [cafes, setCafes] = useState([]);
const [cafe, setCafe] = useState(null);
const [replyMessages, setReplyMessages] = useState({});

useEffect(() => {
if (user && user.email) {
Expand Down Expand Up @@ -46,7 +47,6 @@ const CafePage = () => {
}, [cafes, place_id]);

const [posts, postError] = findCafePosts(cafe?.placeId);
console.log(posts);

if (!cafe) {
return (
Expand All @@ -57,9 +57,30 @@ const CafePage = () => {
);
}

const postItems = posts ? Object.values(posts) : [];
const postItems = posts ? Object.entries(posts) : [];
const sortedPosts = postItems.sort((a, b) => new Date(b[1].date) - new Date(a[1].date));

const sortedPosts = postItems.sort((a, b) => new Date(b.date) - new Date(a.date));
const handleReplyChange = (e, postId) => {
setReplyMessages((prev) => ({
...prev,
[postId]: e.target.value,
}));
};

const handleAddReply = async (postId) => {
const replyMessage = replyMessages[postId];
if (replyMessage && replyMessage.trim()) {
const error = await addReplyToPost(postId, user?.uid, replyMessage);
if (error) {
console.error('Error adding reply:', error);
} else {
setReplyMessages((prev) => ({
...prev,
[postId]: '',
}));
}
}
};

return (
<div>
Expand All @@ -74,7 +95,7 @@ const CafePage = () => {
<div className="posts-section">
<h2>Posts</h2>
<ul className="posts-list">
{sortedPosts.map((post, index) => (
{sortedPosts.map(([postKey, post], index) => (
<li key={index} className="post-item">
<div className="post-header">
<p><strong>Category:</strong> {post.category}</p>
Expand All @@ -83,6 +104,7 @@ const CafePage = () => {
<div className="post-content">
<p>{post.content}</p>
</div>

{post.replies && Object.entries(post.replies).length > 0 ? (
<div className="replies-section">
<h3>Replies:</h3>
Expand All @@ -97,6 +119,24 @@ const CafePage = () => {
) : (
<p>No replies for this post.</p>
)}

{user ? (
<div className="reply-section">
<textarea
value={replyMessages[postKey] || ''}
onChange={(e) => handleReplyChange(e, postKey)}
placeholder="Write your reply..."
/>
<button
onClick={() => handleAddReply(postKey)}
className="reply-btn"
>
Reply
</button>
</div>
) : (
<p>Please log in to reply.</p>
)}
</li>
))}
</ul>
Expand All @@ -113,6 +153,6 @@ const CafePage = () => {
</div>
</div>
);
}
};

export default CafePage;
1 change: 1 addition & 0 deletions src/pages/ReviewsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ReviewsPage = () => {
const [user] = useAuthState();
const [zipcode, setZipcode] = useState(null);
const [cafes, setCafes] = useState([]);
console.log(user);

useEffect(() => {
const fetchData = async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/utilities/findZipcode.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const getZipCode = async (placeId) => {

const getUniversity = (email) => {
const domain = email.split('@')[1];
const university = domain.split('.')[0];
console.log(domain);
const university = domain.split('.')[1];
return university;
}

Expand Down
34 changes: 32 additions & 2 deletions src/utilities/posts.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect } from 'react';
import { onValue, query, orderByChild, equalTo, ref } from 'firebase/database';
import { onValue, query, orderByChild, equalTo, ref, get, update } from 'firebase/database';
import { database } from './firebase';

export const findCafePosts = (cafeId) => {
Expand Down Expand Up @@ -29,4 +29,34 @@ export const findCafePosts = (cafeId) => {
return () => unsubscribe();
}, [cafeId]);
return [data, error];
};
};

export const addReplyToPost = async (postId, userId, replyMessage) => {
try {
const postRef = ref(database, `/posts/${postId}`);
const postSnapshot = await get(postRef);
if (!postSnapshot.exists()) {
return 'Post not found';
}

const postData = postSnapshot.val();
const replies = postData.replies || {};
const replyId = `replyId_${Object.keys(replies).length + 1}`;

const newReply = {
[replyId]: replyMessage
};

await update(postRef, {
replies: {
...replies,
...newReply
}
});

return null;
} catch (error) {
console.error('Error adding reply:', error);
return error.message || 'Error adding reply';
}
};

0 comments on commit cd0b602

Please sign in to comment.