Project

General

Profile

1
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
2
import { formErrorRequiredFields, formErrorWasntSaved, formSubmitting, formSuccessUpdatedRepo, loadingRepoError,
3
  loadingRepoMessage, noServiceMessage } from '../../../domain/shared-messages';
4
import { RepositoryService } from '../../../services/repository.service';
5
import {AbstractControl, FormArray, FormBuilder, FormGroup, Validators} from '@angular/forms';
6
import { Country, Repository, RepositorySnippet, Timezone, Typology } from '../../../domain/typeScriptClasses';
7
import { Description, softwarePlatformDesc, platformNameDesc, officialNameDesc, repoDescriptionDesc, countryDesc,
8
  longtitudeDesc, latitudeDesc, websiteUrlDesc, institutionNameDesc, englishNameDesc, logoUrlDesc, timezoneDesc,
9
  datasourceTypeDesc, adminEmailDesc, lissnDesc, eissnDesc, issnDesc } from '../../../domain/oa-description';
10
import { AuthenticationService } from '../../../services/authentication.service';
11
import {Router} from '@angular/router';
12

    
13
@Component ({
14
  selector: 'datasource-update-terms-form',
15
  templateUrl: './datasource-update-terms-form.component.html'
16
})
17

    
18
export class DatasourceUpdateTermsFormComponent implements OnInit {
19

    
20
  agreementForm = this.fb.group({
21
    acceptTerms: '',
22
    textMining: ''
23
  });
24

    
25
  consentTermsOfUseDate: Date;
26

    
27
  errorMessage: string;
28
  successMessage: string;
29
  loadingMessage: string;
30

    
31
  /*  in sources/register (in literature or data mode) the updated repository is emitted */
32
  @Output() emittedInfo: EventEmitter<Repository> = new EventEmitter();
33

    
34
  @Input() selectedRepo: Repository;
35

    
36
  @Input() showButton: boolean;
37

    
38
  repoId: string;
39
  formSubmitted = false;
40
  // updateGroup: FormGroup;
41
  readonly updateGroupDefinition = {
42
    softwarePlatform : ''
43
  };
44

    
45
  constructor(
46
    private fb: FormBuilder,
47
    private repoService: RepositoryService,
48
    private authService: AuthenticationService,
49
    private router: Router
50
  ) {}
51

    
52
  ngOnInit() {
53
    this.agreementForm.get('acceptTerms').setValue(this.selectedRepo.consentTermsOfUse ? this.selectedRepo.consentTermsOfUse : false);
54
    this.agreementForm.get('textMining').setValue(this.selectedRepo.fullTextDownload ? this.selectedRepo.fullTextDownload : false);
55
    this.selectedRepo.consentTermsOfUse = this.agreementForm.value.acceptTerms;
56
    this.selectedRepo.fullTextDownload = this.agreementForm.value.textMining;
57
    // if (this.router.url.indexOf('/sources/update') > -1) {
58
    //   console.log('update');
59
    // } else
60
      if (this.router.url.indexOf('/sources/register') > -1) {
61
      this.emitRepo();
62
    }
63
  }
64

    
65
  emitRepo() {
66
    this.selectedRepo.consentTermsOfUse = this.agreementForm.value.acceptTerms;
67
    this.selectedRepo.fullTextDownload = this.agreementForm.value.textMining;
68
    this.emittedInfo.emit(this.selectedRepo);
69
  }
70

    
71
  updateRepo() {
72
    this.formSubmitted = true;
73
    this.errorMessage = '';
74
    this.successMessage = '';
75
    window.scroll(1, 1);
76

    
77
    if (this.agreementForm.valid) {
78
      if (this.showButton) {
79
        this.loadingMessage = formSubmitting;
80
        this.errorMessage = '';
81
        this.selectedRepo.consentTermsOfUse = this.agreementForm.value.acceptTerms;
82
        this.selectedRepo.fullTextDownload = this.agreementForm.value.textMining;
83
        if (!this.selectedRepo.consentTermsOfUseDate) {
84
          this.selectedRepo.consentTermsOfUseDate = new Date(Date.now());
85
        }
86
        this.selectedRepo.lastConsentTermsOfUseDate = new Date(Date.now());
87
        this.repoService.updateRepository(this.selectedRepo).subscribe(
88
          response => {
89
            if (response) {
90
              this.selectedRepo = response;
91
              console.log(`updateRepository responded: ${JSON.stringify(response)}`);
92
            }
93
          },
94
          error => {
95
            console.log(error);
96
            this.loadingMessage = '';
97
            this.errorMessage = formErrorWasntSaved;
98
          },
99
          () => {
100
            this.loadingMessage = '';
101
            if (!this.selectedRepo) {
102
              this.errorMessage = formErrorWasntSaved;
103
            } else {
104
              this.successMessage = formSuccessUpdatedRepo;
105
            }
106
          }
107
        );
108
      }
109
    } else {
110
      this.errorMessage = formErrorRequiredFields;
111
    }
112
  }
113

    
114
}
115

    
116

    
(8-8/8)