import {OOElement} from '../oo-element' import {repeat} from 'lit-html/lib/repeat' import {html} from '../../lib/html' import getSign from '../../lib/oo-api-get-user-sign' import getUser from '../../lib/oo-api-get-user' import toMap from '../../lib/extensions-to-map' import weakMap from '../../lib/weak-map' import define from '../../lib/define' import userNameEditor from '../oo-user-name-editor' import patchUser from '../../lib/oo-api-patch-user' import {attach, dispatch} from '../../lib/notification' define('oo-user-name-editor', userNameEditor) interface HTMLElementEvent extends Event { target: T } const stateIam = weakMap() const stateName = weakMap() const stateBio = weakMap() const stateEMail = weakMap() const stateStripeUser = weakMap() const stateNotificationEMail = weakMap() const stateNotificationEMailServiceInformation = weakMap() const stateButton = weakMap() export default class extends OOElement { constructor() { super() this.fetchUserSign() attach() } get iam() { return stateIam.get(this) } connectedCallback() { super.connectedCallback(false) } render() { const {iam, name, bio, email, stripeUser, notificationEMail, notificationEMailServiceInformation, buttonState} = { iam: stateIam.get(this), name: stateName.get(this), bio: stateBio.get(this), email: stateEMail.get(this), stripeUser: stateStripeUser.get(this), notificationEMail: stateNotificationEMail.get(this), notificationEMailServiceInformation: stateNotificationEMailServiceInformation.get(this), buttonState: stateButton.get(this) } if (typeof iam !== 'string') { return html`` } const options = [ {name: 'uid', title: 'Your ID', template: html``}, {name: 'permalink', title: 'User name', template: html``}, {name: 'name', title: 'Display name', template: html``}, {name: 'bio', title: 'Profile', template: html``}, {name: 'stripe', title: 'Stripe', template: html` ${(() => { if (typeof stripeUser === 'string') { return html`Connected` } return html`Disconnected` })()} Connect to your Stripe`}, {name: 'notifications-email', title: 'E-Mail notification', template: html` `}, {name: 'email', title: 'E-Mail address', template: html``} ] return html`
${repeat(options, opt => { const {title, template} = opt return html`
${title}
${template}
` })}
` } async fetchUserSign() { const res = await getSign() const {response} = res if (Array.isArray(response)) { const [uid] = response stateIam.set(this, uid) this.fetchUserData(uid) } else { stateIam.delete(this) this.update() } } async fetchUserData(uid: string) { const res = await getUser(uid) const {response} = res if (Array.isArray(response)) { const [item] = response const ext = toMap(item) const email = ext.get('email') stateName.set(this, ext.get('name')) stateBio.set(this, ext.get('bio')) stateEMail.set(this, typeof email === 'string' ? email : '') stateStripeUser.set(this, ext.get('stripe_user_id')) stateNotificationEMail.set(this, ext.get('notifications_opt_email')) stateNotificationEMailServiceInformation.set(this, ext.get('notifications_opt_email_service_information')) this.update() } else { this.update() dispatch({message: 'Could not get user.', type: 'error'}) } } onChange(e: HTMLElementEvent, name: string) { const {target} = e const {value} = target switch(name) { case 'name': stateName.set(this, value) break case 'bio': stateBio.set(this, value) break case 'email': stateEMail.set(this, value) break case 'notifications_opt_email': const {checked} = target as HTMLInputElement stateNotificationEMail.set(this, checked) break case 'notifications_opt_email_service_information': const {checked: chcd} = target as HTMLInputElement stateNotificationEMailServiceInformation.set(this, chcd) break default: break } } async onSubmit(e: Event) { e.preventDefault() stateButton.set(this, 'progress') this.update() const extensions = { name: stateName.get(this), bio: stateBio.get(this), email: stateEMail.get(this), notifications_opt_email: stateNotificationEMail.get(this), notifications_opt_email_service_information: stateNotificationEMailServiceInformation.get(this) } const res = await patchUser(stateIam.get(this), extensions) const {response} = res if (Array.isArray(response)) { stateButton.set(this, 'resolved') } else { stateButton.set(this, 'rejected') dispatch({message: 'User update failed. Please try again.', type: 'error'}) } this.update() } }