|
| 1 | + |
| 2 | +// Import the core angular services. |
| 3 | +import { Component } from "@angular/core"; |
| 4 | +import { ElementRef } from "@angular/core"; |
| 5 | +import { ViewChild } from "@angular/core"; |
| 6 | + |
| 7 | +// Import the application components and services. |
| 8 | +import { FriendsRuntime } from "./friends.runtime"; |
| 9 | +import { ModalViewComponent } from "./modal-view.component"; |
| 10 | + |
| 11 | +// ----------------------------------------------------------------------------------- // |
| 12 | +// ----------------------------------------------------------------------------------- // |
| 13 | + |
| 14 | +@Component({ |
| 15 | + selector: "add-friend-view", |
| 16 | + queries: { |
| 17 | + nameRef: new ViewChild( "nameRef" ) |
| 18 | + }, |
| 19 | + styleUrls: [ "./add-friend-view.component.less" ], |
| 20 | + template: |
| 21 | + ` |
| 22 | + <a (click)="closeModal()" class="close"> |
| 23 | + × |
| 24 | + </a> |
| 25 | +
|
| 26 | + <h2 class="title"> |
| 27 | + Add New Friend |
| 28 | + </h2> |
| 29 | +
|
| 30 | + <form (submit)="processForm()" class="form"> |
| 31 | +
|
| 32 | + <div class="field"> |
| 33 | + <input |
| 34 | + #nameRef |
| 35 | + type="text" |
| 36 | + name="name" |
| 37 | + [(ngModel)]="form.name" |
| 38 | + class="field__input" |
| 39 | + /> |
| 40 | +
|
| 41 | + <button type="submit" class="field__submit"> |
| 42 | + Add Friend |
| 43 | + </button> |
| 44 | + </div> |
| 45 | +
|
| 46 | + <label for="add-friend-view-bulk-checkbox" class="bulk"> |
| 47 | + <input |
| 48 | + id="add-friend-view-bulk-checkbox" |
| 49 | + type="checkbox" |
| 50 | + name="isBulkAction" |
| 51 | + [(ngModel)]="form.isBulkAction" |
| 52 | + class="bulk__input" |
| 53 | + /> |
| 54 | +
|
| 55 | + <span class="bulk__label"> |
| 56 | + I want to add multiple friends. |
| 57 | + </span> |
| 58 | + </label> |
| 59 | +
|
| 60 | + </form> |
| 61 | + ` |
| 62 | +}) |
| 63 | +export class AddFriendViewComponent { |
| 64 | + |
| 65 | + public form: { |
| 66 | + isBulkAction: boolean; |
| 67 | + name: string; |
| 68 | + }; |
| 69 | + public nameRef!: ElementRef; |
| 70 | + |
| 71 | + private friendsRuntime: FriendsRuntime; |
| 72 | + private modalViewComponent: ModalViewComponent; |
| 73 | + |
| 74 | + // I initialize the add-friend-view component. |
| 75 | + constructor( |
| 76 | + friendsRuntime: FriendsRuntime, |
| 77 | + modalViewComponent: ModalViewComponent |
| 78 | + ) { |
| 79 | + |
| 80 | + this.friendsRuntime = friendsRuntime; |
| 81 | + this.modalViewComponent = modalViewComponent; |
| 82 | + |
| 83 | + this.form = { |
| 84 | + isBulkAction: false, |
| 85 | + name: "" |
| 86 | + }; |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + // --- |
| 91 | + // PUBLIC METHODS. |
| 92 | + // --- |
| 93 | + |
| 94 | + // I close the modal window. |
| 95 | + public closeModal() : void { |
| 96 | + |
| 97 | + this.modalViewComponent.closeModal(); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + // I get called once after the view has been initialized. |
| 103 | + public ngAfterViewInit() : void { |
| 104 | + |
| 105 | + this.focusInput(); |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + // I process the new friend form. |
| 111 | + public processForm() : void { |
| 112 | + |
| 113 | + if ( ! this.form.name.trim() ) { |
| 114 | + |
| 115 | + return; |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + this.friendsRuntime |
| 120 | + .addFriend( this.form.name ) |
| 121 | + .then( |
| 122 | + ( id ) => { |
| 123 | + |
| 124 | + if ( this.form.isBulkAction ) { |
| 125 | + |
| 126 | + this.form.name = ""; |
| 127 | + this.focusInput(); |
| 128 | + |
| 129 | + } else { |
| 130 | + |
| 131 | + this.closeModal(); |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + }, |
| 136 | + ( error ) => { |
| 137 | + |
| 138 | + console.warn( "There was a problem adding the friend." ); |
| 139 | + console.error( error ); |
| 140 | + |
| 141 | + } |
| 142 | + ) |
| 143 | + ; |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + // --- |
| 148 | + // PRIVATE METHODS. |
| 149 | + // --- |
| 150 | + |
| 151 | + // I put the browser focus to the name input. |
| 152 | + private focusInput() : void { |
| 153 | + |
| 154 | + this.nameRef.nativeElement.focus(); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments