Browse Source

Merge pull request #4536 from abpframework/docs/bookstore

Refactored the bookstore tutorial
pull/4543/head
Levent Arman Özak 6 years ago
committed by GitHub
parent
commit
b04ba5e722
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 231
      docs/en/Tutorials/Part-1.md
  2. 576
      docs/en/Tutorials/Part-2.md
  3. BIN
      docs/en/Tutorials/images/bookstore-actions-buttons.png
  4. BIN
      docs/en/Tutorials/images/bookstore-angular-file-tree.png
  5. BIN
      docs/en/Tutorials/images/bookstore-book-list.png
  6. BIN
      docs/en/Tutorials/images/bookstore-confirmation-popup.png
  7. BIN
      docs/en/Tutorials/images/bookstore-empty-new-book-modal.png

231
docs/en/Tutorials/Part-1.md

@ -754,8 +754,8 @@ It's end of this part. The final UI of this work is shown as below:
It's time to create something visible and usable! There are some tools that we will use when developing ABP Angular frontend application: It's time to create something visible and usable! There are some tools that we will use when developing ABP Angular frontend application:
- [Angular CLI](https://angular.io/cli) will be used to create modules, components and services. - [Angular CLI](https://angular.io/cli) will be used to create modules, components and services.
- [NGXS](https://ngxs.gitbook.io/ngxs/) will be used as the state management library.
- [Ng Bootstrap](https://ng-bootstrap.github.io/#/home) will be used as the UI component library. - [Ng Bootstrap](https://ng-bootstrap.github.io/#/home) will be used as the UI component library.
- [ngx-datatable](https://swimlane.gitbook.io/ngx-datatable/) will be used as the datatable library.
- [Visual Studio Code](https://code.visualstudio.com/) will be used as the code editor (you can use your favorite editor). - [Visual Studio Code](https://code.visualstudio.com/) will be used as the code editor (you can use your favorite editor).
#### Install NPM packages #### Install NPM packages
@ -778,26 +778,62 @@ yarn ng generate module book --routing true
#### Routing #### Routing
Open the `app-routing.module.ts` file in `src\app` folder. Add the new `import` and add a route as shown below Open the `app-routing.module.ts` file in `src\app` folder and add a route as shown below:
```js ```js
import { ApplicationLayoutComponent } from '@abp/ng.theme.basic'; //==> added this line to imports <== const routes: Routes = [
// ...
//...added books path with the below to the routes array // added a new route to the routes array
{ {
path: 'books', path: 'books',
component: ApplicationLayoutComponent, loadChildren: () => import('./book/book.module').then(m => m.BookModule)
loadChildren: () => import('./book/book.module').then(m => m.BookModule), }]
data: { ```
routes: {
name: '::Menu:Books', * We added a lazy-loaded route. See the [Lazy-Loading Feature Modules](https://angular.io/guide/lazy-loading-ngmodules#lazy-loading-feature-modules).
iconClass: 'fas fa-book'
} as ABP.Route Open the `route.provider.ts` file in `src\app` folder and replace the content as below:
},
```js
import { RoutesService, eLayoutType } from '@abp/ng.core';
import { APP_INITIALIZER } from '@angular/core';
export const APP_ROUTE_PROVIDER = [
{ provide: APP_INITIALIZER, useFactory: configureRoutes, deps: [RoutesService], multi: true },
];
function configureRoutes(routes: RoutesService) {
return () => {
routes.add([
{
path: '/',
name: '::Menu:Home',
iconClass: 'fas fa-home',
order: 1,
layout: eLayoutType.application,
},
// added below element
{
path: '/books',
name: '::Menu:Books',
iconClass: 'fas fa-book',
order: 101,
layout: eLayoutType.application,
},
]);
};
} }
``` ```
* The `ApplicationLayoutComponent` configuration sets the application layout to the new page. We added the `data` object. The `name` is the menu item name and the `iconClass` is the icon of the menu item. * We added a new route element to show a navigation element labeled "Books" on the menu.
* `path` is the URL of the route.
* `name` is the menu item name. A Localization key can be passed.
* `iconClass` is the icon of the menu item.
* `order` is the order of the menu item. We define 101 to show the route after the "Administration" item.
* `layout` is the layout of the BooksModule's routes. `eLayoutType.application`, `eLayoutType.account` or `eLayoutType.empty` can be defined.
<!-- TODO: Add RoutesService doc link here -->
#### Book list component #### Book list component
@ -865,35 +901,6 @@ Open the browser and navigate to http://localhost:4200/books. We'll see **book-l
![Initial book list page](./images/bookstore-initial-book-list-page.png) ![Initial book list page](./images/bookstore-initial-book-list-page.png)
#### Create BookState
Run the following command in the terminal to create a new state, named `BooksState`:
```bash
npx @ngxs/cli --name book --directory src/app/book
```
* This command creates `book.state.ts` and `book.actions.ts` files in the `src/app/book/state` folder. See the [NGXS CLI documentation](https://www.ngxs.io/plugins/cli).
Import the `BookState` to the `app.module.ts` in the `src/app` folder and then add the `BookState` to `forRoot` static method of `NgxsModule` as an array element of the first parameter of the method.
```js
// ...
import { BookState } from './books/state/book.state'; //<== imported BookState ==>
@NgModule({
imports: [
// other imports
NgxsModule.forRoot([BookState]), //<== added BookState ==>
//other imports
],
// ...
})
export class AppModule {}
```
#### Generate proxies #### Generate proxies
ABP CLI provides `generate-proxy` command that generates client proxies for your HTTP APIs to make easy to consume your services from the client side. Before running generate-proxy command, your host must be up and running. See the [CLI documentation](../CLI.md) ABP CLI provides `generate-proxy` command that generates client proxies for your HTTP APIs to make easy to consume your services from the client side. Before running generate-proxy command, your host must be up and running. See the [CLI documentation](../CLI.md)
@ -910,109 +917,40 @@ The generated files looks like below:
![Generated files](./images/generated-proxies.png) ![Generated files](./images/generated-proxies.png)
#### GetBooks Action
Actions can either be thought of as a command which should trigger something to happen, or as the resulting event of something that has already happened. [See NGXS Actions documentation](https://www.ngxs.io/concepts/actions).
Open the `book.actions.ts` file in `app/book/state` folder and replace the content below:
```js
export class GetBooks {
static readonly type = '[Book] Get';
}
```
#### Implement BookState
Open the `book.state.ts` file in `app/book/state` folder and replace the content below:
```js
import { PagedResultDto } from '@abp/ng.core';
import { State, Action, StateContext, Selector } from '@ngxs/store';
import { GetBooks } from './book.actions';
import { BookService } from '../services';
import { tap } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { BookDto } from '../models';
export class BookStateModel {
public book: PagedResultDto<BookDto>;
}
@State<BookStateModel>({
name: 'BookState',
defaults: { book: {} } as BookStateModel,
})
@Injectable()
export class BookState {
@Selector()
static getBooks(state: BookStateModel) {
return state.book.items || [];
}
constructor(private bookService: BookService) {}
@Action(GetBooks)
get(ctx: StateContext<BookStateModel>) {
return this.bookService.getListByInput().pipe(
tap((booksResponse) => {
ctx.patchState({
book: booksResponse,
});
})
);
}
}
```
* We added the book property to BookStateModel model.
* We added the `GetBooks` action that retrieves the book data via `BookService` that generated via ABP CLI and patches the state.
* `NGXS` requires to return the observable without subscribing it in the get function.
#### BookListComponent #### BookListComponent
Open the `book-list.component.ts` file in `app\book\book-list` folder and replace the content as below: Open the `book-list.component.ts` file in `app\book\book-list` folder and replace the content as below:
```js ```js
import { ListService, PagedResultDto } from '@abp/ng.core';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Select, Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { BookDto, BookType } from '../models'; import { BookDto, BookType } from '../models';
import { GetBooks } from '../state/book.actions'; import { BookService } from '../services';
import { BookState } from '../state/book.state';
@Component({ @Component({
selector: 'app-book-list', selector: 'app-book-list',
templateUrl: './book-list.component.html', templateUrl: './book-list.component.html',
styleUrls: ['./book-list.component.scss'], styleUrls: ['./book-list.component.scss'],
providers: [ListService],
}) })
export class BookListComponent implements OnInit { export class BookListComponent implements OnInit {
@Select(BookState.getBooks) book = { items: [], totalCount: 0 } as PagedResultDto<BookDto>;
books$: Observable<BookDto[]>;
booksType = BookType; booksType = BookType;
loading = false; constructor(public readonly list: ListService, private bookService: BookService) {}
constructor(private store: Store) {}
ngOnInit() { ngOnInit() {
this.get(); const bookStreamCreator = (query) => this.bookService.getListByInput(query);
}
get() { this.list.hookToQuery(bookStreamCreator).subscribe((response) => {
this.loading = true; this.book = response;
this.store });
.dispatch(new GetBooks())
.pipe(finalize(() => (this.loading = false)))
.subscribe(() => {});
} }
} }
``` ```
* We imported and injected the generated `BookService`.
* We added the `get` function that updates store to get the books. * We implemented the [ListService](https://docs.abp.io/en/abp/latest/UI/Angular/List-Service)` that is a utility service to provide easy pagination, sorting, and search implementation.
* See the [Dispatching actions](https://ngxs.gitbook.io/ngxs/concepts/store#dispatching-actions) and [Select](https://ngxs.gitbook.io/ngxs/concepts/select) on the `NGXS` documentation for more information on these `NGXS` features.
Open the `book-list.component.html` file in `app\book\book-list` folder and replace the content as below: Open the `book-list.component.html` file in `app\book\book-list` folder and replace the content as below:
@ -1022,38 +960,31 @@ Open the `book-list.component.html` file in `app\book\book-list` folder and repl
<div class="row"> <div class="row">
<div class="col col-md-6"> <div class="col col-md-6">
<h5 class="card-title"> <h5 class="card-title">
{%{{{ "::Menu:Books" | abpLocalization }}}%} {%{{{ '::Menu:Books' | abpLocalization }}}%}
</h5> </h5>
</div> </div>
<div class="text-right col col-md-6"></div> <div class="text-right col col-md-6"></div>
</div> </div>
</div> </div>
<div class="card-body"> <div class="card-body">
<abp-table <ngx-datatable [rows]="book.items" [count]="book.totalCount" [list]="list" default>
[value]="books$ | async" <ngx-datatable-column [name]="'::Name' | abpLocalization" prop="name"></ngx-datatable-column>
[abpLoading]="loading" <ngx-datatable-column [name]="'::Type' | abpLocalization" prop="type">
[headerTemplate]="tableHeader" <ng-template let-row="row" ngx-datatable-cell-template>
[bodyTemplate]="tableBody" {%{{{ booksType[row.type] }}}%}
[rows]="10" </ng-template>
[scrollable]="true" </ngx-datatable-column>
> <ngx-datatable-column [name]="'::PublishDate' | abpLocalization" prop="publishDate">
</abp-table> <ng-template let-row="row" ngx-datatable-cell-template>
<ng-template #tableHeader> {%{{{ row.publishDate | date }}}%}
<tr> </ng-template>
<th>{%{{{ "::Name" | abpLocalization }}}%}</th> </ngx-datatable-column>
<th>{%{{{ "::Type" | abpLocalization }}}%}</th> <ngx-datatable-column [name]="'::Price' | abpLocalization" prop="price">
<th>{%{{{ "::PublishDate" | abpLocalization }}}%}</th> <ng-template let-row="row" ngx-datatable-cell-template>
<th>{%{{{ "::Price" | abpLocalization }}}%}</th> {%{{{ row.price | currency }}}%}
</tr> </ng-template>
</ng-template> </ngx-datatable-column>
<ng-template #tableBody let-data> </ngx-datatable>
<tr>
<td>{%{{{ data.name }}}%}</td>
<td>{%{{{ booksType[data.type] }}}%}</td>
<td>{%{{{ data.publishDate | date }}}%}</td>
<td>{%{{{ data.price }}}%}</td>
</tr>
</ng-template>
</div> </div>
</div> </div>
``` ```
@ -1074,4 +1005,4 @@ In this tutorial we have applied the rules of official [Angular Style Guide](htt
### Next Part ### Next Part
See the [part 2](part-2.md) for creating, updating and deleting books. See the [part 2](./Part-2.md) for creating, updating and deleting books.

576
docs/en/Tutorials/Part-2.md

@ -454,78 +454,50 @@ Run the application and try to delete a book.
In this section, you will learn how to create a new modal dialog form to create a new book. In this section, you will learn how to create a new modal dialog form to create a new book.
#### State definitions #### Add a modal to BookListComponent
Open `book.action.ts` in `app\book\state` folder and replace the content as below:
```js
import { CreateUpdateBookDto } from '../models'; //<== added this line ==>
export class GetBooks { Open `book-list.component.ts` file in `app\book\book-list` folder and replace the content as below:
static readonly type = '[Book] Get';
}
// added CreateUpdateBook class ```js
export class CreateUpdateBook { import { ListService, PagedResultDto } from '@abp/ng.core';
static readonly type = '[Book] Create Update Book'; import { Component, OnInit } from '@angular/core';
constructor(public payload: CreateUpdateBookDto) { } import { BookDto, BookType } from '../models';
} import { BookService } from '../services';
```
* We imported the `CreateUpdateBookDto` model and created the `CreateUpdateBook` action. @Component({
selector: 'app-book-list',
templateUrl: './book-list.component.html',
styleUrls: ['./book-list.component.scss'],
providers: [ListService],
})
export class BookListComponent implements OnInit {
book = { items: [], totalCount: 0 } as PagedResultDto<BookDto>;
Open `book.state.ts` file in `app\book\state` folder and replace the content as below: booksType = BookType;
```js isModalOpen = false; // <== added this line ==>
import { PagedResultDto } from '@abp/ng.core';
import { State, Action, StateContext, Selector } from '@ngxs/store';
import { GetBooks, CreateUpdateBook } from './book.actions'; // <== added CreateUpdateBook==>
import { BookService } from '../services';
import { tap } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { BookDto } from '../models';
export class BookStateModel { constructor(public readonly list: ListService, private bookService: BookService) {}
public book: PagedResultDto<BookDto>;
}
@State<BookStateModel>({ ngOnInit() {
name: 'BookState', const bookStreamCreator = (query) => this.bookService.getListByInput(query);
defaults: { book: {} } as BookStateModel,
})
@Injectable()
export class BookState {
@Selector()
static getBooks(state: BookStateModel) {
return state.book.items || [];
}
constructor(private bookService: BookService) {} this.list.hookToQuery(bookStreamCreator).subscribe((response) => {
this.book = response;
@Action(GetBooks) });
get(ctx: StateContext<BookStateModel>) {
return this.bookService.getListByInput().pipe(
tap((bookResponse) => {
ctx.patchState({
book: bookResponse,
});
})
);
} }
// added CreateUpdateBook action listener // added createBook method
@Action(CreateUpdateBook) createBook() {
save(ctx: StateContext<BookStateModel>, action: CreateUpdateBook) { this.isModalOpen = true;
return this.bookService.createByInput(action.payload);
} }
} }
``` ```
* We imported `CreateUpdateBook` action and defined the `save` method that will listen to a `CreateUpdateBook` action to create a book. * We defined a variable called `isModalOpen` and `createBook` method.
* We added the `createBook` method.
When the `SaveBook` action dispatched, the save method is being executed. It calls `createByInput` method of the `BookService`.
#### Add a modal to BookListComponent
Open `book-list.component.html` file in `books\book-list` folder and replace the content as below: Open `book-list.component.html` file in `books\book-list` folder and replace the content as below:
@ -534,19 +506,12 @@ Open `book-list.component.html` file in `books\book-list` folder and replace the
<div class="card-header"> <div class="card-header">
<div class="row"> <div class="row">
<div class="col col-md-6"> <div class="col col-md-6">
<h5 class="card-title"> <h5 class="card-title">{%{{{ '::Menu:Books' | abpLocalization }}}%}</h5>
{%{{{ '::Menu:Books' | abpLocalization }}}%}
</h5>
</div> </div>
<!--Added new book button --> <!--Added new book button -->
<div class="text-right col col-md-6"> <div class="text-right col col-md-6">
<div class="text-lg-right pt-2"> <div class="text-lg-right pt-2">
<button <button id="create" class="btn btn-primary" type="button" (click)="createBook()">
id="create"
class="btn btn-primary"
type="button"
(click)="createBook()"
>
<i class="fa fa-plus mr-1"></i> <i class="fa fa-plus mr-1"></i>
<span>{%{{{ "::NewBook" | abpLocalization }}}%}</span> <span>{%{{{ "::NewBook" | abpLocalization }}}%}</span>
</button> </button>
@ -555,47 +520,40 @@ Open `book-list.component.html` file in `books\book-list` folder and replace the
</div> </div>
</div> </div>
<div class="card-body"> <div class="card-body">
<abp-table <ngx-datatable [rows]="book.items" [count]="book.totalCount" [list]="list" default>
[value]="books$ | async" <ngx-datatable-column [name]="'::Name' | abpLocalization" prop="name"></ngx-datatable-column>
[abpLoading]="loading" <ngx-datatable-column [name]="'::Type' | abpLocalization" prop="type">
[headerTemplate]="tableHeader" <ng-template let-row="row" ngx-datatable-cell-template>
[bodyTemplate]="tableBody" {%{{{ booksType[row.type] }}}%}
[rows]="10" </ng-template>
[scrollable]="true" </ngx-datatable-column>
> <ngx-datatable-column [name]="'::PublishDate' | abpLocalization" prop="publishDate">
</abp-table> <ng-template let-row="row" ngx-datatable-cell-template>
<ng-template #tableHeader> {%{{{ row.publishDate | date }}}%}
<tr> </ng-template>
<th>{%{{{ "::Name" | abpLocalization }}}%}</th> </ngx-datatable-column>
<th>{%{{{ "::Type" | abpLocalization }}}%}</th> <ngx-datatable-column [name]="'::Price' | abpLocalization" prop="price">
<th>{%{{{ "::PublishDate" | abpLocalization }}}%}</th> <ng-template let-row="row" ngx-datatable-cell-template>
<th>{%{{{ "::Price" | abpLocalization }}}%}</th> {%{{{ row.price | currency }}}%}
</tr> </ng-template>
</ng-template> </ngx-datatable-column>
<ng-template #tableBody let-data> </ngx-datatable>
<tr>
<td>{%{{{ data.name }}}%}</td>
<td>{%{{{ booksType[data.type] }}}%}</td>
<td>{%{{{ data.publishDate | date }}}%}</td>
<td>{%{{{ data.price }}}%}</td>
</tr>
</ng-template>
</div> </div>
</div> </div>
<!--added modal--> <!--added modal-->
<abp-modal [(visible)]="isModalOpen"> <abp-modal [(visible)]="isModalOpen">
<ng-template #abpHeader> <ng-template #abpHeader>
<h3>{%{{{ '::NewBook' | abpLocalization }}}%}</h3> <h3>{%{{{ '::NewBook' | abpLocalization }}}%}</h3>
</ng-template> </ng-template>
<ng-template #abpBody> </ng-template> <ng-template #abpBody> </ng-template>
<ng-template #abpFooter> <ng-template #abpFooter>
<button type="button" class="btn btn-secondary" #abpClose> <button type="button" class="btn btn-secondary" #abpClose>
{%{{{ 'AbpAccount::Close' | abpLocalization }}}%} {%{{{ 'AbpAccount::Close' | abpLocalization }}}%}
</button> </button>
</ng-template> </ng-template>
</abp-modal> </abp-modal>
``` ```
@ -603,55 +561,6 @@ Open `book-list.component.html` file in `books\book-list` folder and replace the
* `abp-modal` is a pre-built component to show modals. While you could use another approach to show a modal, `abp-modal` provides additional benefits. * `abp-modal` is a pre-built component to show modals. While you could use another approach to show a modal, `abp-modal` provides additional benefits.
* We added `New book` button to the `AbpContentToolbar`. * We added `New book` button to the `AbpContentToolbar`.
Open `book-list.component.ts` file in `app\book\book-list` folder and replace the content as below:
```js
import { Component, OnInit } from '@angular/core';
import { Select, Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { BookDto, BookType } from '../models';
import { GetBooks } from '../state/book.actions';
import { BookState } from '../state/book.state';
@Component({
selector: 'app-book-list',
templateUrl: './book-list.component.html',
styleUrls: ['./book-list.component.scss'],
})
export class BookListComponent implements OnInit {
@Select(BookState.getBooks)
books$: Observable<BookDto[]>;
booksType = BookType;
loading = false;
isModalOpen = false; // <== added this line ==>
constructor(private store: Store) {}
ngOnInit() {
this.get();
}
get() {
this.loading = true;
this.store
.dispatch(new GetBooks())
.pipe(finalize(() => (this.loading = false)))
.subscribe(() => {});
}
// added createBook method
createBook() {
this.isModalOpen = true;
}
}
```
* We added `isModalOpen = false` and `createBook` method.
You can open your browser and click **New book** button to see the new modal. You can open your browser and click **New book** button to see the new modal.
![Empty modal for new book](./images/bookstore-empty-new-book-modal.png) ![Empty modal for new book](./images/bookstore-empty-new-book-modal.png)
@ -663,48 +572,43 @@ You can open your browser and click **New book** button to see the new modal.
Open `book-list.component.ts` file in `app\book\book-list` folder and replace the content as below: Open `book-list.component.ts` file in `app\book\book-list` folder and replace the content as below:
```js ```js
import { ListService, PagedResultDto } from '@abp/ng.core';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Select, Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { BookDto, BookType } from '../models'; import { BookDto, BookType } from '../models';
import { GetBooks } from '../state/book.actions'; import { BookService } from '../services';
import { BookState } from '../state/book.state';
import { FormGroup, FormBuilder, Validators } from '@angular/forms'; // <== added this line ==> import { FormGroup, FormBuilder, Validators } from '@angular/forms'; // <== added this line ==>
@Component({ @Component({
selector: 'app-book-list', selector: 'app-book-list',
templateUrl: './book-list.component.html', templateUrl: './book-list.component.html',
styleUrls: ['./book-list.component.scss'], styleUrls: ['./book-list.component.scss'],
providers: [ListService],
}) })
export class BookListComponent implements OnInit { export class BookListComponent implements OnInit {
@Select(BookState.getBooks) book = { items: [], totalCount: 0 } as PagedResultDto<BookDto>;
books$: Observable<BookDto[]>;
booksType = BookType; booksType = BookType;
loading = false;
isModalOpen = false; isModalOpen = false;
form: FormGroup; // <== added this line ==> form: FormGroup; // <== added this line ==>
constructor(private store: Store, private fb: FormBuilder) {} // <== added FormBuilder ==> constructor(
public readonly list: ListService,
private bookService: BookService,
private fb: FormBuilder // <== injected FormBuilder ==>
) {}
ngOnInit() { ngOnInit() {
this.get(); const bookStreamCreator = (query) => this.bookService.getListByInput(query);
}
get() { this.list.hookToQuery(bookStreamCreator).subscribe((response) => {
this.loading = true; this.book = response;
this.store });
.dispatch(new GetBooks())
.pipe(finalize(() => (this.loading = false)))
.subscribe(() => {});
} }
createBook() { createBook() {
this.buildForm(); //<== added this line ==> this.buildForm(); // <== added this line ==>
this.isModalOpen = true; this.isModalOpen = true;
} }
@ -799,13 +703,10 @@ export class BookModule {}
Open `book-list.component.ts` file in `app\book\book-list` folder and replace the content as below: Open `book-list.component.ts` file in `app\book\book-list` folder and replace the content as below:
```js ```js
import { ListService, PagedResultDto } from '@abp/ng.core';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Select, Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { BookDto, BookType } from '../models'; import { BookDto, BookType } from '../models';
import { GetBooks } from '../state/book.actions'; import { BookService } from '../services';
import { BookState } from '../state/book.state';
import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { NgbDateNativeAdapter, NgbDateAdapter } from '@ng-bootstrap/ng-bootstrap'; // <== added this line ==> import { NgbDateNativeAdapter, NgbDateAdapter } from '@ng-bootstrap/ng-bootstrap'; // <== added this line ==>
@ -813,37 +714,34 @@ import { NgbDateNativeAdapter, NgbDateAdapter } from '@ng-bootstrap/ng-bootstrap
selector: 'app-book-list', selector: 'app-book-list',
templateUrl: './book-list.component.html', templateUrl: './book-list.component.html',
styleUrls: ['./book-list.component.scss'], styleUrls: ['./book-list.component.scss'],
providers: [{ provide: NgbDateAdapter, useClass: NgbDateNativeAdapter }], // <== added this line ==> providers: [ListService, { provide: NgbDateAdapter, useClass: NgbDateNativeAdapter }], // <== added a provide ==>
}) })
export class BookListComponent implements OnInit { export class BookListComponent implements OnInit {
@Select(BookState.getBooks) book = { items: [], totalCount: 0 } as PagedResultDto<BookDto>;
books$: Observable<BookDto[]>;
booksType = BookType; booksType = BookType;
//added bookTypeArr array // <== added bookTypeArr array ==>
bookTypeArr = Object.keys(BookType).filter( bookTypeArr = Object.keys(BookType).filter(
(bookType) => typeof this.booksType[bookType] === 'number' (bookType) => typeof this.booksType[bookType] === 'number'
); );
loading = false;
isModalOpen = false; isModalOpen = false;
form: FormGroup; form: FormGroup;
constructor(private store: Store, private fb: FormBuilder) {} constructor(
public readonly list: ListService,
private bookService: BookService,
private fb: FormBuilder
) {}
ngOnInit() { ngOnInit() {
this.get(); const bookStreamCreator = (query) => this.bookService.getListByInput(query);
}
get() { this.list.hookToQuery(bookStreamCreator).subscribe((response) => {
this.loading = true; this.book = response;
this.store });
.dispatch(new GetBooks())
.pipe(finalize(() => (this.loading = false)))
.subscribe(() => {});
} }
createBook() { createBook() {
@ -884,13 +782,10 @@ Now, you can open your browser to see the changes:
Open `book-list.component.ts` file in `app\book\book-list` folder and replace the content as below: Open `book-list.component.ts` file in `app\book\book-list` folder and replace the content as below:
```js ```js
import { ListService, PagedResultDto } from '@abp/ng.core';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Select, Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { BookDto, BookType } from '../models'; import { BookDto, BookType } from '../models';
import { GetBooks, CreateUpdateBook } from '../state/book.actions'; // <== added CreateUpdateBook ==> import { BookService } from '../services';
import { BookState } from '../state/book.state';
import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { NgbDateNativeAdapter, NgbDateAdapter } from '@ng-bootstrap/ng-bootstrap'; import { NgbDateNativeAdapter, NgbDateAdapter } from '@ng-bootstrap/ng-bootstrap';
@ -898,11 +793,10 @@ import { NgbDateNativeAdapter, NgbDateAdapter } from '@ng-bootstrap/ng-bootstrap
selector: 'app-book-list', selector: 'app-book-list',
templateUrl: './book-list.component.html', templateUrl: './book-list.component.html',
styleUrls: ['./book-list.component.scss'], styleUrls: ['./book-list.component.scss'],
providers: [{ provide: NgbDateAdapter, useClass: NgbDateNativeAdapter }], providers: [ListService, { provide: NgbDateAdapter, useClass: NgbDateNativeAdapter }],
}) })
export class BookListComponent implements OnInit { export class BookListComponent implements OnInit {
@Select(BookState.getBooks) book = { items: [], totalCount: 0 } as PagedResultDto<BookDto>;
books$: Observable<BookDto[]>;
booksType = BookType; booksType = BookType;
@ -910,24 +804,22 @@ export class BookListComponent implements OnInit {
(bookType) => typeof this.booksType[bookType] === 'number' (bookType) => typeof this.booksType[bookType] === 'number'
); );
loading = false;
isModalOpen = false; isModalOpen = false;
form: FormGroup; form: FormGroup;
constructor(private store: Store, private fb: FormBuilder) {} constructor(
public readonly list: ListService,
private bookService: BookService,
private fb: FormBuilder
) {}
ngOnInit() { ngOnInit() {
this.get(); const bookStreamCreator = (query) => this.bookService.getListByInput(query);
}
get() { this.list.hookToQuery(bookStreamCreator).subscribe((response) => {
this.loading = true; this.book = response;
this.store });
.dispatch(new GetBooks())
.pipe(finalize(() => (this.loading = false)))
.subscribe(() => {});
} }
createBook() { createBook() {
@ -950,19 +842,18 @@ export class BookListComponent implements OnInit {
return; return;
} }
this.store.dispatch(new CreateUpdateBook(this.form.value)).subscribe(() => { this.bookService.createByInput(this.form.value).subscribe(() => {
this.isModalOpen = false; this.isModalOpen = false;
this.form.reset(); this.form.reset();
this.get(); this.list.get();
}); });
} }
} }
``` ```
* We imported `CreateUpdateBook`.
* We added `save` method * We added `save` method
Open `book-list.component.html` in `app\book\book-list` folder and add the following `abp-button` to save the new book. Open `book-list.component.html` in `app\book\book-list` folder, find the `<ng-template #abpFooter>` element and replace this element with the following to create a new book.
```html ```html
<ng-template #abpFooter> <ng-template #abpFooter>
@ -994,63 +885,24 @@ The final modal UI looks like below:
### Updating a book ### Updating a book
#### CreateUpdateBook action Open `book-list.component.ts` in `app\book\book-list` folder and add a variable named `selectedBook`.
Open the `book.actions.ts` in `app\book\state` folder and replace the content as below:
```js
import { CreateUpdateBookDto } from '../models';
export class GetBooks {
static readonly type = '[Book] Get';
}
export class CreateUpdateBook {
static readonly type = '[Book] Create Update Book';
constructor(public payload: CreateUpdateBookDto, public id?: string) {} // <== added id parameter ==>
}
```
* We added `id` parameter to the `CreateUpdateBook` action's constructor.
Open the `book.state.ts` in `app\book\state` folder and replace the `save` method as below:
```js
@Action(CreateUpdateBook)
save(ctx: StateContext<BookStateModel>, action: CreateUpdateBook) {
if (action.id) {
return this.bookService.updateByIdAndInput(action.payload, action.id);
} else {
return this.bookService.createByInput(action.payload);
}
}
```
#### BookListComponent
Open `book-list.component.ts` in `app\book\book-list` folder and inject `BookService` dependency by adding it to the constructor and add a variable named `selectedBook`.
```js ```js
import { ListService, PagedResultDto } from '@abp/ng.core';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Select, Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { BookDto, BookType } from '../models'; import { BookDto, BookType } from '../models';
import { GetBooks, CreateUpdateBook } from '../state/book.actions'; import { BookService } from '../services';
import { BookState } from '../state/book.state';
import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { NgbDateNativeAdapter, NgbDateAdapter } from '@ng-bootstrap/ng-bootstrap'; import { NgbDateNativeAdapter, NgbDateAdapter } from '@ng-bootstrap/ng-bootstrap';
import { BookService } from '../services'; // <== imported BookService ==>
@Component({ @Component({
selector: 'app-book-list', selector: 'app-book-list',
templateUrl: './book-list.component.html', templateUrl: './book-list.component.html',
styleUrls: ['./book-list.component.scss'], styleUrls: ['./book-list.component.scss'],
providers: [{ provide: NgbDateAdapter, useClass: NgbDateNativeAdapter }], providers: [ListService, { provide: NgbDateAdapter, useClass: NgbDateNativeAdapter }],
}) })
export class BookListComponent implements OnInit { export class BookListComponent implements OnInit {
@Select(BookState.getBooks) book = { items: [], totalCount: 0 } as PagedResultDto<BookDto>;
books$: Observable<BookDto[]>;
booksType = BookType; booksType = BookType;
@ -1058,26 +910,24 @@ export class BookListComponent implements OnInit {
(bookType) => typeof this.booksType[bookType] === 'number' (bookType) => typeof this.booksType[bookType] === 'number'
); );
loading = false;
isModalOpen = false; isModalOpen = false;
form: FormGroup; form: FormGroup;
selectedBook = {} as BookDto; // <== declared selectedBook ==> selectedBook = {} as BookDto; // <== declared selectedBook ==>
constructor(private store: Store, private fb: FormBuilder, private bookService: BookService) {} //<== injected BookService ==> constructor(
public readonly list: ListService,
private bookService: BookService,
private fb: FormBuilder
) {}
ngOnInit() { ngOnInit() {
this.get(); const bookStreamCreator = (query) => this.bookService.getListByInput(query);
}
get() { this.list.hookToQuery(bookStreamCreator).subscribe((response) => {
this.loading = true; this.book = response;
this.store });
.dispatch(new GetBooks())
.pipe(finalize(() => (this.loading = false)))
.subscribe(() => {});
} }
// <== this method is replaced ==> // <== this method is replaced ==>
@ -1109,30 +959,31 @@ export class BookListComponent implements OnInit {
}); });
} }
// <== this method is replaced ==>
save() { save() {
if (this.form.invalid) { if (this.form.invalid) {
return; return;
} }
//<== added this.selectedBook.id ==> // <== added request ==>
this.store const request = this.selectedBook.id
.dispatch(new CreateUpdateBook(this.form.value, this.selectedBook.id)) ? this.bookService.updateByIdAndInput(this.form.value, this.selectedBook.id)
.subscribe(() => { : this.bookService.createByInput(this.form.value);
this.isModalOpen = false;
this.form.reset(); request.subscribe(() => {
this.get(); this.isModalOpen = false;
}); this.form.reset();
this.list.get();
});
} }
} }
``` ```
* We imported `BookService`.
* We declared a variable named `selectedBook` as `BookDto`. * We declared a variable named `selectedBook` as `BookDto`.
* We injected `BookService` to the constructor. `BookService` is being used to retrieve the book data which is being edited.
* We added `editBook` method. This method fetches the book with the given `Id` and sets it to `selectedBook` object. * We added `editBook` method. This method fetches the book with the given `Id` and sets it to `selectedBook` object.
* We replaced the `buildForm` method so that it creates the form with the `selectedBook` data. * We replaced the `buildForm` method so that it creates the form with the `selectedBook` data.
* We replaced the `createBook` method so it sets `selectedBook` to an empty object. * We replaced the `createBook` method so it sets `selectedBook` to an empty object.
* We added `selectedBook.id` to the constructor of the new `CreateUpdateBook`. * We replaced the `save` method.
#### Add "Actions" dropdown to the table #### Add "Actions" dropdown to the table
@ -1140,27 +991,14 @@ Open the `book-list.component.html` in `app\book\book-list` folder and replace
```html ```html
<div class="card-body"> <div class="card-body">
<abp-table <ngx-datatable [rows]="book.items" [count]="book.totalCount" [list]="list" default>
[value]="books$ | async" <!-- added actions column -->
[abpLoading]="loading" <ngx-datatable-column
[headerTemplate]="tableHeader" [name]="'::Actions' | abpLocalization"
[bodyTemplate]="tableBody" [maxWidth]="150"
[rows]="10" [sortable]="false"
[scrollable]="true" >
> <ng-template let-row="row" ngx-datatable-cell-template>
</abp-table>
<ng-template #tableHeader>
<tr>
<th>{%{{{ "::Actions" | abpLocalization }}}%}</th>
<th>{%{{{ "::Name" | abpLocalization }}}%}</th>
<th>{%{{{ "::Type" | abpLocalization }}}%}</th>
<th>{%{{{ "::PublishDate" | abpLocalization }}}%}</th>
<th>{%{{{ "::Price" | abpLocalization }}}%}</th>
</tr>
</ng-template>
<ng-template #tableBody let-data>
<tr>
<td>
<div ngbDropdown container="body" class="d-inline-block"> <div ngbDropdown container="body" class="d-inline-block">
<button <button
class="btn btn-primary btn-sm dropdown-toggle" class="btn btn-primary btn-sm dropdown-toggle"
@ -1168,25 +1006,37 @@ Open the `book-list.component.html` in `app\book\book-list` folder and replace
aria-haspopup="true" aria-haspopup="true"
ngbDropdownToggle ngbDropdownToggle
> >
<i class="fa fa-cog mr-1"></i>{%{{{ "::Actions" | abpLocalization }}}%} <i class="fa fa-cog mr-1"></i>{%{{{ '::Actions' | abpLocalization }}}%}
</button> </button>
<div ngbDropdownMenu> <div ngbDropdownMenu>
<button ngbDropdownItem (click)="editBook(data.id)"> <button ngbDropdownItem (click)="editBook(row.id)">
{%{{{ "::Edit" | abpLocalization }}}%} {%{{{ '::Edit' | abpLocalization }}}%}
</button> </button>
</div> </div>
</div> </div>
</td> </ng-template>
<td>{%{{{ data.name }}}%}</td> </ngx-datatable-column>
<td>{%{{{ booksType[data.type] }}}%}</td> <ngx-datatable-column [name]="'::Name' | abpLocalization" prop="name"></ngx-datatable-column>
<td>{%{{{ data.publishDate | date }}}%}</td> <ngx-datatable-column [name]="'::Type' | abpLocalization" prop="type">
<td>{%{{{ data.price }}}%}</td> <ng-template let-row="row" ngx-datatable-cell-template>
</tr> {%{{{ booksType[row.type] }}}%}
</ng-template> </ng-template>
</ngx-datatable-column>
<ngx-datatable-column [name]="'::PublishDate' | abpLocalization" prop="publishDate">
<ng-template let-row="row" ngx-datatable-cell-template>
{%{{{ row.publishDate | date }}}%}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column [name]="'::Price' | abpLocalization" prop="price">
<ng-template let-row="row" ngx-datatable-cell-template>
{%{{{ row.price | currency }}}%}
</ng-template>
</ngx-datatable-column>
</ngx-datatable>
</div> </div>
``` ```
- We added a `th` for the "Actions" column. - We added a `ngx-datatable-column` for the "Actions" column.
- We added `button` with `ngbDropdownToggle` to open actions when clicked the button. - We added `button` with `ngbDropdownToggle` to open actions when clicked the button.
- We have used to [NgbDropdown](https://ng-bootstrap.github.io/#/components/dropdown/examples) for the dropdown menu of actions. - We have used to [NgbDropdown](https://ng-bootstrap.github.io/#/components/dropdown/examples) for the dropdown menu of actions.
@ -1198,7 +1048,7 @@ Open `book-list.component.html` in `app\book\book-list` folder and find the `<ng
```html ```html
<ng-template #abpHeader> <ng-template #abpHeader>
<h3>{%{{{ (selectedBook.id ? 'AbpIdentity::Edit' : '::NewBook' ) | abpLocalization }}}%}</h3> <h3>{%{{{ (selectedBook.id ? '::Edit' : '::NewBook' ) | abpLocalization }}}%}</h3>
</ng-template> </ng-template>
``` ```
@ -1206,81 +1056,9 @@ Open `book-list.component.html` in `app\book\book-list` folder and find the `<ng
### Deleting a book ### Deleting a book
#### DeleteBook action
Open `book.actions.ts` in `app\book\state` folder and add an action named `DeleteBook`.
```js
export class DeleteBook {
static readonly type = '[Book] Delete';
constructor(public id: string) {}
}
```
Open the `book.state.ts` in `app\book\state` folder and replace the content as below:
```js
import { PagedResultDto } from '@abp/ng.core';
import { State, Action, StateContext, Selector } from '@ngxs/store';
import { GetBooks, CreateUpdateBook, DeleteBook } from './book.actions'; // <== added DeleteBook==>
import { BookService } from '../services';
import { tap } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { BookDto } from '../models';
export class BookStateModel {
public book: PagedResultDto<BookDto>;
}
@State<BookStateModel>({
name: 'BookState',
defaults: { book: {} } as BookStateModel,
})
@Injectable()
export class BookState {
@Selector()
static getBooks(state: BookStateModel) {
return state.book.items || [];
}
constructor(private bookService: BookService) {}
@Action(GetBooks)
get(ctx: StateContext<BookStateModel>) {
return this.bookService.getListByInput().pipe(
tap((booksResponse) => {
ctx.patchState({
book: booksResponse,
});
})
);
}
@Action(CreateUpdateBook)
save(ctx: StateContext<BookStateModel>, action: CreateUpdateBook) {
if (action.id) {
return this.bookService.updateByIdAndInput(action.payload, action.id);
} else {
return this.bookService.createByInput(action.payload);
}
}
// <== added DeleteBook action listener ==>
@Action(DeleteBook)
delete(ctx: StateContext<BookStateModel>, action: DeleteBook) {
return this.bookService.deleteById(action.id);
}
}
```
- We imported `DeleteBook` .
- We added `DeleteBook` action listener to the end of the file.
#### Delete confirmation popup #### Delete confirmation popup
Open `book-list.component.ts` in`app\book\book-list` folder and inject the `ConfirmationService`. Open `book-list.component.ts` in `app\book\book-list` folder and inject the `ConfirmationService`.
Replace the constructor as below: Replace the constructor as below:
@ -1289,11 +1067,11 @@ import { ConfirmationService } from '@abp/ng.theme.shared';
//... //...
constructor( constructor(
private store: Store, public readonly list: ListService,
private fb: FormBuilder, private bookService: BookService,
private bookService: BookService, private fb: FormBuilder,
private confirmation: ConfirmationService // <== added this line ==> private confirmation: ConfirmationService // <== added this line ==>
) { } ) {}
``` ```
* We imported `ConfirmationService`. * We imported `ConfirmationService`.
@ -1301,28 +1079,24 @@ constructor(
See the [Confirmation Popup documentation](https://docs.abp.io/en/abp/latest/UI/Angular/Confirmation-Service) See the [Confirmation Popup documentation](https://docs.abp.io/en/abp/latest/UI/Angular/Confirmation-Service)
In the `book-list.component.ts` add a delete method : In the `book-list.component.ts` add a delete method:
```js ```js
import { GetBooks, CreateUpdateBook, DeleteBook } from '../state/book.actions' ;// <== imported DeleteBook ==> import { ConfirmationService, Confirmation } from '@abp/ng.theme.shared'; //<== imported Confirmation namespace ==>
import { ConfirmationService, Confirmation } from '@abp/ng.theme.shared'; //<== imported Confirmation ==>
//... //...
delete(id: string) { delete(id: string) {
this.confirmation this.confirmation.warn('::AreYouSureToDelete', 'AbpAccount::AreYouSure').subscribe((status) => {
.warn('::AreYouSureToDelete', 'AbpAccount::AreYouSure') if (status === Confirmation.Status.confirm) {
.subscribe(status => { this.bookService.deleteById(id).subscribe(() => this.list.get());
if (status === Confirmation.Status.confirm) { }
this.store.dispatch(new DeleteBook(id)).subscribe(() => this.get()); });
}
});
} }
``` ```
The `delete` method shows a confirmation popup and subscribes for the user response. `DeleteBook` action dispatched only if user clicks to the `Yes` button. The confirmation popup looks like below: The `delete` method shows a confirmation popup and subscribes for the user response. The `deleteById` method of `BookService` called only if user clicks to the `Yes` button. The confirmation popup looks like below:
![bookstore-confirmation-popup](./images/bookstore-confirmation-popup.png) ![bookstore-confirmation-popup](./images/bookstore-confirmation-popup.png)
@ -1335,7 +1109,7 @@ Open `book-list.component.html` in `app\book\book-list` folder and modify the `n
```html ```html
<div ngbDropdownMenu> <div ngbDropdownMenu>
<!-- added Delete button --> <!-- added Delete button -->
<button ngbDropdownItem (click)="delete(data.id)"> <button ngbDropdownItem (click)="delete(row.id)">
{%{{{ 'AbpAccount::Delete' | abpLocalization }}}%} {%{{{ 'AbpAccount::Delete' | abpLocalization }}}%}
</button> </button>
</div> </div>

BIN
docs/en/Tutorials/images/bookstore-actions-buttons.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 25 KiB

BIN
docs/en/Tutorials/images/bookstore-angular-file-tree.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 57 KiB

After

Width:  |  Height:  |  Size: 49 KiB

BIN
docs/en/Tutorials/images/bookstore-book-list.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

After

Width:  |  Height:  |  Size: 55 KiB

BIN
docs/en/Tutorials/images/bookstore-confirmation-popup.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 22 KiB

BIN
docs/en/Tutorials/images/bookstore-empty-new-book-modal.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 102 KiB

After

Width:  |  Height:  |  Size: 56 KiB

Loading…
Cancel
Save