Browse Source

Move detection device to App.js

master
Loëck Vézien 7 years ago
parent
commit
c5477b277a
No known key found for this signature in database GPG Key ID: CBCDCE384E853AC4
  1. 77
      src/components/App.js
  2. 34
      src/components/Home.js

77
src/components/App.js

@ -1,10 +1,13 @@
// @flow // @flow
import React from 'react' import React, { PureComponent } from 'react'
import { ThemeProvider } from 'styled-components' import { ThemeProvider } from 'styled-components'
import { ConnectedRouter } from 'react-router-redux' import { ConnectedRouter } from 'react-router-redux'
import { Provider } from 'react-redux' import { Provider, connect } from 'react-redux'
import { Route } from 'react-router' import { Route } from 'react-router'
import { ipcRenderer } from 'electron'
import { devicesUpdate, deviceAdd, deviceRemove } from 'actions/devices'
import theme from 'styles/theme' import theme from 'styles/theme'
@ -13,18 +16,58 @@ import SideBar from 'components/SideBar'
import TopBar from 'components/TopBar' import TopBar from 'components/TopBar'
import Home from 'components/Home' import Home from 'components/Home'
export default ({ store, history }: { store: Object, history: Object }) => ( type Props = {
<Provider store={store}> deviceAdd: (device: Object) => void,
<ThemeProvider theme={theme}> deviceRemove: (device: Object) => void,
<ConnectedRouter history={history}> devicesUpdate: (devices: Object) => void,
<Box grow horizontal> history: Object,
<SideBar /> store: Object,
<Box grow bg="cream"> }
<TopBar />
<Route path="/" component={Home} /> class App extends PureComponent<Props> {
</Box> componentWillMount() {
</Box> const { devicesUpdate, deviceAdd, deviceRemove } = this.props
</ConnectedRouter>
</ThemeProvider> ipcRenderer.on('updateDevices', (e, devices) => devicesUpdate(devices))
</Provider> ipcRenderer.on('addDevice', (e, device) => deviceAdd(device))
) ipcRenderer.on('removeDevice', (e, device) => deviceRemove(device))
// First renderer, we get all devices
ipcRenderer.send('getDevices')
// Start detection when we plug/unplug devices
ipcRenderer.send('listenDevices')
}
componentWillUnmount() {
ipcRenderer.removeAllListeners('updateDevices')
ipcRenderer.removeAllListeners('addDevice')
ipcRenderer.removeAllListeners('removeDevice')
}
render() {
const { history, store } = this.props
return (
<Provider store={store}>
<ThemeProvider theme={theme}>
<ConnectedRouter history={history}>
<Box grow horizontal>
<SideBar />
<Box grow bg="cream">
<TopBar />
<Route path="/" component={Home} />
</Box>
</Box>
</ConnectedRouter>
</ThemeProvider>
</Provider>
)
}
}
export default connect(null, {
deviceAdd,
deviceRemove,
devicesUpdate,
})(App)

34
src/components/Home.js

@ -2,38 +2,12 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { ipcRenderer } from 'electron'
import { devicesUpdate, deviceAdd, deviceRemove } from 'actions/devices'
type Props = { type Props = {
devicesUpdate: (devices: Object) => void, devices: Array<Object>,
deviceAdd: (device: Object) => void,
deviceRemove: (device: Object) => void,
devices: Object,
} }
class Home extends PureComponent<Props> { class Home extends PureComponent<Props> {
componentWillMount() {
const { devicesUpdate, deviceAdd, deviceRemove } = this.props
ipcRenderer.on('updateDevices', (e, devices) => devicesUpdate(devices))
ipcRenderer.on('addDevice', (e, device) => deviceAdd(device))
ipcRenderer.on('removeDevice', (e, device) => deviceRemove(device))
// First renderer, we get all devices
ipcRenderer.send('getDevices')
// Start detection when we plug/unplug devices
ipcRenderer.send('listenDevices')
}
componentWillUnmount() {
ipcRenderer.removeAllListeners('updateDevices')
ipcRenderer.removeAllListeners('addDevice')
ipcRenderer.removeAllListeners('removeDevice')
}
render() { render() {
const { devices } = this.props const { devices } = this.props
@ -41,8 +15,4 @@ class Home extends PureComponent<Props> {
} }
} }
export default connect(({ devices }) => ({ devices }), { export default connect(({ devices }: Props): Object => ({ devices }))(Home)
deviceAdd,
deviceRemove,
devicesUpdate,
})(Home)

Loading…
Cancel
Save