-import React, { useState, useEffect } from 'react';
-import { Container, Typography, TextField, Button } from '@mui/material';
-import { MaterialReactTable } from 'material-react-table';
+import React, { useEffect, useState } from 'react';
+import { Button, Container, TextField, Typography } from '@mui/material';
+import { MaterialReactTable, useMaterialReactTable } from 'material-react-table';
import { DockerService } from '../../services';
-
const Docker = () => {
- const [containers, setContainers] = useState([]);
- const [images, setImages] = useState([]);
- const [composeFile, setComposeFile] = useState('');
- const [message, setMessage] = useState('');
+ const [containers, setContainers] = useState( [] );
+ const [images, setImages] = useState( [] );
+ const [composeFile, setComposeFile] = useState( '' );
+ const [message, setMessage] = useState( '' );
const fetchContainers = async () => {
try {
- const containersData = await DockerService.fetchContainers( { all: true } ); // Assuming you have a service to fetch containers
- setContainers(containersData);
+ const containersData = await DockerService.fetchContainers( { all:true } ); // Assuming you have a service to fetch containers
+ setContainers( containersData );
} catch (error) {
- setMessage(error);
+ setMessage( error );
}
};
const fetchImages = async () => {
try {
const imagesData = await DockerService.fetchImages(); // Assuming you have a service to fetch images
- setImages(imagesData);
+ setImages( imagesData );
} catch (error) {
- setMessage(error);
+ setMessage( error );
}
};
fetchContainers().catch();
}, [] );
- const handleContainerAction = async (action, containerId) => {
- await DockerService.containerAction(action, containerId);
+ const handleContainerAction = async ( action, containerId ) => {
+ await DockerService.containerAction( action, containerId );
await fetchContainers();
};
- const handleComposeSubmit = async (event) => {
+ const handleComposeSubmit = async ( event ) => {
event.preventDefault();
setMessage( await DockerService.composeDown( composeFile ) );
};
- return(
+ const containerTable = useMaterialReactTable( {
+ columns:[
+ { accessorKey:'id', header:'ID' },
+ { accessorKey:'name', header:'Name' },
+ { accessorKey:'state', header:'State' },
+ { accessorKey:'status', header:'Status' },
+ {
+ header:'Actions',
+ Cell:( { row } ) => (
+ <>
+ <Button variant="contained"
+ color="primary"
+ onClick={ () => handleContainerAction( 'start', row.original.id ) }
+ disabled={ row.original.state === 'running' }>
+ Start
+ </Button>
+ <Button variant="contained"
+ color="secondary"
+ onClick={ () => handleContainerAction( 'stop', row.original.id ) }
+ disabled={ row.original.state !== 'running' }>
+ Stop
+ </Button>
+ </>
+ ),
+ },
+ ],
+ data:containers
+ } );
+
+ const imageTable = useMaterialReactTable( {
+ columns:[
+ { accessorKey:'id', header:'ID' },
+ { accessorKey:'tags', header:'Tags', Cell:( { cell } ) => cell.getValue().join( ', ' ) },
+ { accessorKey:'created', header:'Created' },
+ ],
+ data:images
+ } );
+
+ return (
<Container>
- <Typography variant="h5" className="mt-6">Docker Containers</Typography>
- <MaterialReactTable
- columns={[
- { accessorKey: 'id', header: 'ID' },
- { accessorKey: 'name', header: 'Name' },
- { accessorKey: 'state', header: 'State' },
- { accessorKey: 'status', header: 'Status' },
- {
- header: 'Actions',
- Cell: ({ row }) => (
- <>
- <Button
- variant="contained"
- color="primary"
- onClick={() => handleContainerAction('start', row.original.id)}
- disabled={row.original.state === 'running'}
- >
- Start
- </Button>
- <Button
- variant="contained"
- color="secondary"
- onClick={() => handleContainerAction('stop', row.original.id)}
- disabled={row.original.state !== 'running'}
- >
- Stop
- </Button>
- </>
- ),
- },
- ]}
- data={containers}
- />
+ <Typography variant="h5"
+ className="mt-6">Docker Containers</Typography>
+ <MaterialReactTable table={ containerTable }/>
- <Typography variant="h5" className="mt-6">Docker Images</Typography>
- <MaterialReactTable
- columns={[
- { accessorKey: 'id', header: 'ID' },
- { accessorKey: 'tags', header: 'Tags', Cell: ({ cell }) => cell.getValue().join(', ') },
- { accessorKey: 'created', header: 'Created' },
- ]}
- data={images}
- />
+ <Typography variant="h5"
+ className="mt-6">Docker Images</Typography>
+ <MaterialReactTable table={ imageTable }/>
- <Typography variant="h5" className="mt-6">Stop Docker Compose Services</Typography>
- <form onSubmit={handleComposeSubmit} className="space-y-4">
- <TextField
- label="Compose File Path"
- value={composeFile}
- onChange={(e) => setComposeFile(e.target.value)}
- fullWidth
- />
- <Button type="submit" variant="contained" color="secondary">Stop Compose Services</Button>
+ <Typography variant="h5"
+ className="mt-6">Stop Docker Compose Services</Typography>
+ <form onSubmit={ handleComposeSubmit }
+ className="space-y-4">
+ <TextField label="Compose File Path"
+ value={ composeFile }
+ onChange={ ( e ) => setComposeFile( e.target.value ) }
+ fullWidth/>
+ <Button type="submit"
+ variant="contained"
+ color="secondary">Stop Compose Services</Button>
</form>
- {message && <Typography color={message?.startsWith('Error') ? 'error' : 'success'}>{message}</Typography>}
+ { message && <Typography color={ message?.startsWith( 'Error' ) ? 'error' : 'success' }>{ message }</Typography> }
</Container>
- )
-}
+ );
+};
export default Docker;
\ No newline at end of file