Project

General

Profile

1
/*
2
*  created by myrto on 1/22/2018
3
*/
4

    
5
import { Component, OnInit } from '@angular/core';
6
import {
7
  formErrorInvalidFields, formErrorRequiredFields, formErrorWasntSaved, formSuccessRegisteredDatasource,
8
  noServiceMessage
9
} from '../../../domain/shared-messages';
10
import { RepositoryService } from "../../../services/repository.service";
11
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
12
import { Country, Repository } from '../../../domain/typeScriptClasses';
13
import { typologies } from '../../../domain/typologies';
14
import { timezones } from '../../../domain/timezones';
15
import {
16
  Description,
17
  softwarePlatformDesc,
18
  platformNameDesc,
19
  officialNameDesc,
20
  issnDesc,
21
  eissnDesc,
22
  lissnDesc,
23
  repoDescriptionDesc,
24
  countryDesc,
25
  longtitudeDesc,
26
  latitudeDesc,
27
  websiteUrlDesc,
28
  institutionNameDesc,
29
  englishNameDesc,
30
  logoUrlDesc,
31
  timezoneDesc,
32
  journalTypeDesc,
33
  adminEmailDesc
34
} from '../../../domain/oa-description';
35
import { ValidatorService } from '../../../services/validator.service';
36

    
37
@Component ({
38
  selector: 'journal-info-form',
39
  templateUrl: 'journal-info-form.component.html'
40
})
41

    
42
export class JournalInfoFormComponent implements OnInit {
43
  errorMessage: string;
44
  successMessage: string;
45
  showSpinner: boolean;
46
  loadingMessage: string;
47

    
48
  typologies = typologies;
49
  timezones = timezones;
50
  countries: Country[] = [];
51
  datasourceClasses: Map<string,string> = new Map<string,string>();
52
  classCodes: string[] = [];
53

    
54
  newDatasource: Repository;
55

    
56
  group: FormGroup;
57
  readonly groupDefinition = {
58
    softwarePlatform : '',
59
    platformName : '',
60
    officialName : ['', Validators.required],
61
    issn : ['', [Validators.required, Validators.minLength(8), Validators.maxLength(8)]],
62
    eissn : ['', [Validators.minLength(8), Validators.maxLength(8)]],
63
    lissn : ['', [Validators.minLength(8), Validators.maxLength(8)]],
64
    repoDescription : ['', Validators.required],
65
    country : ['', Validators.required],
66
    longtitude : ['', [Validators.required, Validators.maxLength(9), Validators.min(-180), Validators.max(180)] ],
67
    latitude : ['', [Validators.required, Validators.maxLength(9), Validators.min(-90), Validators.max(90)] ],
68
    websiteUrl : ['', Validators.required],
69
    institutionName : ['', Validators.required],
70
    englishName: ['', Validators.required],
71
    logoUrl: '',
72
    timezone: ['', Validators.required],
73
    journalType: ['', Validators.required],
74
    adminEmail: ['', [Validators.required, Validators.email] ]
75
  };
76

    
77
  softwarePlatformDesc : Description = softwarePlatformDesc;
78
  platformNameDesc : Description = platformNameDesc;
79
  officialNameDesc : Description = officialNameDesc;
80
  issnDesc : Description = issnDesc;
81
  eissnDesc : Description = eissnDesc;
82
  lissnDesc : Description = lissnDesc;
83
  repoDescriptionDesc : Description = repoDescriptionDesc;
84
  countryDesc : Description = countryDesc;
85
  longtitudeDesc : Description = longtitudeDesc;
86
  latitudeDesc : Description = latitudeDesc;
87
  websiteUrlDesc : Description = websiteUrlDesc;
88
  institutionNameDesc : Description = institutionNameDesc;
89
  englishNameDesc : Description = englishNameDesc;
90
  logoUrlDesc : Description = logoUrlDesc;
91
  timezoneDesc : Description = timezoneDesc;
92
  journalTypeDesc : Description = journalTypeDesc;
93
  adminEmailDesc : Description = adminEmailDesc;
94

    
95
  constructor(
96
    private fb: FormBuilder,
97
    private repoService: RepositoryService
98
  ) {}
99

    
100
  ngOnInit() {
101
    this.loadForm();
102
  }
103

    
104
  loadForm(){
105
    this.group = this.fb.group(this.groupDefinition);
106
    this.getCountries();
107
    this.getDatasourceClasses();
108
  }
109

    
110
  getCountries(){
111
    this.repoService.getCountries()
112
      .subscribe(
113
        countries => this.countries = countries.sort( function(a,b){
114
          if(a.name<b.name){
115
            return -1;
116
          } else if(a.name>b.name){
117
            return 1;
118
          } else {
119
            return 0;
120
          }
121
        } ),
122
        error => {
123
          this.errorMessage = noServiceMessage;
124
          console.log(error);
125
        });
126
  }
127

    
128
  getDatasourceClasses() {
129
    this.repoService.getDatasourceClasses('journal').subscribe(
130
      classes => this.datasourceClasses = classes,
131
      error => {
132
        this.errorMessage = noServiceMessage;
133
        console.log(error);
134
      },
135
      () => {
136
        for (let key in this.datasourceClasses){
137
          this.classCodes.push(key);
138
        }
139
      }
140
    );
141
  }
142

    
143
  registerDatasource(): boolean {
144
    if(this.group.valid){
145
        this.successMessage = formSuccessRegisteredDatasource;
146
        this.errorMessage = '';
147
        return true;
148
    } else {
149
      this.errorMessage = formErrorRequiredFields;
150
      return false;
151
    }
152
  }
153

    
154
  updateEnglishName(id: string, name: string) {
155
    let status: boolean;
156
    this.repoService.updateEnglishName(id,name).subscribe(
157
      response => {
158
        console.log(`answered ${response}`);
159
        status = true;
160
      },
161
      error => {
162
        console.log(error);
163
        this.errorMessage = formErrorWasntSaved;
164
        status = false;
165
      }
166
    );
167
    console.log(status);
168
    return status;
169
  }
170

    
171

    
172
}
(8-8/8)