Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A small code refactoring index.js in docs #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 67 additions & 75 deletions docs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,93 +3,85 @@ import ReactDOM from 'react-dom';
import InfiniteScroll from '../../dist/InfiniteScroll';
import qwest from 'qwest';

const imageList = [];
const api = {
baseUrl: 'https://api.soundcloud.com',
client_id: 'caf73ef1e709f839664ab82bef40fa96'
baseUrl: 'https://api.soundcloud.com',
client_id: 'caf73ef1e709f839664ab82bef40fa96'
};

class App extends Component {
constructor(props) {
super(props);
constructor(props) {
super(props);

this.state = {
tracks: [],
hasMoreItems: true,
nextHref: null
};
}

loadItems(page) {
var self = this;
this.state = {
tracks: [],
hasMoreItems: true,
nextHref: null
};
}

var url = api.baseUrl + '/users/8665091/favorites';
if(this.state.nextHref) {
url = this.state.nextHref;
}

qwest.get(url, {
client_id: api.client_id,
linked_partitioning: 1,
page_size: 10
}, {
cache: true
})
.then(function(xhr, resp) {
if(resp) {
var tracks = self.state.tracks;
resp.collection.map((track) => {
if(track.artwork_url == null) {
track.artwork_url = track.user.avatar_url;
}
loadItems = (page) => {
const url = `${api.baseUrl}/users/8665091/favorites`;
if (this.state.nextHref) {
url = this.state.nextHref;
}

tracks.push(track);
});
qwest.get(url, {
client_id: api.client_id,
linked_partitioning: 1,
page_size: 10
}, {
cache: true
})
.then((xhr, resp) => {
if (resp) {
const tracks = this.state.tracks;
resp.collection.map((track) => {
if (track.artwork_url == null) {
track.artwork_url = track.user.avatar_url;
}
tracks.push(track);
});

if(resp.next_href) {
self.setState({
tracks: tracks,
nextHref: resp.next_href
});
} else {
self.setState({
hasMoreItems: false
});
}
}
if (resp.next_href) {
this.setState({
tracks: tracks,
nextHref: resp.next_href
});
}

render() {
const loader = <div className="loader">Loading ...</div>;
} else {
this.setState({
hasMoreItems: false
});
}
}
});
}

var items = [];
this.state.tracks.map((track, i) => {
items.push(
<div className="track" key={i}>
<a href={track.permalink_url} target="_blank">
<img src={track.artwork_url} width="150" height="150" />
<p className="title">{track.title}</p>
</a>
</div>
);
});
render() {
const loader = <div className="loader">Loading ...</div>;

return (
<InfiniteScroll
pageStart={0}
loadMore={this.loadItems.bind(this)}
hasMore={this.state.hasMoreItems}
loader={loader}>
const items = this.state.tracks.map((track, i) => {
<div className="track" key={i}>
<a href={track.permalink_url} target="_blank">
<img src={track.artwork_url} width="150" height="150" />
<p className="title">{track.title}</p>
</a>
</div>
});

<div className="tracks">
{items}
</div>
</InfiniteScroll>
);
}
return (
<InfiniteScroll
pageStart={0}
loadMore={this.loadItems.bind(this)}
hasMore={this.state.hasMoreItems}
loader={loader}>
<div className="tracks">
{items}
</div>
</InfiniteScroll>
);
}
};

ReactDOM.render(
<App />
, document.getElementById('root'));
<App />
, document.getElementById('root'));