import { useState } from 'react' import { useNavigate } from 'react-router-dom' import { useForm, hasLength } from '@mantine/form' import { Center, Container, Paper, Title, Text, TextInput, Textarea, Stack, Button } from '@mantine/core' import { useAppSelector } from '../app/hooks' import { useCreatePanelMutation } from '../app/api/panels' import type { CreatePanelData } from '../app/types/panels' const NewPanelPage = () => { const navigate = useNavigate() const [errorMsg, setErrorMsg] = useState('') // Ensure the user is authenticated const currentUser = useAppSelector((state) => state.auth.currentUser) if (currentUser === null) { throw Error('Authentication Required') } const panelForm = useForm({ initialValues: { name: '', description: '', }, validate: { name: hasLength({ min: 3, max: 20 }, 'Panel name must be between 3 and 20 characters long'), description: hasLength({ min: 3, max: 512 }, 'Description must be between 3 and 512 characters'), } }) const [createPanel, { isLoading }] = useCreatePanelMutation() const submitPanelForm = async (values: CreatePanelData) => { await createPanel({ ...values }).unwrap().then((panel) => { navigate(`/panel/${panel.name}`) }).catch((error) => { if (!error.data) { setErrorMsg('Failed to access the API') } else { setErrorMsg(error.data.msg) } }) } return (
Create a Panel