1
- import { useCallback , useEffect , useMemo , useReducer , useState } from "react" ;
1
+ import { useCallback , useEffect , useReducer , useState } from "react" ;
2
2
import { resources } from "./reducers" ;
3
3
import {
4
4
setLoading ,
@@ -54,45 +54,30 @@ export const useResourceData = (resourceId, relativeUrlRoot) => {
54
54
55
55
export const useResourceOptions = ( resourceData ) => {
56
56
return {
57
- allowedActionsOptions : useMemo (
58
- ( ) =>
59
- resourceData ?. resource_state_types_available ?. map ( ( state ) => ( {
60
- value : state . resource_state_type_id ,
61
- label : state . display_resource_state_type ,
62
- additionalInfo : state . action_types
63
- . map ( ( action ) => action . display_action_type )
64
- . join ( ", " ) , // Pass action_types as additionalInfo Prop
65
- } ) ) || [ ] ,
66
- [ resourceData ]
67
- ) ,
68
-
69
- resourceTypesOptions : useMemo (
70
- ( ) =>
71
- resourceData ?. resource_types_available ?. map ( ( type ) => ( {
72
- value : type . resource_type_id ,
73
- label : type . display_resource_type ,
74
- } ) ) || [ ] ,
75
- [ resourceData ]
76
- ) ,
77
-
78
- unitTypesOptions : useMemo (
79
- ( ) =>
80
- resourceData ?. unit_types_available ?. map ( ( type ) => ( {
81
- value : type . unit_type_id ,
82
- label : type . display_unit_type ,
83
- } ) ) || [ ] ,
84
- [ resourceData ]
85
- ) ,
86
-
87
- availableResources : useMemo (
88
- ( ) => resourceData ?. required_resources_available || [ ] ,
89
- [ resourceData ]
90
- ) ,
91
-
92
- availableAllocationTypes : useMemo (
93
- ( ) => resourceData ?. unassigned_allocation_types || [ ] ,
94
- [ resourceData ]
95
- ) ,
57
+ allowedActionsOptions :
58
+ resourceData ?. resource_state_types_available ?. map ( ( state ) => ( {
59
+ value : state . resource_state_type_id ,
60
+ label : state . display_resource_state_type ,
61
+ additionalInfo : state . action_types
62
+ . map ( ( action ) => action . display_action_type )
63
+ . join ( ", " ) ,
64
+ } ) ) || [ ] ,
65
+
66
+ resourceTypesOptions :
67
+ resourceData ?. resource_types_available ?. map ( ( type ) => ( {
68
+ value : type . resource_type_id ,
69
+ label : type . display_resource_type ,
70
+ } ) ) || [ ] ,
71
+
72
+ unitTypesOptions :
73
+ resourceData ?. unit_types_available ?. map ( ( type ) => ( {
74
+ value : type . unit_type_id ,
75
+ label : type . display_unit_type ,
76
+ } ) ) || [ ] ,
77
+
78
+ availableResources : resourceData ?. required_resources_available || [ ] ,
79
+
80
+ availableAllocationTypes : resourceData ?. unassigned_allocation_types || [ ] ,
96
81
} ;
97
82
} ;
98
83
@@ -105,7 +90,7 @@ export const useAllocationRowsAndColumns = (
105
90
handleCommentChange ,
106
91
handleRequiredResourceChange
107
92
) => {
108
- const requiredResourcesColumns = useMemo ( ( ) => {
93
+ const requiredResourcesColumns = ( ) => {
109
94
if ( ! resourceDetails ) return [ ] ;
110
95
111
96
const requiredResources = new Set ( ) ;
@@ -119,36 +104,34 @@ export const useAllocationRowsAndColumns = (
119
104
. sort ( ( a , b ) => a . localeCompare ( b ) )
120
105
. map ( ( resourceName ) => ( {
121
106
key : resourceName ,
122
- name : resourceName ,
107
+ name : `Require ${ resourceName } ` ,
123
108
width : 150 ,
124
109
type : "checkbox" ,
125
110
} ) ) ;
126
- } , [ resourceDetails ] ) ;
127
-
128
- const allocationColumns = useMemo (
129
- ( ) => [
130
- { key : "display_name" , name : "Allocation Type" , width : 200 } ,
131
- {
132
- key : "allowed_actions" ,
133
- name : "Allowed Actions" ,
134
- width : 200 ,
135
- type : "select" ,
136
- tooltip : "Tooltip text" ,
137
- } ,
138
- {
139
- key : "comment" ,
140
- name : "Descriptive Text" ,
141
- width : 200 ,
142
- type : "input" ,
143
- tooltip :
144
- "Appears below the resource name and allocations description in the form when making a new request" ,
145
- } ,
146
- ...requiredResourcesColumns ,
147
- ] ,
148
- [ requiredResourcesColumns ]
149
- ) ;
111
+ } ;
112
+
113
+ const generatedColumns = requiredResourcesColumns ( ) ;
150
114
151
- const allocationRows = useMemo ( ( ) => {
115
+ const allocationColumns = [
116
+ { key : "display_name" , name : "Allocation Type" , width : 200 } ,
117
+ {
118
+ key : "allowed_actions" ,
119
+ name : "Allowed Actions" ,
120
+ width : 200 ,
121
+ type : "select" ,
122
+ } ,
123
+ {
124
+ key : "comment" ,
125
+ name : "Descriptive Text" ,
126
+ width : 200 ,
127
+ type : "input" ,
128
+ tooltip :
129
+ "Appears below the resource name and allocations description in the form when making a new request" ,
130
+ } ,
131
+ ...generatedColumns ,
132
+ ] ;
133
+
134
+ const allocationRows = ( ) => {
152
135
if ( ! resourceDetails ?. allocation_types ) return [ ] ;
153
136
154
137
return resourceDetails . allocation_types . map ( ( type ) => ( {
@@ -166,7 +149,7 @@ export const useAllocationRowsAndColumns = (
166
149
handleCommentChange ( type . allocation_type_id , newValue ) ,
167
150
} ,
168
151
...Object . fromEntries (
169
- requiredResourcesColumns . map ( ( column ) => [
152
+ generatedColumns . map ( ( column ) => [
170
153
column . key ,
171
154
{
172
155
checked :
@@ -184,16 +167,13 @@ export const useAllocationRowsAndColumns = (
184
167
] )
185
168
) ,
186
169
} ) ) ;
187
- } , [
188
- resourceDetails ,
189
- allowedActionsOptions ,
190
- requiredResourcesColumns ,
191
- handleAllowedActionChange ,
192
- handleCommentChange ,
193
- handleRequiredResourceChange ,
194
- ] ) ;
170
+ } ;
195
171
196
- return { allocationColumns, allocationRows, requiredResourcesColumns } ;
172
+ return {
173
+ allocationColumns,
174
+ allocationRows : allocationRows ( ) ,
175
+ requiredResourcesColumns : generatedColumns ,
176
+ } ;
197
177
} ;
198
178
199
179
export const useAllocationGrid = ( resourceData , resourceDetails , dispatch ) => {
0 commit comments