From 3645e3b09679d42b7bcbb49436a289a30d92e36a Mon Sep 17 00:00:00 2001 From: sua yoo Date: Wed, 19 Jan 2022 11:01:17 -0800 Subject: [PATCH] Create crawl config UX enhancements (#90) closes #87 --- frontend/src/index.ts | 14 +- frontend/src/pages/archive/crawl-templates.ts | 165 +++++++++++------- 2 files changed, 106 insertions(+), 73 deletions(-) diff --git a/frontend/src/index.ts b/frontend/src/index.ts index 2d91f664..bcd4fee6 100644 --- a/frontend/src/index.ts +++ b/frontend/src/index.ts @@ -372,6 +372,7 @@ export class App extends LiteElement { class="w-full" @navigate=${this.onNavigateTo} @need-login=${this.onNeedLogin} + @notify="${this.onNotify}" .authState=${this.authService.authState} .userInfo=${this.userInfo} archiveId=${this.viewState.params.id} @@ -480,12 +481,15 @@ export class App extends LiteElement { onNotify( event: CustomEvent<{ title?: string; + /** Can contain HTML */ message?: string; type?: "success" | "warning" | "danger" | "primary"; icon?: string; duration?: number; }> ) { + event.stopPropagation(); + const { title, message, @@ -494,12 +498,6 @@ export class App extends LiteElement { duration = 5000, } = event.detail; - const escapeHtml = (html: any) => { - const div = document.createElement("div"); - div.textContent = html; - return div.innerHTML; - }; - const alert = Object.assign(document.createElement("sl-alert"), { type: type, closable: true, @@ -513,8 +511,8 @@ export class App extends LiteElement { innerHTML: ` - ${title ? `${escapeHtml(title)}` : ""} - ${message ? `
${escapeHtml(message)}
` : ""} + ${title ? `${title}` : ""} + ${message ? `
${message}
` : ""}
`, diff --git a/frontend/src/pages/archive/crawl-templates.ts b/frontend/src/pages/archive/crawl-templates.ts index 5775f522..c43db112 100644 --- a/frontend/src/pages/archive/crawl-templates.ts +++ b/frontend/src/pages/archive/crawl-templates.ts @@ -63,9 +63,9 @@ export class CrawlTemplates extends LiteElement { @state() private scheduleTime: { hour: number; minute: number; period: "AM" | "PM" } = { - hour: 12, + hour: new Date().getHours() % 12 || 12, minute: 0, - period: "AM", + period: new Date().getHours() > 11 ? "PM" : "AM", }; @state() @@ -91,27 +91,26 @@ export class CrawlTemplates extends LiteElement { return getLocaleTimeZone(); } - private get nextScheduledCrawlMessage() { + private get formattededNextCrawlDate() { const utcSchedule = this.getUTCSchedule(); return this.scheduleInterval - ? msg(html`Next scheduled crawl: - `) + ? html`` : undefined; } @@ -136,7 +135,8 @@ export class CrawlTemplates extends LiteElement {
- ${this.renderBasicSettings()} ${this.renderPagesSettings()} + ${this.renderBasicSettings()} ${this.renderCrawlConfigSettings()} + ${this.renderScheduleSettings()}
@@ -165,7 +165,16 @@ export class CrawlTemplates extends LiteElement {

` : ""} - ${this.nextScheduledCrawlMessage} + ${this.scheduleInterval + ? html` +

+ ${msg( + html`Scheduled crawl will run + ${this.formattededNextCrawlDate}.` + )} +

+ ` + : ""}
` : ""}
@@ -198,27 +207,38 @@ export class CrawlTemplates extends LiteElement { private renderBasicSettings() { return html`
-

${msg("Basic settings")}

+

${msg("Basic settings")}

+
+
+ +
+ `; + } + + private renderScheduleSettings() { + return html` +
+

${msg("Crawl schedule")}

-
- -
(this.scheduleInterval = e.target.value)} @@ -265,7 +285,7 @@ export class CrawlTemplates extends LiteElement { )} @@ -285,37 +305,37 @@ export class CrawlTemplates extends LiteElement {
- ${this.nextScheduledCrawlMessage || msg("No crawls scheduled")} + ${this.formattededNextCrawlDate + ? msg( + html`Next scheduled crawl: ${this.formattededNextCrawlDate}` + ) + : msg("No crawls scheduled")}
-
- (this.isRunNow = e.target.checked)} - >${msg("Run immediately on save")} -
+ (this.isRunNow = e.target.checked)} + >${msg("Run immediately on save")} -
- - ${msg("minutes")} - -
+ + ${msg("minutes")} +
`; } - private renderPagesSettings() { + private renderCrawlConfigSettings() { return html`
-

${msg("Crawl configuration")}

+

${msg("Crawl configuration")}

@@ -344,11 +364,12 @@ export class CrawlTemplates extends LiteElement { @@ -511,7 +532,7 @@ export class CrawlTemplates extends LiteElement { this.isSubmitting = true; try { - await this.apiFetch( + const data = await this.apiFetch( `/archives/${this.archiveId}/crawlconfigs/`, this.authState, { @@ -520,6 +541,22 @@ export class CrawlTemplates extends LiteElement { } ); + this.dispatchEvent( + new CustomEvent("notify", { + bubbles: true, + detail: { + message: data.run_now_job + ? msg( + str`Crawl running with new template.
View crawl` + ) + : msg("Crawl template created."), + type: "success", + icon: "check2-circle", + duration: 10000, + }, + }) + ); + this.navTo(`/archives/${this.archiveId}/crawl-templates`); } catch (e: any) { if (e?.isApiError) { @@ -551,10 +588,8 @@ export class CrawlTemplates extends LiteElement { if (period === "AM") { periodOffset = -12; } - } else if (hour === 1) { - if (period === "PM") { - periodOffset = 12; - } + } else if (period === "PM") { + periodOffset = 12; } localDate.setHours(+hour + periodOffset);