diff --git a/components/Evaluator/Scoring.js b/components/Evaluator/Scoring.js index 9aa54229..3c282aeb 100644 --- a/components/Evaluator/Scoring.js +++ b/components/Evaluator/Scoring.js @@ -1,4 +1,3 @@ -import moment from 'moment' import { useContext, useEffect, useState } from 'react' import styled from 'styled-components' import { APPLICATION_STATUS, COLOR, MAX_SCORE, SCORING, TAGS } from '../../constants' @@ -36,11 +35,6 @@ const StyledButton = styled(Button)` margin-top: 12px; ` -const SmallText = styled.div` - font-size: 0.8em; - color: ${COLOR.GREY_500}; -` - // const Label = styled.p` // color: ${ASSESSMENT_COLOR.LIGHT_GRAY}; // `; @@ -92,15 +86,25 @@ export default function Scoring({ shouldDisplay, applicant }) { break } const newScores = { ...scores } - newScores[field] = score + newScores[field] = { + ...newScores[field], + score, + } newScores.BonusScore = qualifyingBonus() setScores(newScores) setTotalScore(calculateTotalScore(newScores)) } const handleSave = async () => { - await updateApplicantScore(applicant._id, scores, comment, user.email) + const updatedScores = await updateApplicantScore( + applicant._id, + scores, + applicant?.score?.scores, + comment, + user.email + ) await updateApplicantStatus(applicant._id, APPLICATION_STATUS.scored.text) + setScores(updatedScores) } return ( @@ -164,12 +168,6 @@ export default function Scoring({ shouldDisplay, applicant }) { Total Score: {totalScore} / {MAX_SCORE} - {applicant && ( - - Last updated by: {applicant?.score?.lastUpdatedBy} at{' '} - {moment(applicant?.score?.lastUpdated.toDate()).format('MMM Do, YYYY h:mm:ss A')} - - )} Save diff --git a/components/Evaluator/scoreInput.js b/components/Evaluator/scoreInput.js index 7f8ed02b..d6c23084 100644 --- a/components/Evaluator/scoreInput.js +++ b/components/Evaluator/scoreInput.js @@ -1,7 +1,8 @@ // these are the blue buttons for the applicantScore sidebar import React from 'react' import styled from 'styled-components' -import { ASSESSMENT_COLOR } from '../../constants' +import moment from 'moment' +import { ASSESSMENT_COLOR, COLOR } from '../../constants' import Number from './numberIcon' const Container = styled.div` @@ -10,7 +11,7 @@ const Container = styled.div` const ScoreContainer = styled.div` display: flex; - padding-top: 8px; + padding: 8px 0; gap: 0.5rem; ` @@ -18,6 +19,11 @@ const Label = styled.label` color: ${ASSESSMENT_COLOR.LIGHT_GRAY}; ` +const SmallText = styled.div` + font-size: 0.8em; + color: ${COLOR.GREY_500}; +` + export default function ScoreInput({ label, score, handleClick, maxScore, hasMinusOne }) { const arr = hasMinusOne ? [-1, ...Array(maxScore.value + 1).keys()] : [...Array(maxScore.value + 1).keys()] @@ -33,12 +39,18 @@ export default function ScoreInput({ label, score, handleClick, maxScore, hasMin {arr.map(num => { const isActive = maxScore.weight === 0 - ? score === num // directly compare score when weight is 0 - : score / maxScore.weight === num // use weighted logic otherwise + ? score?.score === num // directly compare score when weight is 0 + : score?.score / maxScore.weight === num // use weighted logic otherwise return })} + {score?.lastUpdated && score?.lastUpdatedBy && ( + + Last updated by: {score?.lastUpdatedBy} at{' '} + {moment(score?.lastUpdated.toDate()).format('MMM Do, YYYY h:mm:ss A')} + + )} ) } diff --git a/utility/firebase.js b/utility/firebase.js index fb9c3e76..d6eb1e66 100644 --- a/utility/firebase.js +++ b/utility/firebase.js @@ -20,6 +20,9 @@ if (!firebase.apps.length) { messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID, } firebase.initializeApp(config) + firebase.firestore().settings({ + ignoreUndefinedProperties: true, + }) } export const db = firebase.firestore() @@ -821,21 +824,33 @@ export const getAllResumes = async () => { download(finishedZip, 'Resumes', 'application/zip') } -export const updateApplicantScore = async (applicantID, scores, comment, adminEmail) => { - const totalScore = scores ? calculateTotalScore(scores) : null +export const updateApplicantScore = async (applicantID, newScores, oldScores, comment, adminEmail) => { + const totalScore = newScores ? calculateTotalScore(newScores) : null + const scoresWithUpdatedTimes = Object.entries(newScores).reduce((prev, [question, scoreObj]) => { + const scoreChanged = oldScores?.[question]?.score !== scoreObj.score + return { + ...prev, + [question]: { + ...scoreObj, + lastUpdated: scoreChanged ? getTimestamp() : oldScores?.[question]?.lastUpdated, + lastUpdatedBy: scoreChanged ? adminEmail : oldScores?.[question]?.lastUpdatedBy, + }, + } + }, {}) + db.collection('Hackathons') .doc(HackerEvaluationHackathon) .collection('Applicants') .doc(applicantID) .update({ score: { - scores, + scores: scoresWithUpdatedTimes, totalScore, comment, - lastUpdated: firebase.firestore.Timestamp.now(), - lastUpdatedBy: adminEmail, }, }) + + return scoresWithUpdatedTimes } export const updateApplicantStatus = async (userId, applicationStatus, hackathon) => { diff --git a/utility/utilities.js b/utility/utilities.js index b30b5320..6be32a73 100644 --- a/utility/utilities.js +++ b/utility/utilities.js @@ -40,7 +40,9 @@ export const calculateTotalScore = hackerScore => { return scoringItem && scoringItem.weight !== 0 }) - const totalScore = Object.values(validScores).reduce(reducer, 0) + const totalScore = Object.values(validScores) + .map(score => score?.score ?? 0) + .reduce(reducer, 0) return Math.min(maxScore, totalScore) }