@@ -7,6 +7,7 @@ interface UploadFileProps {
7
7
}
8
8
9
9
export interface JSONQuestion {
10
+ id : string ;
10
11
title : string ;
11
12
description : string ;
12
13
categories : string ;
@@ -15,38 +16,36 @@ export interface JSONQuestion {
15
16
}
16
17
17
18
const UploadFile : React . FC < UploadFileProps > = ( { setQuestions } ) => {
18
- const handleUpload = ( event : React . ChangeEvent < HTMLInputElement > ) => {
19
+ const handleUpload = async ( event : React . ChangeEvent < HTMLInputElement > ) => {
19
20
const file = event . target . files ?. [ 0 ] ;
20
21
if ( file ) {
21
22
const reader = new FileReader ( ) ;
22
- reader . onload = ( e ) => {
23
+ reader . onload = async ( e ) => {
23
24
const content = e . target ?. result ;
24
- console . log ( "content is " , content ) ;
25
+ console . log ( "Content is: " , content ) ;
25
26
if ( typeof content === "string" ) {
26
27
try {
27
- const jsonQuestions : JSONQuestion [ ] = JSON . parse ( content ) [ "questions" ] ;
28
- console . log ( "json questions are " , jsonQuestions ) ;
29
- for ( let i = 0 ; i < jsonQuestions . length ; i ++ ) {
28
+ const jsonQuestions : JSONQuestion [ ] = JSON . parse ( content ) ;
29
+ console . log ( "JSON questions are: " , jsonQuestions ) ;
30
+ for ( const question of jsonQuestions ) {
31
+ const questionData = {
32
+ title : question . title ,
33
+ description : question . description ,
34
+ link : question . link ,
35
+ categories : question . categories ,
36
+ complexity : question . complexity ,
37
+ } ;
30
38
try {
31
- const questionData = {
32
- "title" : jsonQuestions [ i ] [ "title" ] ,
33
- "description" : jsonQuestions [ i ] [ "description" ] ,
34
- "link" : jsonQuestions [ i ] [ "link" ] ,
35
- "category" : jsonQuestions [ i ] [ "categories" ] ,
36
- "difficulty" : jsonQuestions [ i ] [ "categories" ]
37
- }
38
- // const response = await axios.post('http://localhost:3000/questions', questionData);
39
- // console.log('Question created successfully:', response.data);
39
+ const response = await axios . post ( 'http://localhost:3000/questions' , questionData ) ;
40
+ console . log ( 'Question created successfully:' + response . data ) ;
40
41
} catch ( error ) {
41
- console . error ( 'Error creating question:' , error ) ;
42
+ console . log ( 'Error creating question: ' + error ) ;
42
43
}
43
-
44
44
}
45
-
46
- // setQuestions((prevQuestions) => [
47
- // ...prevQuestions,
48
- // ...jsonQuestions,
49
- // ]);
45
+ setQuestions ( ( prevQuestions ) => [
46
+ ...prevQuestions ,
47
+ ...jsonQuestions ,
48
+ ] ) ;
50
49
} catch ( error ) {
51
50
alert ( "Error parsing JSON: " + error ) ;
52
51
}
0 commit comments