|
| 1 | +import { |
| 2 | + AlignItems, |
| 3 | + Button, |
| 4 | + ButtonType, |
| 5 | + ComponentColor, |
| 6 | + ComponentSize, |
| 7 | + ComponentStatus, |
| 8 | + FlexBox, |
| 9 | + FlexDirection, |
| 10 | + Form, |
| 11 | + Input, |
| 12 | +} from '@influxdata/clockface' |
| 13 | +import React, {ChangeEvent, FC, useContext, useState} from 'react' |
| 14 | +import {AccountContext} from '../context/account' |
| 15 | +import {MigrateOrgsOverlay} from './MigrateOrgsOverlay' |
| 16 | +import {OperatorAccount, getOperatorAccount} from 'src/client/unityRoutes' |
| 17 | +import {useDispatch} from 'react-redux' |
| 18 | +import {notify} from 'src/shared/actions/notifications' |
| 19 | +import {getAccountError} from 'src/shared/copy/notifications' |
| 20 | + |
| 21 | +export const MigrateOrgsTool: FC = () => { |
| 22 | + const {account, setMigrateOverlayVisible} = useContext(AccountContext) |
| 23 | + const [toAccountId, setToAccountId] = useState('') |
| 24 | + const [toAccount, setToAccount] = useState<OperatorAccount>(null) |
| 25 | + const dispatch = useDispatch() |
| 26 | + |
| 27 | + const changeToAccountId = (event: ChangeEvent<HTMLInputElement>) => { |
| 28 | + setToAccountId(event.target.value) |
| 29 | + } |
| 30 | + |
| 31 | + const submit = async () => { |
| 32 | + if (toAccountId === '') { |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + const resp = await getOperatorAccount({accountId: toAccountId}) |
| 38 | + if (resp.status !== 200) { |
| 39 | + dispatch(notify(getAccountError(toAccountId))) |
| 40 | + return |
| 41 | + } |
| 42 | + setToAccount(resp.data) |
| 43 | + setMigrateOverlayVisible(true) |
| 44 | + } catch (error) { |
| 45 | + console.error(error) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const canSubmit = toAccountId !== '' |
| 50 | + |
| 51 | + return account.type === 'cancelled' ? ( |
| 52 | + <></> |
| 53 | + ) : ( |
| 54 | + <> |
| 55 | + <MigrateOrgsOverlay toAccount={toAccount} /> |
| 56 | + <h2 data-testid="migrate-resources--title"> |
| 57 | + Migrate Organizations and Users |
| 58 | + </h2> |
| 59 | + <Form onSubmit={submit}> |
| 60 | + <Form.Label |
| 61 | + id="accounts-migrate--account-id-label" |
| 62 | + label="Please enter an Account ID to migrate to:" |
| 63 | + /> |
| 64 | + <FlexBox |
| 65 | + direction={FlexDirection.Row} |
| 66 | + alignItems={AlignItems.FlexStart} |
| 67 | + margin={ComponentSize.Large} |
| 68 | + > |
| 69 | + <Input |
| 70 | + placeholder="Account ID to migrate resources to" |
| 71 | + inputStyle={{width: '400px'}} |
| 72 | + style={{width: 'auto'}} |
| 73 | + value={toAccountId} |
| 74 | + onChange={changeToAccountId} |
| 75 | + testID="accounts-migrate--account-id" |
| 76 | + /> |
| 77 | + <Button |
| 78 | + text="migrate" |
| 79 | + color={ComponentColor.Primary} |
| 80 | + type={ButtonType.Submit} |
| 81 | + testID="accounts-migrate--button" |
| 82 | + className="accounts-migrate--button" |
| 83 | + active={canSubmit} |
| 84 | + status={ |
| 85 | + canSubmit ? ComponentStatus.Default : ComponentStatus.Disabled |
| 86 | + } |
| 87 | + /> |
| 88 | + </FlexBox> |
| 89 | + </Form> |
| 90 | + </> |
| 91 | + ) |
| 92 | +} |
0 commit comments