diff --git a/frontend/sweet-red-beans/src/components/DMPage/DMDetail.js b/frontend/sweet-red-beans/src/components/DMPage/DMDetail.js
index d0540fd..825e9a0 100644
--- a/frontend/sweet-red-beans/src/components/DMPage/DMDetail.js
+++ b/frontend/sweet-red-beans/src/components/DMPage/DMDetail.js
@@ -1,9 +1,9 @@
import React, {
- useState,
- useCallback,
- useEffect,
- useMemo,
- useRef,
+ useState,
+ useCallback,
+ useEffect,
+ useMemo,
+ useRef,
} from 'react';
import * as StompJs from '@stomp/stompjs';
import SockJS from 'sockjs-client';
@@ -24,414 +24,357 @@ let stompClient = null;
//props를 selectedRoom으로 바꾸고 roomId는 selectedRoom.chat_room_id으로 바꾸기
//transaction_id 값 바꾸기
const DMDetail = ({ selectedRoom }) => {
- const navigation = useNavigate();
-
- const [modalOpen, setModalOpen] = useState(false);
-
- const [reportContent, setReportContent] = useState('');
- //거래완료되면 true
- //초기값 selectedRoom.is_complete로 설정하기
- const [complete, setComplete] = useState(selectedRoom.is_complete);
-
- const [imgFile, setImgFile] = useState(null);
- const [imgBase64, setImgBase64] = useState(null);
-
- const [myNickname, setMyNickname] = useState('');
-
- const openModal = () => {
- setModalOpen(true);
- };
- const closeModal = () => {
- setModalOpen(false);
- };
-
- //대화 내용, 상대방 것 포함
- const [contents, setContents] = useState([]);
- const [username, setUsername] = useState('');
- //내가 보낸 메시지 내용
- const [message, setMessage] = useState('');
-
- let socket = new SockJS('http://localhost:8080/ws-stomp');
- let stompClient = Stomp.over(socket);
-
- useEffect(() => {
- axios
- .get('http://localhost:8080/mypage', {
- withCredentials: true,
- })
- .then((response) => {
- setMyNickname(response.data.user.nickname);
- })
- .catch((e) => console.log(e));
- }, []);
-
- useEffect(() => {
- console.log(myNickname);
- }, [myNickname]);
-
- //전송 버튼 눌렀을 때
- const sendClick = () => {
- // if(!cookies.get("login")) {
- // alert("로그인을 먼저 해주세요");
- // navigation('/')
- // }
- sendMessage(message);
- //서버에서 받아올 때처럼 비슷한 형식으로 넣어주기 위해
- setMessage('');
- };
-
- const sendMessage = (message) => {
- const fd = new FormData();
-
- if (imgFile === null) {
- if (message !== '') {
- stompClient.send(
- '/pub/chat/message',
- {},
- JSON.stringify({
- content: message,
- chat_room_id: selectedRoom.chat_room_id,
- })
- );
- }
- } else {
- stompClient.send(
- '/pub/chat/message',
- {},
- JSON.stringify({
- content: message,
- chat_room_id: selectedRoom.chat_room_id,
- image_url: imgBase64,
- })
- );
- setImgBase64(null);
- setImgFile(null);
+ const navigation = useNavigate();
+
+ const [modalOpen, setModalOpen] = useState(false);
+
+ const [reportContent, setReportContent] = useState('');
+ //거래완료되면 true
+ //초기값 selectedRoom.is_complete로 설정하기
+ // const [complete, setComplete] = useState(selectedRoom.is_complete);
+ const [complete, setComplete] = useState(false);
+ const [imgFile, setImgFile] = useState(null);
+ const [imgBase64, setImgBase64] = useState(null);
+
+ const [myNickname, setMyNickname] = useState('');
+
+ const openModal = () => {
+ setModalOpen(true);
+ };
+ const closeModal = () => {
+ setModalOpen(false);
+ };
+
+ //대화 내용, 상대방 것 포함
+ const [contents, setContents] = useState([]);
+ const [username, setUsername] = useState('');
+ //내가 보낸 메시지 내용
+ const [message, setMessage] = useState('');
+
+ let socket = new SockJS('http://localhost:8080/ws-stomp');
+ let stompClient = Stomp.over(socket);
+
+ //이제까지 메시지 내역 조회
+ useEffect(() => {
+ console.log(selectedRoom);
+ setContents([]);
+ setMessage('');
+ if (selectedRoom !== undefined) {
+ axios
+ .get('http://localhost:8080/direct-message/detail', {
+ withCredentials: true,
+ params: {
+ room_id: selectedRoom.chat_room_id,
+ },
+ })
+ .then((response) => {
+ setContents(response.data.message);
+ })
+ .catch((error) => console.log(error));
+ }
+
+ stompClient.connect({}, () => {
+ stompClient.subscribe(
+ '/sub/chat/room/' + selectedRoom.chat_room_id,
+ (data) => {
+ console.log(JSON.parse(data.body));
+ const newMessage = JSON.parse(data.body);
+ addMessage(newMessage);
}
+ );
+ });
+ setComplete(selectedRoom.is_complete);
+
+ return () => {
+ if (stompClient != null) {
+ stompClient.disconnect();
+ setContents(null);
+ }
};
-
- useEffect(() => {
- console.log(imgBase64);
- }, [imgBase64]);
-
- useEffect(() => {
- console.log(contents);
- }, [contents]);
-
- const addMessage = (message) => {
- //상대에게 받아온 메시지를 추가함
- setContents((prev) => [...prev, message]);
+ }, [selectedRoom]);
+
+ //전송 버튼 눌렀을 때
+ const sendClick = () => {
+ sendMessage(message);
+ setMessage('');
+ };
+
+ const sendMessage = (message) => {
+ const fd = new FormData();
+
+ if (imgFile === null) {
+ if (message !== '') {
+ stompClient.send(
+ '/pub/chat/message',
+ {},
+ JSON.stringify({
+ content: message,
+ chat_room_id: selectedRoom.chat_room_id,
+ })
+ );
+ }
+ } else {
+ stompClient.send(
+ '/pub/chat/message',
+ {},
+ JSON.stringify({
+ content: message,
+ chat_room_id: selectedRoom.chat_room_id,
+ image_url: imgBase64,
+ })
+ );
+ setImgBase64(null);
+ setImgFile(null);
+ }
+ };
+
+ const addMessage = (message) => {
+ //상대에게 받아온 메시지를 추가함
+ setContents((prev) => [...prev, message]);
+ };
+
+ const onChange = useCallback((e) => {
+ setMessage(e.target.value);
+ }, []);
+
+ //신뢰도 +1 주는 버튼
+ const reliabilityPlusClick = () => {
+ //신뢰도 1 주기
+ if (complete === false) {
+ alert('거래완료를 해야 신뢰도를 줄 수 있습니다');
+ } else {
+ const body = {
+ user_id: selectedRoom.not_mine_id,
+ };
+ axios
+ .post('http://localhost:8080/direct-message/reliability', body, {
+ withCredentials: true,
+ })
+ .then((response) => {
+ if (response.data.result) {
+ alert('상대방의 신뢰도가 올랐습니다.');
+ } else {
+ alert('신뢰도를 올리는 데 실패했습니다.');
+ }
+ })
+ .catch((error) => console.log(error));
+ }
+ };
+
+ //신고 내용
+ const reportContentChange = (e) => {
+ setReportContent(e.target.value);
+ };
+
+ //신고 확인 버튼 눌렀을 때
+ const reportConfirm = () => {
+ const body = {
+ user_id: '1',
+ transaction_id: selectedRoom.transaction_id,
+ report_content: reportContent,
};
-
- const onChange = useCallback((e) => {
- setMessage(e.target.value);
- }, []);
-
- //이제까지 메시지 내역 조회
- useEffect(() => {
- console.log(selectedRoom);
- setContents([]);
- setMessage('');
- if (selectedRoom !== undefined) {
- axios
- .get('http://localhost:8080/direct-message/detail', {
- withCredentials: true,
- params: {
- room_id: selectedRoom.chat_room_id,
- },
- })
- .then((response) => {
- //setMList(response.data.message.map(item => item.message_content));
- setContents(response.data.message);
- })
- .catch((error) => console.log(error));
- }
-
- stompClient.connect({}, () => {
- stompClient.subscribe(
- '/sub/chat/room/' + selectedRoom.chat_room_id,
- (data) => {
- console.log(JSON.parse(data.body));
- const newMessage = JSON.parse(data.body);
- addMessage(newMessage);
- }
- );
- });
- setComplete(selectedRoom.is_complete);
-
- return () => {
- if (stompClient != null) {
- stompClient.disconnect();
- }
- };
- }, [selectedRoom]);
-
- //신뢰도 +1 주는 버튼
- const reliabilityPlusClick = () => {
- //신뢰도 1 주기
- if (complete === false) {
- alert('거래완료를 해야 신뢰도를 줄 수 있습니다');
+ axios
+ .post('http://localhost:8080/direct-message/report', body, {
+ withCredentials: true,
+ })
+ .then((response) => {
+ if (response.data.result) {
+ alert('신고되었습니다.');
+ setReportContent('');
+ setModalOpen(false);
} else {
- const body = {
- user_id: selectedRoom.not_mine_id,
- };
- axios
- .post(
- 'http://localhost:8080/direct-message/reliability',
- body,
- { withCredentials: true }
- )
- .then((response) => {
- if (response.data.result) {
- alert('상대방의 신뢰도가 올랐습니다.');
- } else {
- alert('신뢰도를 올리는 데 실패했습니다.');
- }
- })
- .catch((error) => console.log(error));
+ alert('신고에 실패했습니다.');
}
+ })
+ .catch((error) => console.log(error));
+ };
+
+ //삭제 취소 버튼 눌렀을 때
+ const cancelConfirm = (e) => {
+ console.log('신고 취소');
+ };
+
+ //모달창에서 신고하기 버튼 눌렀을 때
+ const reportClick = (e) => {
+ e.preventDefault();
+ if (reportContent === '') {
+ alert('신고 내용을 입력해주세요');
+ } else {
+ if (window.confirm('정말 신고하시겠습니까?')) {
+ reportConfirm();
+ } else {
+ cancelConfirm();
+ }
+ }
+ };
+
+ //거래완료 버튼 눌렀을 때
+ const completeClick = () => {
+ const body = {
+ transaction_id: selectedRoom.transaction_id,
};
-
- //신고 내용
- const reportContentChange = (e) => {
- setReportContent(e.target.value);
- };
-
- //신고 확인 버튼 눌렀을 때
- const reportConfirm = () => {
- const body = {
- user_id: '1',
- transaction_id: selectedRoom.transaction_id,
- report_content: reportContent,
- };
- axios
- .post('http://localhost:8080/direct-message/report', body, {
- withCredentials: true,
- })
- .then((response) => {
- if (response.data.result) {
- alert('신고되었습니다.');
- setReportContent('');
- setModalOpen(false);
- } else {
- alert('신고에 실패했습니다.');
- }
- })
- .catch((error) => console.log(error));
- };
-
- //삭제 취소 버튼 눌렀을 때
- const cancelConfirm = (e) => {
- console.log('신고 취소');
- };
-
- //모달창에서 신고하기 버튼 눌렀을 때
- const reportClick = (e) => {
- e.preventDefault();
- if (reportContent === '') {
- alert('신고 내용을 입력해주세요');
+ axios
+ .post('http://localhost:8080/direct-message/transaction-complete', body, {
+ withCredentials: true,
+ })
+ .then((response) => {
+ if (response.data.result) {
+ setComplete(true);
} else {
- if (window.confirm('정말 신고하시겠습니까?')) {
- reportConfirm();
- } else {
- cancelConfirm();
- }
+ alert('거래완료에 실패했습니다.');
}
+ })
+ .catch((error) => console.log(error));
+ };
+
+ useEffect(() => {
+ // 메시지가 오면 스크롤 맨 아래로 내려주기
+ let messagebox = document.querySelector('#messagebox');
+ messagebox.scrollTop = messagebox.scrollHeight;
+ }, [contents, imgBase64]);
+
+ const fileChange = (e) => {
+ setImgFile(e.target.files);
+ };
+
+ const previewCancelClick = () => {
+ setImgFile(null);
+ setImgBase64(null);
+ };
+
+ useEffect(() => {
+ if (!imgFile) return false;
+
+ const reader = new FileReader();
+ reader.onloadend = () => {
+ setImgBase64(reader.result);
};
-
- //거래완료 버튼 눌렀을 때
- const completeClick = () => {
- const body = {
- transaction_id: selectedRoom.transaction_id,
- };
- axios
- .post(
- 'http://localhost:8080/direct-message/transaction-complete',
- body,
- { withCredentials: true }
- )
- .then((response) => {
- if (response.data.result) {
- setComplete(true);
- } else {
- alert('거래완료에 실패했습니다.');
- }
- })
- .catch((error) => console.log(error));
- };
-
- useEffect(() => {
- let messagebox = document.querySelector('#messagebox');
- messagebox.scrollTop = messagebox.scrollHeight;
- }, [contents, imgBase64]);
-
- const handleChangeFile = (e) => {
- setImgFile(e.target.files);
- };
-
- const previewCancelClick = () => {
- setImgFile(null);
- setImgBase64(null);
- };
-
- useEffect(() => {
- if (!imgFile) return false;
-
- const reader = new FileReader();
- reader.onloadend = () => {
- setImgBase64(reader.result);
- };
- reader.readAsDataURL(imgFile[0]);
- }, [imgFile]);
-
- return (
- <>
-