@@ -3,23 +3,26 @@ import MyRepositoriesSelect from './MyRepositoriesSelect'
3
3
import ExternalResourcesTable , {
4
4
ExternalResourcesTableBase ,
5
5
} from './ExternalResourcesTable'
6
- import { ExternalResource } from 'common/types/responses'
6
+ import { ExternalResource , GithubResource } from 'common/types/responses'
7
7
import { GitHubResourceSelectProvider } from './GitHubResourceSelectProvider'
8
8
import { useCreateExternalResourceMutation } from 'common/services/useExternalResource'
9
9
import Constants from 'common/constants'
10
10
import Button from './base/forms/Button'
11
11
import GitHubResourcesSelect from './GitHubResourcesSelect'
12
12
import _ from 'lodash'
13
+ import AppActions from 'common/dispatcher/app-actions'
13
14
14
15
type ExternalResourcesLinkTabType = {
15
16
githubId : string
16
17
organisationId : string
17
18
featureId : string
18
19
projectId : string
20
+ environmentId : string
19
21
}
20
22
21
23
type AddExternalResourceRowType = ExternalResourcesTableBase & {
22
24
linkedExternalResources ?: ExternalResource [ ]
25
+ environmentId : string
23
26
}
24
27
25
28
type GitHubStatusType = {
@@ -28,6 +31,7 @@ type GitHubStatusType = {
28
31
}
29
32
30
33
const AddExternalResourceRow : FC < AddExternalResourceRowType > = ( {
34
+ environmentId,
31
35
featureId,
32
36
linkedExternalResources,
33
37
organisationId,
@@ -36,8 +40,9 @@ const AddExternalResourceRow: FC<AddExternalResourceRowType> = ({
36
40
repoOwner,
37
41
} ) => {
38
42
const [ externalResourceType , setExternalResourceType ] = useState < string > ( '' )
39
- const [ featureExternalResource , setFeatureExternalResource ] =
40
- useState < string > ( '' )
43
+ const [ featureExternalResource , setFeatureExternalResource ] = useState <
44
+ GithubResource | undefined
45
+ > ( undefined )
41
46
const [ lastSavedResource , setLastSavedResource ] = useState <
42
47
string | undefined
43
48
> ( undefined )
@@ -68,11 +73,13 @@ const AddExternalResourceRow: FC<AddExternalResourceRowType> = ({
68
73
repoOwner = { repoOwner }
69
74
repoName = { repoName }
70
75
githubResource = {
71
- (
72
- _ . find ( _ . values ( Constants . resourceTypes ) , {
73
- label : externalResourceType ! ,
74
- } ) as any
75
- ) . resourceType || ''
76
+ ( externalResourceType &&
77
+ (
78
+ _ . find ( _ . values ( Constants . resourceTypes ) , {
79
+ label : externalResourceType ! ,
80
+ } ) as any
81
+ ) . resourceType ) ||
82
+ ''
76
83
}
77
84
>
78
85
< GitHubResourcesSelect
@@ -94,19 +101,29 @@ const AddExternalResourceRow: FC<AddExternalResourceRowType> = ({
94
101
key as keyof typeof Constants . resourceTypes
95
102
] . label === externalResourceType ,
96
103
)
97
- createExternalResource ( {
98
- body : {
99
- feature : parseInt ( featureId ) ,
100
- metadata : { } ,
101
- type : type ! ,
102
- url : featureExternalResource ,
103
- } ,
104
- feature_id : featureId ,
105
- project_id : projectId ,
106
- } ) . then ( ( ) => {
107
- toast ( 'External Resource Added' )
108
- setLastSavedResource ( featureExternalResource )
109
- } )
104
+ if ( type && featureExternalResource ) {
105
+ createExternalResource ( {
106
+ body : {
107
+ feature : parseInt ( featureId ) ,
108
+ metadata : {
109
+ 'draft' : featureExternalResource . draft ,
110
+ 'merged' : featureExternalResource . merged ,
111
+ 'state' : featureExternalResource . state ,
112
+ 'title' : featureExternalResource . title ,
113
+ } ,
114
+ type : type ,
115
+ url : featureExternalResource . html_url ,
116
+ } ,
117
+ feature_id : featureId ,
118
+ project_id : projectId ,
119
+ } ) . then ( ( ) => {
120
+ toast ( 'External Resource Added' )
121
+ setLastSavedResource ( featureExternalResource . html_url )
122
+ AppActions . refreshFeatures ( parseInt ( projectId ) , environmentId )
123
+ } )
124
+ } else {
125
+ throw new Error ( 'Invalid External Resource Data' )
126
+ }
110
127
} }
111
128
>
112
129
Save
@@ -118,6 +135,7 @@ const AddExternalResourceRow: FC<AddExternalResourceRowType> = ({
118
135
}
119
136
120
137
const ExternalResourcesLinkTab : FC < ExternalResourcesLinkTabType > = ( {
138
+ environmentId,
121
139
featureId,
122
140
githubId,
123
141
organisationId,
@@ -157,6 +175,7 @@ const ExternalResourcesLinkTab: FC<ExternalResourcesLinkTabType> = ({
157
175
/>
158
176
{ repoName && repoOwner && (
159
177
< AddExternalResourceRow
178
+ environmentId = { environmentId }
160
179
featureId = { featureId }
161
180
projectId = { projectId }
162
181
organisationId = { organisationId }
0 commit comments