import {OOElement} from '../../oo-element' import {html} from '../../../lib/html' import getUser from '../../../lib/oo-api-get-user' import toMap from '../../../lib/extensions-to-map' import getPicture from '../../../lib/get-picture' import {href} from '../../../lib/href' type Size = 'small' | 'medium' interface HTMLOptions { iam: string, name: string, picture: string, size: Size } const ATTR = { DATA_IAM: 'data-iam', DATA_SIZE: 'data-size' } const iam: WeakMap = new WeakMap() const name: WeakMap = new WeakMap() const picture: WeakMap = new WeakMap() const size: WeakMap = new WeakMap() const asValidSize = (data: string): Size => { if(data === 'small' || data === 'medium') { return data } return 'medium' } export default class extends OOElement { static get observedAttributes() { return [ATTR.DATA_IAM, ATTR.DATA_SIZE] } constructor() { super() size.set(this, asValidSize(this.getAttribute(ATTR.DATA_SIZE))) } attributeChangedCallback(attr, prev, next) { if (prev === next) { return } switch(attr) { case ATTR.DATA_IAM: if (!next) { return } iam.set(this, next) this.fetchUserData() break case ATTR.DATA_SIZE: size.set(this, asValidSize(next)) break default: break } if (this.connected) { this.update() } } connectedCallback() { super.connectedCallback(false) } render() { const options: HTMLOptions = { iam: iam.get(this), name: name.get(this), picture: picture.get(this), size: size.get(this) } const {iam: uid, name: n, picture: img, size: s} = options return html`

${n}

` } async fetchUserData() { if (!iam.get(this)) { return } const res = await getUser(iam.get(this)) if (Array.isArray(res.response)) { const [item] = res.response const ext = toMap(item) name.set(this, ext.get('name')) picture.set(this, ext.get('picture')) } else { name.delete(this) picture.delete(this) } this.update() } }