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

Fix loading bugs #131

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion app/components/article-list.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{else}}
<div class="article-preview">No articles are here... yet.</div>
{{/each}}
{{#if this.articles}}
{{#if this.showPagination}}
<Pagination @total={{this.articles.meta.articlesCount}} @perPage={{10}} @current={{@page}}
@existingParams={{hash author=@feed tag=@tag}} />
{{/if}}
Expand Down
22 changes: 16 additions & 6 deletions app/components/article-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { tracked } from '@glimmer/tracking';
import { task } from 'ember-concurrency-decorators';
import { inject as service } from '@ember/service';

const NUMBER_OF_ARTICLES_PER_PAGE = 10;

export default class ArticleListComponent extends Component {
@service session;
@service store;
@service router;
@tracked articles = [];

constructor() {
Expand All @@ -15,16 +18,23 @@ export default class ArticleListComponent extends Component {

@task({ restartable: true })
*loadArticles() {
let NUMBER_OF_ARTICLES = 10;
let offset = (parseInt(this.args.page, 10) - 1) * NUMBER_OF_ARTICLES;
let offset = (parseInt(this.args.page, 10) - 1) * NUMBER_OF_ARTICLES_PER_PAGE;
if (this.args.feed === 'your') {
this.articles = yield this.session.user.fetchFeed(this.args.page);
if (this.session.isLoggedIn) {
this.articles = yield this.session.user.fetchFeed(this.args.page);
} else {
this.router.transitionTo('index', { queryParams: { feed: null } });
}
} else {
this.articles = yield this.store.query('article', {
limit: NUMBER_OF_ARTICLES,
offset,
tag: this.args.tag,
limit: NUMBER_OF_ARTICLES_PER_PAGE,
offset: offset,
tag: this.args.tag || undefined,
});
}
}

get showPagination() {
return this.articles && this.articles.length > NUMBER_OF_ARTICLES_PER_PAGE;
}
}
1 change: 0 additions & 1 deletion app/components/favorite-article.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
article favorited: {{@article.favorited}}
<button class="btn btn-sm btn{{unless @article.favorited '-outline'}}-primary" data-test-favorite-article-button={{if
@article.favorited "favorited" "unfavorited" }} {{on "click" (fn this.favoriteArticle (if
@article.favorited "unfavorite" "favorite" ))}} type="button" ...attributes>
Expand Down
1 change: 1 addition & 0 deletions app/serializers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default class ArticleSerializer extends RESTSerializer.extend(EmbeddedRec

attrs = {
author: { embedded: 'always' },
comments: { embedded: 'always' },
};

extractMeta(store, typeClass, payload) {
Expand Down
1 change: 1 addition & 0 deletions app/serializers/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ApplicationSerializer from './application';

export default class UserSerializer extends ApplicationSerializer {
primaryKey = 'email';
attrs = {
token: {
serialize: false,
Expand Down
4 changes: 2 additions & 2 deletions app/services/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class SessionService extends Service {
users: [userPayload.user],
});
this.setToken(userPayload.user.token);
this.user = this.store.peekRecord('user', userPayload.user.id);
this.user = this.store.peekRecord('user', userPayload.user.email);
return this.user;
}
}
Expand All @@ -93,7 +93,7 @@ export default class SessionService extends Service {
this.store.pushPayload({
users: [user],
});
this.user = this.store.peekRecord('user', user.id);
this.user = this.store.peekRecord('user', user.email);
return this.user;
}

Expand Down