import { html } from 'lit-html'; import '../components/comment-section'; import { customElement, instance } from '@lit-html-free/core'; import { ArticleService } from 'src/resources/services/articleservice'; import { CommentService } from 'src/resources/services/commentservice'; import { UserService } from 'src/resources/services/userservice'; import { SharedState } from 'src/resources/state/sharedstate'; import { ProfileService } from 'src/resources/services/profileservice'; import { unsafeHTML } from 'lit-html/directives/unsafe-html.js'; import * as marked from 'marked'; import { href } from '@lit-html-free/router'; function format(date: Date) { return new Date(date).toLocaleDateString('en', { month: 'long', day: '2-digit', year: 'numeric' }); } @customElement('article-comp') export default class extends HTMLElement { public article: any; public comments: any = []; public myComment: any; public routeConfig: any; public slug: any; public articleService: ArticleService; public commentService: CommentService; public userService: UserService; public sharedState: SharedState; public profileService: ProfileService; constructor() { super(); this.articleService = instance(ArticleService); this.commentService = instance(CommentService); this.userService = instance(UserService); this.sharedState = instance(SharedState); this.profileService = instance(ProfileService); } public async activate(_route: string, params: any, _routeConfig: any) { this.slug = params.slug; this.article = await this.articleService.get(this.slug); this.comments = (await this.commentService.getList(this.slug)) || []; } public onToggleFavorited() { if (this.sharedState.isAuthenticated) { this.article.favorited = !this.article.favorited; if (this.article.favorited) { this.article.favoritesCount++; this.articleService.favorite(this.article.slug); } else { this.article.favoritesCount--; this.articleService.unfavorite(this.article.slug); } this.render(); } else { location.hash = 'login'; } } public onToggleFollowing() { if (this.sharedState.isAuthenticated) { this.article.author.following = !this.article.author.following; if (this.article.author.following) { this.profileService.follow(this.article.author.username); } else { this.profileService.unfollow(this.article.author.username); } this.render(); } else { location.hash = 'login'; } } public async postComment() { const comment = await this.commentService.add(this.slug, this.myComment); this.comments.push(comment); this.myComment = ''; this.render(); } get canModify() { return ( this.sharedState.currentUser && this.article.author.username === this.sharedState.currentUser.username ); } public async deleteArticle() { await this.articleService.destroy(this.article.slug); location.hash = 'home'; this.render(); } public async deleteComment(commentId: any) { await this.commentService.destroy(commentId, this.slug); this.comments = await this.commentService.getList(this.slug); this.render(); } public markedhtml(value: any) { let markup: string; if (value) { const renderer = new marked.Renderer(); markup = marked(value, { renderer: renderer }); } else { markup = ''; } return markup; } public render() { console.log('loaded'); return html`
${unsafeHTML(this.markedhtml(this.article.body))}

${articleMeta.call(this)}
${this.sharedState.isAuthenticated ? html`
` : ''} ${this.comments.map((comment: any) => { return html` { this.deleteComment(id); }} > `; })}
`; } } // same part repeated... function articleMeta() { return html`
${this.article.author.username} ${format(this.article.createdAt)}
${this.canModify ? html`  Edit Article    ` : ''} ${!this.canModify ? html`    ` : ''}
`; }