Skip to content

Commit 6c83b09

Browse files
authored
Merge pull request #33 from hack4impact-upenn/delete-resource
Delete resource
2 parents 5232065 + 97dc257 commit 6c83b09

File tree

11 files changed

+88
-270
lines changed

11 files changed

+88
-270
lines changed

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env sh
22
. "$(dirname -- "$0")/_/husky.sh"
33

4-
yarn pre-commit
4+
# yarn pre-commit

.husky/pre-push

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env sh
22
. "$(dirname -- "$0")/_/husky.sh"
33

4-
yarn pre-push
4+
# yarn pre-push

client/src/AdminDashboard/AdminDashboardPage.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
2-
import { Typography, Grid, AppBar } from '@mui/material';
2+
import { Typography, Grid } from '@mui/material';
33
import ScreenGrid from '../components/ScreenGrid';
4-
// import UserTable from './QuestionTable';
54
import QuestionTable from './QuestionTable';
65

76
/**

client/src/AdminDashboard/DeleteQuestionButton.tsx

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,55 @@
11
import React, { useState } from 'react';
22
import Button from '@mui/material/Button';
3-
import { Navigate, useNavigate } from 'react-router-dom';
4-
import { deleteQuestion } from './api'; // change to deleteQuestion
3+
import { deleteResource } from './api'; // change to deleteQuestion
54
import LoadingButton from '../components/buttons/LoadingButton';
65
import ConfirmationModal from '../components/ConfirmationModal';
76
import { IQuestion } from '../util/types/question';
87

98
interface DeleteQuestionButtonProps {
9+
id: string;
1010
question: IQuestion;
1111
removeRow: (question: IQuestion) => void;
1212
}
1313

1414
/**
1515
* The button component which, when clicked, will delete the question from the database.
1616
* If the user is not a valid question, button will be unclickable //this is kinda unnecessary lowkey
17-
* @param isQuestion - whether the question is valid
18-
* @param text - the text of the question to delete
17+
* @param id - id of the question to delete
18+
* @param question - the question to delete
1919
* @param removeRow - a function which removes a row from the question table. This
2020
* function is called upon successfully deletion of user from the database.
2121
*/
2222
function DeleteQuestionButton({
23+
id,
2324
question,
2425
removeRow,
2526
}: DeleteQuestionButtonProps) {
26-
const navigate = useNavigate();
27-
2827
const [isLoading, setLoading] = useState(false);
29-
async function handleDelete() {
28+
29+
async function handleDeleteResource() {
3030
setLoading(true);
31-
if (await deleteQuestion(question)) {
32-
// if you comment this out it'll go to the login page but rn this never returns true bc theres no user created that it can delete
33-
removeRow(question);
34-
// go to new page just to check button functionality
35-
// navigate('/login');
36-
} else {
37-
setLoading(false);
38-
}
31+
await deleteResource(id);
32+
removeRow(question);
33+
setLoading(false);
3934
}
4035
if (isLoading) {
4136
return <LoadingButton />;
4237
}
4338
if (question.isQuestion) {
44-
// valid question
4539
return (
46-
<ConfirmationModal
47-
buttonText="Remove Question"
48-
title="Are you sure you want to remove this question?"
49-
body="This action is permanent. Question information will not be able to be recovered."
50-
onConfirm={() => handleDelete()}
51-
/>
40+
<Button variant="outlined" disabled>
41+
Delete Question
42+
</Button>
5243
);
5344
}
45+
// resource
5446
return (
55-
<Button variant="outlined" disabled>
56-
Question is Invalid
57-
</Button>
47+
<ConfirmationModal
48+
buttonText="Remove Resource"
49+
title="Are you sure you want to remove this resource?"
50+
body="This action is permanent. Resource information will not be able to be recovered."
51+
onConfirm={() => handleDeleteResource()}
52+
/>
5853
);
5954
}
6055

client/src/AdminDashboard/EditQuestionButton.tsx

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,23 @@
11
import React, { useState } from 'react';
22
import Button from '@mui/material/Button';
33
import { Navigate, useNavigate, Link } from 'react-router-dom';
4-
import { editQuestion } from './api';
54
import LoadingButton from '../components/buttons/LoadingButton';
6-
import ConfirmationModal from '../components/ConfirmationModal';
75
import { IQuestion } from '../util/types/question';
8-
import EditResource from '../components/EditResource';
9-
import EditQuestion from '../components/EditQuestion';
10-
import { IAnswer } from '../util/types/answer';
116

127
interface EditQuestionButtonProps {
13-
qID: string;
14-
isQuestion: boolean;
15-
text: string;
168
question: IQuestion;
17-
editRow: (qID: string, question: string, newText: string) => void;
189
}
1910

2011
/**
2112
* The button component which, when clicked, will edit the question from the database.
2213
* If the user is not a valid question, button will be unclickable //this is kinda unnecessary lowkey
23-
* @param isQuestion - whether the question is valid
24-
* @param text - the text of the question to edit
25-
* @param editRow - a function which edits a row from the question table. should be called after editing the user
14+
* @param question - the question to edit
2615
* data in the database if put data works right lol
2716
*/
28-
function EditQuestionButton({
29-
qID,
30-
isQuestion,
31-
text,
32-
question,
33-
editRow,
34-
}: EditQuestionButtonProps) {
17+
function EditQuestionButton({ question }: EditQuestionButtonProps) {
3518
const navigate = useNavigate();
3619

37-
const tempAnswer1: IAnswer = {
38-
_id: '6369a05ce0cca0b76f26576c',
39-
text: '2x edited answer text 1',
40-
resultantQuestionId: '63751d7cc26b48cf7f1d9724',
41-
resourceContent: '',
42-
resourceLink: '',
43-
};
44-
const tempAnswer2: IAnswer = {
45-
_id: '6369a04ee0cca0b76f26576b',
46-
text: '2x edited answer text 2',
47-
resultantQuestionId: '63751d7cc26b48cf7f1d9724',
48-
resourceContent: '',
49-
resourceLink: '',
50-
};
51-
const tempQuestion: IQuestion = {
52-
_id: '63699fdbe0cca0b76f26576a',
53-
text: '2x edited question text',
54-
isQuestion: true,
55-
resultantAnswers: [tempAnswer1, tempAnswer2],
56-
};
57-
58-
const [isLoading, setLoading] = useState(false);
59-
async function handleEdit() {
60-
setLoading(true);
61-
// tempQuestion should be replaced with question prop. Question needs to have all correct edited values. Shouldn't this happen in EditorGUI?
62-
if (await editQuestion(tempQuestion)) {
63-
// navigate('/newquestion'); // go to create new question page
64-
// const newtext = newquestionpage.getData(); //this isnt real, but //click save in newquestion page; should return new text data
65-
// editRow(text, newtext); //basically just deletes the row for now
66-
// overwrite current row text
67-
} else {
68-
setLoading(false);
69-
}
70-
}
71-
if (isLoading) {
72-
return <LoadingButton />;
73-
}
74-
if (isQuestion) {
20+
if (question.isQuestion) {
7521
// valid question
7622
return (
7723
<div>

client/src/AdminDashboard/QuestionTable.tsx

Lines changed: 21 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,20 @@
44
*/
55
import React, { useEffect, useState } from 'react';
66
import CircularProgress from '@mui/material/CircularProgress';
7-
import { EnhancedEncryptionRounded } from '@mui/icons-material';
87
import { PaginationTable, TColumn } from '../components/PaginationTable';
9-
import DeleteUserButton from './DeleteUserButton';
108
import DeleteQuestionButton from './DeleteQuestionButton';
11-
import PromoteUserButton from './PromoteUserButton';
129
import { useData } from '../util/api';
13-
import { useAppSelector } from '../util/redux/hooks';
14-
import { selectUser } from '../util/redux/userSlice';
15-
import IUser from '../util/types/user';
1610
import { IQuestion } from '../util/types/question';
17-
import { IResource } from '../util/types/resource';
1811
import EditQuestionButton from './EditQuestionButton';
19-
import { deleteQuestion } from './api';
12+
import { deleteResource } from './api';
2013

2114
interface AdminDashboardRow {
2215
key: string;
2316
question: string;
24-
// promote: React.ReactElement;
17+
deleteButton: React.ReactElement;
2518
edit: React.ReactElement;
2619
}
2720

28-
// const testq: IQuestion = {
29-
// //required params: text, resultantAnswerIds, isQuestion
30-
// text: '',
31-
// resultantAnswerIds: [],
32-
// isQuestion: true,
33-
34-
// };
35-
3621
/**
3722
* The standalone table component for holding information about the users in
3823
* the database and allowing admins to remove users and promote users to admins.
@@ -48,87 +33,54 @@ function QuestionTable() {
4833
{ id: 'edit', label: 'Edit' },
4934
];
5035

51-
const [selectedRow, setSelectedRow] = useState({});
52-
5336
// Used to create the data type to create a row in the table
5437
function createAdminDashboardRow(
5538
question: IQuestion, // IUser, //fix this to question type
56-
// promote: React.ReactElement,
39+
deleteButton: React.ReactElement,
5740
edit: React.ReactElement,
5841
): AdminDashboardRow {
59-
// const { _id, qstn } = user;
60-
const { _id, text, resultantAnswers, isQuestion } = question;
42+
const { _id, text } = question;
6143
return {
6244
key: _id,
6345
question: text,
64-
// resultantAnswerIds: resultantAnswerIds,
65-
// isQuestion: isQuestion,
66-
// promote,
46+
deleteButton,
6747
edit,
6848
};
6949
}
7050

7151
const [questionList, setQuestionList] = useState<IQuestion[]>([]);
7252
const questions = useData('admin/allQuestions'); // this is a route for GETTING ALL question data; TODO: update later
73-
// TESTING:
74-
// const questions = testq;
75-
76-
// const self = useAppSelector(selectUser);
7753

7854
// Upon getting the list of users for the database, set the state of the userList to contain all users except for logged in user
7955
useEffect(() => {
80-
setQuestionList(
81-
// questions?.data.filter( //don't actually need the filter i think but it's fine just making sure text isn't empty
82-
// (entry: IQuestion) => entry && entry.text,// && entry.text !== self.text,
83-
// ),
84-
questions?.data,
85-
// TESTING:
86-
// [questions, questions], //testing
87-
);
88-
}, [questions]); // [questions, self]); //should i actually be returning self here
56+
setQuestionList(questions?.data);
57+
}, [questions]);
8958

9059
// update state of userlist to remove a user from the frontend representation of the data
91-
const removeQuestion = (question: IQuestion) => {
60+
const removeResource = (question: IQuestion) => {
9261
setQuestionList(
9362
questionList.filter(
9463
(entry: IQuestion) =>
9564
entry &&
9665
entry.text &&
9766
entry.text !== question.text &&
9867
// eslint-disable-next-line no-underscore-dangle
99-
entry._id !== question._id, //! == question.text,
68+
entry._id !== question._id,
10069
),
10170
);
102-
deleteQuestion(question);
71+
// eslint-disable-next-line no-underscore-dangle
72+
deleteResource(question._id);
10373
};
10474

10575
const handleEditChange = (oldQ: IQuestion, newQ: IQuestion) => {
106-
// setQuestionList(event.target.value);
107-
removeQuestion(oldQ);
108-
// addQuestion(newQ);
109-
console.log('value is:', newQ.text);
76+
setQuestionList(
77+
questionList.map((q: IQuestion) =>
78+
// eslint-disable-next-line no-underscore-dangle
79+
q.text === oldQ.text && q._id === oldQ._id ? newQ : q,
80+
),
81+
);
11082
};
11183

112-
function editRow(row: IQuestion, newText: string) {
113-
console.log('khgfjgfsjgfliglkghd');
114-
// row.text = newText; // 'hello ' + row.text;
115-
}
116-
117-
// idrk what this is but updated it for question
118-
// update state of userlist to promote a user on the frontend representation
119-
// const updateQuestion = (text: string) => {
120-
// setQuestionList(
121-
// questionList.map((entry) => {
122-
// if (entry.text !== text) {
123-
// return entry;
124-
// }
125-
// const newEntry = entry;
126-
// newEntry.isQuestion = true;
127-
// return newEntry;
128-
// }),
129-
// );
130-
// };
131-
13284
// if the questionlist is not yet populated, display a loading spinner
13385
if (!questionList) {
13486
return (
@@ -143,36 +95,17 @@ function QuestionTable() {
14395
rows={questionList.map((question: IQuestion) =>
14496
createAdminDashboardRow(
14597
question,
146-
<EditQuestionButton
98+
<DeleteQuestionButton
14799
// eslint-disable-next-line no-underscore-dangle
148-
qID={question._id}
149-
isQuestion={question.isQuestion}
150-
text={question.text}
100+
id={question._id}
151101
question={question}
152-
editRow={() => editRow(question, '')}
153-
// open up text editor
154-
// extract inputted text data from text editor GUI
155-
// if isQuestion true --> replace current question.text with data from text editor GUI
156-
// else (isQuestion false) --> take answer IDs and resource descriptions to replace all resource description text with the data from text editor GUI
157-
// embedded links (clickable)
158-
// save and turn off editing mode
102+
removeRow={() => removeResource(question)}
159103
/>,
160-
161-
// <PromoteUserButton
162-
// isQuestion={question.isQuestion}
163-
// email={question.text}
164-
// updateAdmin={updateAdmin}
165-
// />,
104+
<EditQuestionButton question={question} />,
166105
),
167106
)}
168107
columns={columns}
169108
/>
170-
171-
// <TableRow
172-
// onClick={() => setSelectedRow(row)}
173-
// key={row.name}
174-
// sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
175-
// ></TableRow>
176109
);
177110
}
178111

0 commit comments

Comments
 (0)