diff --git a/src/features/Students/StudentsMetrics/index.jsx b/src/features/Students/StudentsMetrics/index.jsx index 7556da3c..a87cb6ed 100644 --- a/src/features/Students/StudentsMetrics/index.jsx +++ b/src/features/Students/StudentsMetrics/index.jsx @@ -2,28 +2,34 @@ import React, { useEffect } from 'react'; import { useSelector, useDispatch } from 'react-redux'; import { Card, CardGrid, ToggleButton } from '@edx/paragon'; import { ToggleButtonGroup } from 'react-paragon-topaz'; -import { fetchMetricsData } from 'features/Students/data/thunks'; +import { fetchClassesMetricsData, fetchStudentsMetricsData } from 'features/Students/data/thunks'; import 'features/Students/StudentsMetrics/index.scss'; const StudentsMetrics = () => { const dispatch = useDispatch(); - const stateMetrics = useSelector((state) => state.students.metrics.data); + const selectedInstitution = useSelector((state) => state.main.selectedInstitution); + const stateStudentsMetrics = useSelector((state) => state.students.studentsMetrics.data); + const stateClassesMetrics = useSelector((state) => state.students.classesMetrics.data); + const days = 100; // Temporal variable useEffect(() => { - dispatch(fetchMetricsData()); - }, [dispatch]); + if (Object.keys(selectedInstitution).length > 0) { + dispatch(fetchStudentsMetricsData(selectedInstitution.id, days)); + dispatch(fetchClassesMetricsData(selectedInstitution.id, days)); + } + }, [selectedInstitution, dispatch]); return (
- + This week - + Next week - + Next month @@ -39,7 +45,7 @@ const StudentsMetrics = () => { title="New students registered" /> -
{stateMetrics.newStudentsRegistered}
+
{stateStudentsMetrics.numberOfEnrollments}
@@ -47,7 +53,7 @@ const StudentsMetrics = () => { title="Classes scheduled" /> -
{stateMetrics.classesScheduled}
+
{stateClassesMetrics.numberOfClassesCreated}
diff --git a/src/features/Students/data/api.js b/src/features/Students/data/api.js index 1d277935..a425a1cc 100644 --- a/src/features/Students/data/api.js +++ b/src/features/Students/data/api.js @@ -24,18 +24,33 @@ function handleEnrollments(data, courseId) { ); } -function getMetricsStudents() { - const metricsData = { - data: { - new_students_registered: '367', - classes_scheduled: '71%', - }, +function getStudentsMetrics(institutionId, days) { + const params = { + institution_id: institutionId, + days, }; - return metricsData; + + return getAuthenticatedHttpClient().get( + `${getConfig().COURSE_OPERATIONS_API_METRICS_BASE_URL}/students-number/`, + { params }, + ); +} + +function getClassesMetrics(institutionId, days) { + const params = { + institution_id: institutionId, + days, + }; + + return getAuthenticatedHttpClient().get( + `${getConfig().COURSE_OPERATIONS_API_METRICS_BASE_URL}/classes-number/`, + { params }, + ); } export { getStudentbyInstitutionAdmin, handleEnrollments, - getMetricsStudents, + getStudentsMetrics, + getClassesMetrics, }; diff --git a/src/features/Students/data/slice.js b/src/features/Students/data/slice.js index 4234404d..973134be 100644 --- a/src/features/Students/data/slice.js +++ b/src/features/Students/data/slice.js @@ -17,7 +17,10 @@ const initialState = { classes: { ...initialStateService, }, - metrics: { + classesMetrics: { + ...initialStateService, + }, + studentsMetrics: { ...initialStateService, }, filters: {}, @@ -66,15 +69,25 @@ export const studentsSlice = createSlice({ fetchClassesDataFailed: (state) => { state.classes.status = RequestStatus.ERROR; }, - fetchMetricsDataRequest: (state) => { - state.metrics.status = RequestStatus.LOADING; + fetchClassesMetricsDataRequest: (state) => { + state.classesMetrics.status = RequestStatus.LOADING; + }, + fetchClassesMetricsDataSuccess: (state, { payload }) => { + state.classesMetrics.status = RequestStatus.SUCCESS; + state.classesMetrics.data = payload; + }, + fetchClassesMetricsDataFailed: (state) => { + state.classesMetrics.status = RequestStatus.ERROR; + }, + fetchStudentsMetricsDataRequest: (state) => { + state.studentsMetrics.status = RequestStatus.LOADING; }, - fetchMetricsDataSuccess: (state, { payload }) => { - state.metrics.status = RequestStatus.SUCCESS; - state.metrics.data = payload; + fetchStudentsMetricsDataSuccess: (state, { payload }) => { + state.studentsMetrics.status = RequestStatus.SUCCESS; + state.studentsMetrics.data = payload; }, - fetchMetricsDataFailed: (state) => { - state.metrics.status = RequestStatus.ERROR; + fetchStudentsMetricsDataFailed: (state) => { + state.studentsMetrics.status = RequestStatus.ERROR; }, }, }); @@ -91,9 +104,12 @@ export const { fetchClassesDataRequest, fetchClassesDataSuccess, fetchClassesDataFailed, - fetchMetricsDataRequest, - fetchMetricsDataSuccess, - fetchMetricsDataFailed, + fetchClassesMetricsDataRequest, + fetchClassesMetricsDataSuccess, + fetchClassesMetricsDataFailed, + fetchStudentsMetricsDataRequest, + fetchStudentsMetricsDataSuccess, + fetchStudentsMetricsDataFailed, } = studentsSlice.actions; export const { reducer } = studentsSlice; diff --git a/src/features/Students/data/thunks.js b/src/features/Students/data/thunks.js index db8510cd..9507901a 100644 --- a/src/features/Students/data/thunks.js +++ b/src/features/Students/data/thunks.js @@ -10,11 +10,18 @@ import { fetchClassesDataRequest, fetchClassesDataSuccess, fetchClassesDataFailed, - fetchMetricsDataRequest, - fetchMetricsDataSuccess, - fetchMetricsDataFailed, + fetchClassesMetricsDataRequest, + fetchClassesMetricsDataSuccess, + fetchClassesMetricsDataFailed, + fetchStudentsMetricsDataRequest, + fetchStudentsMetricsDataSuccess, + fetchStudentsMetricsDataFailed, } from 'features/Students/data/slice'; -import { getStudentbyInstitutionAdmin, getMetricsStudents } from 'features/Students/data/api'; +import { + getClassesMetrics, + getStudentsMetrics, + getStudentbyInstitutionAdmin, +} from 'features/Students/data/api'; import { getCoursesByInstitution, getClassesByInstitution } from 'features/Common/data/api'; function fetchStudentsData(id, currentPage, filtersData) { @@ -59,15 +66,29 @@ function fetchClassesData(id, courseName) { }; } -function fetchMetricsData() { +function fetchClassesMetricsData(institutionId, days) { + return async (dispatch) => { + dispatch(fetchClassesMetricsDataRequest()); + + try { + const response = camelCaseObject(await getClassesMetrics(institutionId, days)); + dispatch(fetchClassesMetricsDataSuccess(response.data)); + } catch (error) { + dispatch(fetchClassesMetricsDataFailed()); + logError(error); + } + }; +} + +function fetchStudentsMetricsData(institutionId, days) { return async (dispatch) => { - dispatch(fetchMetricsDataRequest()); + dispatch(fetchStudentsMetricsDataRequest()); try { - const response = camelCaseObject(await getMetricsStudents()); - dispatch(fetchMetricsDataSuccess(response.data)); + const response = camelCaseObject(await getStudentsMetrics(institutionId, days)); + dispatch(fetchStudentsMetricsDataSuccess(response.data)); } catch (error) { - dispatch(fetchMetricsDataFailed()); + dispatch(fetchStudentsMetricsDataFailed()); logError(error); } }; @@ -77,5 +98,6 @@ export { fetchStudentsData, fetchCoursesData, fetchClassesData, - fetchMetricsData, + fetchClassesMetricsData, + fetchStudentsMetricsData, };