Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
  4. import { Inject } from '@angular/core';
  5. import { FormBuilder, FormControl, FormGroup, FormArray, Validators, ReactiveFormsModule } from '@angular/forms';
  6.  
  7. import { GridOptions } from "ag-grid-community";
  8.  
  9. import { actions } from '../shared/actions/actions';
  10. import { ApiService } from '../services/api.service';
  11. import { CrudService } from '../services/crud.service';
  12. import { GtoolsService } from '../services/gtools.service';
  13.  
  14. @Component({
  15.   selector: 'app-airline-contract',
  16.   templateUrl: '../shared/views/one-grid.html'
  17. })
  18. export class BunkerComponent implements OnInit {
  19.  
  20.   private entity = 'Bunker';
  21.   private gridApi;
  22.   public gridOptions: GridOptions;
  23.   rowData: any;
  24.   params: any;
  25.   airlines: any;
  26.   companies: any;
  27.  
  28.   errors = [];
  29.  
  30.   // General view init
  31.   listIcon = 'business';
  32.   listTitle = 'Bunker';
  33.  
  34.   // Column definition
  35.   columnDefs = [
  36.     { headerName: '#', field: 'id', width: 75 },
  37.     { headerName: 'JobNo', field: 'job_no' },
  38.     { headerName: 'Customers', field: 'customers.name' },
  39.     { headerName: 'Quay', field: 'quay.name' },
  40.     { headerName: 'Vessel', field: 'vessel_name.name' },
  41.     { headerName: 'Quantity', field: 'quantity' },
  42.     { headerName: 'Shore Tank', field: 'shore_tank_no' }    
  43.   ];
  44.  
  45.   constructor(
  46.     public httpClient: HttpClient,
  47.     private _api: ApiService,
  48.     private _crud: CrudService,
  49.     private _gt: GtoolsService
  50.   ) {
  51.     // Add column action to grid
  52.     this.columnDefs.push(actions.action);
  53.  
  54.     this.gridOptions = <GridOptions>{
  55.       context: {
  56.         componentParent: this
  57.       },
  58.       enableColResize: true,
  59.       suppressCellSelection: true,
  60.     };
  61.   }
  62.  
  63.   ngOnInit() {
  64.   }
  65.  
  66.   onGridReady(params) {
  67.     // Parent list
  68.     this._api.get(this.entity, 0)
  69.       .subscribe(
  70.         response => {
  71.           this.rowData = response['data'];
  72.           params.api.sizeColumnsToFit();
  73.         },
  74.         err => console.log(err)
  75.       );
  76.  
  77.     // Dropdown list for Airlines eg [{"value":1, "label":3}]
  78.     this._api.get('customer', 1)
  79.       .subscribe(
  80.         response => {
  81.           this.airlines = response['data'];
  82.         },
  83.         err => console.log(err)
  84.       );
  85.     // Dropdown list for Companies eg [{"value":1, "label":3}]
  86.     this._api.get('Company', 1)
  87.       .subscribe(
  88.         response => {
  89.           this.companies = response['data'];
  90.         },
  91.         err => console.log(err)
  92.       );
  93.     this.gridApi = params.api;
  94.     this.params = params;
  95.   }
  96.  
  97.   // Update or View  a row
  98.   openDialog(params, crudType) {
  99.     params.airlines = this.airlines;
  100.     params.companies = this.companies;
  101.  
  102.     // modalComponent, entity, params, crudType, gridApi, errors
  103.     this._crud.openDialog(BunkerModalComponent, this.entity, params, crudType, this.gridApi, this.errors);
  104.   }
  105.  
  106.   // Create new row
  107.   openCreateDialog() {
  108.     let params = {};
  109.     params['airlines'] = this.airlines;
  110.     params['companies'] = this.companies;
  111.  
  112.     // modalComponent, entity, params, gridApi, errors
  113.     this._crud.openCreateDialog(BunkerModalComponent, this.entity, params, this.gridApi, this.errors);
  114.   }
  115.  
  116.   // Delete selected row
  117.   deleteRow(params) {
  118.     // params, entity, gridApi
  119.     this._crud.deleteRow(params, this.entity, this.gridApi);
  120.   }
  121.  
  122. }
  123. /*
  124. *   Airline Contract Modal Component
  125. */
  126.  
  127. @Component({
  128.   selector: 'app-bunker-modal',
  129.   templateUrl: './bunker-modal.component.html'
  130. })
  131. export class BunkerModalComponent implements OnInit {
  132.  
  133.   modalIcon: string;
  134.   modalTitle = 'Bunker';
  135.   crudType: string;
  136.   form: FormGroup;
  137.   idText: string = ''; // Display id on edit/view header
  138.  
  139.   params: any;
  140.   airlines: any;
  141.   companies: any;
  142.  
  143.   constructor(
  144.     private _api: ApiService,
  145.     private _gt: GtoolsService,
  146.     private fb: FormBuilder,
  147.     private dialogRef: MatDialogRef<BunkerModalComponent>,
  148.     @Inject(MAT_DIALOG_DATA) public data: any, public HttpClient: HttpClient) {
  149.  
  150.     this.crudType = data.crudType;
  151.     this.modalIcon = (this.crudType == 'new' ? 'add' : (this.crudType == 'view' ? 'visibility' : 'edit'));
  152.     this.params = data.params;
  153.     this.airlines = data.params.airlines;
  154.     this.companies = data.params.companies;
  155.   }
  156.  
  157.   ngOnInit() {
  158.     // console.log(this.params);
  159.  
  160.     if (typeof this.params.data !== 'undefined') {
  161.       this.idText = '(id' + this.params.data.id + ')';
  162.       this.form = this.fb.group({      
  163.         customer: [this.params.data.airline.id],
  164.        
  165.       });
  166.     } else {
  167.  
  168.       this.form = this.fb.group({
  169.         customer: [''],      
  170.       });
  171.  
  172.     }
  173.  
  174.   }
  175.  
  176.   save() {
  177.     // Format bay value from select formfield
  178.     let company = null;
  179.     if (this.companies && this.form.value.company) {
  180.       company = this.companies.find(s => s.value == this.form.value.company);
  181.       this.form.value.company = { 'id': company.value, 'name': company.label };
  182.     }
  183.     let airline = null;
  184.     if (this.airlines && this.form.value.airline) {
  185.       airline = this.airlines.find(s => s.value == this.form.value.airline);
  186.       this.form.value.airline = { 'id': this.form.value.airline, 'name': airline.label };
  187.     }
  188.  
  189.     // Format to insert in DB
  190.     this.form.value.validity_start = this._gt.dbDateFormatter(this.form.value.validity_start);
  191.     // Format to include today in where conditions
  192.     this.form.value.validity_end = this._gt.todayIncludedFormatter(this.form.value.validity_end);
  193.  
  194.     console.log(this.form.value);
  195.     this.dialogRef.close(this.form.value);
  196.   }
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement