1
- import { Component , Fragment } from 'react' ;
1
+ import { Fragment , useEffect , useState } from 'react' ;
2
2
3
- import { Client } from 'sentry/api' ;
4
3
import SelectField from 'sentry/components/forms/fields/selectField' ;
5
4
import type { Organization } from 'sentry/types/organization' ;
6
- import { browserHistory } from 'sentry/utils/browserHistory ' ;
5
+ import { useMutation } from 'sentry/utils/queryClient ' ;
7
6
import {
8
7
getRegionChoices ,
9
8
getRegionDataFromOrganization ,
10
9
getRegions ,
11
10
} from 'sentry/utils/regions' ;
11
+ import type RequestError from 'sentry/utils/requestError/requestError' ;
12
+ import useApi from 'sentry/utils/useApi' ;
13
+ import { useNavigate } from 'sentry/utils/useNavigate' ;
12
14
13
15
import type {
14
16
AdminConfirmParams ,
@@ -19,69 +21,63 @@ type Props = AdminConfirmRenderProps & {
19
21
organization : Organization ;
20
22
} ;
21
23
22
- type State = {
23
- regionUrl : string ;
24
- } ;
25
-
26
24
/**
27
25
* Rendered as part of a openAdminConfirmModal call
28
26
*/
29
- class ForkCustomerAction extends Component < Props > {
30
- state : State = {
31
- regionUrl : '' ,
32
- } ;
33
-
34
- componentDidMount ( ) {
35
- this . props . setConfirmCallback ( this . handleConfirm ) ;
36
- }
27
+ export default function ForkCustomerAction ( {
28
+ organization ,
29
+ onConfirm ,
30
+ setConfirmCallback ,
31
+ } : Props ) {
32
+ const [ regionUrl , setRegionUrl ] = useState ( '' ) ;
33
+ const api = useApi ( { persistInFlight : true } ) ;
34
+ const navigate = useNavigate ( ) ;
37
35
38
- handleConfirm = async ( params : AdminConfirmParams ) => {
39
- const api = new Client ( { headers : { Accept : 'application/json; charset=utf-8' } } ) ;
40
- const { organization} = this . props ;
41
- const { regionUrl} = this . state ;
42
- const regions = getRegions ( ) ;
43
- const region = regions . find ( r => r . url === regionUrl ) ;
44
-
45
- try {
46
- const response = await api . requestPromise (
47
- `/organizations/${ organization . slug } /fork/` ,
48
- {
49
- method : 'POST' ,
50
- host : region ?. url ,
51
- }
52
- ) ;
36
+ const { mutate} = useMutation < any , RequestError , AdminConfirmParams > ( {
37
+ mutationFn : ( ) => {
38
+ const regions = getRegions ( ) ;
39
+ const region = regions . find ( r => r . url === regionUrl ) ;
53
40
54
- browserHistory . push ( `/_admin/relocations/${ region ?. name } /${ response . uuid } /` ) ;
55
- this . props . onConfirm ?.( { regionUrl, ...params } ) ;
56
- } catch ( error ) {
41
+ return api . requestPromise ( `/organizations/${ organization . slug } /fork/` , {
42
+ method : 'POST' ,
43
+ host : region ?. url ,
44
+ } ) ;
45
+ } ,
46
+ onSuccess : ( response , params ) => {
47
+ const regions = getRegions ( ) ;
48
+ const region = regions . find ( r => r . url === regionUrl ) ;
49
+ navigate ( `/_admin/relocations/${ region ?. name } /${ response . uuid } /` ) ;
50
+ onConfirm ?.( { regionUrl, ...params } ) ;
51
+ } ,
52
+ onError : ( error : RequestError , _params ) => {
57
53
if ( error . responseJSON ) {
58
- this . props . onConfirm ?.( { error} ) ;
54
+ onConfirm ?.( { error} ) ;
59
55
}
60
- }
61
- } ;
56
+ } ,
57
+ } ) ;
62
58
63
- render ( ) {
64
- const { organization} = this . props ;
65
- const currentRegionData = getRegionDataFromOrganization ( organization ) ;
66
- const regionChoices = getRegionChoices ( currentRegionData ? [ currentRegionData ] : [ ] ) ;
67
- return (
68
- < Fragment >
69
- < SelectField
70
- name = "regionUrl"
71
- label = { 'Duplicate into Region' }
72
- help = {
73
- "Choose which region to duplicate this organization's low volume metadata into. This will kick off a SAAS->SAAS relocation job, but the source organization will not be affected."
74
- }
75
- choices = { regionChoices }
76
- inline = { false }
77
- stacked
78
- required
79
- value = { this . state . regionUrl }
80
- onChange = { ( val : any ) => this . setState ( { regionUrl : val } ) }
81
- />
82
- </ Fragment >
83
- ) ;
84
- }
85
- }
59
+ useEffect ( ( ) => {
60
+ setConfirmCallback ( mutate ) ;
61
+ } , [ mutate , setConfirmCallback ] ) ;
62
+
63
+ const currentRegionData = getRegionDataFromOrganization ( organization ) ;
64
+ const regionChoices = getRegionChoices ( currentRegionData ? [ currentRegionData ] : [ ] ) ;
86
65
87
- export default ForkCustomerAction ;
66
+ return (
67
+ < Fragment >
68
+ < SelectField
69
+ name = "regionUrl"
70
+ label = { 'Duplicate into Region' }
71
+ help = {
72
+ "Choose which region to duplicate this organization's low volume metadata into. This will kick off a SAAS->SAAS relocation job, but the source organization will not be affected."
73
+ }
74
+ choices = { regionChoices }
75
+ inline = { false }
76
+ stacked
77
+ required
78
+ value = { regionUrl }
79
+ onChange = { ( val : any ) => setRegionUrl ( val ) }
80
+ />
81
+ </ Fragment >
82
+ ) ;
83
+ }
0 commit comments