-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
168 lines (155 loc) · 4.27 KB
/
app.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React, { Component } from 'react';
import Header from './headerBar';
import UserSubs from './dropdown.js';
import Search from './filter';
import Title from './pageTitle';
import RedditsWrapper from './results';
import AddCustom from './addReddit';
import UserReddit from './userAddedReddit';
class App extends Component {
constructor (props) {
super(props);
this.state = {
normal: ['hot', 'new', 'rising', 'controversial', 'promoted'],
user: [],
ignored: [],
clicked: false,
current: 'hot',
dataStorage: [],
data: []
};
this.handleClick = this.handleClick.bind(this);
this.userRedditClick = this.userRedditClick.bind(this);
this.normalClick = this.normalClick.bind(this);
this.filter = this.filter.bind(this);
this.focused = this.focused.bind(this);
this.add = this.add.bind(this);
this.clickRemove = this.clickRemove.bind(this);
}
componentDidMount () {
let val = {
target: {
innerText: 'hot'
}
};
this.normalClick(val);
}
handleClick () {
this.setState({
clicked: !this.state.clicked
});
}
userRedditClick (e) {
let val = e.target.innerText;
let url = 'https://www.reddit.com/r/' + val + '.json';
let data;
let context = this;
axios.get(url)
.then(function (resJson) {
context.setState({
dataStorage: resJson.data.data.children,
data: resJson.data.data.children
});
})
.catch(function (err) {
console.error(err);
});
this.setState({
current: val
});
}
clickRemove (val) {
this.setState({
user: this.state.user.filter((item) => item !== val)
});
}
normalClick (e) {
let val = e.target.innerText;
var urlPlacer = '';
if (val === 'promoted') {
urlPlacer = 'ads';
} else if (val === 'wiki') {
urlPlacer = 'wiki/index';
} else {
urlPlacer = val;
}
let url = 'https://www.reddit.com/' + urlPlacer + '.json';
let data;
let context = this;
axios.get(url)
.then(function (resJson) {
context.setState({
dataStorage: resJson.data.data.children,
data: resJson.data.data.children
});
})
.catch(function (err) {
console.error(err);
});
this.setState({
current: val
});
}
add () {
var element = document.getElementById('add-text');
if (element !== '') {
var text = element.value.toLowerCase();
this.setState({
user: this.state.user.concat([text])
});
element.value = '';
}
}
focused () {
var text = document.getElementById('search-text').value;
if (text === '') {
this.setState({
data: this.state.dataStorage
});
}
}
filter () {
var text = document.getElementById('search-text').value.toLowerCase();
this.setState({
data: this.state.dataStorage.filter(function (item) {
return item.data.title.toLowerCase().indexOf(text) !== -1;
})
});
}
render () {
var context = this;
let userRedditList = this.state.user.map(function (item) {
return <UserReddit key={item} item={item} clickRemove={context.clickRemove} click={context.userRedditClick}/>
});
userRedditList.unshift(<AddCustom key="addCustom" add={this.add}/>);
if (this.state.clicked) {
return (
<div>
<div className="nav-wrapper">
<UserSubs user={this.state.user} click={this.handleClick}/>
<Header normal={this.state.normal} click={this.normalClick}/>
</div>
<Search filter={this.filter} focused={this.focused}/>
<Title title={this.state.current}/>
<aside className="side-wrapper">
{ userRedditList }
</aside>
<RedditsWrapper links={this.state.data}/>
</div>
);
} else {
return (
<div>
<div className="nav-wrapper">
<UserSubs user={this.state.user} click={this.handleClick}/>
<Header normal={this.state.normal} click={this.normalClick}/>
</div>
<Search filter={this.filter} focused={this.focused}/>
<Title title={this.state.current}/>
<RedditsWrapper links={this.state.data}/>
</div>
);
}
}
}
export default App;