import { state, property } from "lit/decorators.js";
import { msg, localized, str } from "@lit/localize";
import type { CollectionSubmitEvent } from "./collection-editor";
import type { AuthState } from "../../utils/AuthService";
import LiteElement, { html } from "../../utils/LiteElement";
import type { Collection } from "../../types/collection";
import "./collection-editor";
@localized()
export class CollectionsNew extends LiteElement {
@property({ type: Object })
authState!: AuthState;
@property({ type: String })
orgId!: string;
@property({ type: Boolean })
isCrawler?: boolean;
@state()
private collection?: Collection;
@state()
private isSubmitting = false;
@state()
private serverError?: string;
render() {
return html`${this.renderHeader()}
${msg("New Collection")}
`;
}
private renderHeader = () => html`
`;
private async onSubmit(e: CollectionSubmitEvent) {
this.isSubmitting = true;
console.log("submit", e.detail.values);
try {
const data = await this.apiFetch(
`/orgs/${this.orgId}/collections`,
this.authState!,
{
method: "POST",
body: JSON.stringify(e.detail.values),
}
);
this.notify({
message: msg(str`Successfully created "${data.name}" Collection.`),
variant: "success",
icon: "check2-circle",
duration: 8000,
});
this.navTo(`/orgs/${this.orgId}/collections`);
} catch (e: any) {
if (e?.isApiError) {
this.serverError = e?.message;
} else {
this.serverError = msg("Something unexpected went wrong");
}
console.log(this.serverError);
}
this.isSubmitting = false;
}
}
customElements.define("btrix-collections-new", CollectionsNew);