Skip to content

Commit

Permalink
fix: image popups and comments on other users' pages
Browse files Browse the repository at this point in the history
  • Loading branch information
rdtabb committed Sep 23, 2023
1 parent 113f9ab commit ed66d83
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 153 deletions.
1 change: 1 addition & 0 deletions _redirects
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* /index.html 200
18 changes: 7 additions & 11 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import Usersearch from "./components/Userlist/Usersearch";
import Auser from "./components/AnotherUser/Auser";
import LikedPosts from "./components/LikedPosts/LikedPosts";
import Chat from "./components/Chat/Chat";
import { Provider } from "react-redux";
import { store } from "./store/store";

export const cookies = new Cookies();

Expand All @@ -31,15 +29,13 @@ const App = () => {

return (
<ErrorBoundary>
<Provider store={store}>
<Routes>
<Route path="/" element={<Profile />} />
<Route path="/usersearch" element={<Usersearch />} />
<Route path="/user/:id" element={<Auser />} />
<Route path="/likedposts" element={<LikedPosts />} />
<Route path="/chat" element={<Chat />} />
</Routes>
</Provider>
<Routes>
<Route path="/" element={<Profile />} />
<Route path="/usersearch" element={<Usersearch />} />
<Route path="/user/:id" element={<Auser />} />
<Route path="/likedposts" element={<LikedPosts />} />
<Route path="/chat" element={<Chat />} />
</Routes>
</ErrorBoundary>
);
};
Expand Down
7 changes: 6 additions & 1 deletion src/components/AnotherUser/Auser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ import Container from "../Container/Container";
import useAuserData from "../../hooks/useQueryHooks/useAuserData";
import LoadingImage from "../LoadingStates/LoadingImage";
import Loading from "../LoadingStates/LoadingPosts";
import { useSelector } from "react-redux";
import { RootState } from "../../store/store";

const Auser = () => {
const { id } = useParams();
const auserData = useAuserData(id!);
const openPopupType = useSelector(
(state: RootState) => state.modal.openPopupType,
);

return (
<Container>
Expand Down Expand Up @@ -50,7 +55,7 @@ const Auser = () => {
<Aposts posts={auserData.data?.newPosts} name={auserData.data?.name} />
)}
<Footer />
<Auserpopup id={id} />
<>{openPopupType === "image" && <Auserpopup id={id} />}</>
</Container>
);
};
Expand Down
102 changes: 58 additions & 44 deletions src/components/AnotherUser/Auserpopup.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,71 @@
import { useState } from "react";
import useGeneralContext from "../../hooks/useContextHooks/useGeneralContext";
import { useRef, useEffect } from "react";
import { useForm } from "react-hook-form";
import { useSelector } from "react-redux";
import { RootState } from "../../store/store";
import Modal from "../Modal/Modal";

import useComments from "../../hooks/useQueryHooks/useComments";
import CommentsSection from "../Modal/components/Comments";
import LoadingComments from "../LoadingStates/LoadingComments";

type PropsType = {
id: string | undefined;
};

const Auserpopup = ({ id }: PropsType) => {
const { handleClose, handleComment, comments } = useGeneralContext();
const [currAMessage, setCurrAMessage] = useState<string>("");
const post = useSelector((state: RootState) => state.modal.selectedPost);
const popupRef = useRef<HTMLDivElement>(null);

const commentsMutation = useComments("mutation");
const commentsQuery = useComments("query", id, post?.id);

const { register, handleSubmit, setFocus, reset } = useForm<{
comment: string;
}>({
mode: "onChange",
});

useEffect(() => {
setFocus("comment");
}, []);

return (
<div data-visible="false" className="popup popup--image">
<div className="popup__container popup__container--image">
<div className="popup--image__container">
<img src="" alt="" className="popup__image" />
<div className="textarea">
<p className="popup__caption"></p>
<ul className="comments">
{comments.map((comment) => (
<li key={comment.id} className="comment">
<img className="comment__icon" src={comment.img} alt="" />
<article>
<div className="comment__info">
<p className="comment__creator">{comment.creator}</p>
<p className="comment__date">{comment.createdAt}</p>
</div>
<p className="comment__message">{comment.message}</p>
</article>
</li>
))}
</ul>
<form
onSubmit={(e) =>
handleComment(e, currAMessage, id, setCurrAMessage)
}
>
<input
value={currAMessage}
onChange={(e) => setCurrAMessage(e.target.value)}
placeholder="Leave your comment..."
type="text"
className="popup__comment"
/>
</form>
</div>
<Modal
ref={popupRef}
modalModifier="popup--image"
containerModifier="popup__container--image"
>
<div className="popup--image__container">
<img src={post?.imgsrc} alt={post?.city} className="popup__image" />
<div className="textarea">
<p className="popup__caption"></p>
<ul className="comments">
{commentsQuery?.isLoading ? (
<LoadingComments />
) : (
<CommentsSection comments={commentsQuery?.data} />
)}
</ul>
<form
onSubmit={handleSubmit((data) => {
reset();
return commentsMutation.mutate({
comment: data.comment,
post,
id,
});
})}
>
<input
placeholder="Leave your comment..."
type="text"
className="popup__comment"
{...register("comment")}
/>
</form>
</div>
<button
onClick={handleClose}
type="button"
className="popup__close popup__close--image"
></button>
</div>
</div>
</Modal>
);
};

Expand Down
7 changes: 2 additions & 5 deletions src/components/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DocumentReference,
DocumentData,
} from "firebase/firestore";
import MeetsLogoIcon from "../assets/meets-logo.svg";
import useGeneralContext from "../hooks/useContextHooks/useGeneralContext";

export const Auth = () => {
Expand Down Expand Up @@ -59,11 +60,7 @@ export const Auth = () => {
return (
<main className="auth">
<div className="img-container">
<img
className="auth__logo"
src="src/assets/meets-logo.svg"
alt="meets-logo"
/>
<img className="auth__logo" src={MeetsLogoIcon} alt="meets-logo" />
</div>
<h1 className="auth__header">Sign in with Google</h1>
<button onClick={signin} className="auth__signin">
Expand Down
62 changes: 52 additions & 10 deletions src/components/LoadingStates/LoadingComments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,58 @@ import React from "react";

const LoadingComments = () => {
return (
<li className="comment comment--empty">
<img className="comment__icon" src="" alt="" />
<article>
<div className="comment__info">
<p className="comment__creator"></p>
<p className="comment__date"></p>
</div>
<p className="comment__message"></p>
</article>
</li>
<>
<li className="comment comment--empty">
<img className="comment__icon" alt="" />
<article>
<div className="comment__info">
<p className="comment__creator"></p>
<p className="comment__date"></p>
</div>
<p className="comment__message"></p>
</article>
</li>
<li className="comment comment--empty">
<img className="comment__icon" alt="" />
<article>
<div className="comment__info">
<p className="comment__creator"></p>
<p className="comment__date"></p>
</div>
<p className="comment__message"></p>
</article>
</li>
<li className="comment comment--empty">
<img className="comment__icon" alt="" />
<article>
<div className="comment__info">
<p className="comment__creator"></p>
<p className="comment__date"></p>
</div>
<p className="comment__message"></p>
</article>
</li>
<li className="comment comment--empty">
<img className="comment__icon" alt="" />
<article>
<div className="comment__info">
<p className="comment__creator"></p>
<p className="comment__date"></p>
</div>
<p className="comment__message"></p>
</article>
</li>
<li className="comment comment--empty">
<img className="comment__icon" alt="" />
<article>
<div className="comment__info">
<p className="comment__creator"></p>
<p className="comment__date"></p>
</div>
<p className="comment__message"></p>
</article>
</li>
</>
);
};

Expand Down
38 changes: 17 additions & 21 deletions src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,23 @@ const Modal = forwardRef(
};
}, []);

return (
<>
{createPortal(
<div
ref={popupRef}
data-visible="false"
onClick={closePopupOnOverlay}
className={`popup ${modalModifier}`}
>
<div className={`popup__container ${containerModifier}`}>
{children}
<button
onClick={closePopup}
type="button"
className="popup__close"
></button>
</div>
</div>,
document.getElementById("root")!,
)}
</>
return createPortal(
<div
ref={popupRef}
data-visible="false"
onClick={closePopupOnOverlay}
className={`popup ${modalModifier}`}
>
<div className={`popup__container ${containerModifier}`}>
{children}
<button
onClick={closePopup}
type="button"
className="popup__close"
></button>
</div>
</div>,
document.getElementById("root")!,
);
},
);
Expand Down
34 changes: 34 additions & 0 deletions src/components/Modal/components/Comments.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useRef, memo, useEffect } from "react";
import { Comment } from "../../../types/Types";

type CommentsSectionProps = {
comments: Comment[] | undefined;
};

const CommentsSection = ({ comments }: CommentsSectionProps) => {
const scrollRef = useRef<HTMLDivElement>(null);

useEffect(() => {
scrollRef.current?.scrollIntoView({ behavior: "smooth" });
}, [comments]);

return (
<>
{comments?.map((comment) => (
<li key={comment.id} className="comment">
<img className="comment__icon" src={comment.img} alt="" />
<article>
<div className="comment__info">
<p className="comment__creator">{comment.creator}</p>
<p className="comment__date">{comment.createdAt}</p>
</div>
<p className="comment__message">{comment.message}</p>
</article>
</li>
))}
<div ref={scrollRef}></div>
</>
);
};

export default memo(CommentsSection);
Loading

0 comments on commit ed66d83

Please sign in to comment.