diff --git a/P3/frontend/src/components/pet/common.jsx b/P3/frontend/src/components/pet/common.jsx index 53bb3b6a..59e46e61 100644 --- a/P3/frontend/src/components/pet/common.jsx +++ b/P3/frontend/src/components/pet/common.jsx @@ -268,10 +268,10 @@ export function ApplicationTable({ applications, pageNumber, hasNextPage, addPag
- -
diff --git a/P3/frontend/src/pages/Message.jsx b/P3/frontend/src/pages/Message.jsx index eabbbe60..e3970820 100644 --- a/P3/frontend/src/pages/Message.jsx +++ b/P3/frontend/src/pages/Message.jsx @@ -1,5 +1,158 @@ -export default function (){ +import React, { useEffect, useState, useRef } from 'react'; +import { Button } from '@material-tailwind/react' +import { FaArrowUp, FaPaperPlane } from 'react-icons/fa'; +import { useParams } from 'react-router-dom'; +import PetApplicationCommentService from '../services/PetApplicationCommentService'; +import { useUser } from '../contexts/UserContext'; +import { useNavigate } from 'react-router-dom'; + +const Message = () => { + const [message, setMessage] = useState(''); + const [messages, setMessages] = useState([]); + const { applicationId } = useParams(); + const { user } = useUser(); + const divRef = useRef(null); + const [totalMessages, setTotalMessages] = useState(0); + const [currPage, setCurrPage] = useState(1); + const navigate = useNavigate(); + + useEffect(() => { + PetApplicationCommentService.list(applicationId) + .then((res) => { + setMessages(res.data.results); + setTotalMessages(res.data.count); + scrollToBottom(); + }) + .catch((err) => { + if (err.response.status === 403) { + navigate('/404') + } + }); + }, [applicationId, navigate]) + + const handleSendMessage = (e) => { + e.preventDefault() + if (message === '') { + return + } + PetApplicationCommentService.create(applicationId, message) + .then((res) => { + setMessages([...messages, res.data]); + setMessage(''); + setTotalMessages(totalMessages + 1); + }) + .catch((err) => { + console.log(err); + }); + } + + const scrollToBottom = () => { + if (divRef.current) { + divRef.current.scrollTop = divRef.current.scrollHeight + } + } + + const scrollToTop = () => { + if (divRef.current) { + divRef.current.scrollTop = 0 + } + } + + useEffect(() => { + scrollToBottom() + }, [messages]); + + useEffect(() => { + scrollToTop() + }, [currPage]); + + const loadMoreMessages = () => { + PetApplicationCommentService.list(applicationId, currPage + 1) + .then((res) => { + setMessages([...messages, ...res.data.results]); + setCurrPage(currPage + 1); + }) + .catch((err) => { + console.log(err); + }); + } + return ( -

Shelter Message

- ) -} \ No newline at end of file +
+
+ { + messages.length < totalMessages && ( +
+ +
+ ) + } + { + messages.sort((a, b) => (new Date(a.date_created)) > (new Date(b.date_created)) ? 1 : -1).map((message, i) => ( +
+ {/* { + message.user !== user.id && ( +

{message.user}

+ ) + } */} +
+ {message.text} +
+

+ {(new Date(message.date_created)).toLocaleTimeString('en-US', { + year: "numeric", + month: "long", + day: "numeric", + })} +

+
+ )) + } +
+
+
+ setMessage(e.target.value)} + className="rounded-full w-full px-6 py-2 border-2 border-gray-800 focus:outline-orange-400" + containerprops={{ + className: "min-w-0", + }} + /> + + +
+
+
+ ); +}; + +export default Message; \ No newline at end of file diff --git a/P3/frontend/src/services/PetApplicationCommentService.js b/P3/frontend/src/services/PetApplicationCommentService.js new file mode 100644 index 00000000..d37df1eb --- /dev/null +++ b/P3/frontend/src/services/PetApplicationCommentService.js @@ -0,0 +1,19 @@ +import { ApiService } from "./ApiService"; + +class PetApplicationCommentService { + static async list(applicationId) { + const response = await ApiService.get(`/applications/${applicationId}/comments`); + return response; + } + static async create(applicationId, text) { + const response = await ApiService.post( + `applications/${applicationId}/comments`, + { + text: text, + } + ); + return response; + } +} + +export default PetApplicationCommentService;