Skip to content

Instantly share code, notes, and snippets.

@mrcnkoba
Created March 27, 2014 10:16
Show Gist options
  • Save mrcnkoba/9804391 to your computer and use it in GitHub Desktop.
Save mrcnkoba/9804391 to your computer and use it in GitHub Desktop.
module Model {
declare var window:Window;
export class Stream {
public reviews: Review[];
public proxy: Proxy;
public constructor(proxy: Proxy) {
this.reviews = new Array<Review>();
this.proxy = proxy;
}
public fetchData(lastReviewDate: string) {
var promise = this.proxy.getGlobalStream(lastReviewDate);
promise.end((res) => {
if (res.ok) {
var newReviews = res.body.StreamItems.map((item) => {
var createdAt = new Date(parseInt(item.CreatedAt.substr(6)));
return new Review(item.ReviewId,
new User(null, item.ReviewerUsername, item.ReviewerName, item.ReviewerProfilePic),
new Product(null, item.ProductName, item.ProductCoverPic, item.ProductUrl, null),
item.ReviewRating,
item.ReviewText,
createdAt);
});
this.reviews = this.reviews.concat(newReviews);
window.view.refresh();
} else {
console.log('Oh no! error ' + res.text);
}
});
}
public loadMore(page){
console.log(page);
console.log(this.reviews);
if (this.reviews !== undefined && this.reviews.length !== 0) {
console.log(this.reviews[this.reviews.length - 1]);
var lastReviewDate = this.reviews[this.reviews.length - 1].createdAt;
this.fetchData(lastReviewDate.toISOString());
}
}
}
}
/** @jsx React.DOM */
/* REVIEW(Arslan): something is wrong with comments below
* Display the user Profile
*
* Child components:
* - avatar
* - favorite apps <- TODO
* - favorite music <- TODO
* - etc
*/
var StreamPage = React.createClass({
getInitialState: function () {
return {
stream: this.props.stream,
hasMore: true
};
},
render: function() {
var currentlyLoggedUser = this.props.user; // todo: hack for materialization? how does 'map' work in JS ? Jacek's question
var streamItems = this.props.stream.reviews.map(function(s) {
return <SingleStream key={s.Id} streamItem={s} user={currentlyLoggedUser} />
});
return (
<div id="main-content">
<div id="review-container">
<AddReview/>
</div>
<div id="stream-container">
<ul>
<InfiniteScroll pageStart="0" loader={<div className="loader">Loading ...</div>} loadMore={this.props.stream.loadMore} hasMore="true">
{ streamItems }
</InfiniteScroll>
</ul>
</div>
</div>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment