import {OOElement} from '../../oo-element' import {html} from '../../../lib/html' import weakMap from '../../../lib/weak-map' import ooMessage from '../../_atoms/oo-atoms-message' import button from '../../_atoms/oo-atoms-button' import define from '../../../lib/define' import toMap from '../../../lib/extensions-to-map' import store from '../../../lib/local-storage' import getProject from '../../../lib/oo-api-get-project' import patchProject from '../../../lib/oo-api-patch-project' import success from '../../../lib/is-api-success' define('oo-atoms-message', ooMessage) define('oo-atoms-button', button) const ATTR = { DATA_PROJECT_UID: 'data-project-uid' } const stateUid = weakMap() const stateProjectUid = weakMap() const stateapprove = weakMap() const stateOfferer = weakMap() const stateProgress = weakMap() export default class extends OOElement { static get observedAttributes() { return [ATTR.DATA_PROJECT_UID] } constructor() { super() stateUid.set(this, store.uid) } attributeChangedCallback(attr, prev, next: string) { if (prev === next && !next) { return } stateProjectUid.set(this, next) this.fetchProject(next) } connectedCallback() { super.connectedCallback(false) } render() { const uid = stateUid.get(this) const offerer = stateOfferer.get(this) const perm = stateapprove.get(this) const progress = stateProgress.get(this) const header = () => { if (perm === true) { return 'Accepted' } else if (perm === false) { return 'Rejected' } else if (uid !== offerer) { return 'Waiting for your answer' } return 'You are waiting for a reply.' } return html`
${header()}
${(() => { if (perm !== undefined || uid === offerer) { return html`` } return html`
Reject Accept
` })()}
` } async projectApprove(ans: boolean) { if (ans === undefined) { return } stateProgress.set(this, ans) this.update() const result = await patchProject({ uid: stateProjectUid.get(this), approve: Boolean(ans) }) const {status} = result if (success(status)) { stateapprove.set(this, ans) } stateProgress.delete(this) this.update() } async fetchProject(uid: string) { const api = await getProject(uid) const {response} = api if (Array.isArray(response)) { const [item] = response const mapedExtensions = toMap(item) stateapprove.set(this, mapedExtensions.get('approve')) stateOfferer.set(this, mapedExtensions.get('author')) } this.update() } }