import * as Yup from "yup"; import { forwardRef, useImperativeHandle, useState } from "react"; import PropTypes from "prop-types"; import { Formik, Form } from "formik"; import accountsService from "../../services/accountsService"; import { Alert } from "../Alert"; import { Button } from "../Button"; import { CopyButton } from "../CopyButton"; import { TextField } from "../Form/TextField"; import { CircledProgressIcon, PlusIcon } from "../Icons"; const newAPIKeySchema = Yup.object().shape({ name: Yup.string(), }); const State = { Pure: "PURE", Success: "SUCCESS", Failure: "FAILURE", }; export const APIKeyType = { Public: "public", General: "general", }; export const AddAPIKeyForm = forwardRef(({ onSuccess, type }, ref) => { const [state, setState] = useState(State.Pure); const [generatedKey, setGeneratedKey] = useState(null); useImperativeHandle(ref, () => ({ reset: () => setState(State.Pure), })); return (
{state === State.Success && ( Success!

Please copy your new API key below. We'll never show it again!

{generatedKey}
)} {state === State.Failure && ( We were not able to generate a new key. Please try again later. )} { try { const { key } = await accountsService .post("user/apikeys", { json: { name, public: type === APIKeyType.Public ? "true" : "false", skylinks: type === APIKeyType.Public ? [] : null, }, }) .json(); resetForm(); setGeneratedKey(key); setState(State.Success); onSuccess(); } catch { setState(State.Failure); } }} > {({ errors, touched, isSubmitting }) => (
{isSubmitting ? ( ) : ( )}
)}
); }); AddAPIKeyForm.displayName = "AddAPIKeyForm"; AddAPIKeyForm.propTypes = { onSuccess: PropTypes.func.isRequired, type: PropTypes.oneOf([APIKeyType.Public, APIKeyType.General]).isRequired, };