SHARE
TWEET

Untitled

a guest Oct 21st, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, Input, OnDestroy } from '@angular/core';
  2. import { FormGroup } from '@angular/forms';
  3. import { NotificationsService } from 'angular2-notifications';
  4. import { map, take, switchMap } from 'rxjs/operators';
  5. import { Subscription, combineLatest } from 'rxjs';
  6. import { FoSharedDataService } from '../../core/fo-shared-data.service';
  7. import { FoSequenceSubscribeDataService } from './data/fo-sequence-subscribe-data.service';
  8. import { SequenceItem } from './data/model/fo-sequence.model';
  9. import { FoSequenceSubscribeApiService } from './data/fo-sequence-subscribe-api.service';
  10. import { FoUnirsonListDataService } from '../fo-unirson-list/data/fo-unirson-list-data.service';
  11. import { foShowErrorNotification } from '../../shared/utils/fo-error-handler';
  12.  
  13. @Component({
  14.   selector: 'fo-sequence-subscribe-modal',
  15.   templateUrl: './fo-sequence-subscribe-modal.component.html',
  16.   styleUrls: ['./fo-sequence-subscribe-modal.component.scss'],
  17.   providers: [FoSequenceSubscribeApiService]
  18. })
  19. export class FoSequenceSubscribeModalComponent implements OnInit, OnDestroy {
  20.   _subscription = new Subscription();
  21.  
  22.   FORM_CONTROL_LOCATION = 'FORM_CONTROL_LOCATION';
  23.  
  24.   @Input() close: Function;
  25.  
  26.   isSubmitting = false;
  27.  
  28.   isReviewSequenceEnabled = false;
  29.  
  30.   sequenceUiItems$ = this.dataService.sequences$.pipe(
  31.     map(sequences =>
  32.       sequences.map(sequence => {
  33.         return {
  34.           ...sequence,
  35.           is_selected: false
  36.         };
  37.       })
  38.     )
  39.   );
  40.  
  41.   form = new FormGroup({});
  42.  
  43.   sequenceUiItems: SequenceUiItem[] = [];
  44.  
  45.   isDataNotAvailable$ = this.dataService.isDataNotAvailable$;
  46.  
  47.  
  48.   constructor(
  49.     private notification: NotificationsService,
  50.     private sharedDataService: FoSharedDataService,
  51.     private unirsonListService: FoUnirsonListDataService,
  52.     private dataService: FoSequenceSubscribeDataService,
  53.     private apiService: FoSequenceSubscribeApiService
  54.  
  55.   ) {}
  56.  
  57.   ngOnInit() {
  58.     this._subscription.add(
  59.       this.sequenceUiItems$.subscribe(seq => (this.sequenceUiItems = seq))
  60.     );
  61.     this.dataService.fetch();
  62.   }
  63.  
  64.   toggleSelectSequence(item: SequenceUiItem) {
  65.     item.is_selected = !item.is_selected;
  66.     this.sequenceUiItems = this.sequenceUiItems.map(sequence => {
  67.       if (sequence.sequence_id === item.sequence_id) {
  68.         return {
  69.           ...sequence,
  70.           ...item
  71.         };
  72.       }
  73.       return sequence;
  74.     });
  75.   }
  76.  
  77.   onSubmit() {
  78.     if (this.isSubmitting) {
  79.       return;
  80.     }
  81.     if (this.form.valid) {
  82.       this.isSubmitting = true;
  83.       combineLatest(
  84.         this.unirsonListService.isAnyUnirsonSelected$,
  85.         this.unirsonListService.selectedSegmentInfo$,
  86.         this.unirsonListService.selectedUnirsonIds$,
  87.         (isAnyUnirsonIdSelected, segmentInfo, unirsonIds) => {
  88.           const payload = {
  89.             tag_id: this.form.value[this.FORM_CONTROL_TAG]
  90.           };
  91.           if (isAnyUnirsonIdSelected) {
  92.             Object.assign(payload, {
  93.               unirson_ids: unirsonIds
  94.             });
  95.           } else {
  96.             Object.assign(payload, {
  97.               segment_info: segmentInfo
  98.             });
  99.           }
  100.  
  101.           return payload;
  102.         }
  103.       )
  104.         .pipe(
  105.           take(1),
  106.           switchMap(payload => this.apiService.segmentUnTag(payload))
  107.         )
  108.         .subscribe(
  109.           res => {
  110.             this.isSubmitting = false;
  111.             this.notification.success(
  112.               'Success',
  113.               `Tag removed from ${res.count} people.`
  114.             );
  115.             this.close();
  116.           },
  117.           err => {
  118.             this.isSubmitting = false;
  119.             foShowErrorNotification(err, this.notification);
  120.           }
  121.         );
  122.   } else {
  123.       this.notification.error('Not allowed', 'One or more invalid inputs');
  124.     }
  125.     // if (this.isSubmitting) {
  126.     //   return;
  127.     // }
  128.     // if (this.form.valid) {
  129.     //   const payload: CheckInPayload = {
  130.     //     unirson_id: this.unirsonInfo.unirson_id,
  131.     //     location_id: this.form.value[this.FORM_CONTROL_LOCATION],
  132.     //     seq_ids: this.sequenceUiItems
  133.     //       .filter(sequence => sequence.is_selected)
  134.     //       .map(sequence => sequence.sequence_id),
  135.     //     review_seq: this.isReviewSequenceEnabled ? 1 : 0
  136.     //   };
  137.     //   this.isSubmitting = true;
  138.     //   this.apiService.checkIn(payload).subscribe(
  139.     //     res => {
  140.     //       this.isSubmitting = false;
  141.     //       this.close();
  142.     //     },
  143.     //     error => {
  144.     //       this.isSubmitting = false;
  145.     //       foShowErrorNotification(error, this.notification);
  146.     //     }
  147.     //   );
  148.     // } else {
  149.     //   this.notification.error('Not allowed', 'One or more invalid inputs');
  150.     // }
  151.   }
  152.  
  153.   ngOnDestroy() {
  154.     this._subscription.unsubscribe();
  155.   }
  156. }
  157.  
  158. export interface SequenceUiItem extends SequenceItem {
  159.   is_selected: boolean;
  160. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top