pslockout/webclient/src/Tool.js

111 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-02-03 06:41:30 +00:00
import React, { Component } from 'react';
2018-02-03 08:48:49 +00:00
import { Breadcrumb, Button, Container, Dropdown, Header, Icon, Image, Item, Label, Menu, Segment, Table, Input } from 'semantic-ui-react'
2018-02-03 06:41:30 +00:00
import { Link } from 'react-router-dom';
class Tool extends Component {
2018-02-04 02:59:39 +00:00
decodeStatus = status => {
if (status === null) {
return { msg: 'Unknown! Connection error?', canArm: false, canDisarm: false, };
2018-02-06 02:31:54 +00:00
} else if (status.armable && !status.armed) {
return { msg: 'Arming...', canArm: false, canDisarm: true, };
} else if (!status.armable && status.armed) {
return { msg: 'Disarming...', canArm: true, canDisarm: false, };
2018-02-04 02:59:39 +00:00
} else if (!status.armed && !status.on) {
return { msg: 'Off', canArm: true, canDisarm: false, };
} else if (status.armed && !status.on) {
return { msg: 'Armed', canArm: false, canDisarm: true, };
} else if (status.armed && status.on) {
return { msg: 'On', canArm: false, canDisarm: true, };
} else if (!status.armed && status.on) {
return { msg: 'Error: Impossible state!', canArm: false, canDisarm: false, };
}
}
2018-02-03 06:41:30 +00:00
render() {
const data = this.props.data;
2018-02-04 00:20:58 +00:00
const user = this.props.user;
2018-02-03 06:41:30 +00:00
const match = this.props.match;
2018-02-04 02:59:39 +00:00
const toolStatus = this.props.toolStatus || [];
const requestInterlock = this.props.requestInterlock;
2018-02-03 06:41:30 +00:00
2018-02-04 02:59:39 +00:00
const category = data.categories.find(x =>
2018-02-04 00:20:58 +00:00
x.slug === match.params.category
2018-02-03 06:41:30 +00:00
);
2018-02-04 02:59:39 +00:00
const tool = category.tools.find(x =>
2018-02-04 00:20:58 +00:00
x.id.toString() === match.params.id
2018-02-03 06:41:30 +00:00
);
2018-02-04 02:59:39 +00:00
const status = toolStatus.find(x =>
x.id.toString() === match.params.id
) || null;
const decodedStatus = this.decodeStatus(status);
console.log(decodedStatus);
2018-02-03 08:48:49 +00:00
const approved = user.authorizedTools.includes(tool.id);
2018-02-03 06:41:30 +00:00
return (
<div>
<Container>
<Breadcrumb size='large'>
<Breadcrumb.Section link as={Link} to='/'>Categories</Breadcrumb.Section>
<Breadcrumb.Divider icon='right angle' />
<Breadcrumb.Section link as={Link} to={'/' + category.slug}>{category.name}</Breadcrumb.Section>
<Breadcrumb.Divider icon='right angle' />
<Breadcrumb.Section active>{tool.name}</Breadcrumb.Section>
</Breadcrumb>
<Segment>
2018-02-03 08:48:49 +00:00
<Image src={tool.photo} size='medium' centered rounded />
2018-02-03 06:41:30 +00:00
<Segment textAlign='center' basic>
2018-02-04 02:59:39 +00:00
<p> Status: {decodedStatus.msg}</p>
2018-02-03 08:48:49 +00:00
<div>
2018-02-04 02:59:39 +00:00
<Button color='green'
disabled={!approved || !decodedStatus.canArm}
onClick={() => requestInterlock({toolId: tool.id, action: 'arm',})}
>
<Icon name='lightning' /> Arm
2018-02-03 06:41:30 +00:00
</Button>
2018-02-04 02:59:39 +00:00
<Button color='red'
disabled={!approved || !decodedStatus.canDisarm}
onClick={() => requestInterlock({toolId: tool.id, action: 'disarm',})}
>
2018-02-03 06:41:30 +00:00
<Icon name='stop' /> Disarm
</Button>
2018-02-03 08:48:49 +00:00
</div>
{approved || <Label basic color='red' pointing>
Not authorized! Please take the xyz course.
</Label>}
2018-02-03 06:41:30 +00:00
</Segment>
<Table basic='very' unstackable>
<Table.Body>
<Table.Row>
<Table.Cell>Cat</Table.Cell>
<Table.Cell>Meow</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Dog</Table.Cell>
<Table.Cell warning>
<Icon name='attention' />
Bark
</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Bird</Table.Cell>
<Table.Cell>Chrip</Table.Cell>
</Table.Row>
</Table.Body>
</Table>
<Header as='h4'>Notes</Header>
<p>{tool.notes}</p>
</Segment>
</Container>
</div>
);
}
}
export default Tool;