forked from BenC14/Dumbledore
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathLogModal.js
More file actions
99 lines (84 loc) · 2.51 KB
/
LogModal.js
File metadata and controls
99 lines (84 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Button, Header, Icon, Modal, Form } from 'semantic-ui-react';
import LogList from '../components/LogList';
import { getLiveQuery, getUserName } from '../helper/fetch';
class LogModal extends Component {
constructor(props) {
super(props);
this.state = {
token: '',
logs: []
};
this.onLive = this.onLive.bind(this);
this.onChange = this.onChange.bind(this);
this.closeModal = this.closeModal.bind(this);
}
onLive() {
const { token } = this.state;
if (!token) return;
getLiveQuery(token).then(subscription => {
subscription.on('open', () => {
this.setState({
logs: [this.logParser('open'), ...this.state.logs]
});
});
subscription.on('create', async object => {
const user = await getUserName(object.attributes.userId);
this.setState({
logs: [this.logParser('point', user, object), ...this.state.logs]
});
});
});
}
onChange(e) {
this.setState({
[e.target.name]: e.target.value
});
}
onKeyDown() {
}
logParser(type, user, object) {
if (type === 'open') return { type: 'open' };
const { createdAt, amount, op } = object.attributes;
return {
userName: user, type, createdAt, amount, op
};
}
closeModal() {
this.props.closeHandler('logModal');
this.setState({
logs: []
});
}
render() {
const { open } = this.props;
const { logs, token } = this.state;
return (
<Modal id="log__modal" size="tiny" open={open} onClose={this.closeModal}>
<Header icon="fort awesome" content="live log" />
<Modal.Content>
<Form>
<Form.Field>
<div className="ui action input">
<input type="text" id="token" name="token" placeholder="api token..." value={token} onChange={this.onChange} />
<div className="ui button" onClick={this.onLive} onKeyDown={this.onKeyDown} role="button" tabIndex="0"><Icon loading name="lightning" /></div>
</div>
</Form.Field>
</Form>
<LogList logs={logs} />
</Modal.Content>
<Modal.Actions>
<Button color="grey" onClick={this.closeModal}>
<Icon name="close" /> close
</Button>
</Modal.Actions>
</Modal>
);
}
}
LogModal.propTypes = {
closeHandler: PropTypes.func.isRequired,
open: PropTypes.bool.isRequired
};
export default LogModal;