import {Component, OnInit} from '@angular/core'; import {OaipmhValidatorService} from "../../../services/oaipmh-validator.service"; import {UntypedFormBuilder, UntypedFormGroup, Validators} from "@angular/forms"; import {Option} from "../../../openaire-library/sharedComponents/input/input.component"; import {StringUtils} from "../../../openaire-library/utils/string-utils.class"; import {JobResult} from "../../entities/JobResult"; import {Router} from "@angular/router"; import {Subscriber} from "rxjs"; @Component({ selector: 'app-oaipmh-validator', templateUrl: './oaipmh-validator.component.html', styleUrls: ['./oaipmh-validator.component.less'] }) export class OaipmhValidatorComponent implements OnInit { public options: Option[] = [ {label: 'OpenAIRE Guidelines for Data Archives Profile v2', value: 'OpenAIRE Guidelines for Data Archives Profile v2'}, {label: 'OpenAIRE Guidelines for Literature Repositories Profile v3', value: 'OpenAIRE Guidelines for Literature Repositories Profile v3'}, {label: 'OpenAIRE Guidelines for Literature Repositories Profile v4', value: 'OpenAIRE Guidelines for Literature Repositories Profile v4'}, {label: 'OpenAIRE FAIR Guidelines for Data Repositories Profile', value: 'OpenAIRE FAIR Guidelines for Data Repositories Profile'} ]; public form: UntypedFormGroup; public sets: Option[] = [{label: 'All sets', value: 'all'}]; public recordsNum: number = 10; public loadingSets: boolean = false; subscriptions = []; constructor(private fb: UntypedFormBuilder, private router: Router, private validator: OaipmhValidatorService) { this.form = this.fb.group({ url: this.fb.control("", StringUtils.urlValidator()),//[Validators.required/*, Validators.email*/]), guidelines: this.fb.control("", Validators.required), recordsNum: this.fb.control(null, Validators.required), set: this.fb.control('all', Validators.required) }); } ngOnInit() {} ngOnDestroy() { this.subscriptions.forEach(subscription => { if (subscription instanceof Subscriber) { subscription.unsubscribe(); } }); } updateRecordsNum(increase: boolean = true) { this.recordsNum = this.recordsNum + (increase ? 10 : -10); this.form.get('recordsNum').setValue(this.recordsNum); } getSets() { this.loadingSets = true; let options: Option[] = [{label: 'All sets', value: 'all'}]; if(this.form.get('setName')) { this.form.get('setName').setValue('all'); } if(this.form.get('url') && this.form.get('url').value && StringUtils.isValidUrl(this.form.get('url').value)) { this.subscriptions.push(this.validator.getSets(this.form.get('url').value).subscribe(sets => { for(let set of sets) { options.push({label: set['setName'], value: set['setSpec']}); } this.sets = options; this.loadingSets = false; }, error => { this.sets = options; this.loadingSets = false; })); } else { this.sets = options; this.loadingSets = false; } } validate() { this.subscriptions.push(this.validator.validate(this.form.get('guidelines').value, this.form.get('url').value, this.form.get('recordsNum').value, this.form.get('set').value).subscribe( (result: JobResult) => { this.router.navigate(['/oaipmh-history'], { queryParams: { 'jobId': result.id } }); } )); } }