import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { useNavigate, Link, useParams } from 'react-router-dom'; import { useBoard } from '../hooks/useBoard'; import { useBoards } from '../hooks/useBoards'; import { useEffect } from 'react'; import { useToast } from '@/context/toasts'; const boardSchema = z.object({ name: z .string() .min(1, 'Board name is required') .max(100, 'Board name must be less than 100 characters'), description: z.string().max(500, 'Description must be less than 500 characters').optional(), }); type BoardFormData = z.infer; export function BoardEdit() { const navigate = useNavigate(); const { id } = useParams<{ id: string }>(); const { board } = useBoard(parseInt(id || '0')); const { updateBoard } = useBoards(); const { addNotification } = useToast(); const { register, handleSubmit, formState: { errors }, setValue, } = useForm({ resolver: zodResolver(boardSchema), }); // Populate form when board data is loaded useEffect(() => { if (board) { setValue('name', board.name); setValue('description', board.description || ''); } }, [board, setValue]); const onSubmit = async (data: BoardFormData) => { if (!id) return; try { await updateBoard(parseInt(id), data); navigate(`/boards/${id}`); } catch (err) { // Error is handled by the hook const errorMessage = err instanceof Error ? err.message : 'Failed to create card'; addNotification({ type: 'error', title: 'Error Login', message: errorMessage, duration: 5000, }); } }; if (!board) { return
Loading...
; } return (
← Back to Board

Edit Board

{errors.name &&

{errors.name.message}

}