Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, Input } from '@angular/core';
  2. import { Subscription } from "rxjs/Subscription";
  3. import { ActivatedRoute, Router } from "@angular/router";
  4. import { RequestManagementViewModel } from '../../view-models/abstract/organization-management-view-models/request-management-view-model';
  5. import { OrganizationManagementRequestService } from "../../services/concrete/organization-management/organization-management-request.service";
  6. import { GoodsTypeViewModel } from "../../view-models/concrete/goodsType-view.model";
  7. import { GoodsCategoryViewModel } from "../../view-models/concrete/goodsCategory-view.model";
  8. import { AmazonUploadComponent } from "../../shared/components/amazonUploader/amazon-upload.component";
  9. import { RequestedImageViewModel } from "../../view-models/abstract/organization-management-view-models/requested-item-view.model";
  10.  
  11. @Component({
  12.     selector: 'manage-request',
  13.     template: require('./organization-manage-request.component.html'),
  14.     styles: [require('./organization-manage-request.component.css')],
  15.     providers: [OrganizationManagementRequestService]
  16. })
  17.  
  18. export class OrganizationManageRequestComponent implements OnInit {
  19.     public uploader: AmazonUploadComponent = new AmazonUploadComponent();
  20.  
  21.     private _requestedItem: RequestManagementViewModel = new RequestManagementViewModel();
  22.     private _errorMessage: string;
  23.     private _goodsTypes: GoodsTypeViewModel[];
  24.     private _selecteType: GoodsTypeViewModel;
  25.     private _subscription: Subscription;
  26.     private _requestedItemId: number;
  27.     private _currentOrgId: number;
  28.     private _currentImageUrl: string[] = [];
  29.  
  30.  
  31.     ngOnInit(): void {
  32.         this.fillGoodtypes();
  33.         this._route.parent.params.subscribe(params => {
  34.             this._currentOrgId = +params["id"];
  35.         });
  36.         this._subscription = this._route
  37.             .params.subscribe(params => {
  38.                 this._requestedItemId = +params["id"];
  39.             });
  40.  
  41.         if (this._requestedItemId > 0) {
  42.             this.getByIdRequestedItem(this._requestedItemId);
  43.         }      
  44.     }
  45.    
  46.     /**
  47.      * Initialize new instance of OrganizationCreateRequestComponent
  48.      * @param _service
  49.      */
  50.     constructor(private _service: OrganizationManagementRequestService,
  51.                 private _route: ActivatedRoute,
  52.                 private _router: Router) { }
  53.  
  54.     /**
  55.      * Fills goods type dropdown
  56.      */
  57.     private fillGoodtypes() {
  58.         this._service.getAllGoodsTypes()
  59.             .subscribe(r => {
  60.                 this._goodsTypes = r;
  61.             },
  62.             error => {
  63.                 this._errorMessage = <any>error;
  64.             })
  65.     }
  66.  
  67.     /**
  68.      * Add new requested item
  69.      */
  70.     private addRequestedItem() {
  71.         console.log(this._requestedItem);
  72.         this._service.addRequestedItem(this._requestedItem)
  73.             .subscribe(error => this._errorMessage = <any>error);
  74.     }
  75.  
  76.     /**
  77.      * Edit requested item
  78.      * @param itemId
  79.      */
  80.     private getByIdRequestedItem(itemId: number) {
  81.         this._service.getRequestedItemById(itemId)
  82.             .subscribe(c => {
  83.                 if (c.errorMessage != "") {
  84.                     this._requestedItem = c;
  85.                     this.setGoodsType(this._requestedItem.goodsTypeId);
  86.                 }
  87.                 else {
  88.                     this._errorMessage = c.errorMessage;
  89.                 }
  90.             },
  91.             error => this._errorMessage = <any>error);
  92.     }
  93.  
  94.     /**
  95.      * Edit requested item
  96.      * @param item
  97.      */
  98.     private editRequestetItem(item: RequestManagementViewModel) {
  99.         this._service.editRequestedItem(item)
  100.             .subscribe(r => this._requestedItem = r,
  101.             error => this._errorMessage = <any>error);
  102.     }
  103.  
  104.     /**
  105.      * Manage requested items wich method will be called
  106.      */
  107.     private manageRequestedItems() {
  108.         console.log(this._requestedItem)
  109.         if (this._requestedItemId > 0) {
  110.             this.editRequestetItem(this._requestedItem);
  111.             //this._router.navigate(['/all-requests'])
  112.         }
  113.         else {
  114.             this.addRequestedItem();
  115.         }
  116.     }
  117.  
  118.     private setGoodsType(goodTypeId: number) {
  119.         this._selecteType = this._goodsTypes.find(c => c.id == goodTypeId);
  120.         console.log("sel typ:"+this._selecteType);
  121.     }
  122.  
  123.     /**
  124.      * Gets extension of specified file
  125.      * @param fileName: name of the file extension of which is needed to be retrieved
  126.      */
  127.     private getFileExtension(fileName: string): string {
  128.         return fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length) || fileName;
  129.     }
  130.  
  131.     /**
  132.      * Saves passed file in Amazon Web Storage
  133.      * @param fileInput: file to be saved in AWS
  134.      */
  135.     private saveFileInAws(fileInput: any): void {
  136.         var that = this;
  137.         var maxFileSize = 4000000;
  138.         let file = fileInput.target.files[0];
  139.         let uploadedFileName = this._requestedItem.name + '.' + this.getFileExtension(file.name);
  140.         if (file.size != null && file.size < maxFileSize) {
  141.             this.uploader.UploadImageToAmazon(file, uploadedFileName).then(function (data) {
  142.                 let requestedItemImage = new RequestedImageViewModel();
  143.                 requestedItemImage.requestedItemId = that._requestedItemId;
  144.                 requestedItemImage.url = data.Location;
  145.  
  146.                 that._requestedItem.images.push(requestedItemImage);
  147.             })
  148.         }
  149.         else {
  150.             alert('Розмр файлу не може перевищувати ' + Math.ceil(maxFileSize / 1000000) + 'МБ');
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement