@@ -23,11 +23,11 @@ import {
23
23
import { Select , SelectOption , SelectVariant } from '@patternfly/react-core/deprecated' ;
24
24
import OutlinedQuestionCircleIcon from '@patternfly/react-icons/dist/js/icons/outlined-question-circle-icon' ;
25
25
import TrashIcon from '@patternfly/react-icons/dist/js/icons/trash-icon' ;
26
- import { apiPaths , fetcher , patchK8sObjectByPath } from '@app/api' ;
27
- import { CatalogItem } from '@app/types' ;
28
- import { BABYLON_DOMAIN , displayName } from '@app/util' ;
26
+ import { apiPaths , fetcher } from '@app/api' ;
27
+ import { CatalogItem , CatalogItemIncident , CatalogItemIncidentStatus } from '@app/types' ;
28
+ import { displayName } from '@app/util' ;
29
29
import CatalogItemIcon from '@app/Catalog/CatalogItemIcon' ;
30
- import { CUSTOM_LABELS , formatString , getProvider } from '@app/Catalog/catalog-utils' ;
30
+ import { formatString , getProvider } from '@app/Catalog/catalog-utils' ;
31
31
import OperationalLogo from '@app/components/StatusPageIcons/Operational' ;
32
32
import DegradedPerformanceLogo from '@app/components/StatusPageIcons/DegradedPerformance' ;
33
33
import PartialOutageLogo from '@app/components/StatusPageIcons/PartialOutage' ;
@@ -36,7 +36,6 @@ import UnderMaintenanceLogo from '@app/components/StatusPageIcons/UnderMaintenan
36
36
import useSession from '@app/utils/useSession' ;
37
37
import LocalTimestamp from '@app/components/LocalTimestamp' ;
38
38
import LoadingIcon from '@app/components/LoadingIcon' ;
39
- import useMatchMutate from '@app/utils/useMatchMutate' ;
40
39
41
40
import './catalog-item-admin.css' ;
42
41
@@ -45,53 +44,34 @@ type comment = {
45
44
createdAt : string ;
46
45
message : string ;
47
46
} ;
48
- export type Ops = {
49
- disabled : boolean ;
50
- status : {
51
- id : string ;
52
- updated : {
53
- author : string ;
54
- updatedAt : string ;
55
- } ;
56
- } ;
57
- incidentUrl ?: string ;
58
- jiraIssueId ?: string ;
59
- comments : comment [ ] ;
60
- updated : {
61
- author : string ;
62
- updatedAt : string ;
63
- } ;
64
- } ;
65
47
66
48
const CatalogItemAdmin : React . FC = ( ) => {
67
49
const { namespace, name } = useParams ( ) ;
68
50
const navigate = useNavigate ( ) ;
69
- const { data : catalogItem , mutate } = useSWR < CatalogItem > ( apiPaths . CATALOG_ITEM ( { namespace, name } ) , fetcher ) ;
70
- const matchMutate = useMatchMutate ( ) ;
51
+ const { data : catalogItem } = useSWR < CatalogItem > ( apiPaths . CATALOG_ITEM ( { namespace, name } ) , fetcher ) ;
52
+ const asset_uuid = catalogItem . metadata . labels [ 'gpte.redhat.com/asset-uuid' ] ;
53
+ const { data : catalogItemIncident } = useSWR < CatalogItemIncident > (
54
+ apiPaths . CATALOG_ITEM_LAST_INCIDENT ( { namespace, asset_uuid } ) ,
55
+ fetcher
56
+ ) ;
71
57
const { email : userEmail } = useSession ( ) . getSession ( ) ;
72
58
const [ isReadOnlyValue , setIsReadOnlyValue ] = useState ( false ) ;
73
59
const [ isOpen , setIsOpen ] = useState ( false ) ;
74
60
const [ isLoading , setIsLoading ] = useState ( false ) ;
75
- const ops : Ops = catalogItem . metadata . annotations ?. [ `${ BABYLON_DOMAIN } /ops` ]
76
- ? JSON . parse ( catalogItem . metadata . annotations ?. [ `${ BABYLON_DOMAIN } /ops` ] )
77
- : null ;
78
- const disabled = catalogItem . metadata . labels ?. [ `${ CUSTOM_LABELS . DISABLED . domain } /${ CUSTOM_LABELS . DISABLED . key } ` ]
79
- ? JSON . parse ( catalogItem . metadata . labels ?. [ `${ CUSTOM_LABELS . DISABLED . domain } /${ CUSTOM_LABELS . DISABLED . key } ` ] )
80
- : false ;
81
- const [ status , setStatus ] = useState ( ops ?. status . id || 'operational' ) ;
82
- const [ isDisabled , setIsDisabled ] = useState ( disabled ) ;
83
- const [ incidentUrl , setIncidentUrl ] = useState ( ops ?. incidentUrl || '' ) ;
84
- const [ jiraIssueId , setJiraIssueId ] = useState ( ops ?. jiraIssueId || '' ) ;
61
+ const [ status , setStatus ] = useState ( catalogItemIncident . status || 'Operational' ) ;
62
+ const [ isDisabled , setIsDisabled ] = useState ( catalogItemIncident . disabled ?? false ) ;
63
+ const [ incidentUrl , setIncidentUrl ] = useState ( catalogItemIncident . incident_url || '' ) ;
64
+ const [ jiraIssueId , setJiraIssueId ] = useState ( catalogItemIncident . jira_url || '' ) ;
85
65
const [ comment , setComment ] = useState ( '' ) ;
86
66
const provider = getProvider ( catalogItem ) ;
87
67
88
68
useEffect ( ( ) => {
89
- if ( status === 'operational ' ) {
69
+ if ( status === 'Operational ' ) {
90
70
setIsDisabled ( false ) ;
91
71
setIsReadOnlyValue ( true ) ;
92
72
setJiraIssueId ( '' ) ;
93
73
setIncidentUrl ( '' ) ;
94
- } else if ( status === 'major- outage' ) {
74
+ } else if ( status === 'Major outage' ) {
95
75
setIsDisabled ( true ) ;
96
76
setIsReadOnlyValue ( true ) ;
97
77
} else {
@@ -100,70 +80,43 @@ const CatalogItemAdmin: React.FC = () => {
100
80
} , [ setIsReadOnlyValue , status ] ) ;
101
81
102
82
async function removeComment ( comment : comment ) {
103
- if ( ! ops ?. comments || ops . comments . length < 1 ) {
83
+ if ( ! catalogItemIncident ?. comments ) {
104
84
throw "Can't find comment to delete" ;
105
85
}
106
- const comments = ops . comments . filter ( ( c ) => c . createdAt !== comment . createdAt ) ;
107
- const patch = {
108
- metadata : {
109
- annotations : { [ `${ BABYLON_DOMAIN } /ops` ] : JSON . stringify ( { ...ops , comments } ) } ,
110
- } ,
111
- } ;
112
- setIsLoading ( true ) ;
113
- const catalogItemUpdated : CatalogItem = await patchK8sObjectByPath ( {
114
- path : apiPaths . CATALOG_ITEM ( {
115
- namespace,
116
- name,
117
- } ) ,
118
- patch,
119
- } ) ;
120
- mutate ( catalogItemUpdated ) ;
121
- matchMutate ( [
122
- { name : 'CATALOG_ITEMS' , arguments : { namespace : 'all-catalogs' } , data : undefined } ,
123
- { name : 'CATALOG_ITEMS' , arguments : { namespace : catalogItemUpdated . metadata . namespace } , data : undefined } ,
124
- ] ) ;
125
- setIsLoading ( false ) ;
86
+ const comments = JSON . parse ( catalogItemIncident . comments ) ;
87
+ if ( comments . length < 1 ) {
88
+ throw "Can't find comment to delete" ;
89
+ }
90
+ const new_comments = comments . filter ( ( c : comment ) => c . createdAt !== comment . createdAt ) ;
91
+ await saveForm ( new_comments ) ;
126
92
}
127
- async function saveForm ( ) {
128
- const comments = ops ?. comments || [ ] ;
93
+ async function saveForm ( comments ?: comment [ ] ) {
94
+ setIsLoading ( true ) ;
95
+ if ( comments === null || comments === undefined ) {
96
+ comments = JSON . parse ( catalogItemIncident ?. comments ) || [ ] ;
97
+ }
129
98
if ( comment ) {
130
99
comments . push ( {
131
100
message : comment ,
132
101
author : userEmail ,
133
102
createdAt : new Date ( ) . toISOString ( ) ,
134
103
} ) ;
135
104
}
136
- const patchObj = {
137
- status : {
138
- id : status ,
139
- updated :
140
- ops ?. status . id !== status ? { author : userEmail , updatedAt : new Date ( ) . toISOString ( ) } : ops ?. status . updated ,
141
- } ,
142
- jiraIssueId,
143
- incidentUrl,
144
- updated : { author : userEmail , updatedAt : new Date ( ) . toISOString ( ) } ,
145
- comments,
146
- } ;
147
105
148
- const patch = {
149
- metadata : {
150
- annotations : { [ `${ BABYLON_DOMAIN } /ops` ] : JSON . stringify ( patchObj ) } ,
151
- labels : { [ `${ BABYLON_DOMAIN } /${ CUSTOM_LABELS . DISABLED . key } ` ] : isDisabled . toString ( ) } ,
152
- } ,
153
- } ;
154
- setIsLoading ( true ) ;
155
- const catalogItemUpdated : CatalogItem = await patchK8sObjectByPath ( {
156
- path : apiPaths . CATALOG_ITEM ( {
157
- namespace,
158
- name,
106
+ await fetch ( apiPaths . CATALOG_ITEM_INCIDENTS ( { asset_uuid, namespace } ) , {
107
+ method : 'POST' ,
108
+ body : JSON . stringify ( {
109
+ created_by : userEmail ,
110
+ disabled : isDisabled ,
111
+ status,
112
+ incident_url : incidentUrl ,
113
+ jira_url : jiraIssueId ,
114
+ comments,
159
115
} ) ,
160
- patch,
116
+ headers : {
117
+ 'Content-Type' : 'application/json' ,
118
+ } ,
161
119
} ) ;
162
- mutate ( catalogItemUpdated ) ;
163
- matchMutate ( [
164
- { name : 'CATALOG_ITEMS' , arguments : { namespace : 'all-catalogs' } , data : undefined } ,
165
- { name : 'CATALOG_ITEMS' , arguments : { namespace : catalogItemUpdated . metadata . namespace } , data : undefined } ,
166
- ] ) ;
167
120
setIsLoading ( false ) ;
168
121
navigate ( '/catalog' ) ;
169
122
}
@@ -196,7 +149,7 @@ const CatalogItemAdmin: React.FC = () => {
196
149
< Select
197
150
aria-label = "StatusPage.io status"
198
151
onSelect = { ( _ , value ) => {
199
- setStatus ( value . toString ( ) ) ;
152
+ setStatus ( value . toString ( ) as CatalogItemIncidentStatus ) ;
200
153
setIsOpen ( false ) ;
201
154
} }
202
155
onToggle = { ( ) => setIsOpen ( ! isOpen ) }
@@ -205,19 +158,19 @@ const CatalogItemAdmin: React.FC = () => {
205
158
variant = { SelectVariant . single }
206
159
className = "select-wrapper"
207
160
>
208
- < SelectOption key = "operational" value = "operational " >
161
+ < SelectOption key = "operational" value = "Operational " >
209
162
< OperationalLogo /> Operational
210
163
</ SelectOption >
211
- < SelectOption key = "degraded-performance" value = "degraded- performance" >
164
+ < SelectOption key = "degraded-performance" value = "Degraded performance" >
212
165
< DegradedPerformanceLogo /> Degraded performance
213
166
</ SelectOption >
214
- < SelectOption key = "partial-outage" value = "partial- outage" >
167
+ < SelectOption key = "partial-outage" value = "Partial outage" >
215
168
< PartialOutageLogo /> Partial outage
216
169
</ SelectOption >
217
- < SelectOption key = "major-outage" value = "major- outage" >
170
+ < SelectOption key = "major-outage" value = "Major outage" >
218
171
< MajorOutageLogo /> Major outage
219
172
</ SelectOption >
220
- < SelectOption key = "under-maintenance" value = "under- maintenance" >
173
+ < SelectOption key = "under-maintenance" value = "Under maintenance" >
221
174
< UnderMaintenanceLogo /> Under maintenance
222
175
</ SelectOption >
223
176
</ Select >
@@ -228,10 +181,10 @@ const CatalogItemAdmin: React.FC = () => {
228
181
/>
229
182
</ Tooltip >
230
183
</ div >
231
- { ops ? (
184
+ { catalogItemIncident ? (
232
185
< p className = "catalog-item-admin__author" >
233
- Changed by: < b > { ops . status . updated . author } </ b > -{ ' ' }
234
- < LocalTimestamp className = "catalog-item-admin__timestamp" timestamp = { ops . status . updated . updatedAt } />
186
+ Changed by: < b > { catalogItemIncident . created_by } </ b > -{ ' ' }
187
+ < LocalTimestamp className = "catalog-item-admin__timestamp" timestamp = { catalogItemIncident . created_at } />
235
188
</ p >
236
189
) : null }
237
190
</ FormGroup >
@@ -268,7 +221,7 @@ const CatalogItemAdmin: React.FC = () => {
268
221
</ FormGroup >
269
222
< FormGroup fieldId = "comment" label = "Comments (only visible to admins)" >
270
223
< ul className = "catalog-item-admin__comments" >
271
- { ( ops ?. comments || [ ] ) . map ( ( comment ) => (
224
+ { ( JSON . parse ( catalogItemIncident ?. comments ) || [ ] ) . map ( ( comment : comment ) => (
272
225
< li key = { comment . createdAt } className = "catalog-item-admin__comment" >
273
226
< p className = "catalog-item-admin__author" >
274
227
< b > { comment . author } </ b > -{ ' ' }
@@ -292,7 +245,7 @@ const CatalogItemAdmin: React.FC = () => {
292
245
</ FormGroup >
293
246
< ActionList >
294
247
< ActionListItem >
295
- < Button isAriaDisabled = { false } isDisabled = { isLoading } onClick = { saveForm } >
248
+ < Button isAriaDisabled = { false } isDisabled = { isLoading } onClick = { ( ) => saveForm ( ) } >
296
249
Save
297
250
</ Button >
298
251
</ ActionListItem >
0 commit comments