result-page.component.ts 3.71 KB
Newer Older
1
import { Component, OnInit, ViewChild } from '@angular/core';
2
import { ActivatedRoute, Router } from '@angular/router';
3
4
5
6
7
8
9
10
import {
    DataDiscoveryCriteria,
    DataDiscoveryCriteriaUtils,
    DataDiscoveryDocument,
    DataDiscoveryFacet,
    DEFAULT_PAGE_SIZE,
    MAX_RESULTS
} from '../model/data-discovery.model';
11
import { BehaviorSubject } from 'rxjs';
12
import { GnpisService } from '../gnpis.service';
13
14
import { filter } from 'rxjs/operators';
import { FormComponent } from '../form/form.component';
15

16
17

@Component({
18
    selector: 'gpds-result',
19
20
    templateUrl: './result-page.component.html',
    styleUrls: ['./result-page.component.scss'],
21
})
22
export class ResultPageComponent implements OnInit {
23

24
    @ViewChild('form') form: FormComponent;
25

26
    criteria$ = new BehaviorSubject<DataDiscoveryCriteria>(DataDiscoveryCriteriaUtils.emptyCriteria());
27
    documents: DataDiscoveryDocument[] = [];
28
    facets: DataDiscoveryFacet[] = [];
29
30
    pagination = {
        startResult: 1,
31
        endResult: DEFAULT_PAGE_SIZE,
32
33
        totalResult: null,
        currentPage: 0,
34
        pageSize: DEFAULT_PAGE_SIZE,
35
        totalPages: null,
36
        maxResults: MAX_RESULTS
37
    };
38

39
40
41
42
    constructor(private route: ActivatedRoute,
                private router: Router,
                private gnpisService: GnpisService
    ) {
43
    }
44

45
46
    fetchDocumentsAndFacets() {
        const criteria = this.criteria$.value;
47
        this.gnpisService.search(criteria)
48
            .subscribe(({ metadata, result, facets }) => {
49
                this.documents = result.data;
50
                this.updatePagination(metadata.pagination);
51
                this.facets = facets;
52
            });
53
54
    }

55
56
57
58
59
60
61
62
63
    private updatePagination({ currentPage, pageSize, totalCount, totalPages }) {
        this.pagination.currentPage = currentPage;
        this.pagination.pageSize = pageSize;
        this.pagination.totalPages = totalPages;
        this.pagination.startResult = pageSize * currentPage + 1;
        this.pagination.endResult = this.pagination.startResult + pageSize - 1;
        this.pagination.totalResult = totalCount;
    }

64
    ngOnInit(): void {
65
66
        const queryParams = this.route.snapshot.queryParams;

67
68
69
70
71
72
        // Parse criteria from URL query params
        const initialCriteria = DataDiscoveryCriteriaUtils.fromQueryParams(queryParams);
        this.criteria$.next(initialCriteria);

        this.form.traitWidgetInitialized.subscribe(() => {
            this.fetchDocumentsAndFacets();
73
        });
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

        this.criteria$
            .pipe(filter(c => c !== initialCriteria))
            .subscribe(newCriteria => {
                // Reset pagination
                newCriteria.page = 0;
                // Fetch documents and facets
                this.fetchDocumentsAndFacets();

                // Update URL query params
                this.router.navigate(['.'], {
                    relativeTo: this.route,
                    queryParams: DataDiscoveryCriteriaUtils.toQueryParams(newCriteria)
                });
            });
89
    }
90

91
    resultCount() {
92
93
        return Math.min(
            this.pagination.totalResult,
94
            MAX_RESULTS - DEFAULT_PAGE_SIZE
95
96
97
        );
    }

98
99
100
101
102
103
104
    resetAll() {
        this.router.navigate(['.'], {
            relativeTo: this.route,

            // Use of empty param to force re-parse of URL in ngOnInit
            queryParams: { empty: [] }
        });
105
        this.criteria$.next(DataDiscoveryCriteriaUtils.emptyCriteria());
106
107
    }

108
109
110
    changePage(page: number) {
        const criteria = this.criteria$.value;
        criteria.page = page - 1;
111
        this.fetchDocumentsAndFacets();
112
113
114
115
116
117
        this.router.navigate(['.'], {
            relativeTo: this.route,
            queryParams: { page },
            queryParamsHandling: 'merge'
        });
    }
118

119
}