From f30b398fea2a0ebef64efd578f3d08735becd3c4 Mon Sep 17 00:00:00 2001 From: sua yoo Date: Mon, 21 Feb 2022 11:37:15 -0800 Subject: [PATCH] Deactivate crawl templates in UI (#145) wip #144 --- .../pages/archive/crawl-templates-detail.ts | 277 +++++++++++++++--- .../src/pages/archive/crawl-templates-list.ts | 246 ++++++++++------ frontend/src/pages/archive/crawls-list.ts | 2 +- frontend/src/pages/archive/types.ts | 7 +- 4 files changed, 407 insertions(+), 125 deletions(-) diff --git a/frontend/src/pages/archive/crawl-templates-detail.ts b/frontend/src/pages/archive/crawl-templates-detail.ts index e63612ff..0f6b2591 100644 --- a/frontend/src/pages/archive/crawl-templates-detail.ts +++ b/frontend/src/pages/archive/crawl-templates-detail.ts @@ -1,10 +1,10 @@ +import type { HTMLTemplateResult } from "lit"; import { state, property } from "lit/decorators.js"; import { msg, localized, str } from "@lit/localize"; import cronstrue from "cronstrue"; // TODO localize import type { AuthState } from "../../utils/AuthService"; import LiteElement, { html } from "../../utils/LiteElement"; -import { getLocaleTimeZone } from "../../utils/localization"; import type { CrawlTemplate } from "./types"; import { getUTCSchedule } from "./utils"; import "../../components/crawl-scheduler"; @@ -72,10 +72,16 @@ export class CrawlTemplatesDetail extends LiteElement { -

- ${this.crawlTemplate?.name || - html``} -

+ ${this.renderInactiveNotice()} + +
+

+ ${this.crawlTemplate?.name || + html``} +

+ +
${this.renderMenu()}
+
${this.renderCurrentlyRunningNotice()} @@ -228,39 +234,43 @@ export class CrawlTemplatesDetail extends LiteElement { : this.renderReadOnlySchedule()} -
- ${this.crawlTemplate - ? html` - { - const hasChanges = - this.isEditing && this.editedSchedule; - if ( - !hasChanges || - window.confirm( - msg( - "You have unsaved schedule changes. Are you sure?" - ) - ) - ) { - this.navLink(e); - this.editedSchedule = ""; - } else { - e.preventDefault(); - } - }} - > - ${this.isEditing ? msg("Cancel") : msg("Edit")} - - ` - : html``} -
+ ${this.crawlTemplate?.inactive + ? "" + : html` +
+ ${this.crawlTemplate + ? html` + { + const hasChanges = + this.isEditing && this.editedSchedule; + if ( + !hasChanges || + window.confirm( + msg( + "You have unsaved schedule changes. Are you sure?" + ) + ) + ) { + this.navLink(e); + this.editedSchedule = ""; + } else { + e.preventDefault(); + } + }} + > + ${this.isEditing ? msg("Cancel") : msg("Edit")} + + ` + : html``} +
+ `} @@ -293,6 +303,8 @@ export class CrawlTemplatesDetail extends LiteElement { @click=${this.navLink} >${msg("View crawl")}` + : this.crawlTemplate.inactive + ? "" : html`${msg("None")} - + ${this.renderCardFooter(t)} ` )} @@ -327,6 +246,137 @@ export class CrawlTemplatesList extends LiteElement { `; } + private renderCardMenu(t: CrawlTemplate) { + const menuItems: HTMLTemplateResult[] = [ + html` + + `, + ]; + + if (!t.inactive) { + menuItems.unshift(html` + + `); + } + + if (t.crawlCount && !t.inactive) { + menuItems.push(html` + + `); + } + + if (!t.crawlCount) { + menuItems.push(html` + + `); + } + + return html` + e.stopPropagation()}> + + + + + `; + } + + private renderCardFooter(t: CrawlTemplate) { + if (t.inactive) { + return ""; + } + + return html` +
+ +
+ `; + } + /** * Fetch crawl templates and record running crawls * associated with the crawl templates @@ -374,6 +424,34 @@ export class CrawlTemplatesList extends LiteElement { }); } + private async deactivateTemplate(template: CrawlTemplate): Promise { + try { + await this.apiFetch( + `/archives/${this.archiveId}/crawlconfigs/${template.id}`, + this.authState!, + { + method: "DELETE", + } + ); + + this.notify({ + message: msg(html`Deactivated ${template.name}.`), + type: "success", + icon: "check2-circle", + }); + + this.crawlTemplates = this.crawlTemplates!.filter( + (t) => t.id !== template.id + ); + } catch { + this.notify({ + message: msg("Sorry, couldn't deactivate crawl template at this time."), + type: "danger", + icon: "exclamation-octagon", + }); + } + } + private async deleteTemplate(template: CrawlTemplate): Promise { try { await this.apiFetch( diff --git a/frontend/src/pages/archive/crawls-list.ts b/frontend/src/pages/archive/crawls-list.ts index 2e74a9f5..61ab6077 100644 --- a/frontend/src/pages/archive/crawls-list.ts +++ b/frontend/src/pages/archive/crawls-list.ts @@ -370,7 +370,7 @@ export class CrawlsList extends LiteElement { diff --git a/frontend/src/pages/archive/types.ts b/frontend/src/pages/archive/types.ts index 88305034..107657d3 100644 --- a/frontend/src/pages/archive/types.ts +++ b/frontend/src/pages/archive/types.ts @@ -11,7 +11,7 @@ export type Crawl = { state: string; // "running" | "complete" | "failed" | "partial_complete" scale: number; stats: { done: string; found: string } | null; - resources?: { name: string; path: string, hash: string; size: number }[]; + resources?: { name: string; path: string; hash: string; size: number }[]; fileCount?: number; fileSize?: number; completions?: number; @@ -32,11 +32,14 @@ export type CrawlTemplate = { name: string; schedule: string; userid: string; - userName?: string; + userName: string | null; created: string; crawlCount: number; lastCrawlId: string; lastCrawlTime: string; currCrawlId: string; + newId: string | null; + oldId: string | null; + inactive: boolean; config: CrawlConfig; };