From e1c4e2c1d40e8e68a79b2734b24f0e38ec4d6a82 Mon Sep 17 00:00:00 2001 From: Casper Lamboo Date: Wed, 7 Mar 2018 11:46:50 +0100 Subject: [PATCH] open printer popup with ip if printer is unknown --- src/interface/Settings.js | 78 +++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/src/interface/Settings.js b/src/interface/Settings.js index a5f2f7d..f203012 100644 --- a/src/interface/Settings.js +++ b/src/interface/Settings.js @@ -90,46 +90,41 @@ class Settings extends React.Component { advancedFields: PropTypes.array.isRequired }; - constructor(props) { - super(props); + state = { + 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) { - const active = Object.entries(localStorage.printers) + if (selectedPrinter && localStorage.active) { + const activePrinter = selectedPrinter && Object.entries(localStorage.printers) .map(([key, value]) => ({ key, value })) .find(({ key, value: { ip } }) => ip === selectedPrinter); - if (active) { - localStorage.active = active.key; - updateLocalStorage(localStorage); + if (activePrinter) { + const state = this.changeSettings('activePrinter', activePrinter.key); + if (onChange) onChange(this.constructSettings(state.localStorage)); + } else { + this.openAddPrinterDialog({ ip: selectedPrinter }); } - } - 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) { + } else if (!selectedPrinter && localStorage.active) { if (onChange) onChange(this.constructSettings(localStorage)); - } else { + } 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);