diff --git a/frontend/src/Rating.jsx b/frontend/src/Rating.jsx index 36b42c3..7f9576e 100644 --- a/frontend/src/Rating.jsx +++ b/frontend/src/Rating.jsx @@ -2,8 +2,10 @@ import { useState, useEffect } from 'react'; import Axios from 'axios'; const Rating = () => { - const [TeamName, setTeamname] = useState(''); - const [UserName, setUsername] = useState(''); + const [teams, setTeams] = useState([]); + const [users, setUsers] = useState([]); + const [selectedTeam, setSelectedTeam] = useState(''); + const [selectedUser, setSelectedUser] = useState(''); const [RatingValue, setRatingValue] = useState(''); const [Comment, setComment] = useState(''); const [errorMessage, setErrorMessage] = useState(''); @@ -16,8 +18,35 @@ const Rating = () => { } else { console.warn('UserID not found in localStorage'); } + + const fetchTeams = async () => { + try { + const response = await Axios.get('http://localhost:3000/team'); + setTeams(response.data); + } catch (error) { + console.error('Error fetching teams:', error); + } + }; + + fetchTeams(); }, []); + useEffect(() => { + const fetchUsers = async () => { + if (selectedTeam) { + try { + const response = await Axios.get(`http://localhost:3000/team/users/${selectedTeam}`); + setUsers(response.data); + } catch (error) { + console.error('Error fetching users:', error); + } + } + console.log(selectedTeam) + }; + + fetchUsers(); + }, [selectedTeam]); + const addrating = async () => { try { if (!UserID) { @@ -30,22 +59,40 @@ const Rating = () => { return; } - // ดึง UserID จาก Username ที่กรอก - const userResponse = await Axios.get(`http://localhost:3000/user/UserID`, { - params: { username: UserName }, + // Fetch TeamID from TeamName + const teamResponse = await Axios.get(`http://localhost:3000/team/getTeam`, { + params: { TeamID: selectedTeam }, }); - const ratedUserID = userResponse.data.userID; + const TeamID = teamResponse.data.TeamID; + + if (!TeamID) { + setErrorMessage('ไม่พบ TeamID ของทีมนี้'); + return; + } + + // // Store TeamID in local storage + // localStorage.setItem('TeamID', TeamID); + + // Fetch UserID from Username + const userResponse = await Axios.get(`http://localhost:3000/user/getUserID/${selectedUser}`); + + console.log(userResponse.data); + + const ratedUserID = userResponse.data.UserID; + if (!ratedUserID) { setErrorMessage('ไม่พบ UserID ของ Username นี้'); return; } - // ตรวจสอบว่า UserID ของ Username ที่กรอกอยู่ในทีมมั้ย - const checkTeam = await Axios.post(`http://localhost:3000/team`, { - teamname: TeamName, - userid: ratedUserID, // ตรวจสอบ UserID นี้ในทีม + // Check if the user is part of the team + const checkTeam = await Axios.get(`http://localhost:3000/team/checkteam`, { + params: { + TeamID: TeamID, + UserID: ratedUserID, + }, }); if (!checkTeam.data.valid) { @@ -53,50 +100,37 @@ const Rating = () => { return; } - // เพิ่ม ratedUserID ลงในตาราง userteam - await Axios.post( - `http://localhost:3000/userteam`, - { - teamname: TeamName, - ratedUserID, // UserID ของผู้ที่ได้รับคะแนน - rateByID: UserID, // UserID ของผู้ให้คะแนน - }, - { - headers: { - Authorization: `Bearer ${localStorage.getItem('token')}`, + console.log(UserID) + console.log(ratedUserID) + console.log(RatingValue) + console.log(Comment) + + // Add rating + // Add rating + await Axios.post(`http://localhost:3000/rating/rateuser`, { + ratedByID: UserID, + ratedUserID: ratedUserID, + ratingValue: RatingValue, + comment: Comment, }, - } - ); - - // add คะแนนลง userrating - await Axios.post( - `http://localhost:3000/userrating`, - { - TeamName, - UserName, - RatingValue: parseInt(RatingValue), - Comment, - ratedUserID, - rateByID: UserID, - }, - { - headers: { - Authorization: `Bearer ${localStorage.getItem('token')}`, - }, - } - ); - - // รีเซ็ตฟอร์ม - setTeamname(''); - setUsername(''); - setRatingValue(''); - setComment(''); - setErrorMessage(''); - alert('บันทึกคะแนนสำเร็จ'); - } catch (error) { - console.error('Error:', error); - setErrorMessage('เกิดข้อผิดพลาดในการบันทึกข้อมูล'); - } + { + headers: { + Authorization: `Bearer ${localStorage.getItem('token')}`, + }, + } + ); + + // Reset form + setSelectedTeam(''); + setSelectedUser(''); + setRatingValue(''); + setComment(''); + setErrorMessage(''); + alert('บันทึกคะแนนสำเร็จ'); + } catch (error) { + console.error('Error:', error); + setErrorMessage('เกิดข้อผิดพลาดในการบันทึกข้อมูล'); + } }; return ( @@ -108,20 +142,30 @@ const Rating = () => {
Select a chat to start messaging
++ Select a chat to start messaging +
+