import {OOElement} from '../oo-element' import {html, render} from '../../lib/html' import getUser from '../../lib/oo-api-get-user' import isSuccess from '../../lib/is-api-success' import toMap from '../../lib/extensions-to-map' import empty from '../oo-empty' import userName from '../_atoms/oo-atoms-user-name' import markdown from '../oo-markdown' import define from '../../lib/define' import weakMap from '../../lib/weak-map' define('oo-empty', empty) define('oo-atoms-user-name', userName) define('oo-markdown', markdown) const ATTR = { DATA_IAM: 'data-iam' } const EVENT = { USER_UPDATED: new Event('userupdated') } const iam = weakMap() const bio = weakMap() const found = weakMap() export default class extends OOElement { static get observedAttributes() { return [ATTR.DATA_IAM] } attributeChangedCallback(attr, prev, next) { if (prev === next || !next) { return } iam.set(this, next) this.placeholder() this.fetchUserData() } connectedCallback() { super.connectedCallback(false) } render() { const {i, f, b} = { i: iam.get(this), f: found.get(this), b: bio.get(this) } if (f === false) { return html` ` } return html`
${b}
` } placeholder() { const template = html`
` render(template, this) } async fetchUserData() { const res = await getUser(iam.get(this)) if (isSuccess(res.status) && Array.isArray(res.response)) { const [item] = res.response const ext = toMap(item) found.set(this, true) bio.set(this, ext.get('bio')) } else { found.set(this, false) bio.set(this, '') } this.update() this.dispatchEvent(EVENT.USER_UPDATED) } }