diff --git a/src/components/StandardNav.jsx b/src/components/StandardNav.jsx index 4ee520a0..b2acfe53 100644 --- a/src/components/StandardNav.jsx +++ b/src/components/StandardNav.jsx @@ -92,7 +92,6 @@ export function StandardNav() { try { const endPoint = process.env.NEXT_PUBLIC_API_URL + '/users/' + username + '/pfp'; const result = await request(endPoint, "GET", null); - console.log(result) if (result) { setPfp(result) } else { @@ -112,6 +111,7 @@ export function StandardNav() { const result = await request(endPoint, 'GET', null); if (!result || !result.length) return; + console.log("Here is the result"); console.log(result); setNotificationData( diff --git a/src/components/dashboard/DashboardHeader.jsx b/src/components/dashboard/DashboardHeader.jsx index f820ea90..2fbe597a 100644 --- a/src/components/dashboard/DashboardHeader.jsx +++ b/src/components/dashboard/DashboardHeader.jsx @@ -84,7 +84,7 @@ export function DashboardHeader() {
- + {(pfp && ( { - document.getElementById('bio').value = data.bio; + if(document.getElementById('bio')) { + document.getElementById('bio').value = data.bio; + } }) .catch((err) => { console.log(err); diff --git a/src/components/groups/assignments/create-challenge.jsx b/src/components/groups/assignments/create-challenge.jsx index e7851986..5879ad6b 100644 --- a/src/components/groups/assignments/create-challenge.jsx +++ b/src/components/groups/assignments/create-challenge.jsx @@ -7,6 +7,7 @@ import { MarkdownViewer } from '@/components/MarkdownViewer'; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import fileApi from '@/utils/file-api'; +import { getAuth } from 'firebase/auth'; import request from '@/utils/request'; const styles = { @@ -18,6 +19,8 @@ const styles = { h6: { fontSize: '1.2rem' }, }; +const auth = getAuth(); + export default function Createchall(props) { const pages = [ { diff --git a/src/components/groups/assignments/updateChallengeInfo.jsx b/src/components/groups/assignments/updateChallengeInfo.jsx index 1fc4c908..5b4e96d5 100644 --- a/src/components/groups/assignments/updateChallengeInfo.jsx +++ b/src/components/groups/assignments/updateChallengeInfo.jsx @@ -9,8 +9,8 @@ import request from '@/utils/request'; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; +import fileApi, { deleteFiles, getFileName, getFile } from '@/utils/file-api'; import { getAuth } from 'firebase/auth'; -import fileApi from '@/utils/file-api'; const auth = getAuth(); @@ -30,6 +30,8 @@ const Editor = (props) => { const [penaltyErr, setPenaltyErr] = useState(''); const [username, setUsername] = useState('anonymous'); + const [existingFiles, setExistingFiles] = useState([]); + const [existingConfig, setExistingConfig] = useState(''); const [newConfig, setNewConfig] = useState(''); const [selectedFile, setSelectedFile] = useState(null); @@ -50,28 +52,47 @@ const Editor = (props) => { }; const sendToFileApi = async () => { - await uploadChallenge([]); - return; - const isValid = await validateNewChallege(); - if (isValid) { - setIsCreating(true); - if (!selectedFile) { - await uploadChallenge(''); + if(!validateNewChallege()) { + return; + } + + const token = await auth.currentUser.accessToken; + setIsCreating(true); + let fileIds = []; + let idsToDelete = []; + + for(let i = 0; i < existingFiles.length; i++) { + if(existingFiles[i].using) { + fileIds.push(existingFiles[i].fileId); + } else { + idsToDelete.push(existingFiles[i].fileId); + } + } + + if(idsToDelete.length > 0) { + const res = await deleteFiles(idsToDelete, token); + if(!res) { + toast.error('Something went wrong with the file upload'); return; + } + } + + if (selectedFile) { + const fileId = await fileApi(token, selectedFile); + if (fileId !== null) { + fileIds.push(fileId); } else { - const token = await auth.currentUser.accessToken; - const fileId = await fileApi(token, selectedFile); - if(fileId !== null) { - await uploadChallenge(fileId); - } else { - toast.error('Something went wrong with the file upload'); - } + toast.error('Something went wrong with the file upload'); + return; } - } else { - console.warn('Either the file, toke, or challenge is invalid'); } + await uploadChallenge(fileIds); }; + const downloadFile = async (fileId, filename) => { + await getFile(fileId, filename); + } + const uploadChallenge = async (fileIds) => { const exConfig = existingConfig.replace('\n', ' && '); const nConfig = newConfig.replace('\n', ' && '); @@ -97,7 +118,6 @@ const Editor = (props) => { const url = `${process.env.NEXT_PUBLIC_API_URL}/challenges/update-assignment-challenge`; const data = await request(url, 'POST', challengeInfo); - //console.log(data); if (data && data.success) { window.location.href = ``; @@ -108,13 +128,12 @@ const Editor = (props) => { async function loadSolution() { const url = `${process.env.NEXT_PUBLIC_API_URL}/challenges/solution/${props.challenge.id}/${props.classCode}`; const data = await request(url, 'GET', null); - console.log(data); if (data && data.success) { setSolution(data.body.keyword); } } - function loadProps() { + async function loadProps() { setClassCode(props.classCode); setHints(props.challenge.hints); setContentPreview(props.challenge.content); @@ -133,13 +152,20 @@ const Editor = (props) => { hintMessage.push(h[i].message); hintPoints.push(h[i].penalty); } + + let tmp = []; + for (let i = 0; i < props.challenge.fileIds.length; i++) { + const name = await getFileName(props.challenge.fileIds[i]); + tmp.push({ name: name || "File " + i, + using: true, fileId: props.challenge.fileIds[i] }); + } + setExistingFiles(tmp); + setPenalty(hintPoints); setHints(hintMessage); loadSolution(); } - console.log(hints); - useEffect(() => { setUsername(localStorage.getItem('username')); loadProps(); @@ -416,10 +442,77 @@ const Editor = (props) => {

-
-

Import files from your computer

-
-

Import files from your computer

-
-
- Return Home - +

diff --git a/src/pages/assignments/student/[code].jsx b/src/pages/assignments/student/[code].jsx index 84b2b82d..f521192c 100644 --- a/src/pages/assignments/student/[code].jsx +++ b/src/pages/assignments/student/[code].jsx @@ -51,6 +51,7 @@ export default function Slug() { const [open, setOpen] = useState(true); const [terminalPopup, setTerminalPopup] = useState(false); const [loadingMessage, setLoadingMessage] = useState(''); + const [isStudent, setIsStudent] = useState(true); const [submissionId, setSubmissionId] = useState(null); @@ -125,6 +126,7 @@ export default function Slug() { setChallenge(data.body.challenge); } else { console.log('You are not apart of this class'); + router.push('/groups'); } } }; @@ -132,7 +134,9 @@ export default function Slug() { const authenticate = async (assignment) => { const url = `${baseUrl}/classroom/inClass/${assignment.classroom.id}`; const data = await request(url, "GET", null); - return data && data.body.isStudent; + if(!data) return false; + setIsStudent(data.body.isStudent); + return (data.body.isStudent || data.body.isTeacher); }; const createTerminal = async (skipToCheckStatus) => { @@ -261,6 +265,11 @@ export default function Slug() { return; } + if(!isStudent) { + toast.error('Teachers cannot submit assignments.'); + return; + } + handleDataAsk(); setLoading(true); @@ -393,7 +402,7 @@ export default function Slug() { Return Home { - assignment && assignment.isOpen && ( + (assignment && assignment.isOpen) && (

@@ -497,6 +434,14 @@
+ { + loading && ( +
+ +
+ ) + } +