Add helper for dispatching notify events (#92)

This commit is contained in:
sua yoo 2022-01-19 21:01:47 -08:00 committed by GitHub
parent 22942527e9
commit cb5cf55c69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 67 deletions

View File

@ -6,6 +6,7 @@ import type { SlDialog } from "@shoelace-style/shoelace";
import "tailwindcss/tailwind.css";
import type { ArchiveTab } from "./pages/archive";
import type { NotifyEvent } from "./utils/LiteElement";
import LiteElement, { html } from "./utils/LiteElement";
import APIRouter from "./utils/APIRouter";
import AuthService from "./utils/AuthService";
@ -478,16 +479,7 @@ export class App extends LiteElement {
};
}
onNotify(
event: CustomEvent<{
title?: string;
/** Can contain HTML */
message?: string;
type?: "success" | "warning" | "danger" | "primary";
icon?: string;
duration?: number;
}>
) {
onNotify(event: NotifyEvent) {
event.stopPropagation();
const {

View File

@ -48,16 +48,12 @@ export class AcceptInvite extends LiteElement {
if (this.isLoggedIn) {
this.getInviteInfo();
} else {
this.dispatchEvent(
new CustomEvent("notify", {
detail: {
message: msg("Log in to continue."),
type: "success",
icon: "check2-circle",
duration: 10000,
},
})
);
this.notify({
message: msg("Log in to continue."),
type: "success",
icon: "check2-circle",
duration: 10000,
});
this.navTo(
`/log-in?redirectUrl=${encodeURIComponent(
@ -160,15 +156,11 @@ export class AcceptInvite extends LiteElement {
}
);
this.dispatchEvent(
new CustomEvent("notify", {
detail: {
message: msg(str`You've joined ${this.inviteInfo.archiveName}.`),
type: "success",
icon: "check2-circle",
},
})
);
this.notify({
message: msg(str`You've joined ${this.inviteInfo.archiveName}.`),
type: "success",
icon: "check2-circle",
});
this.navTo(DASHBOARD_ROUTE);
} catch (err: any) {
@ -181,17 +173,13 @@ export class AcceptInvite extends LiteElement {
}
private onDecline() {
this.dispatchEvent(
new CustomEvent("notify", {
detail: {
message: msg(
str`You've declined to join ${this.inviteInfo.archiveName}.`
),
type: "info",
icon: "info-circle",
},
})
);
this.notify({
message: msg(
str`You've declined to join ${this.inviteInfo.archiveName}.`
),
type: "info",
icon: "info-circle",
});
this.navTo(DASHBOARD_ROUTE);
}

View File

@ -501,21 +501,16 @@ export class CrawlTemplatesNew extends LiteElement {
}
);
this.dispatchEvent(
new CustomEvent("notify", {
bubbles: true,
detail: {
message: data.run_now_job
? msg(
str`Crawl running with new template. <br /><a class="underline hover:no-underline" href="/archives/${this.archiveId}/crawls/${data.run_now_job}">View crawl</a>`
)
: msg("Crawl template created."),
type: "success",
icon: "check2-circle",
duration: 10000,
},
})
);
this.notify({
message: data.run_now_job
? msg(
str`Crawl running with new template. <br /><a class="underline hover:no-underline" href="/archives/${this.archiveId}/crawls/${data.run_now_job}">View crawl</a>`
)
: msg("Crawl template created."),
type: "success",
icon: "check2-circle",
duration: 10000,
});
this.navTo(`/archives/${this.archiveId}/crawl-templates`);
} catch (e: any) {

View File

@ -65,18 +65,13 @@ export class Verify extends LiteElement {
const isLoggedIn = Boolean(this.authState);
const shouldLogOut = isLoggedIn && this.authState?.username !== data.email;
this.dispatchEvent(
new CustomEvent("notify", {
detail: {
title: msg("Email address verified"),
message:
isLoggedIn && !shouldLogOut ? "" : msg("Log in to continue."),
type: "success",
icon: "check2-circle",
duration: 10000,
},
})
);
this.notify({
title: msg("Email address verified"),
message: isLoggedIn && !shouldLogOut ? "" : msg("Log in to continue."),
type: "success",
icon: "check2-circle",
duration: 10000,
});
if (shouldLogOut) {
this.dispatchEvent(new CustomEvent("log-out"));

View File

@ -3,6 +3,23 @@ import { LitElement, html } from "lit";
import type { Auth } from "../utils/AuthService";
import { APIError } from "./api";
export interface NotifyEvent extends CustomEvent {
detail: {
/**
* Notification message body.
* Can contain HTML.
* HTML is rendered as-is without sanitation
**/
message: string;
/** Notification title */
title?: string;
/** Shoelace icon name */
icon?: string;
type?: "success" | "warning" | "danger" | "primary" | "info";
duration?: number;
};
}
export { html };
export default class LiteElement extends LitElement {
@ -27,6 +44,18 @@ export default class LiteElement extends LitElement {
);
}
/**
* Emit global notification
*/
notify(detail: NotifyEvent["detail"]) {
this.dispatchEvent(
new CustomEvent("notify", {
bubbles: true,
detail,
})
);
}
async apiFetch(
path: string,
auth: Auth,