forked from openedx/frontend-app-learner-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.js
More file actions
117 lines (103 loc) · 4.49 KB
/
Copy pathhooks.js
File metadata and controls
117 lines (103 loc) · 4.49 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { useMemo } from 'react';
import { useIntl } from '@edx/frontend-platform/i18n';
import { utilHooks, useCourseData } from 'hooks';
import {
useInitializeLearnerHome, useCourseCompletion, useCourseAssignmentsCardInfo,
} from 'data/hooks';
import { getTransformedCourseDataList } from 'utils/dataTransformers';
import { parseDateOnly } from 'utils/dateFormatter';
import messages from './messages';
/**
* NOTE: duration, rating, per-course progress %, and due date are not yet exposed by the
* learner-home API. Until that data exists, this hook fills them in with fixed placeholders
* (and, for due date, a deterministic-but-varied offset per card) so the card layout can be
* built and demoed now. Swap these for real `course.*` fields as the API grows to support them.
*/
const PLACEHOLDER_DURATION_HOURS = 4;
const PLACEHOLDER_RATING = 4.5;
const PLACEHOLDER_PROGRESS_PERCENT = 35;
const DUE_SOON_WINDOW_DAYS = 7;
const MS_PER_DAY = 1000 * 60 * 60 * 24;
// Deterministic per-card day offset (relative to today) used only when no real due date
// exists, so a list of cards shows a mix of overdue / due-soon / on-track states.
const placeholderDueDateOffset = (cardId) => {
const index = Number(String(cardId).replace(/\D/g, '')) || 0;
const cycleDays = [-10, 3, 21, -2, 9];
return cycleDays[index % cycleDays.length];
};
const formatDurationHours = (estimatedDurationHours) => {
if (estimatedDurationHours === null || estimatedDurationHours === undefined) {
return null;
}
const hours = Number(estimatedDurationHours);
if (Number.isNaN(hours)) {
return null;
}
const rounded = Math.round(hours * 10) / 10;
return rounded % 1 === 0 ? rounded.toFixed(0) : `${rounded}`;
};
const useAllDashboardCourseIds = () => {
const { data } = useInitializeLearnerHome();
return useMemo(() => {
const courseList = getTransformedCourseDataList(data?.courses || []);
return courseList
.map((courseData) => courseData?.courseRun?.courseId)
.filter(Boolean);
}, [data?.courses]);
};
export const useCourseCardMeta = (cardId) => {
const { formatMessage } = useIntl();
const formatDate = utilHooks.useFormatDate();
const courseData = useCourseData(cardId);
const { course, enrollment, courseRun } = courseData || {};
const courseId = courseRun?.courseId;
const { data: completionData } = useCourseCompletion();
const realProgressPercent = useMemo(() => {
if (!courseId || !completionData) {
return null;
}
const entry = completionData.find((item) => item.courseId === courseId);
return entry?.completion ?? null;
}, [completionData, courseId]);
const allCourseIds = useAllDashboardCourseIds();
const { data: cardInfoResponse } = useCourseAssignmentsCardInfo(allCourseIds);
const cardInfo = useMemo(() => {
if (!courseId || !cardInfoResponse?.results) {
return null;
}
return cardInfoResponse.results.find((result) => result.course_id === courseId) || null;
}, [cardInfoResponse, courseId]);
const providerDisplayName = cardInfo?.provider_display_name || null;
const contentTypeDisplay = cardInfo?.content_type_display || null;
const realDurationHours = formatDurationHours(cardInfo?.estimated_duration_hours);
const dueDate = useMemo(() => {
if (cardInfo?.due_date) {
return parseDateOnly(cardInfo.due_date);
}
if (course?.dueDate) {
return parseDateOnly(course.dueDate);
}
const placeholder = new Date();
placeholder.setDate(placeholder.getDate() + placeholderDueDateOffset(cardId));
return placeholder;
}, [cardInfo?.due_date, course?.dueDate, cardId]);
const daysUntilDue = Math.ceil((dueDate.getTime() - Date.now()) / MS_PER_DAY);
const isOverdue = cardInfo?.due_date ? Boolean(cardInfo?.is_overdue) : daysUntilDue < 0;
const isDueSoon = !isOverdue && daysUntilDue <= DUE_SOON_WINDOW_DAYS;
const hasStarted = enrollment?.hasStarted || false;
const progressPercent = course?.progressPercent ?? realProgressPercent ?? PLACEHOLDER_PROGRESS_PERCENT;
const rating = course?.rating ?? PLACEHOLDER_RATING;
const durationHours = course?.durationHours ?? realDurationHours ?? PLACEHOLDER_DURATION_HOURS;
return {
providerDisplayName,
contentType: contentTypeDisplay || formatMessage(messages.contentTypeVideo),
durationLabel: formatMessage(messages.durationHours, { hours: durationHours }),
dueDateLabel: formatMessage(messages.dueDate, { dueDate: formatDate(dueDate) }),
rating,
isOverdue,
isDueSoon,
hasStarted,
progressPercent,
};
};
export default useCourseCardMeta;