From 4d950b1aca47c44443b4cd8a171298ecddd57614 Mon Sep 17 00:00:00 2001 From: Helen Lei Date: Sat, 26 Oct 2024 17:36:32 -0400 Subject: [PATCH] create button to call AI functions from admin page and clean up some endpoint/function names --- client/src/modules/Admin/Components/Admin.tsx | 51 +++++++++++++++++-- server/src/ai/ai.functions.ts | 16 +++--- server/src/ai/ai.router.ts | 14 ++--- 3 files changed, 64 insertions(+), 17 deletions(-) diff --git a/client/src/modules/Admin/Components/Admin.tsx b/client/src/modules/Admin/Components/Admin.tsx index ff76f116..eb8cf868 100644 --- a/client/src/modules/Admin/Components/Admin.tsx +++ b/client/src/modules/Admin/Components/Admin.tsx @@ -29,6 +29,8 @@ export const Admin = () => { const [resettingProfs, setResettingProfs] = useState(0) const [addSemester, setAddSemester] = useState('') const [isAdminModalOpen, setIsAdminModalOpen] = useState(false) + const [isUpdatingAI, setIsUpdatingAI] = useState(false); + const [updateStatusAI, setUpdateStatusAI] = useState(0); const { isLoggedIn, token, isAuthenticating } = useAuthMandatoryLogin('admin') const [loading, setLoading] = useState(true) @@ -53,7 +55,7 @@ export const Admin = () => { // splits the reviews into three categories: approved (visible on the website), // pending (awaiting approval), and reported (hidden and awaiting approval) useEffect(() => { - async function loadReviews() { + async function loadReviews() { const pending = await axios.post('/api/admin/reviews/get/pending', { token: token, }) @@ -107,7 +109,7 @@ export const Admin = () => { review, pendingReviews ) - setPendingReviews(updatedUnapprovedReviews) + setPendingReviews(updatedUnapprovedReviews) } else { const updatedReportedReviews = removeReviewFromList( review, @@ -123,7 +125,7 @@ export const Admin = () => { // Call when admin would like to mass-approve all of the currently pending reviews. async function approveAllReviews(reviews: Review[]) { - const response = await axios.post('/api/admin/reviews/approve/all', {token: token}) + const response = await axios.post('/api/admin/reviews/approve/all', { token: token }) if (response.status === 200) { setPendingReviews([]) } else { @@ -235,6 +237,29 @@ export const Admin = () => { }) } + // Call when user selects "Sumarize Reviews" button. Calls endpoint to generate + // summaries and tags using AI for all courses with a freshness above a certain + // threshold, then updates those courses to include these new summaries and tags. + function summarizeReviews() { + console.log('Updating all courses with AI'); + if (isUpdatingAI) { + return; + } + setIsUpdatingAI(true); + setUpdateStatusAI(1); + + axios.post('/api/ai/update-all-courses') + .then((response) => { + setUpdateStatusAI(2); + }) + .catch((error) => { + setUpdateStatusAI(-1); + }).finally(() => { + setIsUpdatingAI(false); + }); + } + + // handle the first click to the "Initialize Database" button. Show an alert // and update state to remember the next click will be a double click. function firstClickHandler() { @@ -319,6 +344,14 @@ export const Admin = () => { > Reset Professors + {renderInitButton(doubleClick)} @@ -329,6 +362,18 @@ export const Admin = () => { token={token} /> + + + + + +