import { useCallback, useState } from 'react' import { Link, useNavigate } from 'react-router-dom' import { Button, FormControl, FormErrorMessage, FormLabel, Heading, Input, } from '@chakra-ui/react' import { useSWRConfig } from 'swr' import { Field, FieldAttributes, FieldProps, Form, Formik, FormikHelpers, } from 'formik' import * as Yup from 'yup' import cx from 'classnames' import { Helmet } from 'react-helmet-async' import WorkspaceAPI from '@/client/api/workspace' import OrganizationSelector from '@/components/common/organization-selector' import StorageInput from '@/components/common/storage-input' import { gigabyteToByte } from '@/helpers/convert-storage' type FormValues = { name: string organizationId: string storageCapacity: number } const NewWorkspacePage = () => { const navigate = useNavigate() const { mutate } = useSWRConfig() const [isLoading, setIsLoading] = useState(false) const formSchema = Yup.object().shape({ name: Yup.string().required('Name is required').max(255), organizationId: Yup.string().required('Organization is required'), storageCapacity: Yup.number() .required('Storage capacity is required') .positive() .integer() .min(1, 'Invalid storage usage value'), }) const handleSubmit = useCallback( async ( { name, organizationId, storageCapacity }: FormValues, { setSubmitting }: FormikHelpers, ) => { setSubmitting(true) setIsLoading(true) try { const result = await WorkspaceAPI.create({ name, organizationId, storageCapacity, }) mutate(`/workspaces/${result.id}`, result) mutate(`/workspaces`) setSubmitting(false) navigate(`/workspace/${result.id}/file/${result.rootId}`) } catch (e) { setIsLoading(false) } finally { setSubmitting(false) } }, [navigate, mutate], ) return ( <> New Workspace
New Workspace {({ errors, touched, isSubmitting, setFieldValue }) => (
{({ field }: FieldAttributes) => ( Name {errors.name} )} {({ field }: FieldAttributes) => ( Organization setFieldValue(field.name, value.id) } /> {errors.organizationId} )} {(props: FieldAttributes) => ( Storage capacity {errors.storageCapacity} )}
)}
) } export default NewWorkspacePage