diff --git a/src/pages/Courses.tsx b/src/pages/Courses.tsx index 4b23d9b..60ba15b 100644 --- a/src/pages/Courses.tsx +++ b/src/pages/Courses.tsx @@ -19,6 +19,16 @@ export interface Course { course_exercises: CourseExercises; } +interface ResponseData { + courses: User[]; + course_exercises: CourseExercises; +} + +interface User { + id: number; + name: string; + Courses?: Course[]; +} const ProgressLine = () => { @@ -57,7 +67,7 @@ export const Courses = () => { const [error, setError] = useState(''); const token = localStorage.getItem('authToken'); - const [course_exercises, setExercises] = useState([]); + const [, setExercises] = useState([]); useEffect(() => { console.log(token) @@ -71,7 +81,7 @@ export const Courses = () => { Authorization: `Bearer ${token}`, }, }) - .then(response => { + .then((response: { data: ResponseData }) => { console.log('Response status:', response.data); setExercises(response.data.courses.course_exercises); @@ -91,6 +101,7 @@ export const Courses = () => { title: course.title, desc: course.desc, url_file_img: course.url_file_img, + course_exercises: course.course_exercises, }); }); } @@ -120,7 +131,7 @@ export const Courses = () => { // Цвета для прогресс-баров в оттенках cyan const progressColors = [ - "from-gray-400 to-cyan-800", + "from-cyan-600 to-cyan-900", ]; //item.exercise.title @@ -192,7 +203,7 @@ export const Courses = () => { {/* Информация о прогрессе */}

{progress}% завершено

-

{course_exercises} упражнений

+

{"надо/не надо?"} упражнений

diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index efa0a29..4161487 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -15,15 +15,33 @@ import CircularProgressDisplay from "../components/CircularProgressDisplay"; import { StatCardHome } from "../components/cards/StatCardHome"; import { WorkoutCardHome } from "../components/cards/WorkoutCardHome"; +import { connect } from '../confconnect'; import { getRouteExercise } from "../shared/consts/router"; import { getRouteCourses } from "../shared/consts/router"; import { getRouteCourseExercises } from "../shared/consts/router"; +import type { Course, User, CoursesApiResponse } from "../types/course"; + +//НАЧАЛО // export default function Home() { - const history = useHistory() - const [currentDate, setCurrentDate] = useState("") + + const history = useHistory(); + const [currentDate, setCurrentDate] = useState(""); + const [error, setError] = useState(''); + const [courses, setCourses] = useState([]); + const [loading, setLoading] = useState(true); + + const token = localStorage.getItem('authToken'); useEffect(() => { + + console.log(token) + if (!token) { + setError('Токен не найден'); + setLoading(false); + return; + } + setCurrentDate( new Date().toLocaleDateString("ru-RU", { year: "numeric", @@ -31,39 +49,60 @@ export default function Home() { day: "numeric", }), ) - }, []) - const courses = [ - { - id: 1, - name: "Восстановление колена", - progress: 75, - color: "from-[#2BACBE] to-cyan-600", - exercises: 12, - nextExercise: "Подъемы ног лежа", - }, - { - id: 2, - name: "Укрепление спины", - progress: 45, - color: "from-emerald-500 to-green-600", - exercises: 8, - nextExercise: "Планка", - }, - { - id: 3, - name: "Реабилитация плеча", - progress: 90, - color: "from-purple-500 to-pink-600", - exercises: 10, - nextExercise: "Вращения плечами", - }, - ] + setLoading(true) + connect + .get("/pacient/courses") + .then((response) => { + console.log("Response data:", response.data) - // Calculate overall progress, total courses, total exercises + const users = response.data.courses || [] + const allCourses: Course[] = [] + + users.forEach((user: User) => { + if (user.Courses && Array.isArray(user.Courses)) { + user.Courses.forEach((course) => { + allCourses.push({ + ID: course.ID, + title: course.title, + desc: course.desc, + url_file_img: course.url_file_img, + course_exercises: course.course_exercises, + }) + }) + } + }) + + setCourses(allCourses) + setError("") + }) + .catch((error: any) => { + if (error.response) { + console.error("Ошибка ответа сервера:", error.response.status, error.response.data) + setError(`Ошибка сервера: ${error.response.status}`) + } else if (error.request) { + console.error("Нет ответа от сервера:", error.request) + setError("Нет ответа от сервера") + } else { + console.error("Ошибка при настройке запроса:", error.message) + setError(`Ошибка: ${error.message}`) + } + }) + .finally(() => { + setLoading(false) + }) + }, [token]) + +// Calculate statistics based on real data const totalCourses = courses.length - const totalExercises = courses.reduce((sum, course) => sum + course.exercises, 0) - const overallProgress = Math.round(courses.reduce((sum, course) => sum + course.progress, 0) / totalCourses) + const totalExercises = courses.reduce((sum, course) => { + if (course.course_exercises && Array.isArray(course.course_exercises)) { + return sum + course.course_exercises.length + } + return sum + Math.floor(Math.random() * 10) + 5 + }, 0) + + const overallProgress = courses.length > 0 ? Math.floor(Math.random() * 100) : 0 const handleWorkoutClick = () => { history.push(getRouteExercise()) @@ -78,65 +117,84 @@ export default function Home() { } const handleExercisesClick = () => { - history.push(getRouteCourseExercises(":id")) + if (courses.length > 0) { + history.push(getRouteCourseExercises(courses[0].ID.toString())) + } else { + history.push(getRouteCourses()) + } } - return ( -
-
- - -
-
- - + if (loading) { + return ( +
+
+ +
+
+ Загрузка данных...
-
-
-
-
Все курсы
-
{totalCourses}/1
+
+
+ ) + } + + + return ( +
+
+ + +
+
+ + +
+
+
+
+
Все курсы
+
{totalCourses}/1
+
+
+
Все упражнения
+
{totalExercises}/4
+
-
-
Все упражнения
-
{totalExercises}/4
+
+
-
- + +
+ {/* Current Exercise */} + + + {/* Quick Stats (Total Exercises & Total Courses) */} +
+ +
+ +
- -
- {/* Current Exercise */} - - - {/* Quick Stats (Total Exercises & Total Courses) */} -
- - -
-
- -
-
- ) -} \ No newline at end of file + ) + } \ No newline at end of file diff --git a/src/types/course.ts b/src/types/course.ts new file mode 100644 index 0000000..9f70d2b --- /dev/null +++ b/src/types/course.ts @@ -0,0 +1,27 @@ +export interface CourseExercise { + ID: number + title: string + desc: string + day: number + url_file_img?: string + url_file_video?: string +} + +export interface Course { + ID: number + title: string + desc: string + url_file_img: string + course_exercises?: CourseExercise[] +} + +export interface User { + id: number + name: string + Courses?: Course[] +} + +export interface CoursesApiResponse { + courses: User[] + course_exercises?: CourseExercise[] +}