//
// SinkEdit.js
//
// Copyright (c) 2019 2020 Andrea Bondavalli. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
//
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {toast} from 'react-toastify';
import Modal from 'react-modal';
import RestAPI from './Services';
require('./styles.css');
const editCustomStyles = {
content : {
top: '50%',
left: '50%',
right: 'auto',
bottom: 'auto',
marginRight: '-50%',
transform: 'translate(-50%, -50%)'
}
};
class SinkEdit extends Component {
static propTypes = {
sink: PropTypes.object.isRequired,
applyEdit: PropTypes.func.isRequired,
closeEdit: PropTypes.func.isRequired,
editIsOpen: PropTypes.bool.isRequired,
isEdit: PropTypes.bool.isRequired,
editTitle: PropTypes.string.isRequired
};
constructor(props) {
super(props);
this.state = {
id: this.props.sink.id,
name: this.props.sink.name,
nameErr: false,
io: this.props.sink.io,
delay: this.props.sink.delay,
ignoreRefclkGmid: this.props.sink.ignore_refclk_gmid,
useSdp: this.props.sink.use_sdp,
source: this.props.sink.source,
sourceErr: false,
sdp: this.props.sink.sdp,
channels: this.props.sink.map.length,
map: this.props.sink.map,
audioMap: []
}
let v;
for (v = 0; v <= (64 - this.state.channels); v += 1) {
this.state.audioMap.push(v);
}
this.onSubmit = this.onSubmit.bind(this);
this.onCancel = this.onCancel.bind(this);
this.addSink = this.addSink.bind(this);
this.onChangeChannels = this.onChangeChannels.bind(this);
this.onChangeChannelsMap = this.onChangeChannelsMap.bind(this);
this.inputIsValid = this.inputIsValid.bind(this);
}
componentDidMount() {
Modal.setAppElement('body');
}
addSink(message) {
RestAPI.addSink(
this.state.id,
this.state.name,
this.state.io,
this.state.delay,
this.state.useSdp,
this.state.source ? this.state.source : '',
this.state.sdp ? this.state.sdp : '',
this.state.ignoreRefclkGmid,
this.state.map,
this.props.isEdit)
.then(function(response) {
this.props.applyEdit();
toast.success(message);
}.bind(this));
}
onSubmit() {
this.addSink('Sink ' + this.state.id + (this.props.isEdit ? ' updated ' : ' added'));
}
onCancel() {
this.props.closeEdit();
}
onChangeChannels(e) {
if (e.currentTarget.checkValidity()) {
let channels = parseInt(e.target.value, 10);
let audioMap = [], map = [], v;
for (v = 0; v <= (64 - channels); v += 1) {
audioMap.push(v);
}
for (v = 0; v < channels; v++) {
map.push(v + this.state.map[0]);
}
this.setState({ map: map, channels: channels, audioMap: audioMap });
}
}
onChangeChannelsMap(e) {
let startChannel = parseInt(e.target.value, 10);
let map = [], v;
for (v = 0; v < this.state.channels; v++) {
map.push(v + startChannel);
}
this.setState({ map: map });
}
inputIsValid() {
return !this.state.nameErr &&
!this.state.sourceErr &&
(this.state.useSdp || this.state.source) &&
(!this.state.useSdp || this.state.sdp);
}
render() {
return (