Browse Source

Add Wrapper, move events in renderer

master
Loëck Vézien 7 years ago
parent
commit
15165b3822
No known key found for this signature in database GPG Key ID: CBCDCE384E853AC4
  1. 87
      src/components/App.js
  2. 19
      src/components/Wrapper.js
  3. 4
      src/renderer/index.js
  4. 17
      src/renderer/initEvents.js

87
src/components/App.js

@ -1,78 +1,25 @@
// @flow
import React, { PureComponent } from 'react'
import React from 'react'
import { ThemeProvider } from 'styled-components'
import { ConnectedRouter } from 'react-router-redux'
import { Provider, connect } from 'react-redux'
import { Route } from 'react-router'
import { ipcRenderer } from 'electron'
import { Provider } from 'react-redux'
import { I18nextProvider } from 'react-i18next'
import { devicesUpdate, deviceAdd, deviceRemove } from 'actions/devices'
import theme from 'styles/theme'
import Box from 'components/base/Box'
import SideBar from 'components/SideBar'
import TopBar from 'components/TopBar'
import Home from 'components/Home'
import i18n from '../renderer/i18n'
type Props = {
deviceAdd: (device: Object) => void,
deviceRemove: (device: Object) => void,
devicesUpdate: (devices: Object) => void,
history: Object,
store: Object,
}
class App 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() {
const { history, store } = this.props
return (
<Provider store={store}>
<I18nextProvider i18n={i18n}>
<ThemeProvider theme={theme}>
<ConnectedRouter history={history}>
<Box grow horizontal>
<SideBar />
<Box grow bg="cream">
<TopBar />
<Route path="/" component={Home} />
</Box>
</Box>
</ConnectedRouter>
</ThemeProvider>
</I18nextProvider>
</Provider>
)
}
}
export default connect(null, {
deviceAdd,
deviceRemove,
devicesUpdate,
})(App)
import i18n from 'renderer/i18n'
import Wrapper from 'components/Wrapper'
export default ({ store, history }: { store: Object, history: Object }) => (
<Provider store={store}>
<I18nextProvider i18n={i18n}>
<ThemeProvider theme={theme}>
<ConnectedRouter history={history}>
<Wrapper />
</ConnectedRouter>
</ThemeProvider>
</I18nextProvider>
</Provider>
)

19
src/components/Wrapper.js

@ -0,0 +1,19 @@
// @flow
import React from 'react'
import { Route } from 'react-router'
import Box from 'components/base/Box'
import SideBar from 'components/SideBar'
import TopBar from 'components/TopBar'
import Home from 'components/Home'
export default () => (
<Box grow horizontal>
<SideBar />
<Box grow bg="cream">
<TopBar />
<Route path="/" component={Home} />
</Box>
</Box>
)

4
src/renderer/index.js

@ -6,6 +6,8 @@ import { AppContainer } from 'react-hot-loader' // eslint-disable-line import/no
import createHistory from 'history/createBrowserHistory'
import createStore from 'renderer/createStore'
import initEvents from 'renderer/initEvents'
import App from 'components/App'
import 'styles/global'
@ -14,6 +16,8 @@ const history = createHistory()
const store = createStore(history)
const rootNode = document.getElementById('app')
initEvents(store)
function r(Comp) {
if (rootNode) {
render(<AppContainer>{Comp}</AppContainer>, rootNode)

17
src/renderer/initEvents.js

@ -0,0 +1,17 @@
// @flow
import { ipcRenderer } from 'electron'
import { devicesUpdate, deviceAdd, deviceRemove } from 'actions/devices'
export default (store: Object) => {
ipcRenderer.on('updateDevices', (e, devices) => store.dispatch(devicesUpdate(devices)))
ipcRenderer.on('addDevice', (e, device) => store.dispatch(deviceAdd(device)))
ipcRenderer.on('removeDevice', (e, device) => store.dispatch(deviceRemove(device)))
// First time, we get all devices
ipcRenderer.send('getDevices')
// Start detection when we plug/unplug devices
ipcRenderer.send('listenDevices')
}
Loading…
Cancel
Save