-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added more actions
- Loading branch information
Showing
14 changed files
with
581 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Button from '@material-ui/core/Button' | ||
import Dialog from '@material-ui/core/Dialog' | ||
import DialogActions from '@material-ui/core/DialogActions' | ||
import DialogContent from '@material-ui/core/DialogContent' | ||
import DialogContentText from '@material-ui/core/DialogContentText' | ||
import DialogTitle from '@material-ui/core/DialogTitle' | ||
import { useState } from 'react' | ||
|
||
const defaultValue = { | ||
title: 'Konfirmasi', | ||
content: '', | ||
} | ||
|
||
export const useMaterialDialog = () => { | ||
const [isOpen, setOpen] = useState(false) | ||
const [data, setData] = useState({ ...defaultValue, action: null }) | ||
|
||
return { | ||
isOpen, | ||
setOpen, | ||
data, | ||
confirmModal: ({ title = defaultValue.title, content, action }) => { | ||
setData({ title, content, action }) | ||
setOpen(true) | ||
}, | ||
} | ||
} | ||
|
||
export const DialogComponent = ({ | ||
isOpen, | ||
data, | ||
setOpen, | ||
}: ReturnType<typeof useMaterialDialog>) => { | ||
const handleClose = () => { | ||
setOpen(false) | ||
} | ||
|
||
const handleConfirm = async () => { | ||
if (await data.action()) { | ||
setOpen(false) | ||
} | ||
} | ||
|
||
return ( | ||
<Dialog open={isOpen}> | ||
<DialogTitle>{data.title}</DialogTitle> | ||
<DialogContent> | ||
<DialogContentText>{data.content}</DialogContentText> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={handleClose} color='primary'> | ||
Tidak | ||
</Button> | ||
<Button onClick={handleConfirm} color='primary' autoFocus> | ||
Ya | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import { | ||
Drawer, | ||
IconButton, | ||
List, | ||
ListItem, | ||
ListItemIcon, | ||
ListItemSecondaryAction, | ||
ListItemText, | ||
Menu, | ||
MenuItem, | ||
Toolbar, | ||
useTheme, | ||
} from '@material-ui/core' | ||
import { makeStyles, Theme } from '@material-ui/core/styles' | ||
import MoreVertIcon from '@material-ui/icons/MoreVert' | ||
import OfflineBoltIcon from '@material-ui/icons/OfflineBolt' | ||
import RemoveCircleOutlineIcon from '@material-ui/icons/RemoveCircleOutline' | ||
import clsx from 'clsx' | ||
import PopupState, { bindMenu, bindTrigger } from 'material-ui-popup-state' | ||
import { useRouter } from 'next/router' | ||
import { Fragment, useMemo } from 'react' | ||
import useSWR from 'swr' | ||
import Api from '../util/Api' | ||
|
||
export const drawerWidth = 300 | ||
|
||
const useStyles = makeStyles((theme: Theme) => { | ||
return { | ||
drawer: { | ||
width: drawerWidth, | ||
flexShrink: 0, | ||
}, | ||
itemActive: { | ||
background: theme.palette.grey[300], | ||
}, | ||
drawerPaper: { | ||
width: drawerWidth, | ||
}, | ||
drawerContainer: { | ||
overflow: 'auto', | ||
}, | ||
} | ||
}) | ||
|
||
interface Props { | ||
active?: string | ||
} | ||
|
||
const ProcessList = ({ active }: Props) => { | ||
const router = useRouter() | ||
const { data, error, isValidating, revalidate } = useSWR('/all', { | ||
revalidateOnMount: true, | ||
initialData: { data: [] }, | ||
}) | ||
const classes = useStyles() | ||
const theme = useTheme() | ||
|
||
const lists = useMemo(() => { | ||
if (data.data && data.data.length > 0) { | ||
return data.data | ||
} | ||
return [] | ||
}, [data]) | ||
|
||
const callAction = async ({ id, action }, onClose) => { | ||
onClose() | ||
try { | ||
const { data } = await Api().post('/action', { id, action }) | ||
revalidate() | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
|
||
return ( | ||
<Drawer | ||
className={classes['drawer']} | ||
variant='permanent' | ||
classes={{ | ||
paper: classes.drawerPaper, | ||
}} | ||
> | ||
<Toolbar /> | ||
<div className={classes.drawerContainer}> | ||
<List> | ||
{lists.map((item, index) => ( | ||
<ListItem | ||
key={index} | ||
button | ||
onClick={() => { | ||
router.push('/process/[id]', `/process/${item.pm_id}`) | ||
}} | ||
className={clsx({ | ||
[classes.itemActive]: item.pm_id.toString() === active, | ||
})} | ||
> | ||
<ListItemIcon> | ||
{item.pm2_env.status === 'online' ? ( | ||
<OfflineBoltIcon | ||
style={{ color: theme.palette.success.main }} | ||
/> | ||
) : ( | ||
<RemoveCircleOutlineIcon /> | ||
)} | ||
</ListItemIcon> | ||
<ListItemText | ||
primary={item.name} | ||
primaryTypographyProps={{ noWrap: true }} | ||
secondary={item.pm2_env.status} | ||
/> | ||
<ListItemSecondaryAction> | ||
<PopupState variant='popover' popupId={`process-${index}`}> | ||
{(popupState) => ( | ||
<Fragment> | ||
<IconButton {...bindTrigger(popupState)}> | ||
<MoreVertIcon /> | ||
</IconButton> | ||
<Menu {...bindMenu(popupState)}> | ||
{item.pm2_env.status === 'online' ? ( | ||
<Fragment> | ||
<MenuItem | ||
onClick={() => | ||
callAction( | ||
{ id: item.name, action: 'stop' }, | ||
popupState.close | ||
) | ||
} | ||
> | ||
Stop | ||
</MenuItem> | ||
<MenuItem | ||
onClick={() => | ||
callAction( | ||
{ id: item.name, action: 'restart' }, | ||
popupState.close | ||
) | ||
} | ||
> | ||
Restart | ||
</MenuItem> | ||
</Fragment> | ||
) : ( | ||
<MenuItem | ||
onClick={() => | ||
callAction( | ||
{ id: item.name, action: 'start' }, | ||
popupState.close | ||
) | ||
} | ||
> | ||
Start | ||
</MenuItem> | ||
)} | ||
</Menu> | ||
</Fragment> | ||
)} | ||
</PopupState> | ||
</ListItemSecondaryAction> | ||
</ListItem> | ||
))} | ||
</List> | ||
</div> | ||
</Drawer> | ||
) | ||
} | ||
|
||
export default ProcessList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.