diff --git a/src/app/pages/catalog/catalog-routing.module.ts b/src/app/pages/catalog/catalog-routing.module.ts index 92896bf..175de81 100644 --- a/src/app/pages/catalog/catalog-routing.module.ts +++ b/src/app/pages/catalog/catalog-routing.module.ts @@ -3,13 +3,17 @@ import { Routes, RouterModule } from '@angular/router'; import { ProductsComponent } from './products/products.component'; import { CatalogComponent } from './catalog.component'; +import { PreparadoresComponent } from './preparadores/preparadores.component'; const routes: Routes = [{ path: '', - component: ProductsComponent, + component: CatalogComponent, children: [{ path: 'products', - component: CatalogComponent, + component: ProductsComponent, + }, { + path: 'preparadores', + component: PreparadoresComponent, }], }]; @@ -22,4 +26,5 @@ export class CatalogRoutingModule { } export const routedComponents = [ ProductsComponent, CatalogComponent, + PreparadoresComponent, ]; diff --git a/src/app/pages/catalog/preparadores/preparadores.component.html b/src/app/pages/catalog/preparadores/preparadores.component.html new file mode 100644 index 0000000..34af83d --- /dev/null +++ b/src/app/pages/catalog/preparadores/preparadores.component.html @@ -0,0 +1,133 @@ + + + Preparadores CRUD + + + + + + + + + + + + + +
+ + +
+
+ + + + + + + diff --git a/src/app/pages/catalog/preparadores/preparadores.component.scss b/src/app/pages/catalog/preparadores/preparadores.component.scss new file mode 100644 index 0000000..33a91ed --- /dev/null +++ b/src/app/pages/catalog/preparadores/preparadores.component.scss @@ -0,0 +1,29 @@ +@import '../../../@theme/styles/themes'; +@import '~bootstrap/scss/mixins/breakpoints'; +@import '~@nebular/theme/styles/global/bootstrap/breakpoints'; + +@include nb-install-component() { + + nb-card-footer { + padding-bottom: 0.25rem; + + button { + @include nb-ltr(margin, 0 1rem 1rem 0); + @include nb-rtl(margin, 0 0 1rem 1rem); + } + } + + /* stylelint-disable */ + toaster-container /deep/ { + #toast-container .toast-close-button { + right: 0; + } + } + /* stylelint-enable */ + + @include media-breakpoint-down(xs) { + .dropdown-toggle { + font-size: 0.75rem; + } + } +} \ No newline at end of file diff --git a/src/app/pages/catalog/preparadores/preparadores.component.ts b/src/app/pages/catalog/preparadores/preparadores.component.ts new file mode 100644 index 0000000..b846d1f --- /dev/null +++ b/src/app/pages/catalog/preparadores/preparadores.component.ts @@ -0,0 +1,229 @@ +import { Component, OnInit } from '@angular/core'; +import { LocalDataSource } from 'ng2-smart-table'; +import { Preparadormodel } from './preparadores.model'; +import { SmartTableService } from '../../../@core/data/smart-table.service'; +import { NgbModal, NgbModalOptions } from '@ng-bootstrap/ng-bootstrap'; +import { error } from '@angular/compiler/src/util'; +import { ToasterService, ToasterConfig, Toast, BodyOutputType } from 'angular2-toaster'; +import 'style-loader!angular2-toaster/toaster.css'; +import { BaseService } from '../../../app.base.service' + +/** + * Component for crud of preparadores + * + * @export + * @class PreparadoresComponent + */ +@Component({ + selector: 'mac-preparadores', + templateUrl: './preparadores.component.html', + styles: ['./preparadores.component.scss'], + providers: [BaseService] +}) + +export class PreparadoresComponent { + + // smart-table settings + settings = { + mode: 'external', + actions: { + columnTitle: 'Acciones', + position: 'right' + }, + add: { + title:'Agregar', + addButtonContent: '' + }, + edit: { + editButtonContent: '' + }, + delete: { + deleteButtonContent: '' + }, + columns: { + id_document: { + title: 'Cédula', + type: 'string', + editable: false + }, + initials: { + title: 'Iniciales', + type: 'string', + }, + first_name: { + title: 'Nombre', + type: 'string', + }, + last_name: { + title: 'Apellido', + type: 'string', + }, + email: { + title: 'Correo', + type: 'string', + }, + }, + }; + + source: LocalDataSource = new LocalDataSource(); + preparadormodel: Preparadormodel = new Preparadormodel(); + activeModal: any; + apiurl = "assistant/" + config:ToasterConfig = new ToasterConfig({ + positionClass: 'toast-top-full-width', + timeout: 30000, + tapToDismiss: true, + showCloseButton: true + }); + edit: boolean = false; + + + /** + *Creates an instance of PreparadoresComponent. + * @param {SmartTableService} service + * @param {BaseService} preparadoresService + * @param {NgbModal} modalService + * @param {ToasterService} toasterService + * @memberof PreparadoresComponent + */ + constructor(private service: SmartTableService, + private preparadoresService: BaseService, + private modalService: NgbModal, + private toasterService: ToasterService) { + this.getPreparadores() + } + + /** + * Get preparadores and show their info on smart-table + * + * @memberof PreparadoresComponent + */ + getPreparadores(): void{ + this.preparadoresService.getBase(this.apiurl).subscribe(preparadores => + this.source.load(preparadores)) + } + + /** + * Confirm method for delete preparador + * + * @param {preparadormodel} preparador to be deleted + * @memberof PreparadoresComponent + */ + onDeleteConfirm(preparador: Preparadormodel): void { + let apiurl = `${this.apiurl}${preparador.id_document}/` + this.preparadoresService.deleteBase(preparador, apiurl).subscribe(preparador =>{ + if (preparador == null){ + this.activeModal.dismiss() + this.getPreparadores() + }else{ + this.showErrorMessage(preparador.error) + } + }) + } + + /** + * Method that makes the update for a preparador info + * + * @memberof PreparadoresComponent + */ + editPreparador():void { + let apiurl = `${this.apiurl}${this.preparadormodel.id_document}/` + this.preparadoresService.updateBase(this.preparadormodel, apiurl).subscribe(preparador =>{ + if (!preparador.error){ + console.log(preparador) + this.activeModal.dismiss() + this.getPreparadores() + }else{ + this.showErrorMessage(preparador.error) + } + } + ) + } + + /** + * Activate create preparador modal + * + * @param {*} content Template to be render + * @memberof PreparadoresComponent + */ + createPreparadorModal(content): void{ + this.activeModal = this.modalService.open(content, { size: 'lg'}) + } + + /** + * Activate edit preparador modal + * + * @param {*} content Template to be render + * @param {*} event with preparador info + * @memberof PreparadoresComponent + */ + editPreparadorModal(content, event): void{ + this.edit = true + Object.assign(this.preparadormodel, event.data) + const modal_options : NgbModalOptions = { + size: 'lg', + beforeDismiss: () => { + this.preparadormodel = new Preparadormodel(); + this.edit = false + return true + } + } + this.activeModal = this.modalService.open(content, modal_options) + } + + /** + * Delete preparador modal + * + * @param {*} content to be render + * @param {*} event with preparador info + * @memberof PreparadoresComponent + */ + deletePreparadorModal(content, event): void{ + Object.assign(this.preparadormodel, event.data) + const modal_options : NgbModalOptions = { + size: 'lg', + beforeDismiss: () => { + this.preparadormodel = new Preparadormodel(); + return true + } + } + this.activeModal = this.modalService.open(content, modal_options) + } + + /** + * Add preparador to DB + * + * @memberof PreparadoresComponent + */ + createPreparador(): void{ + this.preparadoresService.addBase(this.preparadormodel, this.apiurl).subscribe(preparador =>{ + if(preparador){ + if(!preparador.error){ + this.activeModal.dismiss() + this.getPreparadores() + }else{ + this.showErrorMessage(preparador.error) + } + } + } + ) + } + + /** + * Method to show notifications + * when some error occurs + * + * @param {*} object with error info + * @memberof PreparadoresComponent + */ + showErrorMessage(object): void{ + let error_message = "" + for ( var key in object){ + if (object.hasOwnProperty(key)){ + error_message += object[key] + "\n" + } + } + this.toasterService.pop('warning', 'Your request has the following errors:\n', + error_message) + } +} diff --git a/src/app/pages/catalog/preparadores/preparadores.model.ts b/src/app/pages/catalog/preparadores/preparadores.model.ts new file mode 100644 index 0000000..693ffcb --- /dev/null +++ b/src/app/pages/catalog/preparadores/preparadores.model.ts @@ -0,0 +1,13 @@ +/** + * @export + * @class Preparadormodel + */ +export class Preparadormodel { + id_document: string; + initials: string; + first_name: string; + last_name: string; + debt_amount: number; + debt_date: string; + email: string; +} diff --git a/src/app/pages/catalog/preparadores/preparadores.service.ts b/src/app/pages/catalog/preparadores/preparadores.service.ts new file mode 100644 index 0000000..01b66cd --- /dev/null +++ b/src/app/pages/catalog/preparadores/preparadores.service.ts @@ -0,0 +1,108 @@ +import { of, Observable } from 'rxjs'; +import { Injectable } from '@angular/core'; +import { Preparadormodel } from './preparadores.model'; +import { catchError, tap } from 'rxjs/operators' +import { HttpHeaders, HttpClient } from '@angular/common/http' + +const httpOptions = { + headers: new HttpHeaders({'Content-Type': 'application/json'}) +} + + +/** + * Service with methods to add, edit or + * delete preparadores. + * + * @export + * @class PreparadorService + */ +@Injectable() +export class PreparadorService { + + API_URL:string = "https://mactaquilla.herokuapp.com/taquilla-api" + + /** + *Creates an instance of PreparadorService. + * @param {HttpClient} http + * @memberof PreparadorService + */ + constructor( + private http: HttpClient + ){} + + /** + * Method to get all Preparadores + * + * @returns {Observable} Return list of Preparadores + * if everything is ok or error if not + * @memberof PreparadorService + */ + getPreparadores():Observable{ + let apiURL = `${this.API_URL}/assistant/` + return this.http.get(apiURL).pipe( + catchError(this.handleError('getPreparadores')) + ) + } + + /** + * Method to add Preparador + * + * @param {Preparadormodel} preparador to be added + * @returns {Observable} Return Preparador if everything is ok + * or error if not + * @memberof PreparadorService + */ + addPreparador(preparador: Preparadormodel):Observable{ + let apiURL = `${this.API_URL}/assistant/` + return this.http.post(apiURL, preparador, httpOptions).pipe( + catchError(this.handleError('addPreparadores')) + ) + } + + /** + * Method to edit Preparador + * + * @param {Preparadormodel} Preparador to be edited + * @returns {Observable} return Preparador if everythings is ok + * or error if not + * @memberof PreparadorService + */ + updatePreparador(preparador: Preparadormodel):Observable{ + let apiURL = `${this.API_URL}/assistant/${preparador.id_document}/` + return this.http.put(apiURL, preparador, httpOptions).pipe( + tap(_ => console.log(`update preparador ${preparador.id_document}`)), + catchError(this.handleError('updatePreparador')) + ) + } + + /** + * Method to delete preparador + * + * @param {Preparadormodel} Preparador to be deleted + * @returns {Observable} return null if everythings is ok + * or error if not + * @memberof PreparadorService + */ + deletePreparador(preparador: Preparadormodel):Observable{ + let apiURL = `${this.API_URL}/assistant/${preparador.id_document}/` + return this.http.delete(apiURL, httpOptions).pipe( + catchError(this.handleError('deletePreparador')) + ) + } + + + /** + * Method to handle errors + * + * @private + * @template T + * @param {string} [operation='operation'] operation that generates the error + * @returns Observable with error info + * @memberof PreparadorService + */ + private handleError(operation = 'operation'){ + return (error_object: any):Observable =>{ + return of(error_object) + } + } +} \ No newline at end of file diff --git a/src/app/pages/pages-menu.ts b/src/app/pages/pages-menu.ts index e94ca64..b92c9a2 100644 --- a/src/app/pages/pages-menu.ts +++ b/src/app/pages/pages-menu.ts @@ -7,10 +7,6 @@ export const MENU_ITEMS: NbMenuItem[] = [ link: '/pages/dashboard', home: true, }, - { - title: 'Rama JT', - group: true, - }, { title: 'Catalogo', icon: 'nb-coffee-maker', @@ -19,14 +15,15 @@ export const MENU_ITEMS: NbMenuItem[] = [ { title: 'Articulos', link: '/pages/catalog/products', - }, - ], + }, { + title: 'Preparadores', + link: '/pages/catalog/preparadores', + }], }, { title: 'FEATURES', group: true, - }, - { + },{ title: 'UI Features', icon: 'nb-keypad', link: '/pages/ui-features', diff --git a/src/app/pages/pages-routing.module.ts b/src/app/pages/pages-routing.module.ts index e7355b8..933d9d0 100644 --- a/src/app/pages/pages-routing.module.ts +++ b/src/app/pages/pages-routing.module.ts @@ -4,6 +4,7 @@ import { NgModule } from '@angular/core'; import { PagesComponent } from './pages.component'; import { DashboardComponent } from './dashboard/dashboard.component'; import { NotFoundComponent } from './miscellaneous/not-found/not-found.component'; +import { PreparadoresComponent } from './catalog/preparadores/preparadores.component'; const routes: Routes = [{ path: '', @@ -42,10 +43,12 @@ const routes: Routes = [{ path: '', redirectTo: 'dashboard', pathMatch: 'full', - }, { + },{ path: '**', component: NotFoundComponent, - }], + }, + + ], }]; @NgModule({ diff --git a/src/app/pages/pages.module.ts b/src/app/pages/pages.module.ts index adcae4a..a86f27c 100644 --- a/src/app/pages/pages.module.ts +++ b/src/app/pages/pages.module.ts @@ -5,6 +5,8 @@ import { DashboardModule } from './dashboard/dashboard.module'; import { PagesRoutingModule } from './pages-routing.module'; import { ThemeModule } from '../@theme/theme.module'; import { MiscellaneousModule } from './miscellaneous/miscellaneous.module'; +import { Ng2SmartTableModule } from 'ng2-smart-table'; +import { ToasterModule } from 'angular2-toaster'; const PAGES_COMPONENTS = [ PagesComponent, @@ -16,6 +18,8 @@ const PAGES_COMPONENTS = [ ThemeModule, DashboardModule, MiscellaneousModule, + Ng2SmartTableModule, + ToasterModule.forRoot(), ], declarations: [ ...PAGES_COMPONENTS,