-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeed.jsx
200 lines (184 loc) · 6.5 KB
/
Feed.jsx
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import React from 'react';
import path from 'path';
import {
Button,
Image,
Collapse,
Well,
Row,
Col,
FormGroup,
FormControl,
ControlLabel,
ListGroupItem,
Tooltip,
OverlayTrigger,
Thumbnail
} from 'react-bootstrap';
import SearchInput, {createFilter} from 'react-search-input';
import Reply from './Reply.jsx';
require('./assets/css/feed.scss');
const KEYS_TO_FILTERS = ['question', 'subject', 'tag']
class Feed extends React.Component {
constructor(props) {
super(props);
const data_string = (props.inbox)
? require('./assets/my_questions.json')
: require('./assets/feed.json');
const data = JSON.parse(data_string);
this.questions = data.questions
this.state = {
questions: this.questions,
searchTerm: ''
}
this.searchUpdated = this.tagFilter.bind(this);
}
tagFilter(term) {
this.setState({searchTerm: term});
this.refs['search-input'].setState({searchTerm: term})
}
searchUpdated(term) {
this.setState({searchTerm: term});
}
render() {
const filteredQuestions = this.questions.filter(createFilter(this.state.searchTerm, KEYS_TO_FILTERS))
return (
<div className="container">
<ul>
<ListGroupItem className="no-border neutral-background">
<SearchInput ref="search-input" className="search-input form-control" onChange={(term) => this.searchUpdated(term)}/>
</ListGroupItem>
{filteredQuestions.map(question => <FeedItem question={question} key={question.id} inbox={this.props.inbox} searchUpdated={this.searchUpdated} />)}
</ul>
</div>
);
}
}
class FeedItem extends React.Component {
constructor(props) {
super(props);
this.state = {
open: false,
hasOpenedAnswer: false,
liked: false
}
this.handleTagClick = this.handleTagClick.bind(this);
}
handleTagClick() {
this.props.searchUpdated(this.props.question.tag);
}
renderAboutMe(question) {
if (question.about_me) {
return (
<Row>
<Col xs={1}>
<Image src={require('./assets/img/' + this.props.question.profile)} circle responsive className="pull-right"/>
</Col>
<Col xs={11}>
<Image src={require('./assets/img/' + this.props.question.about_me)} responsive/>
</Col>
</Row>
);
} else if (question.photo) {
return (
<Row>
<Col xs={112}>
<Image src={require('./assets/img/' + this.props.question.photo)} className="fill-horizontal" responsive/>
</Col>
</Row>
);
}
}
renderAnswer() {
const heart_img = (this.state.liked)
? 'heart_filled.png'
: 'heart_empty.png';
const like_count = (this.state.liked)
? this.props.question.likes + 1
: this.props.question.likes;
if (this.props.inbox) {
return (<Reply question={this.props.question}/>);
} else {
return (
<div>
<Well>
<Row>
<Col xs={12}>
<Image src={require('./assets/img/' + this.props.question.answer)} className="question-answer center-block" rounded responsive/>
</Col>
</Row>
</Well>
<Row>
<Col xs={1}>
<Image className="like-button pull-right" src={require('./assets/img/' + heart_img)} onClick={() => this.setState({
liked: !this.state.liked
})}/>
</Col>
<Col xs={2} className="like-button-label">
<h4 className="pull-left">{like_count}</h4>
</Col>
<Col xs={7}></Col>
<Col xs={2} className="date-label">
<weak className="pull-right">{this.props.question.days_ago}
days ago</weak>
</Col>
</Row>
</div>
);
}
}
renderPanelBody() {
const tooltip = (
<Tooltip id="tooltip" className="disabled">Click to see the answer!</Tooltip>
);
const panelBody = (
<div className="panel-body">
<div onClick={() => this.setState({
open: !this.state.open,
hasOpenedAnswer: true
})}>
<p className="question-body">{this.props.question.question}</p>
{this.renderAboutMe(this.props.question)}
</div>
<Collapse in={this.state.open}>
{this.renderAnswer()}
</Collapse>
</div>
);
if (this.state.hasOpenedAnswer) {
return (
<div>
{panelBody}
</div>
);
} else {
return (
<OverlayTrigger placement="left" overlay={tooltip}>
{panelBody}
</OverlayTrigger>
);
}
}
render() {
return (
<li className="no-border neutral-background list-group-item">
<div className="panel panel-default">
<div className="custom-panel-heading panel-heading">
<Row>
<Col xs={12}>
<div onClick={this.handleTagClick}>
<h3 className="panel-title pull-right question-tag">
<weak>{this.props.question.tag}</weak>
</h3>
</div>
<h3 className="panel-title question-subject">{this.props.question.subject}</h3>
</Col>
</Row>
</div>
{this.renderPanelBody()}
</div>
</li>
);
}
}
export default Feed;