diff --git a/console2/src/components/organisms/NewSecretActivity/index.tsx b/console2/src/components/organisms/NewSecretActivity/index.tsx index 47103ec711..9b5a487907 100644 --- a/console2/src/components/organisms/NewSecretActivity/index.tsx +++ b/console2/src/components/organisms/NewSecretActivity/index.tsx @@ -18,88 +18,69 @@ * ===== */ -import * as copyToClipboard from 'copy-to-clipboard'; +import copyToClipboard from 'copy-to-clipboard'; import * as React from 'react'; -import { connect } from 'react-redux'; -import { AnyAction, Dispatch } from 'redux'; -import { push as pushHistory } from 'connected-react-router'; -import { Button, Message, Modal, TextArea } from 'semantic-ui-react'; +import {Button, Message, TextArea} from 'semantic-ui-react'; -import { ConcordKey, RequestError } from '../../../api/common'; +import {ConcordKey} from '../../../api/common'; import { - NewSecretEntry, SecretStoreType, SecretTypeExt, SecretVisibility } from '../../../api/org/secret'; -import { validatePassword } from '../../../api/service/console'; -import { actions, State } from '../../../state/data/secrets'; -import { CreateSecretResponse } from '../../../state/data/secrets'; -import { passwordTooWeakError } from '../../../validation'; -import { NewSecretForm, NewSecretFormValues, RequestErrorMessage } from '../../molecules'; +import {CreateSecretResponse} from '../../../state/data/secrets'; +import { + NewSecretFormValues +} from '../../molecules'; import './styles.css'; -import { RequestErrorActivity } from '../index'; - -interface OwnState { - showPasswordConfirm: boolean; - currentEntry?: NewSecretEntry; -} +import {RequestErrorActivity} from '../index'; +import NewSecretForm from '../../molecules/NewSecretForm'; +import {LoadingDispatch} from "../../../App"; +import {useCallback, useState} from "react"; +import {create as apiCreate} from "../../../api/org/secret"; +import {useApi} from "../../../hooks/useApi"; +import {useHistory} from "react-router"; interface ExternalProps { orgName: ConcordKey; } -interface StateProps { - submitting: boolean; - error: RequestError; - response?: CreateSecretResponse; +const INIT_VALUES: NewSecretFormValues = { + name: '', + visibility: SecretVisibility.PRIVATE, + type: SecretTypeExt.NEW_KEY_PAIR, + storeType: SecretStoreType.CONCORD } -interface DispatchProps { - submit: (values: NewSecretFormValues) => void; - reset: () => void; - done: (secretName: ConcordKey) => void; -} +const NewSecretActivity = (props: ExternalProps) => { + const history = useHistory(); -type Props = ExternalProps & StateProps & DispatchProps; + const {orgName} = props; -class NewSecretActivity extends React.Component { - constructor(props: Props) { - super(props); - this.state = { showPasswordConfirm: false }; - } - - componentDidMount() { - this.props.reset(); - } - - async handleSubmit(entry: NewSecretEntry, confirm?: boolean) { - if (confirm) { - this.setState({ showPasswordConfirm: false }); - } else if (entry.type === SecretTypeExt.USERNAME_PASSWORD && entry.password) { - const valid = await validatePassword(entry.password); - if (!valid) { - this.setState({ showPasswordConfirm: true, currentEntry: entry }); - return; - } - } - - this.props.submit(entry); - } + const dispatch = React.useContext(LoadingDispatch); + const [values, setValues] = useState(INIT_VALUES); - renderResponse() { - const { response, done, error } = this.props; + const postQuery = useCallback(() => { + return apiCreate(orgName, values); + }, [orgName, values]); - if (!response) { - return; - } + const {error, isLoading, data, fetch} = useApi(postQuery, { + fetchOnMount: false, + requestByFetch: true, + dispatch: dispatch + }); - if (error) { - return ; - } + const handleSubmit = useCallback( + (values: NewSecretFormValues) => { + setValues(values); + fetch(); + }, + [fetch] + ); - const { name: secretName, publicKey, password } = response; + if (data) { + const {publicKey, password} = data; return ( <> @@ -115,7 +96,7 @@ class NewSecretActivity extends React.Component { basic={true} onClick={() => (copyToClipboard as any)(publicKey)} /> -