From c2cf0fb9816c7354a5f53fcb223d525491d96386 Mon Sep 17 00:00:00 2001
From: David Sadler <djs21@ic.ac.uk>
Date: Wed, 7 Jun 2023 13:50:03 +0100
Subject: [PATCH] Added updating of star ratings on student evidence

---
 src/app/api/post/route.js           |  3 +++
 src/app/api/rating/route.js         | 20 ++++++++++++++++++++
 src/app/recruiterInternship/page.js | 26 ++++++++++++++++++++++----
 3 files changed, 45 insertions(+), 4 deletions(-)
 create mode 100644 src/app/api/rating/route.js

diff --git a/src/app/api/post/route.js b/src/app/api/post/route.js
index ce171ef..efae2e2 100644
--- a/src/app/api/post/route.js
+++ b/src/app/api/post/route.js
@@ -4,11 +4,13 @@ import { NextResponse } from 'next/server';
 export async function GET() {
   const post = await prisma.post.findFirst({
     select: {
+      id: true,
       name: true,
       applications: {
         select: {
           student: {
             select: {
+              id: true,
               name: true
             }
           },
@@ -18,6 +20,7 @@ export async function GET() {
               rating: true,
               requirement: {
                 select: {
+                  id: true,
                   requirementText: true
                 }
               }
diff --git a/src/app/api/rating/route.js b/src/app/api/rating/route.js
new file mode 100644
index 0000000..3e7e3ba
--- /dev/null
+++ b/src/app/api/rating/route.js
@@ -0,0 +1,20 @@
+import { prisma } from '../../db/client'
+import { NextResponse } from 'next/server';
+
+export async function PUT(request) {
+  const body = await request.json()
+  console.log(body)
+  await prisma.evidence.update({
+    where: {
+      postID_requirementID_studentID: {
+        postID: body.postID,
+        requirementID: body.requirementID,
+        studentID: body.studentID
+      }
+    },
+    data: {
+      rating: body.rating
+    }
+  })
+  return NextResponse.json({})
+}
diff --git a/src/app/recruiterInternship/page.js b/src/app/recruiterInternship/page.js
index 3414957..dcd3220 100644
--- a/src/app/recruiterInternship/page.js
+++ b/src/app/recruiterInternship/page.js
@@ -101,7 +101,12 @@ class SkillList extends Component {
                 <Accordion.Body>
                   <Card><Card.Body>{skill.evidenceText}</Card.Body></Card>
                   <Card className="ratingCard"><Card.Body style={{ alignSelf: "flex-end" }}>
-                    <StarRating initialRating={skill.rating}/>
+                    <StarRating 
+                      initialRating={skill.rating}
+                      postID={this.props.post.id}
+                      studentID={this.props.post.applications[this.props.selectedApplicant].student.id}
+                      requirementID={skill.requirement.id}
+                    />
                   </Card.Body></Card>
                 </Accordion.Body>
               </Accordion.Item>
@@ -113,9 +118,23 @@ class SkillList extends Component {
   }
 }
 
-const StarRating = ({ initialRating }) => {
+const StarRating = ({ initialRating, studentID, postID, requirementID }) => {
   const [rating, setRating] = useState(initialRating);
   const [hover, setHover] = useState(0);
+
+  const selectRating = (n) => {
+    setRating(n);
+    fetch('/api/rating', {
+      method: 'PUT',
+      body: JSON.stringify({
+        studentID: studentID,
+        postID: postID,
+        requirementID: requirementID,
+        rating: n
+      })
+    });
+  }
+
   return (
     <div className="star-rating">
       {[...Array(5)].map((_, index) => {
@@ -124,10 +143,9 @@ const StarRating = ({ initialRating }) => {
             type="button"
             key={index + 1}
             className={index + 1 <= (hover || rating) ? starStyle.on : starStyle.off}
-            onClick={() => setRating(index + 1)}
+            onClick={() => selectRating(index + 1)}
             onMouseEnter={() => setHover(index + 1)}
             onMouseLeave={() => setHover(rating)}
-            onDoubleClick={() => {setRating(0); setHover(0);}}
           >
             <span className="star">&#9733;</span>
           </button>
-- 
GitLab