From d4c149a4fb66ddc45207cc80ef19f6d2b4170eb5 Mon Sep 17 00:00:00 2001 From: jaasen-livefront Date: Wed, 11 Sep 2024 17:43:32 -0700 Subject: [PATCH] create base send details component --- .../base-send-details.component.ts | 112 ++++++++++++++ .../send-details/send-details.component.html | 34 +++++ .../send-details/send-details.component.ts | 64 ++++++++ .../send-file-details.component.html | 0 .../send-file-details.component.ts | 0 .../send-text-details.component.html | 47 ++---- .../send-text-details.component.ts | 137 +----------------- .../components/send-form.component.html | 6 +- .../components/send-form.component.ts | 4 +- .../src/send-form/send-form-container.ts | 4 +- 10 files changed, 230 insertions(+), 178 deletions(-) create mode 100644 libs/tools/send/send-ui/src/send-form/components/send-details/base-send-details.component.ts create mode 100644 libs/tools/send/send-ui/src/send-form/components/send-details/send-details.component.html create mode 100644 libs/tools/send/send-ui/src/send-form/components/send-details/send-details.component.ts create mode 100644 libs/tools/send/send-ui/src/send-form/components/send-details/send-file-details.component.html create mode 100644 libs/tools/send/send-ui/src/send-form/components/send-details/send-file-details.component.ts diff --git a/libs/tools/send/send-ui/src/send-form/components/send-details/base-send-details.component.ts b/libs/tools/send/send-ui/src/send-form/components/send-details/base-send-details.component.ts new file mode 100644 index 00000000000..52ea4c90f3d --- /dev/null +++ b/libs/tools/send/send-ui/src/send-form/components/send-details/base-send-details.component.ts @@ -0,0 +1,112 @@ +import { DatePipe } from "@angular/common"; +import { Component, Input, OnInit } from "@angular/core"; +import { takeUntilDestroyed } from "@angular/core/rxjs-interop"; +import { FormBuilder, FormGroup, FormControl, Validators } from "@angular/forms"; + +import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; +import { SendView } from "@bitwarden/common/tools/send/models/view/send.view"; + +import { SendFormConfig } from "../../abstractions/send-form-config.service"; +import { SendFormContainer } from "../../send-form-container"; + +export interface BaseSendDetailsForm { + name: FormControl; + selectedDeletionDatePreset: FormControl; +} + +// Value = hours +export enum DatePreset { + OneHour = 1, + OneDay = 24, + TwoDays = 48, + ThreeDays = 72, + SevenDays = 168, + FourteenDays = 336, + ThirtyDays = 720, +} + +export interface DatePresetSelectOption { + name: string; + value: DatePreset | string; +} + +@Component({ + selector: "base-send-details-behavior", + template: "", +}) +export class BaseSendDetailsComponent implements OnInit { + @Input() config: SendFormConfig; + @Input() originalSendView: SendView; + + sendDetailsForm: FormGroup; + customDeletionDateOption: DatePresetSelectOption | null = null; + datePresetOptions: DatePresetSelectOption[] = []; + + constructor( + protected sendFormContainer: SendFormContainer, + protected formBuilder: FormBuilder, + protected i18nService: I18nService, + protected datePipe: DatePipe, + ) { + // Initialize the form + this.sendDetailsForm = this.formBuilder.group({ + name: new FormControl("", Validators.required), + selectedDeletionDatePreset: new FormControl(DatePreset.SevenDays || "", Validators.required), + }) as FormGroup; + + this.sendDetailsForm.valueChanges.pipe(takeUntilDestroyed()).subscribe((value) => { + this.sendFormContainer.patchSend((send) => { + return Object.assign(send, { + name: value.name, + deletionDate: new Date(this.formattedDeletionDate), + expirationDate: new Date(this.formattedDeletionDate), + } as SendView); + }); + }); + } + + async ngOnInit() { + this.setupDeletionDatePresets(); + + if (this.originalSendView) { + this.sendDetailsForm.patchValue({ + name: this.originalSendView.name, + selectedDeletionDatePreset: this.originalSendView.deletionDate.toString(), + }); + + if (this.originalSendView.deletionDate) { + this.customDeletionDateOption = { + name: this.datePipe.transform(this.originalSendView.deletionDate, "MM/dd/yyyy, hh:mm a"), + value: this.originalSendView.deletionDate.toString(), + }; + this.datePresetOptions.unshift(this.customDeletionDateOption); + } + } + } + + setupDeletionDatePresets() { + const defaultSelections: DatePresetSelectOption[] = [ + { name: this.i18nService.t("oneHour"), value: DatePreset.OneHour }, + { name: this.i18nService.t("oneDay"), value: DatePreset.OneDay }, + { name: this.i18nService.t("days", "2"), value: DatePreset.TwoDays }, + { name: this.i18nService.t("days", "3"), value: DatePreset.ThreeDays }, + { name: this.i18nService.t("days", "7"), value: DatePreset.SevenDays }, + { name: this.i18nService.t("days", "14"), value: DatePreset.FourteenDays }, + { name: this.i18nService.t("days", "30"), value: DatePreset.ThirtyDays }, + ]; + + this.datePresetOptions = defaultSelections; + } + + get formattedDeletionDate(): string { + const now = new Date(); + const selectedValue = this.sendDetailsForm.controls.selectedDeletionDatePreset.value; + + if (typeof selectedValue === "string") { + return selectedValue; + } + + const milliseconds = now.setTime(now.getTime() + (selectedValue as number) * 60 * 60 * 1000); + return new Date(milliseconds).toString(); + } +} diff --git a/libs/tools/send/send-ui/src/send-form/components/send-details/send-details.component.html b/libs/tools/send/send-ui/src/send-form/components/send-details/send-details.component.html new file mode 100644 index 00000000000..27b6833bc6e --- /dev/null +++ b/libs/tools/send/send-ui/src/send-form/components/send-details/send-details.component.html @@ -0,0 +1,34 @@ + + +

{{ "sendDetails" | i18n }}

+
+ + + + {{ "name" | i18n }} + + + + + + + {{ "deletionDate" | i18n }} + + + + {{ "deletionDateDescV2" | i18n }} + + +
diff --git a/libs/tools/send/send-ui/src/send-form/components/send-details/send-details.component.ts b/libs/tools/send/send-ui/src/send-form/components/send-details/send-details.component.ts new file mode 100644 index 00000000000..4abb7e2e867 --- /dev/null +++ b/libs/tools/send/send-ui/src/send-form/components/send-details/send-details.component.ts @@ -0,0 +1,64 @@ +import { CommonModule, DatePipe } from "@angular/common"; +import { Component, Input, OnInit } from "@angular/core"; +import { FormBuilder, ReactiveFormsModule } from "@angular/forms"; + +import { JslibModule } from "@bitwarden/angular/jslib.module"; +import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; +import { SendType } from "@bitwarden/common/tools/send/enums/send-type"; +import { SendView } from "@bitwarden/common/tools/send/models/view/send.view"; +import { + SectionComponent, + SectionHeaderComponent, + TypographyModule, + CardComponent, + FormFieldModule, + IconButtonModule, + CheckboxModule, + SelectModule, +} from "@bitwarden/components"; + +import { SendFormConfig } from "../../abstractions/send-form-config.service"; +import { SendFormContainer } from "../../send-form-container"; + +import { BaseSendDetailsComponent } from "./base-send-details.component"; +import { SendTextDetailsComponent } from "./send-text-details.component"; + +@Component({ + selector: "tools-send-details", + templateUrl: "./send-details.component.html", + standalone: true, + imports: [ + SectionComponent, + SectionHeaderComponent, + TypographyModule, + JslibModule, + CardComponent, + FormFieldModule, + ReactiveFormsModule, + SendTextDetailsComponent, + IconButtonModule, + CheckboxModule, + CommonModule, + SelectModule, + ], +}) +export class SendDetailsComponent extends BaseSendDetailsComponent implements OnInit { + @Input() config: SendFormConfig; + @Input() originalSendView: SendView; + + FileSendType = SendType.File; + TextSendType = SendType.Text; + + constructor( + protected sendFormContainer: SendFormContainer, + protected formBuilder: FormBuilder, + protected i18nService: I18nService, + protected datePipe: DatePipe, + ) { + super(sendFormContainer, formBuilder, i18nService, datePipe); + } + + async ngOnInit() { + await super.ngOnInit(); + } +} diff --git a/libs/tools/send/send-ui/src/send-form/components/send-details/send-file-details.component.html b/libs/tools/send/send-ui/src/send-form/components/send-details/send-file-details.component.html new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libs/tools/send/send-ui/src/send-form/components/send-details/send-file-details.component.ts b/libs/tools/send/send-ui/src/send-form/components/send-details/send-file-details.component.ts new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libs/tools/send/send-ui/src/send-form/components/send-details/send-text-details.component.html b/libs/tools/send/send-ui/src/send-form/components/send-details/send-text-details.component.html index 7167208fdc9..a774eb398b3 100644 --- a/libs/tools/send/send-ui/src/send-form/components/send-details/send-text-details.component.html +++ b/libs/tools/send/send-ui/src/send-form/components/send-details/send-text-details.component.html @@ -1,38 +1,9 @@ - - -

{{ "sendDetails" | i18n }}

-
- - - - {{ "name" | i18n }} - - - - - {{ "sendTypeTextToShare" | i18n }} - - {{ "sendTextDesc" | i18n }} - - - - {{ "hideTextByDefault" | i18n }} - - - - {{ "deletionDate" | i18n }} - - - - {{ "deletionDateDescV2" | i18n }} - - -
+ + {{ "sendTypeTextToShare" | i18n }} + + {{ "sendTextDesc" | i18n }} + + + + {{ "hideTextByDefault" | i18n }} + diff --git a/libs/tools/send/send-ui/src/send-form/components/send-details/send-text-details.component.ts b/libs/tools/send/send-ui/src/send-form/components/send-details/send-text-details.component.ts index 573f3f44629..a843b99655f 100644 --- a/libs/tools/send/send-ui/src/send-form/components/send-details/send-text-details.component.ts +++ b/libs/tools/send/send-ui/src/send-form/components/send-details/send-text-details.component.ts @@ -1,142 +1,15 @@ -import { CommonModule, DatePipe } from "@angular/common"; -import { Component, Input, OnInit } from "@angular/core"; -import { takeUntilDestroyed } from "@angular/core/rxjs-interop"; -import { FormBuilder, ReactiveFormsModule, Validators } from "@angular/forms"; +import { Component, Input } from "@angular/core"; -import { JslibModule } from "@bitwarden/angular/jslib.module"; -import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { SendView } from "@bitwarden/common/tools/send/models/view/send.view"; -import { - CardComponent, - CheckboxModule, - FormFieldModule, - IconButtonModule, - SectionComponent, - SectionHeaderComponent, - SelectModule, - TypographyModule, -} from "@bitwarden/components"; import { SendFormConfig } from "../../abstractions/send-form-config.service"; -import { SendFormContainer } from "../../send-form-container"; - -// Value = hours -enum DatePreset { - OneHour = 1, - OneDay = 24, - TwoDays = 48, - ThreeDays = 72, - SevenDays = 168, - FourteenDays = 336, - ThirtyDays = 720, -} - -interface DatePresetSelectOption { - name: string; - value: DatePreset | string; -} @Component({ selector: "tools-send-text-details", - templateUrl: "./send-text-details.component.html", + template: "send-text-details.component.html", standalone: true, - imports: [ - SectionComponent, - SectionHeaderComponent, - TypographyModule, - JslibModule, - CardComponent, - FormFieldModule, - ReactiveFormsModule, - IconButtonModule, - CheckboxModule, - CommonModule, - SelectModule, - ], }) -export class SendTextDetailsComponent implements OnInit { - @Input({ required: true }) - config: SendFormConfig; - - @Input() - originalSendView: SendView; - - sendTextDetailsForm = this.formBuilder.group({ - name: ["", [Validators.required]], - textToShare: [""], - hideTextByDefault: [false], - selectedDeletionDatePreset: [DatePreset.SevenDays || "", Validators.required], - }); - - customDeletionDateOption: DatePresetSelectOption | null = null; - datePresetOptions: DatePresetSelectOption[] = []; - - constructor( - private sendFormContainer: SendFormContainer, - private formBuilder: FormBuilder, - private i18nService: I18nService, - protected datePipe: DatePipe, - ) { - this.sendFormContainer.registerChildForm("sendTextDetailsForm", this.sendTextDetailsForm); - - this.sendTextDetailsForm.valueChanges.pipe(takeUntilDestroyed()).subscribe((value) => { - this.sendFormContainer.patchSend((send) => { - return Object.assign(send, { - name: value.name, - text: { text: value.textToShare, hidden: value.hideTextByDefault }, - deletionDate: new Date(this.formattedDeletionDate), - expirationDate: new Date(this.formattedDeletionDate), - } as SendView); - }); - }); - } - - async ngOnInit() { - this.setupDeletionDatePresets(); - - if (this.originalSendView) { - this.sendTextDetailsForm.patchValue({ - name: this.originalSendView.name, - textToShare: this.originalSendView.text.text, - hideTextByDefault: this.originalSendView.text.hidden, - selectedDeletionDatePreset: this.originalSendView.deletionDate.toString(), - }); - - // If existing deletion date exists, calculate it once and store it - if (this.originalSendView.deletionDate) { - this.customDeletionDateOption = { - name: this.datePipe.transform(this.originalSendView.deletionDate, "MM/dd/yyyy, hh:mm a"), - value: this.originalSendView.deletionDate.toString(), - }; - this.datePresetOptions.unshift(this.customDeletionDateOption); - } - } - } - - setupDeletionDatePresets() { - const defaultSelections: DatePresetSelectOption[] = [ - { name: this.i18nService.t("oneHour"), value: DatePreset.OneHour }, - { name: this.i18nService.t("oneDay"), value: DatePreset.OneDay }, - { name: this.i18nService.t("days", "2"), value: DatePreset.TwoDays }, - { name: this.i18nService.t("days", "3"), value: DatePreset.ThreeDays }, - { name: this.i18nService.t("days", "7"), value: DatePreset.SevenDays }, - { name: this.i18nService.t("days", "14"), value: DatePreset.FourteenDays }, - { name: this.i18nService.t("days", "30"), value: DatePreset.ThirtyDays }, - ]; - - this.datePresetOptions = defaultSelections; - } - - get formattedDeletionDate(): string { - const now = new Date(); - const selectedValue = this.sendTextDetailsForm.controls.selectedDeletionDatePreset.value; - - // If existing deletion date is selected, return it as is - if (typeof selectedValue === "string") { - return selectedValue; - } - - const milliseconds = now.setTime(now.getTime() + (selectedValue as number) * 60 * 60 * 1000); - return new Date(milliseconds).toString(); - } +export class SendTextDetailsComponent { + @Input() config: SendFormConfig; + @Input() originalSendView: SendView; } diff --git a/libs/tools/send/send-ui/src/send-form/components/send-form.component.html b/libs/tools/send/send-ui/src/send-form/components/send-form.component.html index 8aa06756d0d..ab10c53f970 100644 --- a/libs/tools/send/send-ui/src/send-form/components/send-form.component.html +++ b/libs/tools/send/send-ui/src/send-form/components/send-form.component.html @@ -1,10 +1,8 @@
- - + >
diff --git a/libs/tools/send/send-ui/src/send-form/components/send-form.component.ts b/libs/tools/send/send-ui/src/send-form/components/send-form.component.ts index fed4b005062..2cb1907d921 100644 --- a/libs/tools/send/send-ui/src/send-form/components/send-form.component.ts +++ b/libs/tools/send/send-ui/src/send-form/components/send-form.component.ts @@ -35,7 +35,7 @@ import { SendFormConfig } from "../abstractions/send-form-config.service"; import { SendFormService } from "../abstractions/send-form.service"; import { SendForm, SendFormContainer } from "../send-form-container"; -import { SendTextDetailsComponent } from "./send-details/send-text-details.component"; +import { SendDetailsComponent } from "./send-details/send-details.component"; @Component({ selector: "tools-send-form", @@ -57,7 +57,7 @@ import { SendTextDetailsComponent } from "./send-details/send-text-details.compo ReactiveFormsModule, SelectModule, NgIf, - SendTextDetailsComponent, + SendDetailsComponent, ], }) export class SendFormComponent implements AfterViewInit, OnInit, OnChanges, SendFormContainer { diff --git a/libs/tools/send/send-ui/src/send-form/send-form-container.ts b/libs/tools/send/send-ui/src/send-form/send-form-container.ts index 75011f7c7e0..7ddd6c91177 100644 --- a/libs/tools/send/send-ui/src/send-form/send-form-container.ts +++ b/libs/tools/send/send-ui/src/send-form/send-form-container.ts @@ -1,13 +1,13 @@ import { SendView } from "@bitwarden/common/tools/send/models/view/send.view"; import { SendFormConfig } from "./abstractions/send-form-config.service"; -import { SendTextDetailsComponent } from "./components/send-details/send-text-details.component"; +import { SendDetailsComponent } from "./components/send-details/send-details.component"; /** * The complete form for a send. Includes all the sub-forms from their respective section components. * TODO: Add additional form sections as they are implemented. */ export type SendForm = { - sendTextDetailsForm?: SendTextDetailsComponent["sendTextDetailsForm"]; + sendDetailsForm?: SendDetailsComponent["sendDetailsForm"]; }; /**