11import { useMutation , useQueryClient } from '@tanstack/react-query'
2- import { useState } from 'react'
32import { planetServiceOrpc } from '../lib/service-planet'
43import { Card } from './ui/Card'
54import { InfoMessage } from './ui/InfoMessage'
65
7- interface PlanetFormData {
8- name : string
9- description ?: string
10- image ?: File
11- }
12-
136export function PlanetCreator ( ) {
147 const queryClient = useQueryClient ( )
15- const [ formData , setFormData ] = useState < PlanetFormData > ( {
16- name : '' ,
17- description : '' ,
18- } )
198
209 const createPlanetMutation = useMutation (
2110 planetServiceOrpc . planet . create . mutationOptions ( {
2211 onSuccess ( ) {
23- // Invalidate and refetch planets list
2412 queryClient . invalidateQueries ( {
2513 queryKey : planetServiceOrpc . planet . key ( ) ,
2614 } )
27-
28- // Reset form
29- setFormData ( { name : '' , description : '' } )
30-
31- // Clear file input manually
32- const fileInput = document . querySelector < HTMLInputElement > ( 'input[type="file"]' )
33- if ( fileInput ) {
34- fileInput . value = ''
35- }
3615 } ,
3716 onError ( error ) {
3817 console . error ( 'Failed to create planet:' , error )
@@ -42,19 +21,21 @@ export function PlanetCreator() {
4221 } ) ,
4322 )
4423
45- const handleSubmit = ( e : React . FormEvent < HTMLFormElement > ) => {
24+ const handleSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
4625 e . preventDefault ( )
4726 const form = new FormData ( e . currentTarget )
4827
4928 const name = form . get ( 'name' ) as string
5029 const description = ( form . get ( 'description' ) as string | null ) ?? undefined
5130 const image = form . get ( 'image' ) as File
5231
53- createPlanetMutation . mutate ( {
32+ await createPlanetMutation . mutateAsync ( {
5433 name,
5534 description,
5635 image : image . size > 0 ? image : undefined ,
5736 } )
37+
38+ e . currentTarget . reset ( )
5839 }
5940
6041 return (
@@ -71,8 +52,6 @@ export function PlanetCreator() {
7152 name = "name"
7253 required
7354 placeholder = "Enter planet name..."
74- value = { formData . name }
75- onChange = { e => setFormData ( { ...formData , name : e . target . value } ) }
7655 />
7756 </ label >
7857
@@ -81,8 +60,6 @@ export function PlanetCreator() {
8160 < textarea
8261 name = "description"
8362 placeholder = "Describe this planet (optional)..."
84- value = { formData . description }
85- onChange = { e => setFormData ( { ...formData , description : e . target . value } ) }
8663 />
8764 </ label >
8865
0 commit comments