Implement instance creation and update functionality in useInstances hook

This commit is contained in:
2025-07-22 21:52:31 +02:00
parent af063dead7
commit 3ecfedea81
2 changed files with 31 additions and 6 deletions

View File

@@ -3,10 +3,12 @@ import Header from '@/components/Header'
import InstanceList from '@/components/InstanceList' import InstanceList from '@/components/InstanceList'
import InstanceModal from '@/components/InstanceModal' import InstanceModal from '@/components/InstanceModal'
import { CreateInstanceOptions, Instance } from '@/types/instance' import { CreateInstanceOptions, Instance } from '@/types/instance'
import { useInstances } from '@/hooks/useInstances'
function App() { function App() {
const [isModalOpen, setIsModalOpen] = useState(false) const [isModalOpen, setIsModalOpen] = useState(false)
const [editingInstance, setEditingInstance] = useState<Instance | undefined>(undefined) const [editingInstance, setEditingInstance] = useState<Instance | undefined>(undefined)
const { createInstance, updateInstance } = useInstances()
const handleCreateInstance = () => { const handleCreateInstance = () => {
setEditingInstance(undefined) setEditingInstance(undefined)
@@ -20,13 +22,10 @@ function App() {
const handleSaveInstance = (name: string, options: CreateInstanceOptions) => { const handleSaveInstance = (name: string, options: CreateInstanceOptions) => {
if (editingInstance) { if (editingInstance) {
// TODO: Implement API call to update instance updateInstance(editingInstance.name, options)
console.log('Updating instance:', { name, options })
} else { } else {
// TODO: Implement API call to create instance createInstance(name, options)
console.log('Creating instance:', { name, options })
} }
// For now, just log the data - you'll implement the API call later
} }
return ( return (

View File

@@ -1,5 +1,5 @@
import { useState, useEffect, useCallback } from 'react' import { useState, useEffect, useCallback } from 'react'
import { Instance } from '@/types/instance' import { CreateInstanceOptions, Instance } from '@/types/instance'
import { instancesApi } from '@/lib/api' import { instancesApi } from '@/lib/api'
interface UseInstancesState { interface UseInstancesState {
@@ -10,6 +10,8 @@ interface UseInstancesState {
interface UseInstancesActions { interface UseInstancesActions {
fetchInstances: () => Promise<void> fetchInstances: () => Promise<void>
createInstance: (name: string, options: CreateInstanceOptions) => Promise<void>
updateInstance: (name: string, options: CreateInstanceOptions) => Promise<void>
startInstance: (name: string) => Promise<void> startInstance: (name: string) => Promise<void>
stopInstance: (name: string) => Promise<void> stopInstance: (name: string) => Promise<void>
restartInstance: (name: string) => Promise<void> restartInstance: (name: string) => Promise<void>
@@ -39,6 +41,28 @@ export const useInstances = (): UseInstancesState & UseInstancesActions => {
} }
}, []) }, [])
const createInstance = useCallback(async (name: string, options: CreateInstanceOptions) => {
try {
setError(null)
await instancesApi.create( name, options )
// Refresh the list to include the new instance
await fetchInstances()
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to create instance')
}
}, [fetchInstances])
const updateInstance = useCallback(async (name: string, options: CreateInstanceOptions) => {
try {
setError(null)
await instancesApi.update(name, options)
// Refresh the list to get updated instance
await fetchInstances()
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to update instance')
}
}, [fetchInstances])
const startInstance = useCallback(async (name: string) => { const startInstance = useCallback(async (name: string) => {
try { try {
setError(null) setError(null)
@@ -95,6 +119,8 @@ export const useInstances = (): UseInstancesState & UseInstancesActions => {
error, error,
// Actions // Actions
fetchInstances, fetchInstances,
createInstance,
updateInstance,
startInstance, startInstance,
stopInstance, stopInstance,
restartInstance, restartInstance,