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