File size: 1,538 Bytes
75514b5
58973c7
 
 
291d559
58973c7
 
 
 
75514b5
58973c7
 
 
75514b5
50f8987
58973c7
 
 
 
 
 
2d3f9e6
 
 
 
 
58973c7
291d559
58973c7
 
 
75514b5
 
58973c7
 
 
 
 
 
 
16339ba
58973c7
 
75514b5
58973c7
75514b5
 
 
 
 
 
 
58973c7
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { TextField, Button, Box, CircularProgress } from '@mui/material';
import { useState } from 'react';

interface QuizGeneratorProps {
  onProblemsGenerated: (problems: string[], query: string) => void;
}

function QuizGenerator({ onProblemsGenerated }: QuizGeneratorProps) {
  const [query, setQuery] = useState('');
  const [isLoading, setIsLoading] = useState(false);

  const handleGenerate = async () => {
    try {
      setIsLoading(true);
      const response = await fetch('/api/problems/', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ user_query: query }),
      });

      if (!response.ok) {
        throw new Error('Failed to generate problems');
      }

      const data = await response.json();
      onProblemsGenerated(data.Problems, query);
      setQuery('');
    } catch (error) {
      console.error('Error:', error);
    } finally {
      setIsLoading(false);
    }
  };

  return (
    <Box sx={{ mb: 4, display: 'flex', gap: 2 }}>
      <TextField
        fullWidth
        label="Quiz focus?"
        value={query}
        onChange={(e) => setQuery(e.target.value)}
        disabled={isLoading}
      />
      <Button 
        variant="contained" 
        onClick={handleGenerate}
        disabled={isLoading}
        startIcon={isLoading ? <CircularProgress size={20} color="inherit" /> : null}
      >
        {isLoading ? 'Generating...' : 'Generate'}
      </Button>
    </Box>
  );
}

export default QuizGenerator;