@selfcommunity/react-templates 0.4.5-courses.179 → 0.4.5-courses.181

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,6 +32,11 @@ export interface LessonProps {
32
32
  * @default false
33
33
  */
34
34
  editMode?: boolean;
35
+ /**
36
+ * Renders preview mode
37
+ * @default false
38
+ */
39
+ previewMode?: boolean;
35
40
  /**
36
41
  * Callback fired on edit mode close
37
42
  * @default null
@@ -20,6 +20,7 @@ const classes = {
20
20
  containerRoot: `${constants_1.PREFIX}-container-root`,
21
21
  navigation: `${constants_1.PREFIX}-navigation`,
22
22
  navigationTitle: `${constants_1.PREFIX}-navigation-title`,
23
+ previewInfo: `${constants_1.PREFIX}-preview-info`,
23
24
  button: `${constants_1.PREFIX}-button`
24
25
  };
25
26
  const Root = (0, styles_1.styled)(material_1.Box, {
@@ -33,22 +34,34 @@ const Container = (0, styles_1.styled)(material_1.Box, {
33
34
  overridesResolver: (props, styles) => styles.containerRoot,
34
35
  shouldForwardProp: (prop) => prop !== 'open'
35
36
  })(() => ({}));
37
+ function getUrlLesson(course, lesson) {
38
+ return {
39
+ id: course.id,
40
+ slug: course.slug,
41
+ section_id: lesson.section_id,
42
+ lesson_id: lesson.id
43
+ };
44
+ }
36
45
  function Lesson(inProps) {
37
- var _a, _b, _c, _d, _e, _f, _g;
46
+ var _a, _b, _c;
38
47
  // PROPS
39
48
  const props = (0, system_1.useThemeProps)({
40
49
  props: inProps,
41
50
  name: constants_1.PREFIX
42
51
  });
43
- const { className = null, courseId, sectionId, lessonId, LessonAppbarProps = {}, LessonDrawerProps = {}, editMode = false, onEditModeClose = null, onLessonChange = null, onActivePanelChange = null } = props, rest = tslib_1.__rest(props, ["className", "courseId", "sectionId", "lessonId", "LessonAppbarProps", "LessonDrawerProps", "editMode", "onEditModeClose", "onLessonChange", "onActivePanelChange"]);
52
+ const { className = null, courseId, sectionId, lessonId, LessonAppbarProps = {}, LessonDrawerProps = {}, editMode = false, previewMode = false, onEditModeClose = null, onLessonChange = null, onActivePanelChange = null } = props, rest = tslib_1.__rest(props, ["className", "courseId", "sectionId", "lessonId", "LessonAppbarProps", "LessonDrawerProps", "editMode", "previewMode", "onEditModeClose", "onLessonChange", "onActivePanelChange"]);
44
53
  // HOOKS
45
54
  const theme = (0, material_1.useTheme)();
46
55
  const isMobile = (0, material_1.useMediaQuery)(theme.breakpoints.down('md'));
47
56
  const [_lessonId, setLessonId] = (0, react_1.useState)(lessonId);
48
57
  const [_sectionId, setSectionId] = (0, react_1.useState)(sectionId);
49
58
  const { scLesson, setSCLesson } = (0, react_core_1.useSCFetchLesson)({ id: _lessonId, courseId, sectionId: _sectionId });
50
- const { scCourse, setSCCourse } = (0, react_core_1.useSCFetchCourse)({ id: courseId, params: { view: editMode ? api_services_1.CourseInfoViewType.EDIT : api_services_1.CourseInfoViewType.USER } });
59
+ const { scCourse, setSCCourse } = (0, react_core_1.useSCFetchCourse)({
60
+ id: courseId,
61
+ params: { view: editMode || previewMode ? api_services_1.CourseInfoViewType.EDIT : api_services_1.CourseInfoViewType.USER }
62
+ });
51
63
  const { enqueueSnackbar } = (0, notistack_1.useSnackbar)();
64
+ const scRoutingContext = (0, react_core_1.useSCRouting)();
52
65
  // STATE
53
66
  const [activePanel, setActivePanel] = (0, react_1.useState)(null);
54
67
  const [settings, setSettings] = (0, react_1.useState)(null);
@@ -57,28 +70,28 @@ function Lesson(inProps) {
57
70
  const [lessonMedias, setLessonMedias] = (0, react_1.useState)((_a = scLesson === null || scLesson === void 0 ? void 0 : scLesson.medias) !== null && _a !== void 0 ? _a : []);
58
71
  const [loading, setLoading] = (0, react_1.useState)(false);
59
72
  const [completed, setCompleted] = (0, react_1.useState)(null);
60
- const currentData = (0, react_1.useMemo)(() => {
61
- if (!scCourse || !scLesson)
62
- return null;
63
- return (0, react_ui_1.getCurrentSectionAndLessonIndex)(scCourse, sectionId, lessonId);
64
- }, [scCourse, sectionId, lessonId]);
65
- const [currentSectionIndex, setCurrentSectionIndex] = (0, react_1.useState)((currentData === null || currentData === void 0 ? void 0 : currentData.currentSectionIndex) || 0);
66
- const [currentLessonIndex, setCurrentLessonIndex] = (0, react_1.useState)((currentData === null || currentData === void 0 ? void 0 : currentData.currentLessonIndex) || 0);
67
- const [currentSection, setCurrentSection] = (0, react_1.useState)(null);
68
- const isPrevDisabled = !(scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections) || (currentSectionIndex === 0 && currentLessonIndex === 0);
69
- const isNextDisabled = !(scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections) ||
70
- (currentSectionIndex === (scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections.length) - 1 && currentLessonIndex === ((_b = currentSection === null || currentSection === void 0 ? void 0 : currentSection.lessons) === null || _b === void 0 ? void 0 : _b.length) - 1) ||
71
- (currentLessonIndex < ((_c = currentSection === null || currentSection === void 0 ? void 0 : currentSection.lessons) === null || _c === void 0 ? void 0 : _c.length) - 1
72
- ? (_d = currentSection.lessons[currentLessonIndex + 1]) === null || _d === void 0 ? void 0 : _d.locked
73
- : (_f = (_e = scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections[currentSectionIndex + 1]) === null || _e === void 0 ? void 0 : _e.lessons[0]) === null || _f === void 0 ? void 0 : _f.locked);
73
+ const availableLessons = (0, react_1.useMemo)(() => {
74
+ if (!(scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections))
75
+ return [];
76
+ return scCourse.sections.flatMap((section) => section.lessons.map((lesson) => (Object.assign(Object.assign({}, lesson), { section }))));
77
+ }, [scCourse]);
78
+ const [currentLessonIndex, setCurrentLessonIndex] = (0, react_1.useState)(availableLessons.findIndex((lesson) => lesson.id === lessonId));
79
+ const [currentSection, setCurrentSection] = (0, react_1.useState)(((_b = availableLessons[currentLessonIndex]) === null || _b === void 0 ? void 0 : _b.section) || null);
80
+ const isPrevDisabled = availableLessons.length === 0 || currentLessonIndex <= 0;
81
+ const isNextDisabled = availableLessons.length === 0 || currentLessonIndex >= availableLessons.length - 1 || ((_c = availableLessons[currentLessonIndex + 1]) === null || _c === void 0 ? void 0 : _c.locked);
74
82
  const [openDialog, setOpenDialog] = (0, react_1.useState)(false);
75
83
  const isCourseAdmin = (0, react_1.useMemo)(() => scCourse && scCourse.join_status === types_1.SCCourseJoinStatusType.CREATOR, [scCourse]);
76
84
  //EFFECTS
77
85
  (0, react_1.useEffect)(() => {
78
- if ((scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections) && currentData) {
79
- setCurrentSection(scCourse.sections[currentData.currentSectionIndex] || null);
86
+ const index = availableLessons.findIndex((lesson) => lesson.id === lessonId);
87
+ setCurrentLessonIndex(index);
88
+ }, [lessonId, availableLessons]);
89
+ (0, react_1.useEffect)(() => {
90
+ var _a;
91
+ if (availableLessons.length > 0 && currentLessonIndex >= 0) {
92
+ setCurrentSection(((_a = availableLessons[currentLessonIndex]) === null || _a === void 0 ? void 0 : _a.section) || null);
80
93
  }
81
- }, [scCourse, currentData]);
94
+ }, [currentLessonIndex, availableLessons]);
82
95
  (0, react_1.useEffect)(() => {
83
96
  if (scLesson) {
84
97
  setCompleted(scLesson.completion_status === types_1.SCCourseLessonCompletionStatusType.COMPLETED);
@@ -142,35 +155,25 @@ function Lesson(inProps) {
142
155
  * Handles prev lesson navigation
143
156
  */
144
157
  const handlePrev = () => {
145
- if (currentLessonIndex > 0) {
146
- const newLessonIndex = currentLessonIndex - 1;
147
- setCurrentLessonIndex(newLessonIndex);
148
- handleChangeLesson(currentSection.lessons[newLessonIndex], currentSection);
149
- }
150
- else if (currentSectionIndex > 0) {
151
- const prevSectionIndex = currentSectionIndex - 1;
152
- const prevSection = scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections[prevSectionIndex];
153
- const newLessonIndex = prevSection.lessons.length - 1;
154
- setCurrentSectionIndex(prevSectionIndex);
155
- setCurrentLessonIndex(newLessonIndex);
156
- handleChangeLesson(prevSection.lessons[newLessonIndex], prevSection);
157
- }
158
+ if (isPrevDisabled)
159
+ return;
160
+ const newLessonIndex = currentLessonIndex - 1;
161
+ const newLesson = availableLessons[newLessonIndex];
162
+ setCurrentLessonIndex(newLessonIndex);
163
+ setCurrentSection(newLesson.section);
164
+ handleChangeLesson(newLesson, newLesson.section);
158
165
  };
159
166
  /**
160
167
  * Handles next lesson navigation
161
168
  */
162
169
  const handleNext = () => {
163
- if (currentLessonIndex < currentSection.lessons.length - 1) {
164
- const newLessonIndex = currentLessonIndex + 1;
165
- setCurrentLessonIndex(newLessonIndex);
166
- handleChangeLesson(currentSection.lessons[newLessonIndex], currentSection);
167
- }
168
- else if (currentSectionIndex < (scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections.length) - 1) {
169
- const newSectionIndex = currentSectionIndex + 1;
170
- setCurrentSectionIndex(newSectionIndex);
171
- setCurrentLessonIndex(0);
172
- handleChangeLesson(scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections[newSectionIndex].lessons[0], scCourse.sections[newSectionIndex]);
173
- }
170
+ if (isNextDisabled)
171
+ return;
172
+ const newLessonIndex = currentLessonIndex + 1;
173
+ const newLesson = availableLessons[newLessonIndex];
174
+ setCurrentLessonIndex(newLessonIndex);
175
+ setCurrentSection(newLesson.section);
176
+ handleChangeLesson(newLesson, newLesson.section);
174
177
  };
175
178
  /**
176
179
  * Handles toggle lesson complete/uncompleted
@@ -208,6 +211,9 @@ function Lesson(inProps) {
208
211
  if (!scLesson || !scCourse) {
209
212
  return (0, jsx_runtime_1.jsx)(react_ui_1.HiddenPlaceholder, {});
210
213
  }
211
- return ((0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: [(0, jsx_runtime_1.jsx)(react_ui_1.LessonAppbar, Object.assign({ showComments: scLesson.comments_enabled, editMode: editMode, activePanel: activePanel, title: scCourse.name, handleOpen: handleOpenDrawer, onSave: handleLessonUpdate, updating: updating }, LessonAppbarProps)), (0, jsx_runtime_1.jsxs)(Container, Object.assign({ open: Boolean(activePanel) || editMode, className: classes.containerRoot }, { children: [(0, jsx_runtime_1.jsx)(material_1.Box, Object.assign({ className: classes.navigation }, { children: (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body2", color: "text.secondary" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "templates.lesson.number", defaultMessage: "templates.lesson.number", values: { from: currentLessonIndex + 1, to: (_g = currentSection === null || currentSection === void 0 ? void 0 : currentSection.lessons) === null || _g === void 0 ? void 0 : _g.length } }) })) })), (0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ className: classes.navigationTitle }, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "h5" }, { children: scLesson.name })), (0, jsx_runtime_1.jsxs)(material_1.Box, { children: [(0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ onClick: handlePrev, disabled: isPrevDisabled }, { children: (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "arrow_back" }) })), (0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ onClick: handleNext, disabled: isNextDisabled }, { children: (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "arrow_next" }) }))] })] })), (0, jsx_runtime_1.jsx)(react_ui_1.LessonObject, { course: scCourse, lesson: scLesson, editMode: editMode, onContentChange: handleLessonContentEdit, onMediaChange: handleLessonMediaEdit }), !isCourseAdmin && !editMode && ((0, jsx_runtime_1.jsx)(lab_1.LoadingButton, Object.assign({ className: classes.button, loading: loading, size: "small", variant: completed ? 'outlined' : 'contained', startIcon: !completed && (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "arrow_next" }), endIcon: completed && (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "circle_checked" }), onClick: toggleLessonCompletion }, { children: completed ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "templates.lesson.button.completed", defaultMessage: "templates.lesson.button.completed" })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "templates.lesson.button.complete", defaultMessage: "templates.lesson.button.complete" })) })))] })), (0, jsx_runtime_1.jsx)(react_ui_1.LessonDrawer, Object.assign({ course: scCourse, lesson: scLesson, editMode: isMobile ? activePanel === react_ui_1.SCLessonActionsType.SETTINGS : editMode, activePanel: activePanel, handleClose: handleCloseDrawer, handleChangeLesson: handleChangeLesson, LessonEditFormProps: { lesson: scLesson, onSave: handleLessonUpdate, updating: updating, onSettingsChange: handleSettingsChange } }, LessonDrawerProps))] })), openDialog && (0, jsx_runtime_1.jsx)(react_ui_1.CourseCompletedDialog, { course: scCourse, onClose: handleCloseDialog })] }));
214
+ return ((0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: [(0, jsx_runtime_1.jsx)(react_ui_1.LessonAppbar, Object.assign({ showComments: scLesson.comments_enabled, editMode: editMode, activePanel: activePanel, title: scCourse.name, handleOpen: handleOpenDrawer, onSave: handleLessonUpdate, updating: updating }, LessonAppbarProps)), (0, jsx_runtime_1.jsxs)(Container, Object.assign({ open: Boolean(activePanel) || editMode, className: classes.containerRoot }, { children: [previewMode && ((0, jsx_runtime_1.jsx)(material_1.Alert, Object.assign({ severity: "info", className: classes.previewInfo }, { children: (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body1" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "templates.lesson.previewMode", defaultMessage: "templates.lesson.previewMode", values: {
215
+ link: (...chunks) => ((0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({ to: scRoutingContext.url(react_core_1.SCRoutes.COURSE_LESSON_EDIT_ROUTE_NAME, getUrlLesson(scCourse, scLesson)) }, { children: chunks }))),
216
+ linkBack: (...chunks) => (0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({ to: scRoutingContext.url(react_core_1.SCRoutes.COURSE_DASHBOARD_ROUTE_NAME, scCourse) }, { children: chunks }))
217
+ } }) })) }))), (0, jsx_runtime_1.jsx)(material_1.Box, Object.assign({ className: classes.navigation }, { children: (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body2", color: "text.secondary" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "templates.lesson.number", defaultMessage: "templates.lesson.number", values: { from: currentLessonIndex + 1, to: availableLessons.length } }) })) })), (0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ className: classes.navigationTitle }, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "h5" }, { children: scLesson.name })), (0, jsx_runtime_1.jsxs)(material_1.Box, { children: [(0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ onClick: handlePrev, disabled: isPrevDisabled }, { children: (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "arrow_back" }) })), (0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ onClick: handleNext, disabled: isNextDisabled }, { children: (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "arrow_next" }) }))] })] })), (0, jsx_runtime_1.jsx)(react_ui_1.LessonObject, { course: scCourse, lesson: scLesson, editMode: editMode, onContentChange: handleLessonContentEdit, onMediaChange: handleLessonMediaEdit }), !isCourseAdmin && !editMode && !previewMode && ((0, jsx_runtime_1.jsx)(lab_1.LoadingButton, Object.assign({ className: classes.button, loading: loading, size: "small", variant: completed ? 'outlined' : 'contained', startIcon: !completed && (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "arrow_next" }), endIcon: completed && (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "circle_checked" }), onClick: toggleLessonCompletion }, { children: completed ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "templates.lesson.button.completed", defaultMessage: "templates.lesson.button.completed" })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "templates.lesson.button.complete", defaultMessage: "templates.lesson.button.complete" })) })))] })), (0, jsx_runtime_1.jsx)(react_ui_1.LessonDrawer, Object.assign({ course: scCourse, lesson: scLesson, editMode: isMobile ? activePanel === react_ui_1.SCLessonActionsType.SETTINGS : editMode, previewMode: previewMode, activePanel: activePanel, handleClose: handleCloseDrawer, handleChangeLesson: handleChangeLesson, LessonEditFormProps: { lesson: scLesson, onSave: handleLessonUpdate, updating: updating, onSettingsChange: handleSettingsChange } }, LessonDrawerProps))] })), openDialog && (0, jsx_runtime_1.jsx)(react_ui_1.CourseCompletedDialog, { course: scCourse, onClose: handleCloseDialog })] }));
212
218
  }
213
219
  exports.default = Lesson;
@@ -32,6 +32,11 @@ export interface LessonProps {
32
32
  * @default false
33
33
  */
34
34
  editMode?: boolean;
35
+ /**
36
+ * Renders preview mode
37
+ * @default false
38
+ */
39
+ previewMode?: boolean;
35
40
  /**
36
41
  * Callback fired on edit mode close
37
42
  * @default null
@@ -3,12 +3,12 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  import { Fragment, useCallback, useEffect, useMemo, useState } from 'react';
4
4
  import { styled } from '@mui/material/styles';
5
5
  import { useThemeProps } from '@mui/system';
6
- import { Box, Icon, IconButton, Typography, useMediaQuery, useTheme } from '@mui/material';
6
+ import { Box, Icon, IconButton, Typography, useMediaQuery, useTheme, Alert } from '@mui/material';
7
7
  import { PREFIX } from './constants';
8
8
  import { SCCourseJoinStatusType, SCCourseLessonCompletionStatusType } from '@selfcommunity/types';
9
- import { useSCFetchCourse, useSCFetchLesson } from '@selfcommunity/react-core';
9
+ import { SCRoutes, useSCFetchCourse, useSCFetchLesson, useSCRouting, Link } from '@selfcommunity/react-core';
10
10
  import classNames from 'classnames';
11
- import { CourseCompletedDialog, getCurrentSectionAndLessonIndex, HiddenPlaceholder, LessonAppbar, LessonDrawer, LessonObject, SCLessonActionsType } from '@selfcommunity/react-ui';
11
+ import { CourseCompletedDialog, HiddenPlaceholder, LessonAppbar, LessonDrawer, LessonObject, SCLessonActionsType } from '@selfcommunity/react-ui';
12
12
  import { CourseInfoViewType, CourseService } from '@selfcommunity/api-services';
13
13
  import { FormattedMessage } from 'react-intl';
14
14
  import { LoadingButton } from '@mui/lab';
@@ -18,6 +18,7 @@ const classes = {
18
18
  containerRoot: `${PREFIX}-container-root`,
19
19
  navigation: `${PREFIX}-navigation`,
20
20
  navigationTitle: `${PREFIX}-navigation-title`,
21
+ previewInfo: `${PREFIX}-preview-info`,
21
22
  button: `${PREFIX}-button`
22
23
  };
23
24
  const Root = styled(Box, {
@@ -31,22 +32,34 @@ const Container = styled(Box, {
31
32
  overridesResolver: (props, styles) => styles.containerRoot,
32
33
  shouldForwardProp: (prop) => prop !== 'open'
33
34
  })(() => ({}));
35
+ function getUrlLesson(course, lesson) {
36
+ return {
37
+ id: course.id,
38
+ slug: course.slug,
39
+ section_id: lesson.section_id,
40
+ lesson_id: lesson.id
41
+ };
42
+ }
34
43
  export default function Lesson(inProps) {
35
- var _a, _b, _c, _d, _e, _f, _g;
44
+ var _a, _b, _c;
36
45
  // PROPS
37
46
  const props = useThemeProps({
38
47
  props: inProps,
39
48
  name: PREFIX
40
49
  });
41
- const { className = null, courseId, sectionId, lessonId, LessonAppbarProps = {}, LessonDrawerProps = {}, editMode = false, onEditModeClose = null, onLessonChange = null, onActivePanelChange = null } = props, rest = __rest(props, ["className", "courseId", "sectionId", "lessonId", "LessonAppbarProps", "LessonDrawerProps", "editMode", "onEditModeClose", "onLessonChange", "onActivePanelChange"]);
50
+ const { className = null, courseId, sectionId, lessonId, LessonAppbarProps = {}, LessonDrawerProps = {}, editMode = false, previewMode = false, onEditModeClose = null, onLessonChange = null, onActivePanelChange = null } = props, rest = __rest(props, ["className", "courseId", "sectionId", "lessonId", "LessonAppbarProps", "LessonDrawerProps", "editMode", "previewMode", "onEditModeClose", "onLessonChange", "onActivePanelChange"]);
42
51
  // HOOKS
43
52
  const theme = useTheme();
44
53
  const isMobile = useMediaQuery(theme.breakpoints.down('md'));
45
54
  const [_lessonId, setLessonId] = useState(lessonId);
46
55
  const [_sectionId, setSectionId] = useState(sectionId);
47
56
  const { scLesson, setSCLesson } = useSCFetchLesson({ id: _lessonId, courseId, sectionId: _sectionId });
48
- const { scCourse, setSCCourse } = useSCFetchCourse({ id: courseId, params: { view: editMode ? CourseInfoViewType.EDIT : CourseInfoViewType.USER } });
57
+ const { scCourse, setSCCourse } = useSCFetchCourse({
58
+ id: courseId,
59
+ params: { view: editMode || previewMode ? CourseInfoViewType.EDIT : CourseInfoViewType.USER }
60
+ });
49
61
  const { enqueueSnackbar } = useSnackbar();
62
+ const scRoutingContext = useSCRouting();
50
63
  // STATE
51
64
  const [activePanel, setActivePanel] = useState(null);
52
65
  const [settings, setSettings] = useState(null);
@@ -55,28 +68,28 @@ export default function Lesson(inProps) {
55
68
  const [lessonMedias, setLessonMedias] = useState((_a = scLesson === null || scLesson === void 0 ? void 0 : scLesson.medias) !== null && _a !== void 0 ? _a : []);
56
69
  const [loading, setLoading] = useState(false);
57
70
  const [completed, setCompleted] = useState(null);
58
- const currentData = useMemo(() => {
59
- if (!scCourse || !scLesson)
60
- return null;
61
- return getCurrentSectionAndLessonIndex(scCourse, sectionId, lessonId);
62
- }, [scCourse, sectionId, lessonId]);
63
- const [currentSectionIndex, setCurrentSectionIndex] = useState((currentData === null || currentData === void 0 ? void 0 : currentData.currentSectionIndex) || 0);
64
- const [currentLessonIndex, setCurrentLessonIndex] = useState((currentData === null || currentData === void 0 ? void 0 : currentData.currentLessonIndex) || 0);
65
- const [currentSection, setCurrentSection] = useState(null);
66
- const isPrevDisabled = !(scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections) || (currentSectionIndex === 0 && currentLessonIndex === 0);
67
- const isNextDisabled = !(scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections) ||
68
- (currentSectionIndex === (scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections.length) - 1 && currentLessonIndex === ((_b = currentSection === null || currentSection === void 0 ? void 0 : currentSection.lessons) === null || _b === void 0 ? void 0 : _b.length) - 1) ||
69
- (currentLessonIndex < ((_c = currentSection === null || currentSection === void 0 ? void 0 : currentSection.lessons) === null || _c === void 0 ? void 0 : _c.length) - 1
70
- ? (_d = currentSection.lessons[currentLessonIndex + 1]) === null || _d === void 0 ? void 0 : _d.locked
71
- : (_f = (_e = scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections[currentSectionIndex + 1]) === null || _e === void 0 ? void 0 : _e.lessons[0]) === null || _f === void 0 ? void 0 : _f.locked);
71
+ const availableLessons = useMemo(() => {
72
+ if (!(scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections))
73
+ return [];
74
+ return scCourse.sections.flatMap((section) => section.lessons.map((lesson) => (Object.assign(Object.assign({}, lesson), { section }))));
75
+ }, [scCourse]);
76
+ const [currentLessonIndex, setCurrentLessonIndex] = useState(availableLessons.findIndex((lesson) => lesson.id === lessonId));
77
+ const [currentSection, setCurrentSection] = useState(((_b = availableLessons[currentLessonIndex]) === null || _b === void 0 ? void 0 : _b.section) || null);
78
+ const isPrevDisabled = availableLessons.length === 0 || currentLessonIndex <= 0;
79
+ const isNextDisabled = availableLessons.length === 0 || currentLessonIndex >= availableLessons.length - 1 || ((_c = availableLessons[currentLessonIndex + 1]) === null || _c === void 0 ? void 0 : _c.locked);
72
80
  const [openDialog, setOpenDialog] = useState(false);
73
81
  const isCourseAdmin = useMemo(() => scCourse && scCourse.join_status === SCCourseJoinStatusType.CREATOR, [scCourse]);
74
82
  //EFFECTS
75
83
  useEffect(() => {
76
- if ((scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections) && currentData) {
77
- setCurrentSection(scCourse.sections[currentData.currentSectionIndex] || null);
84
+ const index = availableLessons.findIndex((lesson) => lesson.id === lessonId);
85
+ setCurrentLessonIndex(index);
86
+ }, [lessonId, availableLessons]);
87
+ useEffect(() => {
88
+ var _a;
89
+ if (availableLessons.length > 0 && currentLessonIndex >= 0) {
90
+ setCurrentSection(((_a = availableLessons[currentLessonIndex]) === null || _a === void 0 ? void 0 : _a.section) || null);
78
91
  }
79
- }, [scCourse, currentData]);
92
+ }, [currentLessonIndex, availableLessons]);
80
93
  useEffect(() => {
81
94
  if (scLesson) {
82
95
  setCompleted(scLesson.completion_status === SCCourseLessonCompletionStatusType.COMPLETED);
@@ -140,35 +153,25 @@ export default function Lesson(inProps) {
140
153
  * Handles prev lesson navigation
141
154
  */
142
155
  const handlePrev = () => {
143
- if (currentLessonIndex > 0) {
144
- const newLessonIndex = currentLessonIndex - 1;
145
- setCurrentLessonIndex(newLessonIndex);
146
- handleChangeLesson(currentSection.lessons[newLessonIndex], currentSection);
147
- }
148
- else if (currentSectionIndex > 0) {
149
- const prevSectionIndex = currentSectionIndex - 1;
150
- const prevSection = scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections[prevSectionIndex];
151
- const newLessonIndex = prevSection.lessons.length - 1;
152
- setCurrentSectionIndex(prevSectionIndex);
153
- setCurrentLessonIndex(newLessonIndex);
154
- handleChangeLesson(prevSection.lessons[newLessonIndex], prevSection);
155
- }
156
+ if (isPrevDisabled)
157
+ return;
158
+ const newLessonIndex = currentLessonIndex - 1;
159
+ const newLesson = availableLessons[newLessonIndex];
160
+ setCurrentLessonIndex(newLessonIndex);
161
+ setCurrentSection(newLesson.section);
162
+ handleChangeLesson(newLesson, newLesson.section);
156
163
  };
157
164
  /**
158
165
  * Handles next lesson navigation
159
166
  */
160
167
  const handleNext = () => {
161
- if (currentLessonIndex < currentSection.lessons.length - 1) {
162
- const newLessonIndex = currentLessonIndex + 1;
163
- setCurrentLessonIndex(newLessonIndex);
164
- handleChangeLesson(currentSection.lessons[newLessonIndex], currentSection);
165
- }
166
- else if (currentSectionIndex < (scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections.length) - 1) {
167
- const newSectionIndex = currentSectionIndex + 1;
168
- setCurrentSectionIndex(newSectionIndex);
169
- setCurrentLessonIndex(0);
170
- handleChangeLesson(scCourse === null || scCourse === void 0 ? void 0 : scCourse.sections[newSectionIndex].lessons[0], scCourse.sections[newSectionIndex]);
171
- }
168
+ if (isNextDisabled)
169
+ return;
170
+ const newLessonIndex = currentLessonIndex + 1;
171
+ const newLesson = availableLessons[newLessonIndex];
172
+ setCurrentLessonIndex(newLessonIndex);
173
+ setCurrentSection(newLesson.section);
174
+ handleChangeLesson(newLesson, newLesson.section);
172
175
  };
173
176
  /**
174
177
  * Handles toggle lesson complete/uncompleted
@@ -206,5 +209,8 @@ export default function Lesson(inProps) {
206
209
  if (!scLesson || !scCourse) {
207
210
  return _jsx(HiddenPlaceholder, {});
208
211
  }
209
- return (_jsxs(Fragment, { children: [_jsxs(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { children: [_jsx(LessonAppbar, Object.assign({ showComments: scLesson.comments_enabled, editMode: editMode, activePanel: activePanel, title: scCourse.name, handleOpen: handleOpenDrawer, onSave: handleLessonUpdate, updating: updating }, LessonAppbarProps)), _jsxs(Container, Object.assign({ open: Boolean(activePanel) || editMode, className: classes.containerRoot }, { children: [_jsx(Box, Object.assign({ className: classes.navigation }, { children: _jsx(Typography, Object.assign({ variant: "body2", color: "text.secondary" }, { children: _jsx(FormattedMessage, { id: "templates.lesson.number", defaultMessage: "templates.lesson.number", values: { from: currentLessonIndex + 1, to: (_g = currentSection === null || currentSection === void 0 ? void 0 : currentSection.lessons) === null || _g === void 0 ? void 0 : _g.length } }) })) })), _jsxs(Box, Object.assign({ className: classes.navigationTitle }, { children: [_jsx(Typography, Object.assign({ variant: "h5" }, { children: scLesson.name })), _jsxs(Box, { children: [_jsx(IconButton, Object.assign({ onClick: handlePrev, disabled: isPrevDisabled }, { children: _jsx(Icon, { children: "arrow_back" }) })), _jsx(IconButton, Object.assign({ onClick: handleNext, disabled: isNextDisabled }, { children: _jsx(Icon, { children: "arrow_next" }) }))] })] })), _jsx(LessonObject, { course: scCourse, lesson: scLesson, editMode: editMode, onContentChange: handleLessonContentEdit, onMediaChange: handleLessonMediaEdit }), !isCourseAdmin && !editMode && (_jsx(LoadingButton, Object.assign({ className: classes.button, loading: loading, size: "small", variant: completed ? 'outlined' : 'contained', startIcon: !completed && _jsx(Icon, { children: "arrow_next" }), endIcon: completed && _jsx(Icon, { children: "circle_checked" }), onClick: toggleLessonCompletion }, { children: completed ? (_jsx(FormattedMessage, { id: "templates.lesson.button.completed", defaultMessage: "templates.lesson.button.completed" })) : (_jsx(FormattedMessage, { id: "templates.lesson.button.complete", defaultMessage: "templates.lesson.button.complete" })) })))] })), _jsx(LessonDrawer, Object.assign({ course: scCourse, lesson: scLesson, editMode: isMobile ? activePanel === SCLessonActionsType.SETTINGS : editMode, activePanel: activePanel, handleClose: handleCloseDrawer, handleChangeLesson: handleChangeLesson, LessonEditFormProps: { lesson: scLesson, onSave: handleLessonUpdate, updating: updating, onSettingsChange: handleSettingsChange } }, LessonDrawerProps))] })), openDialog && _jsx(CourseCompletedDialog, { course: scCourse, onClose: handleCloseDialog })] }));
212
+ return (_jsxs(Fragment, { children: [_jsxs(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { children: [_jsx(LessonAppbar, Object.assign({ showComments: scLesson.comments_enabled, editMode: editMode, activePanel: activePanel, title: scCourse.name, handleOpen: handleOpenDrawer, onSave: handleLessonUpdate, updating: updating }, LessonAppbarProps)), _jsxs(Container, Object.assign({ open: Boolean(activePanel) || editMode, className: classes.containerRoot }, { children: [previewMode && (_jsx(Alert, Object.assign({ severity: "info", className: classes.previewInfo }, { children: _jsx(Typography, Object.assign({ variant: "body1" }, { children: _jsx(FormattedMessage, { id: "templates.lesson.previewMode", defaultMessage: "templates.lesson.previewMode", values: {
213
+ link: (...chunks) => (_jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes.COURSE_LESSON_EDIT_ROUTE_NAME, getUrlLesson(scCourse, scLesson)) }, { children: chunks }))),
214
+ linkBack: (...chunks) => _jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes.COURSE_DASHBOARD_ROUTE_NAME, scCourse) }, { children: chunks }))
215
+ } }) })) }))), _jsx(Box, Object.assign({ className: classes.navigation }, { children: _jsx(Typography, Object.assign({ variant: "body2", color: "text.secondary" }, { children: _jsx(FormattedMessage, { id: "templates.lesson.number", defaultMessage: "templates.lesson.number", values: { from: currentLessonIndex + 1, to: availableLessons.length } }) })) })), _jsxs(Box, Object.assign({ className: classes.navigationTitle }, { children: [_jsx(Typography, Object.assign({ variant: "h5" }, { children: scLesson.name })), _jsxs(Box, { children: [_jsx(IconButton, Object.assign({ onClick: handlePrev, disabled: isPrevDisabled }, { children: _jsx(Icon, { children: "arrow_back" }) })), _jsx(IconButton, Object.assign({ onClick: handleNext, disabled: isNextDisabled }, { children: _jsx(Icon, { children: "arrow_next" }) }))] })] })), _jsx(LessonObject, { course: scCourse, lesson: scLesson, editMode: editMode, onContentChange: handleLessonContentEdit, onMediaChange: handleLessonMediaEdit }), !isCourseAdmin && !editMode && !previewMode && (_jsx(LoadingButton, Object.assign({ className: classes.button, loading: loading, size: "small", variant: completed ? 'outlined' : 'contained', startIcon: !completed && _jsx(Icon, { children: "arrow_next" }), endIcon: completed && _jsx(Icon, { children: "circle_checked" }), onClick: toggleLessonCompletion }, { children: completed ? (_jsx(FormattedMessage, { id: "templates.lesson.button.completed", defaultMessage: "templates.lesson.button.completed" })) : (_jsx(FormattedMessage, { id: "templates.lesson.button.complete", defaultMessage: "templates.lesson.button.complete" })) })))] })), _jsx(LessonDrawer, Object.assign({ course: scCourse, lesson: scLesson, editMode: isMobile ? activePanel === SCLessonActionsType.SETTINGS : editMode, previewMode: previewMode, activePanel: activePanel, handleClose: handleCloseDrawer, handleChangeLesson: handleChangeLesson, LessonEditFormProps: { lesson: scLesson, onSave: handleLessonUpdate, updating: updating, onSettingsChange: handleSettingsChange } }, LessonDrawerProps))] })), openDialog && _jsx(CourseCompletedDialog, { course: scCourse, onClose: handleCloseDialog })] }));
210
216
  }