Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit, Input, ViewChild} from '@angular/core';
- import { Subscription } from "rxjs/Subscription";
- import { ActivatedRoute, Router } from "@angular/router";
- import { RequestManagementViewModel } from '../../view-models/abstract/organization-management-view-models/request-management-view-model';
- import { OrganizationManagementRequestService } from "../../services/concrete/organization-management/organization-management-request.service";
- import { GoodsTypeViewModel } from "../../view-models/concrete/goodsType-view.model";
- import { GoodsCategoryViewModel } from "../../view-models/concrete/goodsCategory-view.model";
- import { AmazonUploadComponent } from "../../shared/components/amazonUploader/amazon-upload.component";
- import { RequestedImageViewModel } from "../../view-models/abstract/organization-management-view-models/requested-item-view.model";
- import { SpinnerComponent } from "../../shared/components/spinner/spinner.component";
- @Component({
- selector: 'manage-request',
- template: require('./organization-manage-request.component.html'),
- styles: [require('./organization-manage-request.component.css')],
- providers: [OrganizationManagementRequestService]
- })
- export class OrganizationManageRequestComponent implements OnInit {
- public uploader: AmazonUploadComponent = new AmazonUploadComponent();
- private _requestedItem: RequestManagementViewModel = new RequestManagementViewModel();
- private _errorMessage: string;
- private _goodsTypes: GoodsTypeViewModel[];
- private _selecteType: GoodsTypeViewModel;
- private _subscription: Subscription;
- private _sub: Subscription;
- private _requestedItemId: number;
- private _currentOrgId: number;
- private _currentImageUrl: string[] = [];
- //@ViewChild(SpinnerComponent) spinner: SpinnerComponent;
- ngOnInit(): void {
- this._requestedItem.images = [];
- this.fillGoodtypes();
- this._route.params.subscribe(params => {
- this._currentOrgId = +params["idOrganization"];
- this._requestedItemId = +params["idRequest"];
- });
- if (this._requestedItemId > 0) {
- this.getByIdRequestedItem(this._requestedItemId);
- }
- }
- /**
- * Initialize new instance of OrganizationCreateRequestComponent
- * @param _service
- */
- constructor(private _service: OrganizationManagementRequestService,
- private _route: ActivatedRoute,
- private _router: Router) { }
- /**
- * Fills goods type dropdown
- */
- private fillGoodtypes() {
- this._service.getAllGoodsTypes()
- .subscribe(r => {
- this._goodsTypes = r;
- },
- error => {
- this._errorMessage = <any>error;
- })
- }
- /**
- * Add new requested item
- */
- private addRequestedItem() {
- this._service.addRequestedItem(this._requestedItem)
- .subscribe(error => this._errorMessage = <any>error);
- }
- /**
- * Edit requested item
- * @param itemId
- */
- private getByIdRequestedItem(itemId: number) {
- this._service.getRequestedItemById(itemId)
- .subscribe(c => {
- if (c.errorMessage != "") {
- this._requestedItem = c;
- this.setGoodsType(this._requestedItem.goodsTypeId);
- }
- else {
- this._errorMessage = c.errorMessage;
- }
- },
- error => this._errorMessage = <any>error);
- }
- private deleteCurrentImage(currentImage: RequestedImageViewModel) {
- if (currentImage.id > 0) {
- this._service.deleteCurrentImage(currentImage.id)
- .subscribe(data => this.deleteImageFromList(currentImage.imageUrl),
- error => this._errorMessage = <any>error);
- }
- else {
- this.deleteImageFromList(currentImage.imageUrl);
- }
- }
- private deleteImageFromList(imageUrl: string) {
- this._requestedItem.images.splice(this._requestedItem.images.findIndex(i => i.imageUrl == imageUrl), 1)
- }
- /**
- * Edit requested item
- * @param item
- */
- private editRequestetItem(item: RequestManagementViewModel) {
- this._service.editRequestedItem(item)
- .subscribe(r => this._requestedItem = r,
- error => this._errorMessage = <any>error);
- }
- /**
- * Manage requested items wich method will be called
- */
- private manageRequestedItems() {
- if (this._requestedItemId > 0) {
- this.editRequestetItem(this._requestedItem);
- }
- else {
- this.addRequestedItem();
- }
- }
- private setGoodsType(goodTypeId: number) {
- this._selecteType = this._goodsTypes.find(c => c.id == goodTypeId);
- }
- /**
- * Gets extension of specified file
- * @param fileName: name of the file extension of which is needed to be retrieved
- */
- private getFileExtension(fileName: string): string {
- return fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length) || fileName;
- }
- /**
- * Saves passed file in Amazon Web Storage
- * @param fileInput: file to be saved in AWS
- */
- private saveFileInAws(fileInput: any): void {
- var that = this;
- var maxFileSize = 4000000;
- let file = fileInput.target.files[0];
- if (file) {
- var currentDate = new Date();
- let uploadedFileName = this._requestedItem.name + currentDate.getHours() + currentDate.getMinutes() + currentDate.getSeconds() + '.' + this.getFileExtension(file.name);
- if (file.size != null && file.size < maxFileSize) {
- this.uploader.UploadImageToAmazon(file, uploadedFileName).then(function (data) {
- let requestedItemImage = new RequestedImageViewModel();
- requestedItemImage.requestedItemId = 0;
- requestedItemImage.imageUrl = data.Location;
- console.log(data.Location);
- if (that._requestedItem.images == null) {
- that._requestedItem.images = [];
- }
- that._requestedItem.images.push(requestedItemImage);
- })
- }
- else {
- alert('Розмр файлу не може перевищувати ' + Math.ceil(maxFileSize / 1000000) + 'МБ');
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement