open printer popup with ip if printer is unknown

This commit is contained in:
Casper Lamboo 2018-03-07 11:46:50 +01:00
parent bc67cab75f
commit e1c4e2c1d4

View File

@ -90,25 +90,8 @@ class Settings extends React.Component {
advancedFields: PropTypes.array.isRequired
};
constructor(props) {
super(props);
const { selectedPrinter } = this.props;
const localStorage = getLocalStorage();
if (selectedPrinter) {
const active = Object.entries(localStorage.printers)
.map(([key, value]) => ({ key, value }))
.find(({ key, value: { ip } }) => ip === selectedPrinter);
if (active) {
localStorage.active = active.key;
updateLocalStorage(localStorage);
}
}
this.state = {
localStorage,
state = {
localStorage: getLocalStorage(),
wifiBoxes: [],
addPrinter: {
open: false,
@ -121,15 +104,27 @@ class Settings extends React.Component {
open: false
}
};
}
componentDidMount() {
const { onChange } = this.props;
const { onChange, selectedPrinter } = this.props;
const { localStorage } = this.state;
if (localStorage.active) {
if (onChange) onChange(this.constructSettings(localStorage));
if (selectedPrinter && localStorage.active) {
const activePrinter = selectedPrinter && Object.entries(localStorage.printers)
.map(([key, value]) => ({ key, value }))
.find(({ key, value: { ip } }) => ip === selectedPrinter);
if (activePrinter) {
const state = this.changeSettings('activePrinter', activePrinter.key);
if (onChange) onChange(this.constructSettings(state.localStorage));
} else {
this.openAddPrinterDialog({ ip: selectedPrinter });
}
} else if (!selectedPrinter && localStorage.active) {
if (onChange) onChange(this.constructSettings(localStorage));
} else if (selectedPrinter && !localStorage.active) {
this.openAddPrinterDialog({ ip: selectedPrinter });
} else if (!selectedPrinter && !localStorage.active) {
this.openAddPrinterDialog();
}
@ -238,6 +233,8 @@ class Settings extends React.Component {
if (onChange) onChange(this.constructSettings(state.localStorage));
updateLocalStorage(state.localStorage);
}
return state;
}
getChildContext() {
@ -357,9 +354,18 @@ class Settings extends React.Component {
if (onChange) onChange(this.constructSettings(localStorage));
};
closeAddPrinterDialog = () => this.setAddPrinterDialog(false);
openAddPrinterDialog = () => this.setAddPrinterDialog(true);
setAddPrinterDialog = (open) => this.setState({ addPrinter: { ip: '', name: '', printer: '', error: null, open } });
closeAddPrinterDialog = (override) => this.setAddPrinterDialog(false, override);
openAddPrinterDialog = (override) => this.setAddPrinterDialog(true, override);
setAddPrinterDialog = (open, override = {}) => this.setState({
addPrinter: {
ip: '',
name: '',
printer: '',
error: null,
open,
...override
}
});
closeManagePrinterDialog = () => this.setManagePrinterDialog(false);
openManagePrinterDialog = () => this.setManagePrinterDialog(true);