Skip to content
Snippets Groups Projects
form-sample.component.ts 1.8 KiB
Newer Older
/**
 * This file is part of Anis Client.
 *
 * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

import { Component, Input, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';

import { Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { SearchService } from 'src/app/instance/store/services/search.service';

@Component({
    selector: 'app-form-attribut',
    templateUrl: './form-sample.component.html',
})
export class FormSampleComponent implements OnInit {
    @Input() instanceName: string = null;
    @Input() faSymbol: string = null;
    @Input() type: string = null;
    @Input() label: string = null;
    @Input() textButton: string = null;
    @Input() datasetName: string = null;

    constructor(
        private router: Router,
        private search: SearchService,
        private toastr: ToastrService
    ) {}

    ngOnInit(): void {}

    submit(f: NgForm): void {
        const query: string = `${this.datasetName}?a=count&c=1::eq::${f.value.inputVal}`;
        this.search.retrieveDataLength(query).subscribe((data) => {
            switch (data[0].nb) {
                case 0:
                    this.toastr.error(
                        'Target ID does not exist',
                        'Navigation error'
                    );
                    break;
                case 1:
                    const route: string = `/instance/${this.instanceName}/search/detail/${this.datasetName}/${f.value.inputVal}`;
                    this.router.navigate([route]);
                    break;
                default:
                    console.log('Problem occured during the navigation');
            }
        });
    }
}