Skip to content

Commit 25c8e3a

Browse files
committed
ref(gsAdmin): Rewrite <ForkCustomerAction> as a functional component
1 parent 3827265 commit 25c8e3a

File tree

1 file changed

+56
-60
lines changed

1 file changed

+56
-60
lines changed
Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import {Component, Fragment} from 'react';
1+
import {Fragment, useEffect, useState} from 'react';
22

3-
import {Client} from 'sentry/api';
43
import SelectField from 'sentry/components/forms/fields/selectField';
54
import type {Organization} from 'sentry/types/organization';
6-
import {browserHistory} from 'sentry/utils/browserHistory';
5+
import {useMutation} from 'sentry/utils/queryClient';
76
import {
87
getRegionChoices,
98
getRegionDataFromOrganization,
109
getRegions,
1110
} 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';
1214

1315
import type {
1416
AdminConfirmParams,
@@ -19,69 +21,63 @@ type Props = AdminConfirmRenderProps & {
1921
organization: Organization;
2022
};
2123

22-
type State = {
23-
regionUrl: string;
24-
};
25-
2624
/**
2725
* Rendered as part of a openAdminConfirmModal call
2826
*/
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();
3735

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);
5340

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) => {
5753
if (error.responseJSON) {
58-
this.props.onConfirm?.({error});
54+
onConfirm?.({error});
5955
}
60-
}
61-
};
56+
},
57+
});
6258

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] : []);
8665

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

Comments
 (0)