-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert ContainerRestoreModal to a functional component
- Loading branch information
Showing
1 changed file
with
54 additions
and
73 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
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; |