refactor(front-end): modify fetch and add fetching data for vnc connection

main
Josue 3 years ago
parent 83883821f4
commit 2c7e2c78ae
  1. 2
      striker-ui/components/Anvils/SelectedAnvil.tsx
  2. 29
      striker-ui/components/Display/FullSize.tsx
  3. 14
      striker-ui/components/Hosts/AnvilHost.tsx
  4. 2
      striker-ui/components/Servers.tsx
  5. 5
      striker-ui/lib/fetchers/putJSON.ts
  6. 2
      striker-ui/pages/server/[uuid].tsx

@ -67,7 +67,7 @@ const SelectedAnvil = ({ list }: { list: AnvilListItem[] }): JSX.Element => {
<Switch <Switch
checked={isAnvilOn(list[index])} checked={isAnvilOn(list[index])}
onChange={() => onChange={() =>
putJSON('/set_power', { putJSON(`${process.env.NEXT_PUBLIC_API_URL}/set_power`, {
anvil_uuid: list[index].anvil_uuid, anvil_uuid: list[index].anvil_uuid,
is_on: !isAnvilOn(list[index]), is_on: !isAnvilOn(list[index]),
}) })

@ -9,6 +9,7 @@ import VncDisplay from './VncDisplay';
import { Panel } from '../Panels'; import { Panel } from '../Panels';
import { BLACK, RED, TEXT } from '../../lib/consts/DEFAULT_THEME'; import { BLACK, RED, TEXT } from '../../lib/consts/DEFAULT_THEME';
import keyCombinations from './keyCombinations'; import keyCombinations from './keyCombinations';
import putJSON from '../../lib/fetchers/putJSON';
const useStyles = makeStyles(() => ({ const useStyles = makeStyles(() => ({
displayBox: { displayBox: {
@ -48,11 +49,21 @@ const useStyles = makeStyles(() => ({
interface PreviewProps { interface PreviewProps {
setMode: Dispatch<SetStateAction<boolean>>; setMode: Dispatch<SetStateAction<boolean>>;
uuid: string;
} }
const FullSize = ({ setMode }: PreviewProps): JSX.Element => { interface VncConnectionProps {
protocol: string;
forward_port: number;
}
const FullSize = ({ setMode, uuid }: PreviewProps): JSX.Element => {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const rfb = useRef<typeof RFB>(undefined); const rfb = useRef<typeof RFB>(undefined);
const vncConnection = useRef<VncConnectionProps>({
protocol: '',
forward_port: 0,
});
const [displaySize, setDisplaySize] = useState< const [displaySize, setDisplaySize] = useState<
| { | {
width: string | number; width: string | number;
@ -67,7 +78,19 @@ const FullSize = ({ setMode }: PreviewProps): JSX.Element => {
width: '75vw', width: '75vw',
height: '80vh', height: '80vh',
}); });
}, []);
(async () => {
const res = await putJSON(
'http://108.168.17.168/cgi-bin/manage_vnc_pipes',
{
server_uuid: uuid,
is_open: true,
},
);
// console.log(res);
vncConnection.current = res.json();
})();
}, [uuid]);
const handleClick = (event: React.MouseEvent<HTMLButtonElement>): void => { const handleClick = (event: React.MouseEvent<HTMLButtonElement>): void => {
setAnchorEl(event.currentTarget); setAnchorEl(event.currentTarget);
@ -97,7 +120,7 @@ const FullSize = ({ setMode }: PreviewProps): JSX.Element => {
<Box> <Box>
<VncDisplay <VncDisplay
rfb={rfb} rfb={rfb}
url="wss://spain.cdot.systems:5000/" url={`ws://108.168.17.168:${vncConnection.current.forward_port}`}
style={displaySize} style={displaySize}
/> />
</Box> </Box>

@ -104,10 +104,13 @@ const AnvilHost = ({
<Switch <Switch
checked={host.state === 'online'} checked={host.state === 'online'}
onChange={() => onChange={() =>
putJSON('/set_power', { putJSON(
`${process.env.NEXT_PUBLIC_API_URL}/set_power`,
{
host_uuid: host.host_uuid, host_uuid: host.host_uuid,
is_on: !(host.state === 'online'), is_on: !(host.state === 'online'),
}) },
)
} }
/> />
</Box> </Box>
@ -119,10 +122,13 @@ const AnvilHost = ({
checked={host.state === 'online'} checked={host.state === 'online'}
disabled={!(host.state === 'online')} disabled={!(host.state === 'online')}
onChange={() => onChange={() =>
putJSON('/set_membership', { putJSON(
`${process.env.NEXT_PUBLIC_API_URL}/set_membership`,
{
host_uuid: host.host_uuid, host_uuid: host.host_uuid,
is_member: !(host.state === 'online'), is_member: !(host.state === 'online'),
}) },
)
} }
/> />
</Box> </Box>

@ -145,7 +145,7 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => {
const handlePower = (label: ButtonLabels) => { const handlePower = (label: ButtonLabels) => {
setAnchorEl(null); setAnchorEl(null);
if (selected.length) { if (selected.length) {
putJSON('/set_power', { putJSON(`${process.env.NEXT_PUBLIC_API_URL}/set_power`, {
server_uuid_list: selected, server_uuid_list: selected,
is_on: label === 'on', is_on: label === 'on',
}); });

@ -1,5 +1,6 @@
const putJSON = <T>(uri: string, data: T): void => { /* eslint-disable @typescript-eslint/no-explicit-any */
fetch(`${process.env.NEXT_PUBLIC_API_URL}${uri}`, { const putJSON = <T>(uri: string, data: T): Promise<any> => {
return fetch(uri, {
method: 'PUT', method: 'PUT',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',

@ -74,7 +74,7 @@ const Server = (): JSX.Element => {
</Box> </Box>
) : ( ) : (
<Box className={classes.container}> <Box className={classes.container}>
<FullSize setMode={setPreviewMode} /> <FullSize setMode={setPreviewMode} uuid={uuid} />
</Box> </Box>
))} ))}
</> </>

Loading…
Cancel
Save