text,length,is_long_context,metric_val,label_metric
":toc: macro
toc::[]
:idprefix:
:idseparator: -
ifdef::env-github[]
:tip-caption: :bulb:
:note-caption: :information_source:
:important-caption: :heavy_exclamation_mark:
:caution-caption: :fire:
:warning-caption: :warning:
endif::[]
= devon4j adding Custom Functionality
Now we have a fully functional blank project. All we have to do now is to create the components and services which will compose our application.
First, we are going to develop the views of the app through its components, then we will create the services with the logic, security and back-end connection.
[NOTE]
====
This tutorial is only going to develop a mobile view. The app is not going to be responsive or well suited for desktop use. The new https://github.com/devonfw-forge/jump-the-queue-v2[JumpTheQueue V2] implements these features, but currently no tutorial walkthrough exists for it.
====
== Creating Components
[NOTE]
====
You have already learned about creating Components in devon4ng https://github.com/devonfw/jump-the-queue/wiki/devon4ng-components#create-a-new-component[here]. +
You can go back and read that section again to refresh your memory.
====
Our app is going to consist of 3 main views:
* Login
* Register
* ViewQueue
To navigate between these views/components, we are going to implement routes using the Angular Router.
To see our progress, move to the root folder of the `angular` project and run `ng serve -o` again. This will recompile and publish our client app to http://localhost:4200. Angular will keep watching for changes, so whenever we modify the code, the app will automatically reload.
=== Root Component
`app.component.ts` inside `angular/src/app` will be our root component, so we don't have to create a new file yet. We are going to add elements to the root component that will be common no matter what view will be displayed.
[NOTE]
====
You have already learned about the Root Component in devon4ng https://github.com/devonfw/jump-the-queue/wiki/devon4ng-components#root-component[here]. +
You can go back and read that section again to refresh your memory.
====
This applies to the header element which will be on top of the window and on top of all other components. If you want, you can read more about https://teradata.github.io/covalent/#/layouts[Covalent layouts], which we are going to use a lot from now on, for every view component.
[NOTE]
====
You have already learned about Covalent Layouts in devon4ng https://github.com/devonfw/jump-the-queue/wiki/devon4ng-components#teradata-covalent-layouts[here]. +
You can go back and read that section again to refresh your memory.
====
We don't really need anything more than a header, so we are going to use the simplest layout for this purpose; the *nav view*.
In order to be able to use Covalent and Angular Material we are going to create a core module, which we will import into every other module where we want to use Covalent and Angular Material. First, we create a folder called `shared` in the `angular/src/app` directory. Inside there we are going to create a file called `core.module.ts` and will fill it with the following content:
[source, typescript]
----
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {
MatAutocompleteModule,
MatButtonModule,
MatButtonToggleModule,
MatCardModule,
MatCheckboxModule,
MatChipsModule,
MatDatepickerModule,
MatDialogModule,
MatExpansionModule,
MatGridListModule,
MatIconModule,
MatInputModule,
MatListModule,
MatMenuModule,
MatNativeDateModule,
MatPaginatorModule,
MatProgressBarModule,
MatProgressSpinnerModule,
MatRadioModule,
MatRippleModule,
MatSelectModule,
MatSidenavModule,
MatSliderModule,
MatSlideToggleModule,
MatSnackBarModule,
MatSortModule,
MatTableModule,
MatTabsModule,
MatToolbarModule,
MatTooltipModule,
} from '@angular/material';
import { CdkTableModule } from '@angular/cdk/table';
import {
CovalentChipsModule,
CovalentLayoutModule,
CovalentExpansionPanelModule,
CovalentDataTableModule,
CovalentPagingModule,
CovalentDialogsModule,
CovalentLoadingModule,
CovalentMediaModule,
CovalentNotificationsModule,
CovalentCommonModule,
} from '@covalent/core';
@NgModule({
imports: [
RouterModule,
BrowserAnimationsModule,
MatCardModule,
MatButtonModule,
MatIconModule,
CovalentMediaModule,
CovalentLayoutModule,
CdkTableModule,
],
exports: [
CommonModule,
CovalentChipsModule,
CovalentLayoutModule,
CovalentExpansionPanelModule,
CovalentDataTableModule,
CovalentPagingModule,
CovalentDialogsModule,
CovalentLoadingModule,
CovalentMediaModule,
CovalentNotificationsModule,
CovalentCommonModule,
CdkTableModule,
MatAutocompleteModule,
MatButtonModule,
MatButtonToggleModule,
MatCardModule,
MatCheckboxModule,
MatChipsModule,
MatDatepickerModule,
MatDialogModule,
MatExpansionModule,
MatGridListModule,
MatIconModule,
MatInputModule,
MatListModule,
MatMenuModule,
MatNativeDateModule,
MatPaginatorModule,
MatProgressBarModule,
MatProgressSpinnerModule,
MatRadioModule,
MatRippleModule,
MatSelectModule,
MatSidenavModule,
MatSliderModule,
MatSlideToggleModule,
MatSnackBarModule,
MatSortModule,
MatTableModule,
MatTabsModule,
MatToolbarModule,
MatTooltipModule,
HttpClientModule,
],
declarations: [],
providers: [
HttpClientModule
],
})
export class CoreModule {}
----
[NOTE]
====
This `CoreModule` has almost every module of the different components for *Angular Material* and *Covalent Teradata*. If you decide to use a component that is not included yet, you need to add the corresponding module here.
====
Remember that we need to import this `CoreModule` module into the `AppModule` and inside every module of the different components that use *Angular Material* and *Covalent Teradata*. If a component does not have a module, it will be imported in the `AppModule` and hence automatically have the `CoreModule`. Our `app.module.ts` should have the following content:
[source, typescript]
----
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
// Application components and services
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CoreModule } from './shared/core.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
CoreModule,
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule { }
----
[NOTE]
====
Remember this step because you will have to repeat it for every other component from Teradata you use in your app.
====
Now we can use this layout, so let's implement it in `app.component.html`. Use the following code:
[source, html]
----
Jump The Queue
app works!
----
[NOTE]
====
You have already learned about Toolbars in devon4ng https://github.com/devonfw/jump-the-queue/wiki/devon4ng-components#toolbars[here]. +
You can go back and read that section again to refresh your memory.
====
Once this is done, our app should have a header and ""app works!"" should appear in the body of the page:
image::images/devon4ng/3.BuildYourOwn/root_header.jpg[Root Header, 250]
To go a step further, we have to modify the body of the root component because it should be the *output of the router*. Now it's time to prepare the routing system.
First, we need to create a component to show as default which will be our access view. We will modify it later. Stop `ng serve` and run:
----
ng generate component form-login
----
It will add a folder to our project with all the files needed for a component. Now we can move on to the router task again. Run `ng serve` again to continue the development.
Let's create a module that navigates between components when the Router checks for routes. The file `app-routing.module.ts` was created automatically when we chose to include Angular Routing during project creation and we only need to modify it now:
[source, typescript]
----
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { FormLoginComponent } from './form-login/form-login.component';
const appRoutes: Routes = [
{ path: 'FormLogin', component: FormLoginComponent}, // Redirect if url path is /FormLogin.
{ path: '**', redirectTo: '/FormLogin', pathMatch: 'full' } // Redirect if url path do not match any other route.
];
@NgModule({
imports: [
RouterModule.forRoot(
appRoutes,
{ enableTracing: true }, // <-- debugging purposes only
),
],
exports: [RouterModule],
})
export class AppRoutingModule {}
----
[NOTE]
====
You have already learned about Routing in devon4ng https://github.com/devonfw/jump-the-queue/wiki/devon4ng-components#routing[here]. +
You can go back and read that section again to refresh your memory.
====
Finally, we remove the `
app works!
` from `app.component.html` and replace it with a `` tag. The final result of our root component will look like this:
image::images/devon4ng/3.BuildYourOwn/root_router.jpg[Root Router, 250]
As you can see, now the body content is the HTML of `FormLoginComponent`. This is because we told the Router to redirect to formlogin when the path is `/FormLogin`, but also, redirect to it by default if any of the other routes match the given path.
For now we are going to leave the header like this. In the future we will separate it into another component inside a layout folder.
=== LoginForm Component
As we have already created this component from the section before, let's move on to building the template of the login view.
First, we need to add the Covalent Layout and the card to the file `form-login.component.html`:
[source, html]
----
Login
----
This will add a grey background to the view and a card on top of it with the title ""Login"" now that we have the basic structure of the view.
Now we are going to add this image:
image::images/devon4ng/3.BuildYourOwn/jumptheq.png[JumpTheQueue Logo Image, 250]
In order to have it available, save it in the following path of the project: `angular/src/assets/images/` and name it `jumptheq.png`.
The final code with the form added will look like this:
[source, html]
----
----
This code will give us as a result similar to this:
image::images/devon4ng/3.BuildYourOwn/formlogin.png[Form Login, 250]
This is going to be the container for the login. +
Now we will continue with the second component: Login.
=== Login Component
Our first step will be to create the component in the exact same way we created the `FormLogin` component but this time we are going to generate it in a new folder called components inside formlogin. Putting every child component inside that folder will allow us to keep a good and clear structure. In order to do this, we use the command:
----
ng generate component form-login/components/login
----
After _Angular/CLI_ has finished generating the component, we have to create two modules, one for the form-login and one for the login:
1.- We create a new file called `login-module.ts` in the login root:
[source, typescript]
----
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CoreModule } from 'src/app/shared/core.module';
import { LoginComponent } from './login.component';
@NgModule({
imports: [CommonModule, CoreModule],
providers: [],
declarations: [LoginComponent],
exports: [LoginComponent],
})
export class LoginModule {}
----
2.- We create a new file called `form-login-module.ts` in the form-login root:
[source, typescript]
----
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormLoginComponent } from './form-login.component';
import { CoreModule } from '../shared/core.module';
import { LoginModule } from './components/login/login-module';
@NgModule({
imports: [CommonModule, CoreModule, LoginModule],
providers: [],
declarations: [FormLoginComponent],
exports: [FormLoginComponent],
})
export class FormLoginModule {}
----
As you can see, the `LoginModule` is already added to the `FormLoginModule`. Once this is done, we need to remove the `FormLoginComponent` and the `LoginComponent` from the `declarations` since they are already declared in their own modules. Then add the `FormLoginModule`. This will be done inside `AppModule`:
[source, typescript]
----
...
import { FormLoginModule } from './form-login/form-login-module';
...
declarations: [
AppComponent,
]
imports: [
BrowserModule,
FormLoginModule,
CoreModule,
AppRoutingModule
]
...
----
[NOTE]
====
This is done so the `form-login` (container/wrapper) and the `login` stay separated allowing us to reuse the login without having the card around in other views.
====
After this, we modify the `login.component.html` and add the form:
[source, typescript]
----
----
[NOTE]
====
You have already learned about Forms in devon4ng https://github.com/devonfw/jump-the-queue/wiki/devon4ng-components#forms[here]. +
You can go back and read that section again to refresh your memory.
====
This form contains two input containers from Material. The containers enclose the input with the properties listed above.
We also need to add a button to send the information and redirect to the `QueueViewer` or show an error if something went wrong in the process. But for the moment, as we neither have another component nor the auth service yet, we will implement the button visually, as well as the validator to disable it if the form is not correct. We will tackle the on-click-event later.
As a last step we will add this component to the `form-login-component.html`:
[source, html]
----
----
Now you should see something like this:
image::images/devon4ng/3.BuildYourOwn/login.png[JumpTheQueue Login Screen, 250]
With two components already created, we need to use the router to navigate between them. Following the application flow of events, we are going to add a _navigate_ function to the register button. When we press it, we will be redirected to our future register component.
=== Register Component
First, we are going to generate the register component via:
----
ng generate component register`
----
This will create our component so we can start working on it. Turning back to `login.component.html` we have to modify these lines of code:
[source, html]
----
----
Now that we have a minimum of navigation flow inside our application, we are going to generate our first service using the command:
----
ng generate service register/services/register
----
This will create a folder ""services"" inside ""register"" and create the service itself. Services are where we keep the logic that connects to our database and fetches data which is going to be used by our `component.ts`.
In order to use the service, we are going to create some interface models. Let's create a folder called `backendModels` inside ""shared"" and inside this folder a file called `interfaces.ts` in which we are going to add the model interfaces that will match our backend:
[source, typescript]
----
export class Visitor {
id?: number;
username: string;
name: string;
password: string;
phoneNumber: string;
acceptedCommercial: boolean;
acceptedTerms: boolean;
userType: boolean;
}
----
[NOTE]
====
You have already learned about creating new services in devon4ng https://github.com/devonfw/jump-the-queue/wiki/devon4ng-services#create-a-new-service[here]. +
You can go back and read that section again to refresh your memory.
====
If we take a closer look, we can see that id has a `?` behind it. This indicates that the id is optional.
[NOTE]
====
At this point we are going to assume that you have finished the https://github.com/devonfw/jump-the-queue/wiki/build-devon4j-application[devon4j] part of this tutorial, or have at least downloaded the project and have the back end running locally on http://localhost:8081.
====
After doing this, we are going to add an environment variable with our base-URL for the REST services. This way we won't have to change every URL when we switch to production. Inside `environments/environment.ts` we add:
[source, typescript]
----
export const environment: {production: boolean, baseUrlRestServices: string} = {
production: false,
baseUrlRestServices: 'http://localhost:8081/jumpthequeue/services/rest'
};
----
Now in the service, we are going to add a `registerVisitor` method.
To call the server in this method we are going to inject the Angular `HttpClient` class from `@angular/common/http`. This class is the standard used by Angular to perform HTTP calls. The register call demands a `Visitor` model which we created in the `interfaces` file. We are going to build a POST call and send the information to the proper URL of the server service. The call will return an observable:
[source, typescript]
----
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Visitor} from 'src/app/shared/backendModels/interfaces';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class RegisterService {
private baseUrl = environment.baseUrlRestServices;
constructor(private http: HttpClient) { }
registerVisitor(visitor: Visitor): Observable {
return this.http.post(`${this.baseUrl}` + '/visitormanagement/v1/visitor', visitor);
}
}
----
This method will send our model to the backend and return an Observable that we will use on the `component.ts`.
[NOTE]
====
You have already learned about Observables and RxJs in devon4ng https://github.com/devonfw/jump-the-queue/wiki/devon4ng-services#server-communication[here]. +
You can go back and read that section again to refresh your memory.
====
Now we are going to modify `register.component.ts` to call this service:
[source, typescript]
----
import { Component, OnInit } from '@angular/core';
import { RegisterService } from './services/register.service';
import { Visitor } from '../shared/backendModels/interfaces';
import { Router } from '@angular/router';
import { MatSnackBar } from '@angular/material/snack-bar';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {
constructor(private registerService: RegisterService, private router: Router, public snackBar: MatSnackBar) { }
submitRegister(formValue): void {
const visitor: Visitor = new Visitor();
visitor.username = formValue.username;
visitor.name = formValue.name;
visitor.phoneNumber = formValue.phoneNumber;
visitor.password = formValue.password;
visitor.acceptedCommercial = formValue.acceptedCommercial;
visitor.acceptedTerms = formValue.acceptedTerms;
visitor.userType = false;
this.registerService.registerVisitor(visitor).subscribe(
(visitorResult: Visitor) => console.log(JSON.stringify(visitorResult)), // When call is received
(err) => this.snackBar.open(err.error.message, 'OK', {
duration: 5000,
}), // When theres an error
);
}
ngOnInit() {
}
}
----
In this file we injected `RegisterService` and `Router` to use them. Then, inside the method `submitRegister`, we created a visitor that we are going to pass to the service. We called the service method `registerVisitor`, we passed the visitor and we subscribed to the `Observable`, which we returned from the service. This subscription allows us to control three things:
. What to do when the data is received.
. What to do when there's an error.
. What to do when the call is complete.
Finally, we modify the `register.component.html` to send the form values to the method:
[source, html]
----
...