Skip to content

Commit

Permalink
progress and csrf tocken
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-rt committed Apr 26, 2024
1 parent fa27c9f commit 42611a3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 116 deletions.
2 changes: 1 addition & 1 deletion frontend/review/src/components/Comments/WriteComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { apiPostComment } from '../../utils/api';
export const WriteComment = forwardRef(({ course, semesters, setUserComment }, ref) => {
const [isEditing, setIsEditing] = useState(false);
const [content, setContent] = useState("");
const [semester, setSemester] = useState(semesters[0]);
const [semester, setSemester] = useState(semesters[0] ?? "2020A");

const handleSubmit = () => {
console.log("Comment submitted");
Expand Down
6 changes: 3 additions & 3 deletions frontend/review/src/components/DetailsBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ const CommentsTab = forwardRef(

useEffect(() => {
// TODO: HARDCODED USER ID BC CANT YET FETCH WHICH COMMENT BELONGS TO WHICH USER
apiComment("10").then(res => {
setUserComment(res);
});
// apiComment("10").then(res => {
// setUserComment(res);
// });
}, [course]);

const hasComments = comments.length > 0;
Expand Down
117 changes: 5 additions & 112 deletions frontend/review/src/utils/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import autocompleteWorker from "workerize-loader!../workers/autocomplete.worker"; // eslint-disable-line import/no-webpack-loader-syntax
import getCsrf from "./csrf";

const autocompleteWorkerInstance = autocompleteWorker();
const compressAutocomplete = autocompleteWorkerInstance.compress;
Expand Down Expand Up @@ -123,116 +124,6 @@ export function apiReviewData(type, code, semester) {
);
}

const fakeComments = {
comments: [
{
content:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla mollis commodo ligula sit amet pharetra.",
id: 10,
created_at: new Date(new Date() - 1000000),
modified_at: new Date(new Date() - 1000),
author_name: "Luke Tong",
likes: 69,
course: "CIS-1200",
semester: "2024A",
professorId: [6],
parent_id: null,
path: "10",
replies: 1
},
{
content:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla mollis commodo ligula sit amet pharetra. Nunc accumsan nec mi eget sagittis.",
id: 20,
created_at: new Date(new Date() - 1000000),
modified_at: new Date(new Date() - 1000000),
author_name: "Shiva Mehta",
likes: 0,
course: "CIS-1200",
semester: "2022A",
professorId: [6],
parent_id: null,
path: "20",
replies: 0
},
{
content:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla mollis commodo ligula sit amet pharetra. Nunc accumsan nec mi eget sagittis. Mauris rutrum hendrerit est, a interdum ipsum convallis et. Etiam vel est ac mauris congue sollicitudin ut quis nulla. Mauris rutrum hendrerit est, a interdum ipsum convallis et. Etiam vel est ac mauris congue sollicitudin ut quis nulla.",
id: 30,
created_at: new Date(new Date() - 5000000),
modified_at: new Date(new Date() - 5000000),
author_name: "Eunsoo Shin",
likes: 10,
course: "CIS-1200",
semester: "2022A",
professorId: [6],
parent_id: null,
path: "30",
replies: 0
}
]
}

const fakeSemesters = {
semesters: [
"2024A",
"2022A",
],
}

const fakeReplies = {
replies: [
{
content:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla mollis commodo ligula sit amet pharetra.",
id: 12,
created_at: new Date(),
modified_at: new Date(),
author_name: "Shiva Mehta",
likes: 100,
course: "CIS-1200",
semester: "2024A",
professorIds: [6],
parent_id: 11,
path: "10.11",
replies: 0
},
{
title: "Luke is so cool and awesome",
content:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla mollis commodo ligula sit amet pharetra. Nunc accumsan nec mi eget sagittis.",
id: 11,
created_at: new Date(),
modified_at: new Date(),
author_name: "Penn Courses",
likes: 100,
course: "CIS-1200",
semester: "2024A",
professorId: [6],
parent_id: 10,
path: "10.11",
replies: 1
}
]

}

const fakeUserComment = {
content:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla mollis commodo ligula sit amet pharetra. Nunc accumsan nec mi eget sagittis. Mauris rutrum hendrerit est, a interdum ipsum convallis et. Etiam vel est ac mauris congue sollicitudin ut quis nulla. Mauris rutrum hendrerit est, a interdum ipsum convallis et. Etiam vel est ac mauris congue sollicitudin ut quis nulla.",
id: 40,
created_at: new Date(),
modified_at: new Date(),
author_name: "Penn Labs",
likes: 1,
course: "CIS-1200",
semester: "2024A",
professorId: [6],
parent_id: null,
path: "30",
replies: 0
}

export function apiComments(course, semester, professorId, sortBy) {
return apiFetch(
`${API_DOMAIN}/api/review/${encodeURIComponent(semester)}/course_comments/${encodeURIComponent(course)}`
Expand All @@ -250,13 +141,15 @@ export function apiPostComment(course, semester, content) {
`${API_DOMAIN}/api/review/comment`,
{
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json"
"Content-Type": "application/json",
"X-CSRFToken": getCsrf(),
},
body: JSON.stringify({
text: content,
course_code: course,
instructor: ["default prof"],
instructor: [6],
semester: semester,
})
}
Expand Down
22 changes: 22 additions & 0 deletions frontend/review/src/utils/csrf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @returns {string | boolean} The CSRF token used by the Django REST Framework
*/
const getCsrf = () => {
const result =
document.cookie &&
document.cookie
.split("; ")
.reduce(
(acc, cookie) =>
acc ||
(cookie.substring(0, "csrftoken".length + 1) ===
"csrftoken=" &&
decodeURIComponent(
cookie.substring("csrftoken=".length)
)),
false
);
return result;
};

export default getCsrf;

0 comments on commit 42611a3

Please sign in to comment.