@@ -4,6 +4,7 @@ import { BehaviorSubject, EMPTY, switchMap, tap } from 'rxjs';
44
55import { inject , Injectable } from '@angular/core' ;
66
7+ import { GetResourcesRequestTypeEnum } from '@osf/features/search/enums/get-resources-request-type.enum' ;
78import { SearchService } from '@osf/shared/services' ;
89import { addFiltersParams , getResourceTypes } from '@osf/shared/utils' ;
910
@@ -30,7 +31,7 @@ import { SearchSelectors } from './search.selectors';
3031export class SearchState implements NgxsOnInit {
3132 searchService = inject ( SearchService ) ;
3233 store = inject ( Store ) ;
33- loadRequests = new BehaviorSubject < boolean | null > ( null ) ;
34+ loadRequests = new BehaviorSubject < { type : GetResourcesRequestTypeEnum ; link ?: string } | null > ( null ) ;
3435
3536 ngxsOnInit ( ctx : StateContext < SearchStateModel > ) : void {
3637 this . loadRequests
@@ -39,43 +40,56 @@ export class SearchState implements NgxsOnInit {
3940 if ( ! query ) return EMPTY ;
4041 const state = ctx . getState ( ) ;
4142 ctx . patchState ( { resources : { ...state . resources , isLoading : true } } ) ;
42- const filters = this . store . selectSnapshot ( ResourceFiltersSelectors . getAllFilters ) ;
43- const filtersParams = addFiltersParams ( filters ) ;
44- const searchText = this . store . selectSnapshot ( SearchSelectors . getSearchText ) ;
45- const sortBy = this . store . selectSnapshot ( SearchSelectors . getSortBy ) ;
46- const resourceTab = this . store . selectSnapshot ( SearchSelectors . getResourceTab ) ;
47- const resourceTypes = getResourceTypes ( resourceTab ) ;
43+ if ( query . type === GetResourcesRequestTypeEnum . GetResources ) {
44+ const filters = this . store . selectSnapshot ( ResourceFiltersSelectors . getAllFilters ) ;
45+ const filtersParams = addFiltersParams ( filters ) ;
46+ const searchText = this . store . selectSnapshot ( SearchSelectors . getSearchText ) ;
47+ const sortBy = this . store . selectSnapshot ( SearchSelectors . getSortBy ) ;
48+ const resourceTab = this . store . selectSnapshot ( SearchSelectors . getResourceTab ) ;
49+ const resourceTypes = getResourceTypes ( resourceTab ) ;
4850
49- return this . searchService . getResources ( filtersParams , searchText , sortBy , resourceTypes ) . pipe (
50- tap ( ( response ) => {
51- ctx . patchState ( { resources : { data : response . resources , isLoading : false , error : null } } ) ;
52- ctx . patchState ( { resourcesCount : response . count } ) ;
53- ctx . patchState ( { first : response . first } ) ;
54- ctx . patchState ( { next : response . next } ) ;
55- ctx . patchState ( { previous : response . previous } ) ;
56- } )
57- ) ;
51+ return this . searchService . getResources ( filtersParams , searchText , sortBy , resourceTypes ) . pipe (
52+ tap ( ( response ) => {
53+ ctx . patchState ( { resources : { data : response . resources , isLoading : false , error : null } } ) ;
54+ ctx . patchState ( { resourcesCount : response . count } ) ;
55+ ctx . patchState ( { first : response . first } ) ;
56+ ctx . patchState ( { next : response . next } ) ;
57+ ctx . patchState ( { previous : response . previous } ) ;
58+ } )
59+ ) ;
60+ } else if ( query . type === GetResourcesRequestTypeEnum . GetResourcesByLink ) {
61+ if ( query . link ) {
62+ return this . searchService . getResourcesByLink ( query . link ! ) . pipe (
63+ tap ( ( response ) => {
64+ ctx . patchState ( { resources : { data : response . resources , isLoading : false , error : null } } ) ;
65+ ctx . patchState ( { resourcesCount : response . count } ) ;
66+ ctx . patchState ( { first : response . first } ) ;
67+ ctx . patchState ( { next : response . next } ) ;
68+ ctx . patchState ( { previous : response . previous } ) ;
69+ } )
70+ ) ;
71+ }
72+ return EMPTY ;
73+ }
74+ return EMPTY ;
5875 } )
5976 )
6077 . subscribe ( ) ;
6178 }
6279
6380 @Action ( GetResources )
6481 getResources ( ) {
65- this . loadRequests . next ( true ) ;
82+ this . loadRequests . next ( {
83+ type : GetResourcesRequestTypeEnum . GetResources ,
84+ } ) ;
6685 }
6786
6887 @Action ( GetResourcesByLink )
6988 getResourcesByLink ( ctx : StateContext < SearchStateModel > , action : GetResourcesByLink ) {
70- return this . searchService . getResourcesByLink ( action . link ) . pipe (
71- tap ( ( response ) => {
72- ctx . patchState ( { resources : { data : response . resources , isLoading : false , error : null } } ) ;
73- ctx . patchState ( { resourcesCount : response . count } ) ;
74- ctx . patchState ( { first : response . first } ) ;
75- ctx . patchState ( { next : response . next } ) ;
76- ctx . patchState ( { previous : response . previous } ) ;
77- } )
78- ) ;
89+ this . loadRequests . next ( {
90+ type : GetResourcesRequestTypeEnum . GetResourcesByLink ,
91+ link : action . link ,
92+ } ) ;
7993 }
8094
8195 @Action ( SetSearchText )
0 commit comments