4
4
*/
5
5
import React , { useEffect , useState } from 'react' ;
6
6
import CircularProgress from '@mui/material/CircularProgress' ;
7
- import { EnhancedEncryptionRounded } from '@mui/icons-material' ;
8
7
import { PaginationTable , TColumn } from '../components/PaginationTable' ;
9
- import DeleteUserButton from './DeleteUserButton' ;
10
8
import DeleteQuestionButton from './DeleteQuestionButton' ;
11
- import PromoteUserButton from './PromoteUserButton' ;
12
9
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' ;
16
10
import { IQuestion } from '../util/types/question' ;
17
- import { IResource } from '../util/types/resource' ;
18
11
import EditQuestionButton from './EditQuestionButton' ;
19
- import { deleteQuestion } from './api' ;
12
+ import { deleteResource } from './api' ;
20
13
21
14
interface AdminDashboardRow {
22
15
key : string ;
23
16
question : string ;
24
- // promote : React.ReactElement;
17
+ deleteButton : React . ReactElement ;
25
18
edit : React . ReactElement ;
26
19
}
27
20
28
- // const testq: IQuestion = {
29
- // //required params: text, resultantAnswerIds, isQuestion
30
- // text: '',
31
- // resultantAnswerIds: [],
32
- // isQuestion: true,
33
-
34
- // };
35
-
36
21
/**
37
22
* The standalone table component for holding information about the users in
38
23
* the database and allowing admins to remove users and promote users to admins.
@@ -48,87 +33,54 @@ function QuestionTable() {
48
33
{ id : 'edit' , label : 'Edit' } ,
49
34
] ;
50
35
51
- const [ selectedRow , setSelectedRow ] = useState ( { } ) ;
52
-
53
36
// Used to create the data type to create a row in the table
54
37
function createAdminDashboardRow (
55
38
question : IQuestion , // IUser, //fix this to question type
56
- // promote : React.ReactElement,
39
+ deleteButton : React . ReactElement ,
57
40
edit : React . ReactElement ,
58
41
) : AdminDashboardRow {
59
- // const { _id, qstn } = user;
60
- const { _id, text, resultantAnswers, isQuestion } = question ;
42
+ const { _id, text } = question ;
61
43
return {
62
44
key : _id ,
63
45
question : text ,
64
- // resultantAnswerIds: resultantAnswerIds,
65
- // isQuestion: isQuestion,
66
- // promote,
46
+ deleteButton,
67
47
edit,
68
48
} ;
69
49
}
70
50
71
51
const [ questionList , setQuestionList ] = useState < IQuestion [ ] > ( [ ] ) ;
72
52
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);
77
53
78
54
// Upon getting the list of users for the database, set the state of the userList to contain all users except for logged in user
79
55
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 ] ) ;
89
58
90
59
// 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 ) => {
92
61
setQuestionList (
93
62
questionList . filter (
94
63
( entry : IQuestion ) =>
95
64
entry &&
96
65
entry . text &&
97
66
entry . text !== question . text &&
98
67
// eslint-disable-next-line no-underscore-dangle
99
- entry . _id !== question . _id , //! == question.text,
68
+ entry . _id !== question . _id ,
100
69
) ,
101
70
) ;
102
- deleteQuestion ( question ) ;
71
+ // eslint-disable-next-line no-underscore-dangle
72
+ deleteResource ( question . _id ) ;
103
73
} ;
104
74
105
75
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
+ ) ;
110
82
} ;
111
83
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
-
132
84
// if the questionlist is not yet populated, display a loading spinner
133
85
if ( ! questionList ) {
134
86
return (
@@ -143,36 +95,17 @@ function QuestionTable() {
143
95
rows = { questionList . map ( ( question : IQuestion ) =>
144
96
createAdminDashboardRow (
145
97
question ,
146
- < EditQuestionButton
98
+ < DeleteQuestionButton
147
99
// eslint-disable-next-line no-underscore-dangle
148
- qID = { question . _id }
149
- isQuestion = { question . isQuestion }
150
- text = { question . text }
100
+ id = { question . _id }
151
101
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 ) }
159
103
/> ,
160
-
161
- // <PromoteUserButton
162
- // isQuestion={question.isQuestion}
163
- // email={question.text}
164
- // updateAdmin={updateAdmin}
165
- // />,
104
+ < EditQuestionButton question = { question } /> ,
166
105
) ,
167
106
) }
168
107
columns = { columns }
169
108
/>
170
-
171
- // <TableRow
172
- // onClick={() => setSelectedRow(row)}
173
- // key={row.name}
174
- // sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
175
- // ></TableRow>
176
109
) ;
177
110
}
178
111
0 commit comments