Skip to content

Commit

Permalink
Documentation; more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdaley committed Oct 20, 2023
1 parent 197db9b commit f538635
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
4 changes: 2 additions & 2 deletions web/app/components/new/doc-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
@matchAnchorWidth={{hash enabled=true additionalWidth=6}}
class="max-h-[240px]"
/>
{{#if this.formErrors.productAbbreviation}}
{{#if this.formErrors.product}}
<Hds::Form::Error data-test-product-error class="mt-2">
{{this.formErrors.productAbbreviation}}
{{this.formErrors.product}}
</Hds::Form::Error>
{{/if}}
</div>
Expand Down
11 changes: 5 additions & 6 deletions web/app/components/new/doc-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export default class NewDocFormComponent extends Component<NewDocFormComponentSi
/**
* An object containing error messages for each applicable form field.
*/
@tracked protected formErrors: DocFormErrors | undefined;
@tracked protected formErrors: DocFormErrors = {
title: null,
productAbbreviation: null,
};

/**
* Whether to validate eagerly, that is, after every change to the form.
Expand All @@ -76,9 +79,6 @@ export default class NewDocFormComponent extends Component<NewDocFormComponentSi
* Whether the form has errors.
*/
private get hasErrors(): boolean {
if (!this.formErrors) {
return false;
}
return Object.values(this.formErrors).some((error) => error !== null);
}

Expand Down Expand Up @@ -137,7 +137,7 @@ export default class NewDocFormComponent extends Component<NewDocFormComponentSi
return;
}

if (this.formErrors?.title || this.formErrors?.productAbbreviation) {
if (this.formErrors.title || this.formErrors.productAbbreviation) {
// Validate once the input values are captured
next("afterRender", () => {
this.validate();
Expand Down Expand Up @@ -214,7 +214,6 @@ export default class NewDocFormComponent extends Component<NewDocFormComponentSi
} catch (err: unknown) {
this.docIsBeingCreated = false;

// TODO: Improve error handling.
this.flashMessages.add({
title: "Error creating document draft",
message: `${err}`,
Expand Down
3 changes: 0 additions & 3 deletions web/app/components/new/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const FORM_RESIZE_DURATION = Ember.testing ? 0 : 750;

class HermesFormResize extends Resize {
*animate() {
console.log("calllllt");
this.opts.easing = easeOutExpo;
this.opts.duration = FORM_RESIZE_DURATION;
yield* super.animate();
Expand All @@ -37,14 +36,12 @@ export default class NewFormComponent extends Component<NewFormComponentSignatur

*transition({ insertedSprites, removedSprites }: TransitionContext) {
for (const sprite of insertedSprites) {
console.log("insertedSprite", sprite);
sprite.startTranslatedBy(0, -2);
void fadeIn(sprite, { duration: 50 });
void move(sprite, { easing: easeOutQuad, duration: 350 });
}

for (const sprite of removedSprites) {
console.log("removedSprite", sprite);
void fadeOut(sprite, { duration: 0 });
}
}
Expand Down
33 changes: 19 additions & 14 deletions web/app/components/new/project-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import { task } from "ember-concurrency";
import FetchService from "hermes/services/fetch";
import cleanString from "hermes/utils/clean-string";

interface NewProjectFormComponentSignature {
Args: {};
}
interface NewProjectFormComponentSignature {}

export default class NewProjectFormComponent extends Component<NewProjectFormComponentSignature> {
@service("fetch") declare fetchSvc: FetchService;
Expand All @@ -29,14 +27,14 @@ export default class NewProjectFormComponent extends Component<NewProjectFormCom

@tracked protected title: string = "";
@tracked protected description: string = "";

@tracked protected titleErrorIsShown = false;

/**
* The action to attempt a form submission.
* If the form is valid, the createProject task is run.
*/
@action maybeSubmitForm(event?: SubmitEvent) {
if (event) {
event.preventDefault();
}

event?.preventDefault();
this.validate();

if (!this.titleErrorIsShown) {
Expand All @@ -48,10 +46,14 @@ export default class NewProjectFormComponent extends Component<NewProjectFormCom
this.titleErrorIsShown = this.title.length === 0;
}

@action protected onKeydown(e: KeyboardEvent) {
if (e.key === "Enter") {
// Override newline function
e.preventDefault();
/**
* The action run on title- and description-input keydown.
* If the key is Enter, a form submission is attempted.
* If the title error is shown, validation is run eagerly.
*/
@action protected onKeydown(event: KeyboardEvent) {
if (event.key === "Enter") {
event.preventDefault();
this.maybeSubmitForm();
}
if (this.titleErrorIsShown) {
Expand All @@ -62,6 +64,11 @@ export default class NewProjectFormComponent extends Component<NewProjectFormCom
}
}

/**
* The task that creates a project and, if successful,
* transitions to it. On error, displays a FlashMessage
* and reverts the `projectIsBeingCreated` state.
*/
private createProject = task(async () => {
try {
this.projectIsBeingCreated = true;
Expand All @@ -77,15 +84,13 @@ export default class NewProjectFormComponent extends Component<NewProjectFormCom
this.router.transitionTo("authenticated.projects.project", project.id);
} catch (error: unknown) {
const typedError = error as Error;

this.flashMessages.add({
title: "Error creating project",
message: typedError.message,
type: "critical",
timeout: 6000,
extendedTimeout: 1000,
});

this.projectIsBeingCreated = false;
}
});
Expand Down

0 comments on commit f538635

Please sign in to comment.