1
- import { Component , OnDestroy , OnInit } from '@angular/core' ;
1
+ import { Component , inject , OnDestroy , OnInit } from '@angular/core' ;
2
2
import { ActivatedRoute , Router } from '@angular/router' ;
3
- import { APIUser , APIUsersRequest } from '@grpc/spec.pb' ;
4
- import { UsersClient } from '@grpc/spec.pbsc' ;
3
+ import {
4
+ APIItem ,
5
+ APIUser ,
6
+ APIUsersRequest ,
7
+ AttrUserValue ,
8
+ AttrUserValuesFields ,
9
+ AttrUserValuesRequest ,
10
+ ItemFields ,
11
+ ItemRequest ,
12
+ } from '@grpc/spec.pb' ;
13
+ import { AttrsClient , ItemsClient , UsersClient } from '@grpc/spec.pbsc' ;
5
14
import { NgbTypeaheadSelectItemEvent } from '@ng-bootstrap/ng-bootstrap' ;
6
15
import { ACLService , Privilege , Resource } from '@services/acl.service' ;
7
- import { APIPaginator } from '@services/api.service' ;
8
- import { APIItem } from '@services/item' ;
16
+ import { LanguageService } from '@services/language' ;
9
17
import { PageEnvService } from '@services/page-env.service' ;
10
18
import { UserService } from '@services/user' ;
11
- import { getAttrsTranslation , getUnitAbbrTranslation } from '@utils/translations' ;
19
+ import { getUnitAbbrTranslation } from '@utils/translations' ;
12
20
import { combineLatest , EMPTY , Observable , of , Subscription } from 'rxjs' ;
13
21
import { catchError , debounceTime , distinctUntilChanged , map , shareReplay , switchMap } from 'rxjs/operators' ;
14
22
15
- import { APIAttrAttributeValue , APIAttrsService , APIAttrUnit } from '../../api/attrs/attrs.service' ;
23
+ import { APIAttrsService } from '../../api/attrs/attrs.service' ;
16
24
import { ToastsService } from '../../toasts/toasts.service' ;
17
25
26
+ interface AttrUserValueListItem {
27
+ item$ : Observable < APIItem | null > ;
28
+ path$ : Observable < string [ ] > ;
29
+ unitAbbr$ : Observable < null | string | undefined > ;
30
+ user$ : Observable < APIUser | null > ;
31
+ userValue : AttrUserValue ;
32
+ }
33
+
18
34
@Component ( {
19
35
selector : 'app-cars-attrs-change-log' ,
20
36
styleUrls : [ './attrs-change-log.component.scss' ] ,
21
37
templateUrl : './attrs-change-log.component.html' ,
22
38
} )
23
39
export class CarsAttrsChangeLogComponent implements OnInit , OnDestroy {
40
+ private readonly attrsClient = inject ( AttrsClient ) ;
41
+ private readonly languageService = inject ( LanguageService ) ;
42
+ private readonly itemsClient = inject ( ItemsClient ) ;
43
+ private readonly attrsService = inject ( APIAttrsService ) ;
44
+
45
+ private readonly itemsCache = new Map < string , Observable < APIItem > > ( ) ;
46
+
24
47
private querySub ?: Subscription ;
25
48
26
49
protected readonly isModer$ = this . acl . isAllowed$ ( Resource . GLOBAL , Privilege . MODERATE ) ;
27
50
28
- protected readonly userID$ : Observable < number > = this . route . queryParamMap . pipe (
29
- map ( ( params ) => parseInt ( params . get ( 'user_id' ) || '' , 10 ) ) ,
30
- map ( ( userID ) => ( userID ? userID : 0 ) ) ,
51
+ protected readonly userID$ : Observable < string > = this . route . queryParamMap . pipe (
52
+ map ( ( params ) => params . get ( 'user_id' ) || '' ) ,
31
53
distinctUntilChanged ( ) ,
32
54
debounceTime ( 10 ) ,
33
55
) ;
34
56
35
57
protected readonly itemID$ = this . route . queryParamMap . pipe (
36
- map ( ( params ) => parseInt ( params . get ( 'item_id' ) || '' , 10 ) ) ,
37
- distinctUntilChanged ( ) ,
38
- debounceTime ( 10 ) ,
39
- ) ;
40
-
41
- protected readonly page$ = this . route . queryParamMap . pipe (
42
- map ( ( params ) => parseInt ( params . get ( 'page' ) || '' , 10 ) ) ,
58
+ map ( ( params ) => params . get ( 'item_id' ) || '' ) ,
43
59
distinctUntilChanged ( ) ,
44
60
debounceTime ( 10 ) ,
45
61
) ;
46
62
47
63
protected readonly items$ : Observable < {
48
- items : {
49
- attribute_id : number ;
50
- empty : boolean ;
51
- item : APIItem | null ;
52
- item_id : number ;
53
- path : null | string [ ] ;
54
- unit : APIAttrUnit | null ;
55
- update_date : null | string ;
56
- user$ : Observable < APIUser | null > ;
57
- user_id : string ;
58
- value : APIAttrAttributeValue | null ;
59
- value_text : null | string ;
60
- } [ ] ;
61
- paginator : APIPaginator ;
62
- } > = combineLatest ( [ this . userID$ , this . itemID$ , this . page$ ] ) . pipe (
63
- switchMap ( ( [ userID , itemID , page ] ) =>
64
- this . attrService . getUserValues$ ( {
65
- fields : 'item.name_html,path,unit,value_text' ,
66
- item_id : itemID ,
67
- page : page ,
68
- user_id : userID ? userID : undefined ,
69
- } ) ,
64
+ items : AttrUserValueListItem [ ] ;
65
+ } > = combineLatest ( [ this . userID$ , this . itemID$ ] ) . pipe (
66
+ switchMap ( ( [ userID , itemID ] ) =>
67
+ this . attrsClient . getUserValues (
68
+ new AttrUserValuesRequest ( {
69
+ fields : new AttrUserValuesFields ( { valueText : true } ) ,
70
+ itemId : itemID ,
71
+ language : this . languageService . language ,
72
+ userId : userID ? userID : undefined ,
73
+ } ) ,
74
+ ) ,
70
75
) ,
71
76
map ( ( response ) => ( {
72
- items : response . items . map ( ( item ) => ( { ...item , user$ : this . userService . getUser$ ( item . user_id ) } ) ) ,
73
- paginator : response . paginator ,
77
+ items : ( response . items || [ ] ) . map ( ( userValue ) => {
78
+ const attr$ = this . attrsService . getAttribute$ ( userValue . attributeId ) ;
79
+ return {
80
+ item$ : this . getItem$ ( userValue . itemId ) ,
81
+ path$ : this . attrsService . getPath$ ( userValue . attributeId ) ,
82
+ unitAbbr$ : attr$ . pipe ( map ( ( attr ) => ( attr ?. unitId ? getUnitAbbrTranslation ( attr . unitId ) : null ) ) ) ,
83
+ user$ : this . userService . getUser$ ( userValue . userId ) ,
84
+ userValue,
85
+ } ;
86
+ } ) ,
74
87
} ) ) ,
75
88
shareReplay ( 1 ) ,
76
89
) ;
@@ -114,7 +127,6 @@ export class CarsAttrsChangeLogComponent implements OnInit, OnDestroy {
114
127
) ;
115
128
116
129
constructor (
117
- private readonly attrService : APIAttrsService ,
118
130
private readonly route : ActivatedRoute ,
119
131
private readonly router : Router ,
120
132
private readonly acl : ACLService ,
@@ -124,6 +136,17 @@ export class CarsAttrsChangeLogComponent implements OnInit, OnDestroy {
124
136
private readonly userService : UserService ,
125
137
) { }
126
138
139
+ private getItem$ ( id : string ) : Observable < APIItem | null > {
140
+ let o$ = this . itemsCache . get ( id ) ;
141
+ if ( ! o$ ) {
142
+ o$ = this . itemsClient
143
+ . item ( new ItemRequest ( { fields : new ItemFields ( { nameHtml : true } ) , id : id } ) )
144
+ . pipe ( shareReplay ( 1 ) ) ;
145
+ this . itemsCache . set ( id , o$ ) ;
146
+ }
147
+ return o$ ;
148
+ }
149
+
127
150
ngOnInit ( ) : void {
128
151
setTimeout ( ( ) => this . pageEnv . set ( { pageId : 103 } ) , 0 ) ;
129
152
@@ -162,10 +185,4 @@ export class CarsAttrsChangeLogComponent implements OnInit, OnDestroy {
162
185
this . querySub . unsubscribe ( ) ;
163
186
}
164
187
}
165
-
166
- protected getAttrsTranslation ( id : string ) : string {
167
- return getAttrsTranslation ( id ) ;
168
- }
169
-
170
- protected readonly getUnitAbbrTranslation = getUnitAbbrTranslation ;
171
188
}
0 commit comments