This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from CharVstack/CVS-142-フロントエンドからvmの操作
CVS-142-フロントエンドからvmの操作
- Loading branch information
Showing
12 changed files
with
218 additions
and
55 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { within } from '@storybook/testing-library'; | ||
import { composeStories } from '@storybook/testing-react'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import * as stories from './VmControlMenu.stories'; | ||
|
||
describe('VmControlMenu', () => { | ||
describe('Interaction', () => { | ||
const { Default, Disabled } = composeStories(stories); | ||
|
||
test('ボタンが有効', () => { | ||
const { container } = render(<Default />); | ||
const canvas = within(container); | ||
const button = canvas.getByText<HTMLButtonElement>('Actions'); | ||
expect(button.disabled).toBeFalsy(); | ||
}); | ||
test('ボタンが無効', () => { | ||
const { container } = render(<Disabled />); | ||
const canvas = within(container); | ||
const button = canvas.getByText<HTMLButtonElement>('Actions'); | ||
expect(button.disabled).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
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,24 @@ | ||
import { ComponentMeta, ComponentStoryObj } from '@storybook/react'; | ||
|
||
import schema from '@openapi-spec/v1.json'; | ||
|
||
import { BaseVmControlMenu as VmControlMenu } from './VmControlMenu'; | ||
|
||
export default { | ||
component: VmControlMenu, | ||
} as ComponentMeta<typeof VmControlMenu>; | ||
|
||
export const Default: ComponentStoryObj<typeof VmControlMenu> = { | ||
args: { | ||
vms: [ | ||
schema.components.responses.GetAllVMsList200Response.content['application/json'].examples['example-1'].value | ||
.vms[0].metadata.id, | ||
], | ||
}, | ||
}; | ||
|
||
export const Disabled: ComponentStoryObj<typeof VmControlMenu> = { | ||
args: { | ||
vms: [], | ||
}, | ||
}; |
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,81 @@ | ||
import { getSWRDefaultKey } from '@aspida/swr'; | ||
import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown'; | ||
import PowerSettingsNewIcon from '@mui/icons-material/PowerSettingsNew'; | ||
import { Box } from '@mui/material'; | ||
import Button from '@mui/material/Button'; | ||
import Menu from '@mui/material/Menu'; | ||
import MenuItem from '@mui/material/MenuItem'; | ||
import { useState, useCallback } from 'react'; | ||
import { useSWRConfig } from 'swr'; | ||
|
||
import { useSelectedVmReadOnlyAtom } from '@components/organisms/Tables'; | ||
import { apiClient } from '@lib/apiClient'; | ||
|
||
type BaseVmControlMenuProps = { | ||
vms: string[]; | ||
}; | ||
|
||
export const VmControlMenu = () => { | ||
const vms = useSelectedVmReadOnlyAtom(); | ||
|
||
return <BaseVmControlMenu vms={vms} />; | ||
}; | ||
|
||
export const BaseVmControlMenu = ({ vms }: BaseVmControlMenuProps) => { | ||
const { mutate } = useSWRConfig(); | ||
const handleSubmit = useCallback( | ||
(action: 'start' | 'shutdown' | 'reboot' | 'reset') => async () => { | ||
await Promise.all( | ||
vms.map(async (vm) => { | ||
await apiClient.api.v1.vms | ||
._vmId(vm) | ||
.power.$post({ body: { action } }) | ||
.then(async () => { | ||
await mutate(getSWRDefaultKey(apiClient.api.v1.vms._vmId(vm))); | ||
handleClose(); | ||
}) | ||
.catch(() => { | ||
handleClose(); | ||
}); | ||
}) | ||
); | ||
}, | ||
[vms, mutate] | ||
); | ||
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); | ||
const open = Boolean(anchorEl); | ||
const handleClick = (event: React.MouseEvent<HTMLElement>) => { | ||
setAnchorEl(event.currentTarget); | ||
}; | ||
const handleClose = () => { | ||
setAnchorEl(null); | ||
}; | ||
|
||
return ( | ||
<Box sx={{ ml: 1, mb: 1 }}> | ||
<Button | ||
disabled={vms.length === 0} | ||
variant="contained" | ||
onClick={handleClick} | ||
color="success" | ||
endIcon={<KeyboardArrowDownIcon />} | ||
> | ||
<PowerSettingsNewIcon /> | ||
Actions | ||
</Button> | ||
<Menu anchorEl={anchorEl} open={open} onClose={handleClose}> | ||
<Box sx={{ width: 180 }}> | ||
<MenuItem onClick={handleSubmit('start')} disableRipple> | ||
Start | ||
</MenuItem> | ||
<MenuItem onClick={handleSubmit('shutdown')} disableRipple> | ||
Shutdown | ||
</MenuItem> | ||
<MenuItem onClick={handleSubmit('reboot')} disableRipple> | ||
Restart | ||
</MenuItem> | ||
</Box> | ||
</Menu> | ||
</Box> | ||
); | ||
}; |
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
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