|
1 | 1 | import React from 'react'; |
2 | 2 | import styled from 'react-emotion'; |
3 | | - |
| 3 | +import { withRouter } from 'next/router'; |
4 | 4 | import TreeView from './syllabus-tree-component'; |
5 | 5 |
|
6 | | -export default class SyllabusTree extends React.Component { |
7 | | - state = { |
8 | | - nodeStateTracker: this.props.data.map(() => true), |
9 | | - activeUnit: this.props.data[0].unit.name, |
10 | | - activeChapter: this.props.data[0].chapters[0].url, |
11 | | - }; |
| 6 | +export default withRouter( |
| 7 | + class SyllabusTree extends React.Component { |
| 8 | + state = { |
| 9 | + nodeStateTracker: [false, ...this.props.data.map(() => true).slice(1)], |
| 10 | + activeUnit: this.props.data[0].unit, |
| 11 | + activeChapter: this.props.data[0].chapters[0].cdnUrl, |
| 12 | + }; |
12 | 13 |
|
13 | | - handleClick = i => { |
14 | | - this.setState({ |
15 | | - nodeStateTracker: [ |
16 | | - ...this.state.nodeStateTracker.slice(0, i), |
17 | | - !this.state.nodeStateTracker[i], |
18 | | - ...this.state.nodeStateTracker.slice(i + 1), |
19 | | - ], |
20 | | - }); |
21 | | - }; |
| 14 | + handleClick = i => { |
| 15 | + this.setState({ |
| 16 | + nodeStateTracker: [ |
| 17 | + ...this.state.nodeStateTracker.slice(0, i), |
| 18 | + !this.state.nodeStateTracker[i], |
| 19 | + ...this.state.nodeStateTracker.slice(i + 1), |
| 20 | + ], |
| 21 | + }); |
| 22 | + }; |
22 | 23 |
|
23 | | - clickOnChapter(chapter, unitName) { |
24 | | - if (chapter.url !== this.state.activeChapter) { |
25 | | - this.setState({ activeChapter: chapter.url, activeUnit: unitName }); |
26 | | - this.props.changeChapter(chapter); |
| 24 | + clickOnChapter(chapter, unitName) { |
| 25 | + if (chapter.cdnUrl !== this.state.activeChapter) { |
| 26 | + this.setState({ activeChapter: chapter.cdnUrl, activeUnit: unitName }); |
| 27 | + this.props.changeChapter(chapter); |
| 28 | + } |
27 | 29 | } |
28 | | - } |
29 | 30 |
|
30 | | - render() { |
31 | | - const Container = styled.div` |
32 | | - & .chapter { |
33 | | - padding: 5px; |
34 | | - font-size: 0.85rem; |
35 | | - user-select: none; |
36 | | - border-left: 2px solid #fff; |
37 | | - color: #888; |
38 | | - :hover { |
| 31 | + render() { |
| 32 | + const Container = styled.div` |
| 33 | + & .chapter { |
| 34 | + padding: 5px; |
| 35 | + font-size: 0.85rem; |
| 36 | + user-select: none; |
| 37 | + border-left: 2px solid #fff; |
| 38 | + color: #888; |
| 39 | + :hover { |
| 40 | + background-color: #f5f5f5; |
| 41 | + border-left: 2px solid #374355; |
| 42 | + cursor: pointer; |
| 43 | + } |
| 44 | + } |
| 45 | +
|
| 46 | + & .active { |
| 47 | + color: #374355; |
39 | 48 | background-color: #f5f5f5; |
40 | 49 | border-left: 2px solid #374355; |
41 | | - cursor: pointer; |
| 50 | + :hover { |
| 51 | + cursor: default; |
| 52 | + } |
42 | 53 | } |
43 | | - } |
44 | 54 |
|
45 | | - & .active { |
46 | | - color: #374355; |
47 | | - background-color: #f5f5f5; |
48 | | - border-left: 2px solid #374355; |
49 | | - :hover { |
50 | | - cursor: default; |
| 55 | + & .unit_name { |
| 56 | + order: 1; |
| 57 | + flex: 1 1 auto; |
| 58 | + align-self: auto; |
51 | 59 | } |
52 | | - } |
| 60 | + `; |
53 | 61 |
|
54 | | - & .unit_name { |
55 | | - order: 1; |
56 | | - flex: 1 1 auto; |
57 | | - align-self: auto; |
58 | | - } |
59 | | - `; |
60 | | - |
61 | | - return ( |
62 | | - <Container> |
63 | | - {this.props.data.map((unitNode, i) => { |
64 | | - const UnitNameComponent = ( |
65 | | - <div className="unit_name" key={unitNode.unit.name} onClick={() => this.handleClick(i)}> |
66 | | - {unitNode.unit.name} |
67 | | - </div> |
68 | | - ); |
69 | | - return ( |
70 | | - <TreeView |
71 | | - key={i} |
72 | | - unitName={unitNode.unit.name} |
73 | | - UnitNameComponent={UnitNameComponent} |
74 | | - activeUnit={this.state.activeUnit} |
75 | | - collapsed={this.state.nodeStateTracker[i]} |
76 | | - onClick={() => this.handleClick(i)}> |
77 | | - {unitNode.chapters.map(chapter => ( |
78 | | - <div |
79 | | - className={`chapter ${this.state.activeChapter === chapter.url ? 'active' : ''}`} |
80 | | - key={chapter.url} |
81 | | - onClick={() => this.clickOnChapter(chapter, unitNode.unit.name)}> |
82 | | - {chapter.name} |
83 | | - </div> |
84 | | - ))} |
85 | | - </TreeView> |
86 | | - ); |
87 | | - })} |
88 | | - </Container> |
89 | | - ); |
| 62 | + return ( |
| 63 | + <Container> |
| 64 | + {this.props.data.map((unitNode, i) => { |
| 65 | + const UnitNameComponent = ( |
| 66 | + <div className="unit_name" key={unitNode.unit} onClick={() => this.handleClick(i)}> |
| 67 | + {unitNode.unit} |
| 68 | + </div> |
| 69 | + ); |
| 70 | + return ( |
| 71 | + <TreeView |
| 72 | + key={i} |
| 73 | + unitName={unitNode.unit} |
| 74 | + UnitNameComponent={UnitNameComponent} |
| 75 | + activeUnit={this.state.activeUnit} |
| 76 | + collapsed={this.state.nodeStateTracker[i]} |
| 77 | + onClick={() => this.handleClick(i)}> |
| 78 | + {unitNode.chapters.map(chapter => ( |
| 79 | + <div |
| 80 | + className={`chapter ${this.state.activeChapter === chapter.cdnUrl ? 'active' : ''}`} |
| 81 | + key={chapter.cdnUrl} |
| 82 | + onClick={() => this.clickOnChapter(chapter, unitNode.unit)}> |
| 83 | + {chapter.name} |
| 84 | + </div> |
| 85 | + ))} |
| 86 | + </TreeView> |
| 87 | + ); |
| 88 | + })} |
| 89 | + </Container> |
| 90 | + ); |
| 91 | + } |
90 | 92 | } |
91 | | -} |
| 93 | +); |
0 commit comments