@@ -3,6 +3,7 @@ import { FormGroup } from '@angular/forms';
3
3
import { QueryParams } from './deeplink.service' ;
4
4
import { Injectable } from '@angular/core' ;
5
5
import { ActivatedRoute , Router , Params } from '@angular/router' ;
6
+ import { map } from 'rxjs/operators' ;
6
7
7
8
export interface QueryParams {
8
9
q : string ;
@@ -12,7 +13,11 @@ export interface QueryParams {
12
13
providedIn : 'root'
13
14
} )
14
15
export class DeeplinkService {
15
- constructor ( private router : Router , private route : ActivatedRoute , private search : AlgoliaService ) { }
16
+ constructor (
17
+ private router : Router ,
18
+ private route : ActivatedRoute ,
19
+ private search : AlgoliaService
20
+ ) { }
16
21
17
22
/**
18
23
* Register a given FormGroup instance with the current "q" queryParams Observbale. Every time the
@@ -22,19 +27,32 @@ export class DeeplinkService {
22
27
registerFormGroup ( form : FormGroup , controlName : string ) {
23
28
this . route . queryParams . subscribe ( ( query : QueryParams ) => {
24
29
if ( query . q ) {
25
- form . setValue ( {
30
+ form . patchValue ( {
26
31
[ controlName ] : query . q
27
32
} ) ;
28
33
}
29
34
} ) ;
30
35
}
31
36
37
+ registerState ( queryParam : string ) {
38
+ return this . route . queryParams . pipe ( map ( query => query [ queryParam ] ) ) ;
39
+ }
40
+
32
41
/**
33
42
* This method is used to update the current URL queryParams.
34
43
* It is used to keep both the URL and the formControl in sync.
35
44
* @param queryParams A given Params object containing the queryParams to set.
36
45
*/
37
46
syncUrl ( queryParams : Params ) {
47
+ if ( ! queryParams . t ) {
48
+ queryParams . t = this . route . snapshot . queryParams . t ;
49
+ } else {
50
+ if ( ! queryParams . q ) {
51
+ queryParams . q = this . route . snapshot . queryParams . q ;
52
+ }
53
+ }
54
+
55
+ console . log ( queryParams ) ;
38
56
this . router . navigate ( [ ] , {
39
57
relativeTo : this . route ,
40
58
queryParams
0 commit comments