import { html } from 'lit-html'; import { customElement, instance, property } from '@lit-html-free/core'; import { ArticleService } from 'src/resources/services/articleservice'; import { SharedState } from 'src/resources/state/sharedstate'; 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-preview') export default class extends HTMLElement { public articleService: ArticleService; public sharedState: SharedState; @property() public article: any; constructor() { super(); this.articleService = instance(ArticleService); this.sharedState = instance(SharedState); } public async onToggleFavorited() { if (this.sharedState.isAuthenticated) { this.article.favorited = !this.article.favorited; if (this.article.favorited) { this.article.favoritesCount++; await this.articleService.favorite(this.article.slug); } else { this.article.favoritesCount--; await this.articleService.unfavorite(this.article.slug); } this.render(); } else { location.hash = 'login'; } } public render() { return html`
`; } }