Add helper for dispatching notify events (#92)
This commit is contained in:
		
							parent
							
								
									22942527e9
								
							
						
					
					
						commit
						cb5cf55c69
					
				| @ -6,6 +6,7 @@ import type { SlDialog } from "@shoelace-style/shoelace"; | |||||||
| import "tailwindcss/tailwind.css"; | import "tailwindcss/tailwind.css"; | ||||||
| 
 | 
 | ||||||
| import type { ArchiveTab } from "./pages/archive"; | import type { ArchiveTab } from "./pages/archive"; | ||||||
|  | import type { NotifyEvent } from "./utils/LiteElement"; | ||||||
| import LiteElement, { html } from "./utils/LiteElement"; | import LiteElement, { html } from "./utils/LiteElement"; | ||||||
| import APIRouter from "./utils/APIRouter"; | import APIRouter from "./utils/APIRouter"; | ||||||
| import AuthService from "./utils/AuthService"; | import AuthService from "./utils/AuthService"; | ||||||
| @ -478,16 +479,7 @@ export class App extends LiteElement { | |||||||
|     }; |     }; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   onNotify( |   onNotify(event: NotifyEvent) { | ||||||
|     event: CustomEvent<{ |  | ||||||
|       title?: string; |  | ||||||
|       /** Can contain HTML */ |  | ||||||
|       message?: string; |  | ||||||
|       type?: "success" | "warning" | "danger" | "primary"; |  | ||||||
|       icon?: string; |  | ||||||
|       duration?: number; |  | ||||||
|     }> |  | ||||||
|   ) { |  | ||||||
|     event.stopPropagation(); |     event.stopPropagation(); | ||||||
| 
 | 
 | ||||||
|     const { |     const { | ||||||
|  | |||||||
| @ -48,16 +48,12 @@ export class AcceptInvite extends LiteElement { | |||||||
|     if (this.isLoggedIn) { |     if (this.isLoggedIn) { | ||||||
|       this.getInviteInfo(); |       this.getInviteInfo(); | ||||||
|     } else { |     } else { | ||||||
|       this.dispatchEvent( |       this.notify({ | ||||||
|         new CustomEvent("notify", { |  | ||||||
|           detail: { |  | ||||||
|         message: msg("Log in to continue."), |         message: msg("Log in to continue."), | ||||||
|         type: "success", |         type: "success", | ||||||
|         icon: "check2-circle", |         icon: "check2-circle", | ||||||
|         duration: 10000, |         duration: 10000, | ||||||
|           }, |       }); | ||||||
|         }) |  | ||||||
|       ); |  | ||||||
| 
 | 
 | ||||||
|       this.navTo( |       this.navTo( | ||||||
|         `/log-in?redirectUrl=${encodeURIComponent( |         `/log-in?redirectUrl=${encodeURIComponent( | ||||||
| @ -160,15 +156,11 @@ export class AcceptInvite extends LiteElement { | |||||||
|         } |         } | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|       this.dispatchEvent( |       this.notify({ | ||||||
|         new CustomEvent("notify", { |  | ||||||
|           detail: { |  | ||||||
|         message: msg(str`You've joined ${this.inviteInfo.archiveName}.`), |         message: msg(str`You've joined ${this.inviteInfo.archiveName}.`), | ||||||
|         type: "success", |         type: "success", | ||||||
|         icon: "check2-circle", |         icon: "check2-circle", | ||||||
|           }, |       }); | ||||||
|         }) |  | ||||||
|       ); |  | ||||||
| 
 | 
 | ||||||
|       this.navTo(DASHBOARD_ROUTE); |       this.navTo(DASHBOARD_ROUTE); | ||||||
|     } catch (err: any) { |     } catch (err: any) { | ||||||
| @ -181,17 +173,13 @@ export class AcceptInvite extends LiteElement { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   private onDecline() { |   private onDecline() { | ||||||
|     this.dispatchEvent( |     this.notify({ | ||||||
|       new CustomEvent("notify", { |  | ||||||
|         detail: { |  | ||||||
|       message: msg( |       message: msg( | ||||||
|         str`You've declined to join ${this.inviteInfo.archiveName}.` |         str`You've declined to join ${this.inviteInfo.archiveName}.` | ||||||
|       ), |       ), | ||||||
|       type: "info", |       type: "info", | ||||||
|       icon: "info-circle", |       icon: "info-circle", | ||||||
|         }, |     }); | ||||||
|       }) |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     this.navTo(DASHBOARD_ROUTE); |     this.navTo(DASHBOARD_ROUTE); | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -501,10 +501,7 @@ export class CrawlTemplatesNew extends LiteElement { | |||||||
|         } |         } | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|       this.dispatchEvent( |       this.notify({ | ||||||
|         new CustomEvent("notify", { |  | ||||||
|           bubbles: true, |  | ||||||
|           detail: { |  | ||||||
|         message: data.run_now_job |         message: data.run_now_job | ||||||
|           ? msg( |           ? 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>` |               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>` | ||||||
| @ -513,9 +510,7 @@ export class CrawlTemplatesNew extends LiteElement { | |||||||
|         type: "success", |         type: "success", | ||||||
|         icon: "check2-circle", |         icon: "check2-circle", | ||||||
|         duration: 10000, |         duration: 10000, | ||||||
|           }, |       }); | ||||||
|         }) |  | ||||||
|       ); |  | ||||||
| 
 | 
 | ||||||
|       this.navTo(`/archives/${this.archiveId}/crawl-templates`); |       this.navTo(`/archives/${this.archiveId}/crawl-templates`); | ||||||
|     } catch (e: any) { |     } catch (e: any) { | ||||||
|  | |||||||
| @ -65,18 +65,13 @@ export class Verify extends LiteElement { | |||||||
|     const isLoggedIn = Boolean(this.authState); |     const isLoggedIn = Boolean(this.authState); | ||||||
|     const shouldLogOut = isLoggedIn && this.authState?.username !== data.email; |     const shouldLogOut = isLoggedIn && this.authState?.username !== data.email; | ||||||
| 
 | 
 | ||||||
|     this.dispatchEvent( |     this.notify({ | ||||||
|       new CustomEvent("notify", { |  | ||||||
|         detail: { |  | ||||||
|       title: msg("Email address verified"), |       title: msg("Email address verified"), | ||||||
|           message: |       message: isLoggedIn && !shouldLogOut ? "" : msg("Log in to continue."), | ||||||
|             isLoggedIn && !shouldLogOut ? "" : msg("Log in to continue."), |  | ||||||
|       type: "success", |       type: "success", | ||||||
|       icon: "check2-circle", |       icon: "check2-circle", | ||||||
|       duration: 10000, |       duration: 10000, | ||||||
|         }, |     }); | ||||||
|       }) |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     if (shouldLogOut) { |     if (shouldLogOut) { | ||||||
|       this.dispatchEvent(new CustomEvent("log-out")); |       this.dispatchEvent(new CustomEvent("log-out")); | ||||||
|  | |||||||
| @ -3,6 +3,23 @@ import { LitElement, html } from "lit"; | |||||||
| import type { Auth } from "../utils/AuthService"; | import type { Auth } from "../utils/AuthService"; | ||||||
| import { APIError } from "./api"; | 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 { html }; | ||||||
| 
 | 
 | ||||||
| export default class LiteElement extends LitElement { | 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( |   async apiFetch( | ||||||
|     path: string, |     path: string, | ||||||
|     auth: Auth, |     auth: Auth, | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user