🐛 Fix bugs with async events from dockerode

This commit is contained in:
ajnart
2022-07-22 16:19:56 +02:00
parent a9b840452e
commit 7b719c2273
2 changed files with 32 additions and 31 deletions

View File

@@ -26,8 +26,9 @@ function sendDockerCommand(action: string, containerId: string, containerName: s
autoClose: false, autoClose: false,
disallowClose: true, disallowClose: true,
}); });
axios.get(`/api/docker/container/${containerId}?action=${action}`).then((res) => { axios
setTimeout(() => { .get(`/api/docker/container/${containerId}?action=${action}`)
.then((res) => {
if (res.data.success === true) { if (res.data.success === true) {
updateNotification({ updateNotification({
id: containerId, id: containerId,
@@ -37,18 +38,16 @@ function sendDockerCommand(action: string, containerId: string, containerName: s
autoClose: 2000, autoClose: 2000,
}); });
} }
if (res.data.success === false) { })
updateNotification({ .catch((err) => {
id: containerId, updateNotification({
color: 'red', id: containerId,
title: 'There was an error with your container.', color: 'red',
message: undefined, title: 'There was an error',
icon: <IconX />, message: err.response.data.reason,
autoClose: 2000, autoClose: 2000,
}); });
} });
}, 500);
});
} }
export interface ContainerActionBarProps { export interface ContainerActionBarProps {
@@ -81,7 +80,17 @@ export default function ContainerActionBar({ selected, reload }: ContainerAction
selected.map((container) => selected.map((container) =>
sendDockerCommand('restart', container.Id, container.Names[0].substring(1)) sendDockerCommand('restart', container.Id, container.Names[0].substring(1))
) )
).then(() => reload()) )
.catch((err) => {
showNotification({
color: 'red',
title: 'There was an error with your container.',
message: err.message,
icon: <IconX />,
autoClose: 2000,
});
})
.then(() => reload())
} }
variant="light" variant="light"
color="orange" color="orange"
@@ -93,21 +102,9 @@ export default function ContainerActionBar({ selected, reload }: ContainerAction
leftIcon={<IconPlayerStop />} leftIcon={<IconPlayerStop />}
onClick={() => onClick={() =>
Promise.all( Promise.all(
selected.map((container) => { selected.map((container) =>
if ( sendDockerCommand('stop', container.Id, container.Names[0].substring(1))
container.State === 'stopped' || )
container.State === 'created' ||
container.State === 'exited'
) {
return showNotification({
id: container.Id,
title: `Failed to stop ${container.Names[0].substring(1)}`,
message: "You can't stop a stopped container",
autoClose: 1000,
});
}
return sendDockerCommand('stop', container.Id, container.Names[0].substring(1));
})
).then(() => reload()) ).then(() => reload())
} }
variant="light" variant="light"

View File

@@ -1,7 +1,7 @@
import { Table, Checkbox, Group, Badge, createStyles, ScrollArea, TextInput } from '@mantine/core'; import { Table, Checkbox, Group, Badge, createStyles, ScrollArea, TextInput } from '@mantine/core';
import { IconSearch } from '@tabler/icons'; import { IconSearch } from '@tabler/icons';
import Dockerode from 'dockerode'; import Dockerode from 'dockerode';
import { useState } from 'react'; import { useEffect, useState } from 'react';
import ContainerState from './ContainerState'; import ContainerState from './ContainerState';
const useStyles = createStyles((theme) => ({ const useStyles = createStyles((theme) => ({
@@ -26,6 +26,10 @@ export default function DockerTable({
const { classes, cx } = useStyles(); const { classes, cx } = useStyles();
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
useEffect(() => {
setContainers(containers);
}, [containers]);
const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => { const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { value } = event.currentTarget; const { value } = event.currentTarget;
setSearch(value); setSearch(value);