diff --git a/src/components/SessionCommentItem.tsx b/src/components/SessionCommentItem.tsx new file mode 100644 index 0000000..8022da3 --- /dev/null +++ b/src/components/SessionCommentItem.tsx @@ -0,0 +1,26 @@ +import { motion } from "motion/react"; +import { IComment } from "../types/IComments"; +import { format } from "date-fns"; + +function SessionCommentItem({ comment }: { comment: IComment }) { + return ( + +
+

{comment.owner.fullname}

+
+

+ {comment.text} +

+
+

+ {format(comment.createdAt, "HH:mm")} +

+
+
+
+
+ + ); +} + +export default SessionCommentItem; diff --git a/src/components/SessionComments.tsx b/src/components/SessionComments.tsx index 0cde377..11376e9 100644 --- a/src/components/SessionComments.tsx +++ b/src/components/SessionComments.tsx @@ -1,17 +1,13 @@ import { useRef } from "react"; import SendIcon from "./icons/SendIcon"; import NewButton from "./NewButton"; -import { IComment } from "../types/IComments"; -import { useMutation, useQueryClient } from "@tanstack/react-query"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import api from "../utils/api"; +import { IComment } from "../types/IComments"; +import SessionCommentItem from "./SessionCommentItem"; +import { AnimatePresence } from "motion/react"; -function SessionComments({ - comments, - sessionId, -}: { - comments: IComment[]; - sessionId: string; -}) { +function SessionComments({ sessionId }: { sessionId: string }) { const textareaRef = useRef(null); const formRef = useRef(null); @@ -21,25 +17,43 @@ function SessionComments({ if (!textarea || !form) return; textarea.style.height = "auto"; - form.style.height = "auto"; - const newHeight = Math.min( - textarea.scrollHeight + (32 / 1440) * innerWidth, - (225 / 1440) * innerWidth + const minTextareaHeight = (24 / 1440) * window.innerWidth; + const maxTextareaHeight = (180 / 1440) * window.innerWidth; + const formPadding = (1.111 / 100) * window.innerWidth * 2; + const minFormHeight = (80 / 1440) * window.innerWidth; + const maxFormHeight = (225 / 1440) * window.innerWidth; + + const scrollHeight = textarea.scrollHeight; + + let newTextareaHeight; + if (scrollHeight <= minTextareaHeight) { + newTextareaHeight = minTextareaHeight; + } else if (scrollHeight <= maxTextareaHeight) { + newTextareaHeight = scrollHeight; + } else { + newTextareaHeight = maxTextareaHeight; + } + + const newFormHeight = Math.max( + Math.min(newTextareaHeight + formPadding, maxFormHeight), + minFormHeight ); - const formHeight = Math.min( - form.clientHeight >= 80 ? newHeight + (32 / 1440) * innerWidth : 80, - (225 / 1440) * innerWidth - ); - textarea.style.height = `${newHeight}px`; - form.style.height = `${formHeight}px`; + + textarea.style.height = `${newTextareaHeight}px`; + form.style.height = `${newFormHeight}px`; textarea.style.overflowY = - textarea.scrollHeight > (225 / 1440) * innerWidth ? "auto" : "hidden"; + scrollHeight > maxTextareaHeight ? "auto" : "hidden"; }; const queryClient = useQueryClient(); + const { data: comments } = useQuery({ + queryKey: ["sessions", "comments", sessionId], + queryFn: () => api.get(`comments/${sessionId}`).json(), + }); + const { mutate: createComment } = useMutation({ mutationFn: (comment: string) => api.post("comments", { @@ -48,14 +62,17 @@ function SessionComments({ text: comment, }, }), - onSuccess: () => queryClient.invalidateQueries({ queryKey: ["sessions"] }), + onSuccess: () => + queryClient.invalidateQueries({ + queryKey: ["sessions", "comments"], + }), }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const textarea = textareaRef.current; - if (!textarea?.value) return; + if (!textarea?.value || textarea.value.length > 200) return; createComment(textarea.value); textarea.value = ""; @@ -71,35 +88,39 @@ function SessionComments({ return (
-
- {comments.length > 0 ? ( -
Комменты
- ) : ( -
- ghost -
-

Оставьте заметку

-

- {`В дальнейшем это поможет быстро найти +

+ + {comments && comments.length > 0 ? ( + comments.map((comment) => ( + + )) + ) : ( +
+ ghost +
+

Оставьте заметку

+

+ {`В дальнейшем это поможет быстро найти клиента и не запутаться.`} -

+

+
-
- )} + )} +