From 2332855e7c97e2f4cc68b1ef60dfc3448e973224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=ABck=20V=C3=A9zien?= Date: Wed, 10 Jan 2018 16:11:06 +0100 Subject: [PATCH] Add device in TopBar --- src/actions/devices.js | 10 +-- src/components/Home.js | 28 +++++---- src/components/TopBar.js | 107 +++++++++++++++++++++++++++++++-- src/components/base/Overlay.js | 4 +- 4 files changed, 124 insertions(+), 25 deletions(-) diff --git a/src/actions/devices.js b/src/actions/devices.js index 3a8f2894..6df2ebd3 100644 --- a/src/actions/devices.js +++ b/src/actions/devices.js @@ -4,11 +4,11 @@ import type { Dispatch } from 'redux' -import { getDevices } from 'reducers/devices' +import { getDevices, getCurrentDevice } from 'reducers/devices' import type { Device, Devices } from 'types/common' -type deviceChooseType = (?Device) => { type: string, payload: ?Device } +export type deviceChooseType = (?Device) => { type: string, payload: ?Device } export const deviceChoose: deviceChooseType = payload => ({ type: 'DEVICE_CHOOSE', payload, @@ -41,10 +41,10 @@ export const deviceRemove: devicesRemoveType = payload => (dispatch, getState) = payload, }) - const devices = getDevices(getState()) + const currentDevice = getCurrentDevice(getState()) - // If we don't detect any device we reset currentDevice - if (devices.length === 0) { + // If we disconnect the currentDevice we reset it + if (currentDevice.path === payload.path) { dispatch(deviceChoose(null)) } } diff --git a/src/components/Home.js b/src/components/Home.js index e7494a57..0d37bf87 100644 --- a/src/components/Home.js +++ b/src/components/Home.js @@ -1,30 +1,32 @@ // @flow import React, { PureComponent } from 'react' -import { compose } from 'redux' import { connect } from 'react-redux' -import { translate } from 'react-i18next' import type { MapStateToProps } from 'react-redux' -import type { Devices, T } from 'types/common' +import type { Device } from 'types/common' -import { getDevices } from 'reducers/devices' +import { getCurrentDevice } from 'reducers/devices' -type Props = { - devices: Devices, - t: T, -} +import Box from 'components/base/Box' const mapStateToProps: MapStateToProps<*, *, *> = state => ({ - devices: getDevices(state), + currentDevice: getCurrentDevice(state), }) +type Props = { + currentDevice: Device | null, +} + class Home extends PureComponent { render() { - const { devices, t } = this.props - - return
{t('common.connectedDevices', { count: devices.length })}
+ const { currentDevice } = this.props + return currentDevice !== null ? ( + + Your current device: {currentDevice.path} + + ) : null } } -export default compose(connect(mapStateToProps), translate())(Home) +export default connect(mapStateToProps)(Home) diff --git a/src/components/TopBar.js b/src/components/TopBar.js index 06ec7f34..dc2f8aa6 100644 --- a/src/components/TopBar.js +++ b/src/components/TopBar.js @@ -1,17 +1,112 @@ // @flow -import React, { PureComponent } from 'react' +import React, { PureComponent, Fragment } from 'react' +import { connect } from 'react-redux' + +import type { MapStateToProps, MapDispatchToProps } from 'react-redux' +import type { Device, Devices } from 'types/common' +import type { deviceChooseType } from 'actions/devices' + +import { getDevices, getCurrentDevice } from 'reducers/devices' + +import { deviceChoose } from 'actions/devices' import Box from 'components/base/Box' +import Overlay from 'components/base/Overlay' + +const mapStateToProps: MapStateToProps<*, *, *> = state => ({ + devices: getDevices(state), + currentDevice: getCurrentDevice(state), +}) + +const mapDispatchToProps: MapDispatchToProps<*, *, *> = { + deviceChoose, +} + +type Props = { + devices: Devices, + currentDevice: Device | null, + deviceChoose: deviceChooseType, +} + +type State = { + changeDevice: boolean, +} + +const hasDevices = props => props.currentDevice === null && props.devices.length > 0 + +class TopBar extends PureComponent { + state = { + changeDevice: hasDevices(this.props), + } + + componentWillReceiveProps(nextProps) { + if (hasDevices(nextProps) && this.props.currentDevice !== null) { + this.setState({ + changeDevice: true, + }) + } + } + + handleChangeDevice = () => + this.setState({ + changeDevice: true, + }) + + handleSelectDevice = device => () => { + const { deviceChoose } = this.props + + deviceChoose(device) + + this.setState({ + changeDevice: false, + }) + } -class TopBar extends PureComponent<{}> { render() { + const { devices } = this.props + const { changeDevice } = this.state + return ( - - {''} - + + {changeDevice && ( + + {devices.map(device => ( + + {device.path} + + ))} + + )} + + + + ) } } -export default TopBar +const CountDevices = ({ count, onChangeDevice } = { count: Number, onChangeDevice: Function }) => ( + + + + + {count} + +) + +const DeviceIcon = props => ( + + + +) + +export default connect(mapStateToProps, mapDispatchToProps)(TopBar) diff --git a/src/components/base/Overlay.js b/src/components/base/Overlay.js index d0e01fed..93be10ad 100644 --- a/src/components/base/Overlay.js +++ b/src/components/base/Overlay.js @@ -3,10 +3,12 @@ import React from 'react' import styled from 'styled-components' +import { rgba } from 'styles/helpers' + import Box from 'components/base/Box' const Overlay = styled(({ sticky, ...props }) => )` - background-color: ${p => p.theme.colors.night}; + background-color: ${p => rgba(p.theme.colors.night, 0.4)}; position: fixed; `