@@ -3,31 +3,55 @@ import React from 'react';
3
3
import { listChaosInfra } from '@api/core' ;
4
4
import { getScope } from '@utils' ;
5
5
import ChaosInfrastructureReferenceFieldView from '@views/ChaosInfrastructureReferenceField' ;
6
- import type { ChaosInfrastructureReferenceFieldProps } from '@models' ;
6
+ import { AllEnv , type ChaosInfrastructureReferenceFieldProps } from '@models' ;
7
7
import type { InfrastructureDetails } from '@views/ChaosInfrastructureReferenceField/ChaosInfrastructureReferenceField' ;
8
+ import { listEnvironment } from '@api/core/environments' ;
8
9
9
10
function KubernetesChaosInfrastructureReferenceFieldController ( {
10
11
setFieldValue,
11
- initialInfrastructureID
12
+ initialInfrastructureID,
13
+ initialEnvironmentID
12
14
} : ChaosInfrastructureReferenceFieldProps ) : React . ReactElement {
13
15
const scope = getScope ( ) ;
14
16
const { showError } = useToaster ( ) ;
15
17
const [ searchInfrastructure , setSearchInfrastructure ] = React . useState < string > ( '' ) ;
16
-
17
18
const [ page , setPage ] = React . useState < number > ( 0 ) ;
18
- const limit = 8 ;
19
+ const [ limit , setLimit ] = React . useState < number > ( 5 ) ;
20
+ const [ envID , setEnvID ] = React . useState < string > ( AllEnv . AllEnv ) ;
21
+ const [ initialAllInfrastructureLength , setInitialAllInfrastructureLength ] = React . useState < number > ( 0 ) ;
19
22
20
23
const { data : listChaosInfraData , loading : listChaosInfraLoading } = listChaosInfra ( {
21
24
...scope ,
22
- filter : { name : searchInfrastructure , isActive : true } ,
25
+ environmentIDs : envID === AllEnv . AllEnv ? undefined : [ envID ] ,
26
+ filter : { name : searchInfrastructure } ,
23
27
pagination : { page, limit } ,
24
28
options : { onError : error => showError ( error . message ) }
25
29
} ) ;
26
30
31
+ const { data : listEnvironmentData } = listEnvironment ( {
32
+ ...scope ,
33
+ options : {
34
+ onError : err => showError ( err . message )
35
+ }
36
+ } ) ;
37
+
38
+ const environmentList = listEnvironmentData ?. listEnvironments ?. environments ;
39
+
40
+ React . useEffect ( ( ) => {
41
+ if ( envID === AllEnv . AllEnv ) {
42
+ setInitialAllInfrastructureLength ( listChaosInfraData ?. listInfras . totalNoOfInfras || 0 ) ;
43
+ }
44
+ } , [ listChaosInfraData ] ) ;
45
+
46
+ const preSelectedEnvironment = listEnvironmentData ?. listEnvironments ?. environments ?. find (
47
+ ( { environmentID } ) => environmentID === initialEnvironmentID
48
+ ) ;
49
+
27
50
// TODO: replace with get API as this becomes empty during edit
28
51
const preSelectedInfrastructure = listChaosInfraData ?. listInfras . infras . find (
29
52
( { infraID } ) => infraID === initialInfrastructureID
30
53
) ;
54
+
31
55
const preSelectedInfrastructureDetails : InfrastructureDetails | undefined = preSelectedInfrastructure && {
32
56
id : preSelectedInfrastructure ?. infraID ,
33
57
name : preSelectedInfrastructure ?. name ,
@@ -38,6 +62,16 @@ function KubernetesChaosInfrastructureReferenceFieldController({
38
62
environmentID : preSelectedInfrastructure ?. environmentID
39
63
} ;
40
64
65
+ React . useEffect ( ( ) => {
66
+ setPage ( 0 ) ;
67
+ } , [ envID ] ) ;
68
+
69
+ React . useEffect ( ( ) => {
70
+ if ( preSelectedEnvironment ) {
71
+ setEnvID ( preSelectedEnvironment ?. environmentID ) ;
72
+ }
73
+ } , [ preSelectedEnvironment , setFieldValue ] ) ;
74
+
41
75
React . useEffect ( ( ) => {
42
76
if ( preSelectedInfrastructure ) {
43
77
setFieldValue ( 'chaosInfrastructure.id' , preSelectedInfrastructure . infraID , true ) ;
@@ -69,7 +103,10 @@ function KubernetesChaosInfrastructureReferenceFieldController({
69
103
pageSize = { limit }
70
104
pageCount = { Math . ceil ( totalNoOfInfras / limit ) }
71
105
pageIndex = { page }
72
- gotoPage = { pageNumber => setPage ( pageNumber ) }
106
+ gotoPage = { setPage }
107
+ showPagination = { true }
108
+ pageSizeOptions = { [ 5 , 10 , 15 ] }
109
+ onPageSizeChange = { setLimit }
73
110
/>
74
111
) ;
75
112
} ;
@@ -87,6 +124,10 @@ function KubernetesChaosInfrastructureReferenceFieldController({
87
124
} }
88
125
searchInfrastructure = { searchInfrastructure }
89
126
setSearchInfrastructure = { setSearchInfrastructure }
127
+ allInfrastructureLength = { initialAllInfrastructureLength }
128
+ environmentList = { environmentList }
129
+ envID = { envID }
130
+ setEnvID = { setEnvID }
90
131
loading = { {
91
132
listChaosInfra : listChaosInfraLoading
92
133
} }
0 commit comments