-
Notifications
You must be signed in to change notification settings - Fork 0
/
Topics.js
122 lines (118 loc) · 3.77 KB
/
Topics.js
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
118
119
120
121
122
/**
* Copyright Vlad Shamgin (c) 2019
* Alphaux Lightning Hints
* Topics.js component
*
* @summary short description for the file
* @author Vlad Shamgin <[email protected]>
*/
import React, { Component } from 'react';
import ReactGA from 'react-ga';
import '../styles/Topics.css';
class Topics extends Component {
constructor(props) {
super(props);
this.model = {
topics:[]
};
this.state = {
currentTopic: {},
showHideLoadingStatus: 'show-loading-status'
};
this.topicsAPIUrl = '/getTopics';
this.prevSelected = null;
this.onTopicClick= this.handleTopicClick.bind(this);
this.loadingStatusText = React.createRef();
}
componentDidMount() {
this.initModel();
}
initModel=()=>{
new Promise((resolve, reject) =>{
fetch(this.topicsAPIUrl)
.then(res => res.text())
.then(res => {
resolve(res);
}).catch(err => err) /** todo: load fallback model */
}).then((modelFromResponse)=>{
this.model = JSON.parse(modelFromResponse).body;
this.setState({
currentTopic: this.model.topics[0], /** set default topic */
showHideLoadingStatus: 'hide-loading-status'
});
this.props.onTopicsMountCallback(this.state.currentTopic);
}).catch((error)=>{
/* fallback model */
this.model = {
topics:[
{
id: 'error',
name: 'Error',
url: ''
}
]
}
});
}
handleTopicClick = (e) => {
e.currentTarget.className = 'topic-button-clicked';
if(this.prevSelected && this.prevSelected !== e.currentTarget){
this.prevSelected.className = 'topic-button-default';
}
const topicName = e.currentTarget.name;
let newTopic = {};
this.model.topics.forEach((value, key)=>{
if(value.id === topicName){
Object.assign(newTopic, this.model.topics[key]);
}
});
this.setState({
currentTopic: newTopic
});
this.prevSelected = e.currentTarget;
ReactGA.event({
category: 'Topic selected',
action: topicName
});
this.props.onClickCallback(newTopic);
}
getButtonClassName = (el) => {
if(el.id===null) {
return 'topic-button-disabled';
} else if (this.state.currentTopic.id === el.id) {
return 'topic-button-clicked';
} else {
return 'topic-button-default';
}
}
createTopicsList = () => {
const items = [];
this.model.topics.forEach((value, key)=>{
items.push(
<button key={`button-topic-key-${key}`}
name={this.model.topics[key].id}
disabled={(this.model.topics[key].id===null) ? true : false}
onClick={this.handleTopicClick}
className={this.getButtonClassName(this.model.topics[key])}
>
{this.model.topics[key].name}
</button>
);
});
return items;
}
render() {
return (
<div className="topics-list-container">
<span className="topics-selected-text">
Choose Lightning Hints topic:
</span>
<div className="topics-list-content">
<span className={this.state.showHideLoadingStatus}>loading..</span>
{this.createTopicsList()}
</div>
</div>
);
}
}
export default Topics;