import { html } from 'lit-html'; import { customElement, instance, subscribe } from '@lit-html-free/core'; import { ProfileService } from 'src/resources/services/profileservice'; import { SharedState } from 'src/resources/state/sharedstate'; import * as marked from 'marked'; import { unsafeHTML } from 'lit-html/directives/unsafe-html'; import { href } from '@lit-html-free/router'; @customElement('profile-comp') export default class extends HTMLElement { public username: any; public profile: any; public curHash: any; public curRoute = ''; public sharedState: SharedState; public profileService: ProfileService; constructor() { super(); this.profileService = instance(ProfileService); this.sharedState = instance(SharedState); } connectedCallback() { subscribe('routeChange', this, this.routechange.bind(this)); } routechange(x: any) { this.curRoute = x.options.name; this.render(); } public async activate(_x: string, params: any) { this.username = params.name; this.profile = await this.profileService.get(this.username); } public isUser() { if (this.profile && this.sharedState.currentUser) { return this.profile.username === this.sharedState.currentUser.username; } else { return false; } } public markedhtml(value: any) { let markup: string; if (value) { const renderer = new marked.Renderer(); markup = marked(value, { renderer: renderer }); } else { markup = ''; } return markup; } public onToggleFollowing() { if (this.sharedState.isAuthenticated) { this.profile.following = !this.profile.following; if (this.profile.following) { this.profileService.follow(this.profile.username); } else { this.profileService.unfollow(this.profile.username); } this.render(); } else { location.hash = 'login'; } } public render() { return html`
${this.profile ? html` ` : ''} ${!this.profile ? html`
` : ''}

${this.profile.username ? this.profile.username : 'loading..'}

${unsafeHTML(this.markedhtml(this.profile.bio))}

${!this.isUser() && this.profile ? html` ` : ''} ${this.isUser() && this.profile ? html` Edit Profile Settings ` : ''}
`; } }