File size: 1,934 Bytes
f0a83bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React, { createContext, useContext, useState, useEffect } from 'react';

interface Module {
  id: string;
  name: string;
  description: string;
  concepts: string[];
  difficulty: 'beginner' | 'intermediate' | 'advanced';
}

interface LearningPath {
  id: string;
  name: string;
  description: string;
  modules: Module[];
}

interface LearningContextType {
  paths: LearningPath[];
  currentPath: string | null;
  currentModule: string | null;
  setCurrentPath: (pathId: string) => void;
  setCurrentModule: (moduleId: string) => void;
  loading: boolean;
  error: string | null;
}

const LearningContext = createContext<LearningContextType | undefined>(undefined);

export const LearningProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const [paths, setPaths] = useState<LearningPath[]>([]);
  const [currentPath, setCurrentPath] = useState<string | null>(null);
  const [currentModule, setCurrentModule] = useState<string | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    const fetchPaths = async () => {
      try {
        const response = await fetch('/api/learning-paths');
        const data = await response.json();
        setPaths(data);
        setLoading(false);
      } catch (err) {
        setError('Failed to load learning paths');
        setLoading(false);
      }
    };

    fetchPaths();
  }, []);

  return (
    <LearningContext.Provider 
      value={{
        paths,
        currentPath,
        currentModule,
        setCurrentPath,
        setCurrentModule,
        loading,
        error
      }}
    >
      {children}
    </LearningContext.Provider>
  );
};

export const useLearning = () => {
  const context = useContext(LearningContext);
  if (context === undefined) {
    throw new Error('useLearning must be used within a LearningProvider');
  }
  return context;
};