Skip to content

Commit

Permalink
Add a FormValues object and link them to the form fields so we set …
Browse files Browse the repository at this point in the history
…values more declaratively.
  • Loading branch information
sbruens committed Sep 25, 2023
1 parent 1b177cb commit 7863f5d
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/www/views/contact_view/support_form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {html, css, LitElement, TemplateResult, nothing} from 'lit';
import {createRef, Ref, ref} from 'lit/directives/ref.js';
import {live} from 'lit/directives/live.js';
import {customElement, property, state} from 'lit/decorators.js';
import '@material/mwc-button';
import '@material/mwc-linear-progress';
Expand All @@ -25,9 +26,20 @@ import '@material/mwc-textfield';
import {CardType} from '../../shared/card';
import {IssueType} from '../issue_type';
import {AppType} from '../app_type';
import {TextField} from '@material/mwc-textfield';
import {SelectedDetail} from '@material/mwc-menu/mwc-menu-base';

type FormControl = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;

/** Interface for tracking form data. */
export declare interface FormValues {
email?: string;
subject?: string;
description?: string;
source?: string;
cloudProvider?: string;
}

@customElement('support-form')
export class SupportForm extends LitElement {
static styles = [
Expand Down Expand Up @@ -65,6 +77,7 @@ export class SupportForm extends LitElement {
@property({type: String}) issueType: IssueType = IssueType.GENERAL;

private readonly formRef: Ref<HTMLFormElement> = createRef();

Check warning on line 79 in src/www/views/contact_view/support_form/index.ts

View check run for this annotation

Codecov / codecov/patch

src/www/views/contact_view/support_form/index.ts#L79

Added line #L79 was not covered by tests
@state() private formData: FormValues = {};

@state() private isFormValid = false;
@state() private isSubmitting = false;
Expand All @@ -77,7 +90,7 @@ export class SupportForm extends LitElement {

/** Resets the form. */
private reset() {

Check warning on line 92 in src/www/views/contact_view/support_form/index.ts

View check run for this annotation

Codecov / codecov/patch

src/www/views/contact_view/support_form/index.ts#L92

Added line #L92 was not covered by tests
this.formRef.value.reset();
this.formData = {};
}

Check warning on line 95 in src/www/views/contact_view/support_form/index.ts

View check run for this annotation

Codecov / codecov/patch

src/www/views/contact_view/support_form/index.ts#L95

Added line #L95 was not covered by tests
/** Cancels the form. */
Expand All @@ -97,9 +110,7 @@ export class SupportForm extends LitElement {

this.isSubmitting = true;

Check warning on line 111 in src/www/views/contact_view/support_form/index.ts

View check run for this annotation

Codecov / codecov/patch

src/www/views/contact_view/support_form/index.ts#L109-L111

Added lines #L109 - L111 were not covered by tests
// TODO: Actually send the form data using the error reporter.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const formData = new FormData(this.formRef.value);
console.log('Submitting form data...');
console.log('Submitting form data...', this.formData);

const event = new CustomEvent<boolean>('submit', {detail: true});
this.dispatchEvent(event);
Expand Down Expand Up @@ -137,6 +148,12 @@ export class SupportForm extends LitElement {
label="Cloud provider"
helper="Which cloud provider does this relate to?"
helperPersistent
.value=${live(this.formData.cloudProvider ?? '')}
@selected=${(e: CustomEvent<SelectedDetail<number>>) => {
if (e.detail.index !== -1) {
this.formData.cloudProvider = providers[e.detail.index][0];
}
}}
.disabled="${this.isSubmitting}"

Check warning on line 157 in src/www/views/contact_view/support_form/index.ts

View check run for this annotation

Codecov / codecov/patch

src/www/views/contact_view/support_form/index.ts#L157

Added line #L157 was not covered by tests
required
outlined
Expand All @@ -161,6 +178,8 @@ export class SupportForm extends LitElement {
label="Source"
helper="Where did you get your access key?"
helperPersistent
.value=${live(this.formData.source ?? '')}
@input=${(e: Event) => (this.formData.source = (e.target as TextField).value)}
maxLength="225"
.disabled="${this.isSubmitting}"
required
Expand All @@ -178,9 +197,11 @@ export class SupportForm extends LitElement {
name="Email"
type="email"
label="Email address"
maxLength="225"
helper="Please provide an email address where we can reach you."
helperPersistent
.value=${live(this.formData.email ?? '')}
@input=${(e: Event) => (this.formData.email = (e.target as TextField).value)}
maxLength="225"
autoValidate
validationMessage="Please provide a correct email address."
.disabled="${this.isSubmitting}"
Expand All @@ -194,6 +215,8 @@ export class SupportForm extends LitElement {
<mwc-textfield
name="Subject"
label="Subject"
.value=${live(this.formData.subject ?? '')}
@input=${(e: Event) => (this.formData.subject = (e.target as TextField).value)}
maxLength="225"
.disabled="${this.isSubmitting}"
required
Expand All @@ -205,6 +228,8 @@ export class SupportForm extends LitElement {
label="Description"
helper="Please provide a detailed description of your issue."
helperPersistent
.value=${live(this.formData.description ?? '')}
@input=${(e: Event) => (this.formData.description = (e.target as TextField).value)}
rows="5"
maxLength="131072"
charCounter
Expand All @@ -225,6 +250,7 @@ export class SupportForm extends LitElement {
<span slot="card-actions">
<mwc-button label="Cancel" .disabled="${this.isSubmitting}" @click=${this.cancel}></mwc-button>
<mwc-button
type="submit"
label="Submit"
.disabled="${!this.isFormValid || this.isSubmitting}"
@click=${this.submit}
Expand Down

0 comments on commit 7863f5d

Please sign in to comment.