Skip to content

Commit

Permalink
Merge pull request #455 from cornell-dti/jacqueline/update-subjects
Browse files Browse the repository at this point in the history
Update Subjects to Full Names
  • Loading branch information
jacquelinecai authored Oct 23, 2024
2 parents 0b874d6 + 3eb752d commit 1e11a5e
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 159 deletions.
194 changes: 97 additions & 97 deletions client/src/modules/Admin/Components/Admin.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from 'react'
import { Redirect } from 'react-router-dom'
import { Redirect, useParams } from 'react-router-dom'

Check warning on line 2 in client/src/modules/Admin/Components/Admin.tsx

View workflow job for this annotation

GitHub Actions / build

'useParams' is defined but never used

Check warning on line 2 in client/src/modules/Admin/Components/Admin.tsx

View workflow job for this annotation

GitHub Actions / build

'useParams' is defined but never used

import axios from 'axios'

Expand All @@ -20,13 +20,21 @@ import Loading from '../../Globals/Loading'
export const Admin = () => {
const [pendingReviews, setPendingReviews] = useState<Review[]>([])
const [reportedReviews, setReportedReviews] = useState<Review[]>([])
const [disableInit, setDisableInit] = useState<boolean>(false)
const [disableNewSem, setDisableNewSem] = useState<boolean>(false)
const [doubleClick, setDoubleClick] = useState<boolean>(false)
const [loadingInit, setLoadingInit] = useState<number>(0)
const [loadingSemester, setLoadingSemester] = useState<number>(0)
const [loadingProfs, setLoadingProfs] = useState<number>(0)
const [resettingProfs, setResettingProfs] = useState<number>(0)

const [updating, setUpdating] = useState<boolean>(false);
type updatedStates = 'empty' | 'semester' | 'profsReset' | 'profsUpdate' | 'subjects' | 'database';
const [updated, setUpdated] = useState<updatedStates>('empty');
const successMessages = {
'empty': '',
'semester': "New semester data successfully added",
'profsReset': "Professor data successfully reset to empty",
'profsUpdate': "Professor data successfully updated",
'subjects': "Subject full name data successfully updated",
'database': "Database successfully initialized",
};
const [updatingField, setUpdatingField] = useState<string>("");

const [addSemester, setAddSemester] = useState('')

Check warning on line 38 in client/src/modules/Admin/Components/Admin.tsx

View workflow job for this annotation

GitHub Actions / build

'setAddSemester' is assigned a value but never used

Check warning on line 38 in client/src/modules/Admin/Components/Admin.tsx

View workflow job for this annotation

GitHub Actions / build

'setAddSemester' is assigned a value but never used
const [isAdminModalOpen, setIsAdminModalOpen] = useState<boolean>(false)

Expand Down Expand Up @@ -55,8 +63,8 @@ export const Admin = () => {
* pending (awaiting approval), and reported (hidden and awaiting approval)
*/
useEffect(() => {
async function loadReviews() {
const pending = await axios.post('/api/admin/reviews/get-pending', {
async function loadReviews() {
const pending = await axios.post('/api/admin/reviews/get/pending', {
token: token,
})
if (pending.status === 200) {
Expand Down Expand Up @@ -115,7 +123,7 @@ export const Admin = () => {
review,
pendingReviews
)
setPendingReviews(updatedUnapprovedReviews)
setPendingReviews(updatedUnapprovedReviews)
} else {
const updatedReportedReviews = removeReviewFromList(
review,
Expand All @@ -133,7 +141,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 {
Expand Down Expand Up @@ -164,86 +172,99 @@ export const Admin = () => {
*/
async function addNewSem(semester: string) {
console.log('Adding new semester...')
setDisableNewSem(true)
setDisableInit(true)
setLoadingSemester(1)
setUpdating(true)
setUpdatingField("new semester")
//wz
const response = await axios.post('/api/admin/semester/add', {
semester,
token: token
token: token,
})

if (response.data.result === true) {
console.log('New semester added')
setDisableNewSem(false)
setDisableInit(false)
setLoadingSemester(2)
const result = response.data.result;
if (result === true) {
console.log('New Semester Added')
setUpdating(false)
setUpdated('semester')
} else {
console.log('Unable to add new semester')
console.log('Unable to add new semester!')
}
}

/**
* Call when user selects "Initialize Database" button. Scrapes the Cornell
* Course API to store all classes and subjects in the local database.
* Then, runs code to store id's of cross-listed classes against each class.
* Should only be run ONCE when the app is initialized.
*
* NOTE: requires an initialize flag to ensure the function is only run on
* a button click. Without this, it will run every time this component is created.
*/
// Call when user selects "Initialize Database" button. Scrapes the Cornell
// Course API to store all classes and subjects in the local database.
// Then, runs code to store id's of cross-listed classes against each class.
// Should only be run ONCE when the app is initialzied.
//
// NOTE: requires an initialize flag to ensure the function is only run on
// a button click without this, it will run every time this component is created.
async function addAllCourses() {
console.log('Initializing database')
setDisableInit(true)
setLoadingInit(1)

const response = await axios.post('/api/admin/db/initialize', {
token: token
})

setUpdating(true)
setUpdatingField("all database")
//wz
const response = await axios.post('/api/admin/db/initialize', { token: token });
if (response.status === 200) {
setDisableInit(false)
setLoadingInit(2)
setUpdating(false)
setUpdated('database')
} else {
console.log('Error at dbInit')
}
}

/**
* Call when admin wants to update professors for users to search through
* when clicking the "Update Professors" button
*/
async function updateProfessors() {
console.log('Updating professors')
setDisableInit(true)
setLoadingProfs(1)

const response = await axios.post('api/admin/professors/add', {
token: token
})

if (response.status === 200) {
console.log('Updated professors')
setDisableInit(false)
setLoadingProfs(2)
setUpdating(true)
setUpdatingField("professors")
//wz
const response = await axios.post('/api/admin/professors/add', { token: token });
if (response.status === 200) {
console.log('Updated the professors')
setUpdating(false)
setUpdated('profsUpdate')
} else {
console.log('Error at setProfessors')
}
}

/**
* Call when admin wants to reset all professors in classes when clicking the
* "Reset Professors" button
*/
async function resetProfessors() {
console.log('Setting the professors to an empty array')
setDisableInit(true)
setResettingProfs(1)

const response = await axios.post('/api/admin/professors/reset', {
token: token
})

setUpdating(true)
setUpdatingField("professors to empty arrays")
// wz
const response = await axios.post('/api/admin/professors/reset', { token: token });
if (response.status === 200) {
console.log('Reset all the professors to empty arrays')
setDisableInit(false)
setResettingProfs(2)
setUpdating(false)
setUpdated('profsReset')
} else {
console.log('Error at resetProfessors')
}
}

/**
* Call when admin wants to update the list of subjects users can search through
* when clicking the "Update Subjects" button
*/
async function updateSubjects() {
setUpdating(true);
setUpdatingField("subjects");
const response = await axios.post('/api/admin/subjects/update', { token: token });
if (response.status === 200) {
console.log('Updated all subject names');
setUpdating(false);
setUpdated('subjects');
} else {
console.log('Error at updateSubjects');
}
}

/**
* 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.
Expand All @@ -267,7 +288,7 @@ export const Admin = () => {
return (
<div className="">
<button
disabled={disableInit}
disabled={updating}
type="button"
className={styles.adminButtons}
onClick={() => addAllCourses()}
Expand Down Expand Up @@ -309,29 +330,37 @@ export const Admin = () => {
Manage Administrators
</button>
<button
disabled={disableNewSem}
disabled={updating}
type="button"
className={styles.adminButtons}
onClick={() => addNewSem(addSemester)}
>
Add New Semester
</button>
<button
disabled={disableInit}
disabled={updating}
type="button"
className={styles.adminButtons}
onClick={() => updateProfessors()}
>
Update Professors
</button>
<button
disabled={disableInit}
disabled={updating}
type="button"
className={styles.adminButtons}
onClick={() => resetProfessors()}
>
Reset Professors
</button>
<button
disabled={updating}
type="button"
className={styles.adminButtons}
onClick={() => updateSubjects()}
>
Update Subjects
</button>
{renderInitButton(doubleClick)}
</div>
</div>
Expand All @@ -342,43 +371,14 @@ export const Admin = () => {
token={token}
/>

<div hidden={!(loadingSemester === 1)} className="">
<p>
Adding New Semester Data. This process can take up to 15 minutes.
</p>
</div>

<div hidden={!(loadingSemester === 2)} className="">
<p>New Semester Data import is complete!</p>
<div>
{successMessages[updated]}
</div>

<div hidden={!(resettingProfs === 1)} className="">
<p>Clearing all associated professors from Classes.</p>
<div hidden={!updating} className="">
<p>Updating {updatingField} in the Course database.</p>
<p>This process can take up to 15 minutes.</p>
</div>

<div hidden={!(resettingProfs === 2)} className="">
<p>All professor arrays in Classes reset to empty!</p>
</div>

<div hidden={!(loadingProfs === 1)} className="">
<p>Updating professor data to Classes.</p>
<p>This process can take up to 15 minutes.</p>
</div>

<div hidden={!(loadingProfs === 2)} className="">
<p>Professor data import to Classes is complete!</p>
</div>

<div hidden={!(loadingInit === 1)} className="">
<p>
Database Initializing. This process can take up to 15 minutes.
</p>
</div>

<div hidden={!(loadingInit === 2)} className="">
<p>Database initialization is complete!</p>
</div>
</div>

<div className="StagedReviews">
Expand Down
Loading

0 comments on commit 1e11a5e

Please sign in to comment.