cabana/src/components/LoadDbcModal.js

130 lines
3.3 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
2017-06-13 18:40:05 -06:00
import DBC from '../models/can/dbc';
import OpenDbc from '../api/OpenDbc';
import Modal from './Modals/baseModal';
import GithubDbcList from './GithubDbcList';
import DbcUpload from './DbcUpload';
2017-06-13 18:40:05 -06:00
export default class LoadDbcModal extends Component {
2017-12-12 19:27:20 -07:00
static propTypes = {
handleClose: PropTypes.func.isRequired,
onDbcSelected: PropTypes.func.isRequired,
openDbcClient: PropTypes.instanceOf(OpenDbc).isRequired,
loginWithGithub: PropTypes.element.isRequired
};
constructor(props) {
super(props);
this.state = {
tab: 'OpenDBC',
tabs: ['OpenDBC', 'GitHub', 'Upload'],
2017-12-12 19:27:20 -07:00
dbc: null,
dbcSource: null,
userOpenDbcRepo: null
};
2017-12-12 19:27:20 -07:00
this.onDbcLoaded = this.onDbcLoaded.bind(this);
this.handleSave = this.handleSave.bind(this);
this.renderTabNavigation = this.renderTabNavigation.bind(this);
this.renderTabContent = this.renderTabContent.bind(this);
this.renderActions = this.renderActions.bind(this);
}
2017-06-13 18:40:05 -06:00
2017-12-12 19:27:20 -07:00
componentWillMount() {
this.props.openDbcClient.getUserOpenDbcFork().then((userOpenDbcRepo) => {
2017-12-12 19:27:20 -07:00
this.setState({ userOpenDbcRepo });
});
}
2017-06-13 18:40:05 -06:00
2017-12-12 19:27:20 -07:00
onDbcLoaded(dbcSource, dbcText) {
const dbc = new DBC(dbcText);
this.setState({ dbcSource, dbc });
}
2017-06-13 18:40:05 -06:00
2017-12-12 19:27:20 -07:00
handleSave() {
const { dbc, dbcSource } = this.state;
if (dbc !== null) {
this.props.onDbcSelected(dbcSource, dbc);
}
2017-12-12 19:27:20 -07:00
}
2017-06-16 23:28:51 -06:00
2017-12-12 19:27:20 -07:00
renderTabNavigation() {
return (
<div className="cabana-tabs-navigation">
{this.state.tabs.map((tab) => (
<button
className={cx({ 'is-active': this.state.tab === tab })}
onClick={() => {
this.setState({ tab });
}}
key={tab}
>
<span>{tab}</span>
</button>
))}
2017-12-12 19:27:20 -07:00
</div>
);
}
2017-06-13 18:40:05 -06:00
2017-12-12 19:27:20 -07:00
renderTabContent() {
const { tab } = this.state;
if (tab === 'OpenDBC') {
2017-12-12 19:27:20 -07:00
return (
<GithubDbcList
onDbcLoaded={this.onDbcLoaded}
repo="commaai/opendbc"
openDbcClient={this.props.openDbcClient}
/>
);
}
if (tab === 'GitHub') {
2017-12-12 19:27:20 -07:00
if (!this.props.openDbcClient.hasAuth()) {
return this.props.loginWithGithub;
}
if (this.state.userOpenDbcRepo === null) {
2017-12-12 19:27:20 -07:00
return <div>Fork it</div>;
}
return (
<GithubDbcList
onDbcLoaded={this.onDbcLoaded}
repo={this.state.userOpenDbcRepo}
openDbcClient={this.props.openDbcClient}
/>
);
}
if (tab === 'Upload') {
2017-12-12 19:27:20 -07:00
return <DbcUpload onDbcLoaded={this.onDbcLoaded} />;
2017-06-13 18:40:05 -06:00
}
2017-12-12 19:27:20 -07:00
}
2017-06-13 18:40:05 -06:00
2017-12-12 19:27:20 -07:00
renderActions() {
return (
<div>
<button className="button--inverted" onClick={this.props.handleClose}>
<span>Cancel</span>
</button>
<button className="button--primary" onClick={this.handleSave}>
<span>Load DBC</span>
</button>
</div>
);
}
render() {
return (
<Modal
title="Load DBC File"
subtitle="Modify an existing DBC file with Cabana"
handleClose={this.props.handleClose}
navigation={this.renderTabNavigation()}
actions={this.renderActions()}
>
{this.renderTabContent()}
</Modal>
);
}
2017-06-13 18:40:05 -06:00
}