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

short description in posts list #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions modules/posts/post/post.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ angular
bindings: {
post: '=',
},
controller: function ($sce, PostRepository, PostFactory, $q, $routeParams) {
controller: function ($sce, PostRepository, $q, $routeParams) {
this.commentsLoaded = false;
this.showSpinner = true;
this.loadComments = () => {
Expand All @@ -15,6 +15,10 @@ angular
this.dislikesCount = 0;

this.$onInit = () => {
setTimeout(()=> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug code should be removed

console.log();
}, 0)

$q
.resolve(PostRepository.getReactionCounters(this.post.id))
.then(result => {
Expand All @@ -26,7 +30,7 @@ angular
const delimiter = '<hr>'

if ($routeParams && !$routeParams.id && this.post.body.includes(delimiter)) {
description = PostFactory.getShortDescription(this.post, delimiter)
description = Post().getShortDescription(this.post)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = Post().getShortDescription(this.post)
description = this.post.getShortDescription()

}

this.post.body = $sce.trustAsHtml(description);
Expand Down
76 changes: 38 additions & 38 deletions modules/posts/post/post.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ angular
resolve: {
post: function (PostRepository, $route) {
post = PostRepository.getById($route.current.params.id);
return post
return post;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert any not related changes

},
}
})
Expand All @@ -19,31 +19,26 @@ angular
let tags = issue.labels.map((label) => label.name);

if (!tags.includesArray(POST_REQUIRED_TAGS)) {
throw new Error("You can't see unpublished posts.")
throw new Error('You can\'t see unpublished posts.');
}
tags = tags.filter(n => !POST_REQUIRED_TAGS.includes(n))
tags = tags.filter(n => !POST_REQUIRED_TAGS.includes(n));

return new Post(issue.number, issue.title, issue.body_html, author, tags, issue.comments, issue.created_at);
}

function createFromIssueList(issueList) {
return issueList.map(issue => createFromIssue(issue))
}

function getShortDescription(post, delimiter) {
return post.body.substring(0, post.body.indexOf(delimiter));
return issueList.map(issue => createFromIssue(issue));
}

return {
createFromIssue: createFromIssue,
createFromIssueList: createFromIssueList,
getShortDescription: getShortDescription,
}
};
})
.factory('PostRepository', function ($http, $log, PostFactory, PromiseCacheService, $q, BASE_API_URL, POST_REQUIRED_TAGS) {
return {
getReactionCounters: async (id) => {
id = Number(id)
id = Number(id);
const cacheKey = 'reactions-id-' + id;
return PromiseCacheService.getOrSet(
cacheKey,
Expand All @@ -55,7 +50,7 @@ angular
cache: true,
})
.then(response => {
$log.debug('response', response.config.url, response.data)
$log.debug('response', response.config.url, response.data);
let reactionCounters = {
'+1': 0,
'-1': 0,
Expand All @@ -65,21 +60,21 @@ angular
'hooray': 0,
'rocket': 0,
'eyes': 0,
}
};
response.data.map(data => {
const emoji = data.content;
if (reactionCounters.hasOwnProperty(emoji)) {
reactionCounters[emoji]++
reactionCounters[emoji]++;
}
})
});

const arguments = Object.entries(reactionCounters).map(property => property[1]);
return ReactionCounters.apply(null, arguments)
return ReactionCounters.apply(null, arguments);
})
)
);
},
getById: async (id) => {
id = Number(id)
id = Number(id);
const cacheKey = 'post-id-' + id;
return PromiseCacheService.getOrSet(
cacheKey,
Expand All @@ -91,31 +86,31 @@ angular
cache: true,
})
.then(response => {
$log.debug('response', response.config.url, response.data)
$log.debug('response', response.config.url, response.data);
const post = PostFactory.createFromIssue(response.data);
$log.debug('post', post)
return post
$log.debug('post', post);
return post;
}),
'5hours'
)
);
},
getByFilter: async (filter) => {
let url = `${BASE_API_URL}/issues`;
url += '?sort=created'
url += '?sort=created';
if (filter.state) {
url += '&state=' + filter.state
url += '&state=' + filter.state;
}
if (filter.limit) {
url += '&per_page=' + filter.limit
url += '&per_page=' + filter.limit;
}
if (filter.offset) {
url += '&page=' + filter.offset
url += '&page=' + filter.offset;
}
url += '&labels=' + POST_REQUIRED_TAGS.join(',')
url += '&labels=' + POST_REQUIRED_TAGS.join(',');
if (filter.tag) {
url += ',' + filter.tag
url += ',' + filter.tag;
}
const cacheKey = 'posts-filter-' + Object.values(filter).join('-')
const cacheKey = 'posts-filter-' + Object.values(filter).join('-');
return PromiseCacheService.getOrSet(
cacheKey,
() => $http
Expand All @@ -126,14 +121,14 @@ angular
cache: true,
})
.then(response => {
$log.debug('response', response.config.url, response.data)
$log.debug('response', response.config.url, response.data);
const posts = PostFactory.createFromIssueList(response.data);
$log.debug('posts', posts)
$log.debug('posts', posts);

return posts
return posts;
}),
'2hours'
)
);
}
};
})
Expand All @@ -149,7 +144,7 @@ function ReactionCounters(thumbUp, thumbDown, laugh, confused, heart, hooray, ro
hooray: hooray,
rocket: rocket,
eyes: eyes,
}
};
}

function PostRepositoryFilter(limit, offset, tag) {
Expand All @@ -158,11 +153,12 @@ function PostRepositoryFilter(limit, offset, tag) {
offset: offset,
state: 'open',
tag: tag,
}
};
}


function Post(id, title, body, author, tags, commentsCount, createdAt) {
function Post(id, title, body, author, tags, commentsCount, createdAt, getShortDescription) {

return {
id: id,
title: title,
Expand All @@ -172,13 +168,17 @@ function Post(id, title, body, author, tags, commentsCount, createdAt) {
selfUrl: id,
commentsCount: commentsCount,
createdAt: createdAt,
}
getShortDescription: function (post) {
const delimiter = '<hr>'
return post.body.substring(0, post.body.indexOf(delimiter));
}
};
Comment on lines +171 to +175
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
getShortDescription: function (post) {
const delimiter = '<hr>'
return post.body.substring(0, post.body.indexOf(delimiter));
}
};
getShortDescription: function () {
const delimiter = '<hr>'
return this.body.substring(0, this.body.indexOf(delimiter));
}
};

}

function Author(username, url, avatarUrl) {
return {
username: username,
url: url,
avatarUrl: avatarUrl,
}
};
}