Skip to content

Commit

Permalink
fix: only update single service, enable delete
Browse files Browse the repository at this point in the history
  • Loading branch information
truemiller committed Feb 23, 2024
1 parent 9487311 commit 5902bb3
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions frontend/components/YourAgents/ServiceCard/ServiceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Deployment,
DeploymentStatus,
Service,
ServiceHash,
ServiceTemplate,
} from '@/client';
import { useMarketplace } from '@/hooks/useMarketplace';
Expand All @@ -23,7 +24,7 @@ export const ServiceCard = ({ service }: ServiceCardProps) => {
stopService,
deployService,
deleteServices,
updateServicesState,
updateServiceState,
getServiceStatus,
} = useServices();
const { getServiceTemplates } = useMarketplace();
Expand All @@ -37,41 +38,41 @@ export const ServiceCard = ({ service }: ServiceCardProps) => {
const [isDeleting, setIsDeleting] = useState(false);

const updateServiceStatus = useCallback(
(): Promise<void> =>
getServiceStatus(service.hash)
(serviceHash: ServiceHash): Promise<void> =>
getServiceStatus(serviceHash)
.then((r: Deployment) => setServiceStatus(r.status))
.catch(() => {
setServiceStatus(undefined);
message.error('Failed to update service status');
}),
[getServiceStatus, service.hash],
[getServiceStatus],
);

useInterval(updateServiceStatus, STATUS_POLLING_INTERVAL);
useInterval(() => updateServiceStatus(service.hash), STATUS_POLLING_INTERVAL);

const handleStart = useCallback(() => {
if (isStarting) return;
setIsStarting(true);
deployService(service.hash)
.then(async () => {
message.success('Service started successfully');
updateServicesState().catch(() =>
updateServiceState(service.hash).catch(() =>
message.error('Failed to update services'),
);
})
.catch(() => {
message.error('Failed to start service');
})
.finally(() => {
updateServiceStatus()
updateServiceStatus(service.hash)
.catch(() => message.error('Failed to update service status'))
.finally(() => setIsStarting(false));
});
}, [
isStarting,
deployService,
service.hash,
updateServicesState,
updateServiceState,
updateServiceStatus,
]);

Expand All @@ -80,45 +81,45 @@ export const ServiceCard = ({ service }: ServiceCardProps) => {
setIsStopping(true);
stopService(service.hash)
.then(() => {
updateServicesState().catch(() =>
updateServiceState(service.hash).catch(() =>
message.error('Failed to update services'),
);
})
.catch(() => {
message.error('Failed to stop service');
})
.finally(() => {
updateServiceStatus()
updateServiceStatus(service.hash)
.catch(() => message.error('Failed to update service status'))
.finally(() => setIsStopping(false));
});
}, [
isStopping,
service.hash,
stopService,
updateServiceState,
updateServiceStatus,
updateServicesState,
]);

const handleDelete = useCallback(() => {
if (isDeleting) return;
setIsDeleting(true);
deleteServices([service.hash])
.then(async () => {
updateServicesState().catch(() =>
updateServiceState(service.hash).catch(() =>
message.error('Failed to update services'),
);
})
.catch(() => message.error('Failed to delete service'))
.finally(() => {
updateServiceStatus().finally(() => setIsDeleting(false));
updateServiceStatus(service.hash).finally(() => setIsDeleting(false));
});
}, [
deleteServices,
isDeleting,
service.hash,
updateServiceState,
updateServiceStatus,
updateServicesState,
]);

const buttons: JSX.Element = useMemo(() => {
Expand Down Expand Up @@ -151,7 +152,7 @@ export const ServiceCard = ({ service }: ServiceCardProps) => {
<Button
danger
onClick={handleDelete}
disabled //={isDeleting} disabled until /delete endpoint is implemented
disabled={isDeleting}
loading={isDeleting}
>
Delete this agent
Expand Down

0 comments on commit 5902bb3

Please sign in to comment.