|
1 | 1 | import {Fragment} from 'react'; |
2 | 2 |
|
| 3 | +import {Heading} from '@sentry/scraps/text'; |
| 4 | + |
3 | 5 | import {addSuccessMessage} from 'sentry/actionCreators/indicator'; |
4 | 6 | import {openModal, type ModalRenderProps} from 'sentry/actionCreators/modal'; |
5 | | -import Form from 'sentry/components/deprecatedforms/form'; |
6 | | -import useApi from 'sentry/utils/useApi'; |
| 7 | +import {Alert} from 'sentry/components/core/alert'; |
| 8 | +import Form from 'sentry/components/forms/form'; |
| 9 | +import type {OnSubmitCallback} from 'sentry/components/forms/types'; |
| 10 | +import {fetchMutation, useMutation} from 'sentry/utils/queryClient'; |
7 | 11 |
|
8 | 12 | import type {Subscription} from 'getsentry/types'; |
9 | 13 |
|
10 | | -type Props = { |
| 14 | +interface EndPeriodEarlyModalProps extends ModalRenderProps { |
11 | 15 | onSuccess: () => void; |
12 | 16 | orgId: string; |
13 | 17 | subscription: Subscription; |
14 | | -}; |
15 | | - |
16 | | -type ModalProps = Props & ModalRenderProps; |
| 18 | +} |
17 | 19 |
|
18 | | -function EndPeriodEarlyModal({orgId, onSuccess, closeModal, Header, Body}: ModalProps) { |
19 | | - const api = useApi(); |
| 20 | +function EndPeriodEarlyModal({ |
| 21 | + orgId, |
| 22 | + onSuccess, |
| 23 | + closeModal, |
| 24 | + Header, |
| 25 | + Body, |
| 26 | +}: EndPeriodEarlyModalProps) { |
| 27 | + const {mutateAsync: endPeriodEarly, isPending} = useMutation<any>({ |
| 28 | + mutationFn: () => |
| 29 | + fetchMutation({ |
| 30 | + url: `/customers/${orgId}/`, |
| 31 | + method: 'PUT', |
| 32 | + data: {endPeriodEarly: true}, |
| 33 | + }), |
| 34 | + }); |
20 | 35 |
|
21 | | - async function onSubmit(_: any, _onSubmitSuccess: any, onSubmitError: any) { |
| 36 | + const onSubmit: OnSubmitCallback = async ( |
| 37 | + _formData, |
| 38 | + onSubmitSuccess, |
| 39 | + onSubmitError |
| 40 | + ) => { |
22 | 41 | try { |
23 | | - const postData = { |
24 | | - endPeriodEarly: true, |
25 | | - }; |
26 | | - |
27 | | - await api.requestPromise(`/customers/${orgId}/`, { |
28 | | - method: 'PUT', |
29 | | - data: postData, |
30 | | - success: () => { |
31 | | - addSuccessMessage('Currrent period ended successfully'); |
32 | | - onSuccess(); |
33 | | - }, |
34 | | - }); |
| 42 | + const response = await endPeriodEarly(); |
35 | 43 |
|
| 44 | + addSuccessMessage('Current period ended successfully'); |
| 45 | + onSubmitSuccess(response); |
| 46 | + onSuccess(); |
36 | 47 | closeModal(); |
37 | 48 | } catch (err: any) { |
38 | 49 | onSubmitError({ |
39 | 50 | responseJSON: err.responseJSON, |
40 | 51 | }); |
41 | 52 | } |
42 | | - } |
| 53 | + }; |
43 | 54 |
|
44 | 55 | return ( |
45 | 56 | <Fragment> |
46 | | - <Header>End Current Period Immediately</Header> |
| 57 | + <Header closeButton> |
| 58 | + <Heading as="h3">End Current Period Immediately</Heading> |
| 59 | + </Header> |
47 | 60 | <Body> |
48 | 61 | <Form |
49 | 62 | onSubmit={onSubmit} |
50 | 63 | onCancel={closeModal} |
51 | 64 | submitLabel="Submit" |
| 65 | + submitDisabled={isPending} |
52 | 66 | cancelLabel="Cancel" |
53 | 67 | > |
| 68 | + <Alert.Container> |
| 69 | + <Alert type="warning" showIcon={false}> |
| 70 | + Ending the current billing period will immediately start the next billing |
| 71 | + cycle and may impact invoicing and usage proration. |
| 72 | + </Alert> |
| 73 | + </Alert.Container> |
54 | 74 | <p>End the current billing period immediately and start a new one.</p> |
55 | 75 | </Form> |
56 | 76 | </Body> |
57 | 77 | </Fragment> |
58 | 78 | ); |
59 | 79 | } |
60 | 80 |
|
61 | | -type Options = Pick<Props, 'orgId' | 'subscription' | 'onSuccess'>; |
| 81 | +type Options = Omit<EndPeriodEarlyModalProps, keyof ModalRenderProps>; |
62 | 82 |
|
63 | 83 | const triggerEndPeriodEarlyModal = (opts: Options) => |
64 | 84 | openModal(deps => <EndPeriodEarlyModal {...deps} {...opts} />); |
|
0 commit comments