pslockout/webclient/src/Tool.js

88 lines
2.9 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 06:37:44 +00:00
} else if (status.action == 'arm') {
2018-02-06 02:31:54 +00:00
return { msg: 'Arming...', canArm: false, canDisarm: true, };
2018-02-06 06:37:44 +00:00
} else if (status.action == 'disarm') {
2018-11-13 09:45:16 +00:00
return { msg: 'Disarming...', canArm: false, canDisarm: false, };
2018-02-06 06:37:44 +00:00
} else if (status.state == 'off') {
return { msg: 'Off.', canArm: true, canDisarm: false, };
} else if (status.state == 'armed') {
return { msg: 'Armed.', canArm: false, canDisarm: true, };
} else if (status.state == 'on') {
2019-01-27 07:35:28 +00:00
return { msg: 'On.', canArm: false, canDisarm: false, };
2018-02-06 06:37:44 +00:00
} else {
2018-02-04 02:59:39 +00:00
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-11-13 09:45:16 +00:00
x.slug === match.params.slug
2018-02-03 06:41:30 +00:00
);
2018-11-13 09:45:16 +00:00
const status = toolStatus[match.params.slug] || null;
2018-02-04 02:59:39 +00:00
const decodedStatus = this.decodeStatus(status);
console.log(decodedStatus);
2018-11-13 09:45:16 +00:00
const approved = user.profile.authorized_tools.includes(tool.slug);
2018-02-03 08:48:49 +00:00
2018-02-03 06:41:30 +00:00
return (
2019-01-28 23:42:33 +00:00
<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>
2018-02-03 06:41:30 +00:00
2019-01-28 23:42:33 +00:00
<Segment>
<Image src={tool.photo} size='medium' centered rounded />
<Segment textAlign='center' basic>
<p>Status: {decodedStatus.msg}</p>
<div>
<Button color='green'
disabled={!approved || !decodedStatus.canArm}
onClick={() => requestInterlock({toolSlug: tool.slug, action: 'arm',})}
>
<Icon name='lightning' /> Arm
</Button>
<Button color='red'
disabled={!approved || !decodedStatus.canDisarm}
onClick={() => requestInterlock({toolSlug: tool.slug, action: 'disarm',})}
>
<Icon name='stop' /> Disarm
</Button>
<br />
{approved || <Label basic color='red' pointing>
Not authorized! Please take the xyz course.
</Label>}
</div>
</Segment>
2018-02-03 06:41:30 +00:00
2019-03-22 03:55:33 +00:00
<Header as='h4'>Information</Header>
<p>{tool.info}</p>
2019-01-28 23:42:33 +00:00
</Segment>
</Container>
2018-02-03 06:41:30 +00:00
);
}
}
export default Tool;