Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(SBL-676) Feature: Add Date range for results (Analytics) #44

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions front/src/components/molecules/FunnelContainer/FunnelContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import { useMemo } from 'react';
import { useQuery } from 'react-query';

import { interactiveTypesBlocks } from '@sb-ui/utils/api/config';
import {
getLesson,
getTeacherLessonStudents,
} from '@sb-ui/utils/api/v1/teacher';
import {
TEACHER_LESSON_BASE_KEY,
TEACHER_LESSON_STUDENTS_BASE_KEY,
} from '@sb-ui/utils/queries';
import { getLesson } from '@sb-ui/utils/api/v1/teacher';
import { TEACHER_LESSON_BASE_KEY } from '@sb-ui/utils/queries';

import LessonFunnel from './LessonFunnel';
import { FunnelContainerWrapper } from './FunnelContainer.styled';
Expand All @@ -20,15 +14,7 @@ const isLastFinish = (index, bites, student) =>
index === bites?.length - 1 &&
student.results?.[student.results?.length - 1]?.action === 'finish';

const FunnelContainer = ({ lessonId }) => {
const { data: students, isLoading: isStudentsLoading } = useQuery(
[TEACHER_LESSON_STUDENTS_BASE_KEY, { lessonId }],
getTeacherLessonStudents,
{
keepPreviousData: true,
},
);

const FunnelContainer = ({ lessonId, students, isStudentsLoading }) => {
const { data: lessonData, isLoading: isLessonLoading } = useQuery(
[TEACHER_LESSON_BASE_KEY, { id: lessonId }],
getLesson,
Expand All @@ -42,7 +28,7 @@ const FunnelContainer = ({ lessonId }) => {
return [];
}

const initialLanded = students.total;
const initialLanded = students.length;

const chunks = lessonData.lesson.blocks.reduce(
(list, block) => {
Expand Down Expand Up @@ -75,7 +61,7 @@ const FunnelContainer = ({ lessonId }) => {

const interactiveBlock = bite[bite.length - 1];

const landedStudents = students.students.filter((student) => {
const landedStudents = students.filter((student) => {
const theResult = student.results.find(
(result) => result?.revision === interactiveBlock?.revision,
);
Expand Down Expand Up @@ -115,7 +101,7 @@ const FunnelContainer = ({ lessonId }) => {
});

return chunkBites;
}, [students, lessonData, isStudentsLoading, isLessonLoading]);
}, [isStudentsLoading, isLessonLoading, lessonData, students]);

const isLoading = isStudentsLoading || isLessonLoading;

Expand All @@ -132,6 +118,12 @@ const FunnelContainer = ({ lessonId }) => {

FunnelContainer.propTypes = {
lessonId: T.string.isRequired,
students: T.arrayOf(
T.shape({
results: T.arrayOf(T.shape({})),
}),
),
isStudentsLoading: T.bool,
};

export default FunnelContainer;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import T from 'prop-types';
import { useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useQuery } from 'react-query';
import { useParams } from 'react-router-dom';
Expand All @@ -8,26 +9,47 @@ import { isLessonIdCorrect } from '@sb-ui/pages/Teacher/LessonEdit/useGetLesson'
import { getTeacherLessonStudents } from '@sb-ui/utils/api/v1/teacher';
import { TEACHER_LESSON_STUDENTS_BASE_KEY } from '@sb-ui/utils/queries';

import { createMapTimeRangeStudent } from './analyticsHelper';
import Students from './Students';
import * as S from './Analytics.styled';

const Analytics = ({ opened }) => {
const { id: lessonId } = useParams();
const { t } = useTranslation('teacher');
const { data: studentsData } = useQuery(
const { data: studentsData, isLoading: isStudentsLoading } = useQuery(
[TEACHER_LESSON_STUDENTS_BASE_KEY, { lessonId }],
getTeacherLessonStudents,
{
enabled: isLessonIdCorrect(lessonId),
},
);

const { students } = studentsData || {};
const [rangeDate, setRangeDate] = useState({
from: '',
to: '',
});

const students = useMemo(
() =>
studentsData?.students
?.map(createMapTimeRangeStudent(rangeDate))
?.filter(Boolean),
[rangeDate, studentsData?.students],
);

const handleRangePickerChange = (_, dateStrings) => {
const [from, to] = dateStrings;
setRangeDate({
from,
to,
});
};

return (
<S.Wrapper opened={opened}>
<S.Title>{t('right_bar.analytics')}</S.Title>
<S.Content>
<S.RangePicker onChange={handleRangePickerChange} />
{students?.length > 0 ? (
<>
<S.FunnelTitle>
Expand All @@ -37,7 +59,11 @@ const Analytics = ({ opened }) => {
<span>{t('right_bar.funnel.description')}</span>
</S.FunnelTitle>
<S.FunnelContainerWrapper>
<FunnelContainer lessonId={lessonId} />
<FunnelContainer
lessonId={lessonId}
students={students}
isStudentsLoading={isStudentsLoading}
/>
</S.FunnelContainerWrapper>
<Students students={students} />
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { DatePicker } from 'antd';
import styled from 'styled-components';

import { HEADER_HEIGHT } from '@sb-ui/components/molecules/Header/Header.styled';
import variables from '@sb-ui/theme/variables';

import { RIGHT_BAR_WIDTH } from '../../constants';

const { RangePicker: RangePickerAntd } = DatePicker;

export const Wrapper = styled.div`
position: absolute;
right: 0;
Expand Down Expand Up @@ -33,12 +36,21 @@ export const Title = styled.div`
`;

export const Content = styled.div`
display: flex;
flex-direction: column;
padding-left: 1rem;
padding-right: 1rem;
height: calc(100% - 2 * ${HEADER_HEIGHT}px);
overflow-y: auto;
`;

export const RangePicker = styled(RangePickerAntd).attrs({
format: 'YYYY/MM/DD',
allowEmpty: [true, true],
})`
margin-top: 1rem;
`;

export const FunnelTitle = styled.div`
display: flex;
flex-direction: column;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const getTimeFromData = (data) => (data ? new Date(data).getTime() : null);

const isTimeInRange = ({ fromTime, toTime, time }) => {
if (fromTime && toTime) {
return time >= fromTime && time <= toTime;
}
if (fromTime && !toTime) {
return time >= fromTime;
}
if (!fromTime && toTime) {
return time <= toTime;
}
return true;
};

export const createMapTimeRangeStudent = (rangeData) => {
const fromTime = getTimeFromData(rangeData?.from);
const toTime = getTimeFromData(rangeData?.to);

return (student) => {
if (student.results.length === 0) {
return student;
}
const results = student.results.filter(({ createdAt }) =>
isTimeInRange({ fromTime, toTime, time: new Date(createdAt).getTime() }),
);

if (results.length === 0) {
return null;
}
return {
...student,
results,
};
};
};