Skip to content

Commit e0ebbfa

Browse files
authored
update day-07 post with missing code snippets (#62)
* update day-07 post with missing code snippets * typo fix
1 parent f0b6849 commit e0ebbfa

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

day-07/post.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,40 @@ class Content extends React.Component {
8787
}
8888
```
8989

90+
> Let's also update our `ActivityItem` component slightly to reflect our new `activity` object structure.
91+
> We're also using [Moment.js](https://momentjs.com/) library to format the dates into a human friendly string e.g `30 min ago`
92+
> To include it in your file, add the following `script` tag to your document
93+
> ```html
94+
> <script src="https://unpkg.com/[email protected]/min/moment.min.js"></script>
95+
>```
96+
97+
```javascript
98+
class ActivityItem extends React.Component {
99+
render() {
100+
const { activity } = this.props;
101+
102+
return (
103+
<div className='item'>
104+
<div className={'avatar'}>
105+
<img
106+
alt='avatar'
107+
src={activity.actor.avatar_url} />
108+
</div>
109+
110+
<span className={'time'}>
111+
{moment(activity.created_at).fromNow()}
112+
</span>
113+
114+
<p>{activity.actor.display_login} {activity.payload.action}</p>
115+
<div className={'right'}>
116+
{activity.repo.name}
117+
</div>
118+
</div>
119+
)
120+
}
121+
}
122+
```
123+
90124
Notice that we didn't change anything else from our `Content` component and it just works.
91125

92126
<div class="demo" id="fetchedTimeline"></div>
@@ -171,7 +205,23 @@ Using this new `prop` (the `requestRefresh` prop), we can update the `activities
171205

172206
```javascript
173207
class Content extends React.Component {
174-
// ...
208+
constructor {
209+
this.state = {
210+
activities: [],
211+
loading: false // <~ set loading to false
212+
};
213+
}
214+
// ...
215+
updateData() {
216+
this.setState(
217+
{
218+
loading: false,
219+
activities: data.sort(() => 0.5 - Math.random()).slice(0, 4)
220+
},
221+
this.props.onComponentRefresh
222+
);
223+
}
224+
175225
componentWillReceiveProps(nextProps) {
176226
// Check to see if the requestRefresh prop has changed
177227
if (nextProps.requestRefresh === true) {
@@ -182,6 +232,18 @@ class Content extends React.Component {
182232
}
183233
```
184234

235+
Let's also update our `componentWillMount` method to call `this.updateData()` instead of `this.setState`
236+
237+
```javascript
238+
class Content extends React.Component {
239+
// ...
240+
componentDidMount() {
241+
this.updateData();
242+
}
243+
// ...
244+
}
245+
```
246+
185247
<div class="demo" id="requestRefresh"></div>
186248

187249
> This demo is using static data from a JSON file and randomly picking four elements when we refresh. This is set up to _simulate_ a refresh.

0 commit comments

Comments
 (0)