import { useState } from 'react' import { useMutation, useQueryClient } from '@tanstack/react-query' import { useTranslation } from 'react-i18next' import { toast } from '@/shared/ui/toast-store' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/shared/ui/dialog' import { Button } from '@/shared/ui/button' import { Input } from '@/shared/ui/input' import { Label } from '@/shared/ui/label' import { HttpError } from '@/shared/api/client' import type { NodeDto } from '@/shared/api/types' import { updateNode } from './api' export function EditNodeDialog({ node, open, onOpenChange }: { node: NodeDto; open: boolean; onOpenChange: (open: boolean) => void }) { const { t } = useTranslation() const queryClient = useQueryClient() const [name, setName] = useState(node.name) const [baseAddress, setBaseAddress] = useState(node.baseAddress) const [location, setLocation] = useState(node.location ?? '') const [isEnabled, setIsEnabled] = useState(node.isEnabled) const [notifyOnStatusChange, setNotifyOnStatusChange] = useState(node.notifyOnStatusChange) const [username, setUsername] = useState(node.username) const [password, setPassword] = useState('') const mutation = useMutation({ mutationFn: () => updateNode( node.id, name.trim(), baseAddress.trim(), location.trim() || undefined, isEnabled, notifyOnStatusChange, username.trim() || undefined, password || undefined, ), onSuccess: async () => { toast.success(t('admin.nodes.updated')) await queryClient.invalidateQueries({ queryKey: ['admin-nodes'] }) onOpenChange(false) }, onError: (error) => toast.error(error instanceof HttpError ? error.detail : t('auth.genericError')), }) return ( e.preventDefault()}> {node.name}
{ e.preventDefault() mutation.mutate() }} >
setName(e.target.value)} required />
setBaseAddress(e.target.value)} required />
setLocation(e.target.value)} />
setUsername(e.target.value)} placeholder={t('admin.nodes.username')} />
setPassword(e.target.value)} />
) }