-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathUnitButton.jsx
More file actions
94 lines (86 loc) · 2.38 KB
/
UnitButton.jsx
File metadata and controls
94 lines (86 loc) · 2.38 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
import { useCallback } from 'react';
import { Link, useLocation } from 'react-router-dom';
import PropTypes from 'prop-types';
import { connect, useSelector } from 'react-redux';
import classNames from 'classnames';
import { Button, Icon } from '@openedx/paragon';
import { Bookmark } from '@openedx/paragon/icons';
import UnitIcon from './UnitIcon';
import CompleteIcon from './CompleteIcon';
const UnitButton = ({
onClick,
title,
contentType,
isActive,
bookmarked,
complete,
showCompletion,
unitId,
className,
showTitle,
}) => {
const { courseId, sequenceId } = useSelector(state => state.courseware);
const { pathname } = useLocation();
const basePath = `/course/${courseId}/${sequenceId}/${unitId}`;
const unitPath = pathname.startsWith('/preview') ? `/preview${basePath}` : basePath;
const handleClick = useCallback(() => {
onClick(unitId);
}, [onClick, unitId]);
return (
<Button
className={classNames({
active: isActive,
complete: showCompletion && complete,
}, className)}
variant="link"
onClick={handleClick}
title={title}
role="tab"
aria-selected={isActive}
aria-controls={title}
as={Link}
to={unitPath}
>
<UnitIcon type={contentType} />
{showTitle && <span className="unit-title">{title}</span>}
{showCompletion && complete ? <CompleteIcon size="sm" className="text-success ml-2" /> : null}
{bookmarked ? (
<Icon
data-testid="bookmark-icon"
src={Bookmark}
className="text-primary small position-absolute"
style={{ top: '-3px', right: '5px' }}
/>
) : null}
</Button>
);
};
UnitButton.propTypes = {
bookmarked: PropTypes.bool,
className: PropTypes.string,
complete: PropTypes.bool,
contentType: PropTypes.string.isRequired,
isActive: PropTypes.bool,
onClick: PropTypes.func.isRequired,
showCompletion: PropTypes.bool,
showTitle: PropTypes.bool,
title: PropTypes.string.isRequired,
unitId: PropTypes.string.isRequired,
};
UnitButton.defaultProps = {
className: undefined,
isActive: false,
bookmarked: false,
complete: false,
showTitle: false,
showCompletion: true,
};
const mapStateToProps = (state, props) => {
if (props.unitId) {
return {
...state.models.units[props.unitId],
};
}
return {};
};
export default connect(mapStateToProps)(UnitButton);