Skip to content

Commit

Permalink
Convert ContainerRestoreModal to a functional component
Browse files Browse the repository at this point in the history
  • Loading branch information
jelly committed Nov 30, 2022
1 parent 79e8426 commit 341a30b
Showing 1 changed file with 54 additions and 73 deletions.
127 changes: 54 additions & 73 deletions src/ContainerRestoreModal.jsx
Original file line number Diff line number Diff line change
@@ -1,90 +1,71 @@
import React from 'react';
import React, { useState } from 'react';
import { Button, Checkbox, Form, Modal } from '@patternfly/react-core';
import { DialogsContext } from "dialogs.jsx";
import { useDialogs } from "dialogs.jsx";
import cockpit from 'cockpit';

import * as client from './client.js';

const _ = cockpit.gettext;

class ContainerRestoreModal extends React.Component {
static contextType = DialogsContext;
const ContainerRestoreModal = ({ containerWillRestore, onAddNotification }) => {
const Dialogs = useDialogs();

constructor(props) {
super(props);
this.state = {
inProgress: false,
keep: false,
tcpEstablished: false,
ignoreRootFS: false,
ignoreStaticIP: false,
ignoreStaticMAC: false
};
this.handleChange = this.handleChange.bind(this);
}
const [inProgress, setInProgress] = useState(false);
const [keep, setKeep] = useState(false);
const [tcpEstablished, setTcpEstablished] = useState(false);
const [ignoreStaticIP, setIgnoreStaticIP] = useState(false);
const [ignoreStaticMAC, setIgnoreStaticMAC] = useState(false);

handleChange(checked, event) {
if (event.target.type === "checkbox")
this.setState({ [event.target.name]: event.target.checked });
}

handleRestoreContainer(args) {
const Dialogs = this.context;
const container = this.props.containerWillRestore;
this.setState({ inProgress: true });
client.postContainer(container.isSystem, "restore", container.Id, args)
const handleRestoreContainer = (args) => {
setInProgress(true);
client.postContainer(containerWillRestore.isSystem, "restore", containerWillRestore.Id, args)
.catch(ex => {
const error = cockpit.format(_("Failed to restore container $0"), container.Names);
this.props.onAddNotification({ type: 'danger', error, errorDetail: ex.message });
this.setState({ inProgress: false });
const error = cockpit.format(_("Failed to restore container $0"), containerWillRestore.Names);
onAddNotification({ type: 'danger', error, errorDetail: ex.message });
setInProgress(false);
})
.finally(() => {
Dialogs.close();
});
}
};

render() {
const Dialogs = this.context;
return (
<Modal isOpen
showClose={false}
position="top" variant="medium"
title={cockpit.format(_("Restore container $0"), this.props.containerWillRestore.Names)}
footer={<>
<Button variant="primary" isDisabled={this.state.inProgress}
isLoading={this.state.inProgress}
onClick={() => {
this.handleRestoreContainer({
keep: this.state.keep,
leaveRunning: this.state.leaveRunning,
tcpEstablished: this.state.tcpEstablished,
ignoreRootFS: this.state.ignoreRootFS
});
}}>
{_("Restore")}
</Button>
<Button variant="link" isDisabled={this.state.inProgress}
onClick={Dialogs.close}>
{_("Cancel")}
</Button>
</>}
>
<Form isHorizontal>
<Checkbox label={_("Keep all temporary checkpoint files")} id="restore-dialog-keep" name="keep"
isChecked={this.state.keep} onChange={this.handleChange} />
<Checkbox label={_("Restore with established TCP connections")}
id="restore-dialog-tcpEstablished" name="tcpEstablished"
isChecked={this.state.tcpEstablished} onChange={this.handleChange} />
<Checkbox label={_("Ignore IP address if set statically")} id="restore-dialog-ignoreStaticIP"
name="ignoreStaticIP" isChecked={this.state.ignoreStaticIP}
onChange={this.handleChange} />
<Checkbox label={_("Ignore MAC address if set statically")} id="restore-dialog-ignoreStaticMAC"
name="ignoreStaticMAC" isChecked={this.state.ignoreStaticMAC}
onChange={this.handleChange} />
</Form>
</Modal>
);
}
}
return (
<Modal isOpen
showClose={false}
position="top" variant="medium"
title={cockpit.format(_("Restore container $0"), containerWillRestore.Names)}
footer={<>
<Button variant="primary" isDisabled={inProgress}
isLoading={inProgress}
onClick={() => {
handleRestoreContainer({
keep: keep,
tcpEstablished: tcpEstablished,
});
}}>
{_("Restore")}
</Button>
<Button variant="link" isDisabled={inProgress}
onClick={Dialogs.close}>
{_("Cancel")}
</Button>
</>}
>
<Form isHorizontal>
<Checkbox label={_("Keep all temporary checkpoint files")} id="restore-dialog-keep" name="keep"
isChecked={keep} onChange={setKeep} />
<Checkbox label={_("Restore with established TCP connections")}
id="restore-dialog-tcpEstablished" name="tcpEstablished"
isChecked={tcpEstablished} onChange={setTcpEstablished} />
<Checkbox label={_("Ignore IP address if set statically")} id="restore-dialog-ignoreStaticIP"
name="ignoreStaticIP" isChecked={ignoreStaticIP}
onChange={setIgnoreStaticIP} />
<Checkbox label={_("Ignore MAC address if set statically")} id="restore-dialog-ignoreStaticMAC"
name="ignoreStaticMAC" isChecked={ignoreStaticMAC}
onChange={setIgnoreStaticMAC} />
</Form>
</Modal>
);
};

export default ContainerRestoreModal;

0 comments on commit 341a30b

Please sign in to comment.