Skip to content

Commit e28d838

Browse files
authored
Merge pull request #90 from ramillim/challenge-6
Challenge 6
2 parents a877770 + a8cac52 commit e28d838

File tree

8 files changed

+3401
-3263
lines changed

8 files changed

+3401
-3263
lines changed

package-lock.json

+3,083-3,083
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Badges/index.jsx

+59-57
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,45 @@ import {NavLink} from 'react-router-dom';
88
class Badges extends Component {
99
constructor(props) {
1010
super(props);
11-
this.setActiveStory = this.setActiveStory.bind(this);
12-
this.clearActiveStory = this.clearActiveStory.bind(this);
11+
this.setActiveBadge = this.setActiveBadge.bind(this);
12+
this.clearActiveBadge = this.clearActiveBadge.bind(this);
1313
this.prev = this.prev.bind(this);
1414
this.next = this.next.bind(this);
15-
this.setStoryNextPrevIndex = this.setStoryNextPrevIndex.bind(this);
15+
this.setBadgeNextPrevIndex = this.setBadgeNextPrevIndex.bind(this);
1616
this.updatePopupActive = this.updatePopupActive.bind(this);
1717
this.state = {
18-
activeStory: '',
19-
prevStory: '',
20-
nextStory: '',
18+
activeBadge: '',
19+
prevBadge: '',
20+
nextBadge: '',
2121
saluted: false,
2222
};
2323
this.type = 'Badge';
2424
}
2525

26-
componentDidMount() {
27-
this.setState({
28-
prevStory: this.props.stories[ 1 ],
29-
nextStory: this.props.stories[ 3 ]
30-
});
31-
}
32-
33-
setActiveStory(index) {
34-
this.setState({
35-
activeStory: this.props.stories[ index ],
36-
activeSlideIndex: index
37-
});
38-
APIService.isSaluted(this.type, this.props.stories[ index ].id).then((rsp) => {
26+
setActiveBadge(index) {
27+
// Re-fetch the individual badge to increment the post views counter
28+
this.props.fetchBadge(index).then(() => {
29+
this.setState({
30+
saluted: false,
31+
});
32+
return APIService.isSaluted(this.type, this.props.badges[index].id)
33+
}).then((rsp) => {
3934
this.setState({ saluted: rsp.saluted });
40-
});
41-
this.setStoryNextPrevIndex(index, this.props.stories.length);
35+
this.setBadgeNextPrevIndex(index, this.props.badges.length);
36+
}).catch(err => CommonService.showError(err));
4237
}
4338

4439
/**
4540
* salute post
4641
*/
4742
salutePost() {
48-
APIService.salutePost(this.type, this.state.activeStory.id).then(() => {
49-
this.setState({ saluted: true });
43+
APIService.salutePost(this.type, this.state.activeBadge.id).then(() => {
44+
const badge = this.state.activeBadge;
45+
badge.saluteCount = parseInt(badge.shareCount, 10) + 1;
46+
this.setState({
47+
activeBadge: badge,
48+
saluted: true
49+
});
5050
CommonService.showSuccess(`${this.type} saluted successfully`);
5151
}).catch(err => CommonService.showError(err));
5252
}
@@ -55,42 +55,45 @@ class Badges extends Component {
5555
* share post
5656
*/
5757
sharePost() {
58-
APIService.sharePost(this.type, this.state.activeStory.id).then(() => {
59-
this.setState({ saluted: true });
58+
APIService.sharePost(this.type, this.state.activeBadge.id).then(() => {
59+
const badge = this.state.activeBadge;
60+
badge.shareCount = parseInt(badge.shareCount, 10) + 1;
61+
this.setState({
62+
activeBadge: badge
63+
});
6064
CommonService.showSuccess(`${this.type} shared successfully`);
6165
}).catch(err => CommonService.showError(err));
6266
}
6367

64-
clearActiveStory() {
68+
clearActiveBadge() {
6569
this.setState({
66-
activeStory: ''
70+
activeBadge: ''
6771
});
6872
}
6973

7074
next() {
71-
const len = this.props.stories.length;
75+
const len = this.props.badges.length;
7276
let newIndex = !!this.state.activeSlideIndex ? this.state.activeSlideIndex : 0;
7377
newIndex += 1;
7478
newIndex = Math.min(newIndex, len - 1);
75-
this.setStoryNextPrevIndex(newIndex, len);
79+
this.setActiveBadge(newIndex);
7680
}
7781

7882
prev() {
79-
const len = this.props.stories.length;
8083
let newIndex = !!this.state.activeSlideIndex ? this.state.activeSlideIndex : 0;
8184
newIndex -= 1;
8285
newIndex = Math.max(newIndex, 0);
83-
this.setStoryNextPrevIndex(newIndex, len);
86+
this.setActiveBadge(newIndex);
8487
}
8588

86-
setStoryNextPrevIndex(newIndex, len) {
89+
setBadgeNextPrevIndex(newIndex, len) {
8790
const prevIndex = Math.max(newIndex - 1, 0);
8891
const nextIndex = Math.min(newIndex + 1, len - 1);
8992
this.setState({
90-
activeStory: this.props.stories[ newIndex ],
93+
activeBadge: this.props.badges[ newIndex ],
9194
activeSlideIndex: newIndex,
92-
prevStory: this.props.stories[ prevIndex ],
93-
nextStory: this.props.stories[ nextIndex ]
95+
prevBadge: this.props.badges[ prevIndex ],
96+
nextBadge: this.props.badges[ nextIndex ]
9497
});
9598
}
9699

@@ -99,9 +102,8 @@ class Badges extends Component {
99102
}
100103

101104
render() {
102-
const stories = this.props.stories;
103-
const profileName = this.props.profileName;
104-
const activeStory = this.state.activeStory;
105+
const { profileName, badges } = this.props;
106+
const activeBadge = this.state.activeBadge;
105107

106108
return (
107109
<div className="collection-list-wrap collection-badges">
@@ -111,15 +113,15 @@ class Badges extends Component {
111113
<a className="btn btn-badge btn-rt-1" onClick={this.updatePopupActive}><span className="tx">Add Badge</span> </a>
112114
</span>
113115

114-
{!this.state.activeStory
116+
{!activeBadge
115117
? (
116118
<div>
117119
<div className="viewport bd-collection-view">
118-
{stories.map((item, i) => {
120+
{badges.map((item, i) => {
119121
return (
120122
<div key={i} className="bd-collection-item-card-wrap">
121123
<div className="collection-item-card con-centered con-badge"
122-
onClick={() => { this.setActiveStory(i) }}>
124+
onClick={() => { this.setActiveBadge(i) }}>
123125
<div className="desc desc-md">
124126
<figure className={"fig-badge " + item.badgeType.iconURL}/>
125127
</div>
@@ -147,37 +149,37 @@ class Badges extends Component {
147149
<div className="viewport fullstory-view">
148150
<div className="fullstory-slide">
149151
<div className="fullstory-card fullstory-card-md alt">
150-
<div className="postedby">Badge from <strong>{activeStory.createdBy.username}</strong>
151-
<span className="hide-md">and <a>5 other pepole</a></span></div>
152-
<div className="dateval">{CommonService.getCreateTime(activeStory)}</div>
152+
<div className="postedby">Badge from <strong>{activeBadge.createdBy.username}</strong>
153+
<span className="hide-md">and <a>5 other people</a></span></div>
154+
<div className="dateval">{CommonService.getCreateTime(activeBadge)}</div>
153155
<a className="close"
154-
onClick={this.clearActiveStory}
156+
onClick={this.clearActiveBadge}
155157
> </a>
156-
<a className="flag" onClick={() => window.showProfileFlagPopUp('Badge', activeStory.id)}>{''}</a>
158+
<a className="flag" onClick={() => window.showProfileFlagPopUp('Badge', activeBadge.id)}>{''}</a>
157159
<article className="article centered article-badges">
158-
<h3 className="show-md">{activeStory.title}</h3>
160+
<h3 className="show-md">{activeBadge.title}</h3>
159161
<div className="fullstory">
160-
<figure className={"fig-badge bdg-md " + activeStory.badgeType.iconURL}></figure>
162+
<figure className={"fig-badge bdg-md " + activeBadge.badgeType.iconURL}></figure>
161163
</div>
162-
<h3 className="hide-md">{activeStory.badgeType.name}</h3>
164+
<h3 className="hide-md">{activeBadge.badgeType.name}</h3>
163165
<footer className="article-footer alt">
164166
<div className="col col-meta">
165167
<div className="meta-gr">
166168
<h6>Views</h6>
167169
<div className="meta-val reads">
168-
{'1,333'}
170+
{activeBadge.viewCount}
169171
</div>
170172
</div>
171173
<div className="meta-gr">
172174
<h6>Salutes</h6>
173175
<div className="meta-val salutes">
174-
{'469'}
176+
{activeBadge.saluteCount}
175177
</div>
176178
</div>
177179
<div className="meta-gr">
178180
<h6>Shares</h6>
179181
<div className="meta-val shares">
180-
{'256'}
182+
{activeBadge.shareCount}
181183
</div>
182184
</div>
183185
</div>
@@ -193,7 +195,7 @@ class Badges extends Component {
193195
onClick={this.prev}
194196
> </a>)
195197
}
196-
{this.state.activeSlideIndex < this.props.stories.length - 1
198+
{this.state.activeSlideIndex < this.props.badges.length - 1
197199
&& (<a className="slide-arrow next"
198200
onClick={this.next}
199201
> </a>)
@@ -206,17 +208,17 @@ class Badges extends Component {
206208
{this.state.activeSlideIndex > 0
207209
&&
208210
(<div><h5><a onClick={this.prev} className="prev">Previous Badge</a></h5>
209-
<h4><a onClick={this.prev}>{this.state.prevStory.badgeType.name}</a></h4></div>)
211+
<h4><a onClick={this.prev}>{this.state.prevBadge.badgeType.name}</a></h4></div>)
210212
}
211213
</div>
212214
<div className="col col-btn show-md">
213215
<div className="action"><a className="btn btn-more btn-md">Load More Badges</a></div>
214216
</div>
215217
<div className="col">
216-
{this.state.activeSlideIndex < this.props.stories.length - 1
218+
{this.state.activeSlideIndex < this.props.badges.length - 1
217219
&&
218220
(<div><h5><a onClick={this.next} className="next">Next Badge</a></h5>
219-
<h4><a onClick={this.next}>{this.state.nextStory.badgeType.name}</a></h4></div>)
221+
<h4><a onClick={this.next}>{this.state.nextBadge.badgeType.name}</a></h4></div>)
220222
}
221223
</div>
222224
</div>
@@ -230,6 +232,6 @@ class Badges extends Component {
230232

231233
Badges.propTypes = {
232234
prop: PropTypes.object
233-
}
235+
};
234236

235237
export default Badges;

0 commit comments

Comments
 (0)