import { state, property } from "lit/decorators.js";
import { msg, localized } from "@lit/localize";
import type { AuthState } from "../utils/AuthService";
import LiteElement, { html } from "../utils/LiteElement";
/**
* @event success
*/
@localized()
export class InviteForm extends LiteElement {
@property({ type: Object })
authState?: AuthState;
@state()
private isSubmitting: boolean = false;
@state()
private serverError?: string;
render() {
let formError;
if (this.serverError) {
formError = html`
${this.serverError}
`;
}
return html`
`;
}
async onSubmit(event: SubmitEvent) {
event.preventDefault();
if (!this.authState) 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(`/users/invite`, this.authState, {
method: "POST",
body: JSON.stringify({
email: inviteEmail,
}),
});
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;
}
}