import { state, property } from "lit/decorators.js"; import { msg, localized } from "@lit/localize"; import sortBy from "lodash/fp/sortBy"; import type { AuthState } from "../utils/AuthService"; import LiteElement, { html } from "../utils/LiteElement"; import { OrgData } from "../types/org"; const sortByName = sortBy("name"); /** * @event success */ @localized() export class InviteForm extends LiteElement { @property({ type: Object }) authState?: AuthState; @property({ type: Array }) orgs: OrgData[] = []; @property({ type: Object }) defaultOrg: OrgData | null = null; @state() private isSubmitting: boolean = false; @state() private serverError?: string; @state() private selectedOrgId?: string; willUpdate(changedProperties: Map) { if ( changedProperties.has("defaultOrg") && this.defaultOrg && !this.selectedOrgId ) { this.selectedOrgId = this.defaultOrg.id; } } render() { let formError; if (this.serverError) { formError = html`
${this.serverError}
`; } const sortedOrgs = sortByName(this.orgs) as any as OrgData[]; return html`
{ this.selectedOrgId = (e.target as HTMLSelectElement).value; }} ?disabled=${sortedOrgs.length === 1} required > ${sortedOrgs.map( (org) => html` ${org.name} ` )}
${formError}
${msg("Invite")}
`; } async onSubmit(event: SubmitEvent) { event.preventDefault(); if (!this.authState || !this.selectedOrgId) return; const formEl = event.target as HTMLFormElement; if (!(await this.checkFormValidity(formEl))) return; this.serverError = undefined; this.isSubmitting = true; const formData = new FormData(event.target as HTMLFormElement); const inviteEmail = formData.get("inviteEmail") as string; try { const data = await this.apiFetch( `/orgs/${this.selectedOrgId}/invite`, this.authState, { method: "POST", body: JSON.stringify({ email: inviteEmail, role: 10, }), } ); this.dispatchEvent( new CustomEvent("success", { detail: { inviteEmail, isExistingUser: data.invited === "existing_user", }, }) ); } catch (e: any) { if (e?.isApiError) { this.serverError = e?.message; } else { this.serverError = msg("Something unexpected went wrong"); } } this.isSubmitting = false; } async checkFormValidity(formEl: HTMLFormElement) { await this.updateComplete; return !formEl.querySelector("[data-invalid]"); } }