import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { Card, Form, Button, Modal, Row, Col } from 'react-bootstrap';
import Axios from 'axios';
import { toast } from 'react-toastify';
import SettingsModalCard from '../Settings/SettingsModalCard';
export default class SettingWithModal extends Component {
constructor(props) {
super(props)
this.state = {
title: this.props.title,
description: this.props.description,
settings: this.props.settings,
show: false,
autoClose: this.props.autoClose
}
}
ucfirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
update = () => {
var url = 'api/settings/bulk?token=' + window.token;
var data = [];
var settings = this.state.settings;
settings.forEach(e => {
if(e.type !== 'button-get') {
var res = {
name: e.obj.name,
value: e.obj.value
};
data.push(res);
}
});
data = {
data: data
};
Axios.post(url, data)
.then((resp) => {
toast.success(this.state.title + ' updated');
if(this.state.autoClose) {
this.toggleShow();
}
})
.catch((err) => {
if(err.response.status == 422) {
toast.error('Your input was invalid');
} else {
toast.error('Something went wrong')
}
})
}
updateValue = (e) => {
var name = e.target.id;
if(e.target.type == 'checkbox') {
var val = e.target.checked;
} else {
var val = e.target.value;
}
var settings = this.state.settings;
var i = 0;
settings.forEach(ele => {
if(ele.obj.name == name) {
ele.obj.value = val;
}
settings[i] = ele;
i++;
});
this.setState({
settings: settings
});
}
toggleShow = () => {
var show = this.state.show;
if(show) {
this.setState({
show: false
});
} else {
this.setState({
show: true
});
}
}
render() {
var title = this.state.title;
var description = this.state.description;
var show = this.state.show;
var settings = this.state.settings;
return (
<>
{title}
{settings.map((e,i) => {
var name = e.obj.name.split('_');
name[0] = this.ucfirst(name[0]);
name = name.join(' ');
if(e.obj.description == null || e.obj.description == '') {
var sm = { span: 12 };
var md = { span: 12 };
} else {
var sm = { span: 12 };
var md = { span: 6 };
}
var readonly = false;
if(window.config.editable[e.obj.name] == false) {
readonly = true;
}
if(e.type == 'info') {
return (
{e.obj.content}
)
} else if(e.type == 'checkbox') {
return (
{readonly ?
<>
This setting is defined as an env variable and is not editable.
>
:
}
{e.description == null &&
{e.obj.description}
}
);
} else if(e.type == 'number') {
return (
{name}
{readonly ?
<>
This setting is defined as an env variable and is not editable.
>
:
}
{e.description == null &&
{e.obj.description}
}
);
} else if(e.type == 'text') {
return (
{name}
{readonly ?
<>
This setting is defined as an env variable and is not editable.
>
:
}
{e.description == null &&
{e.obj.description}
}
);
} else if(e.type == 'select') {
return (
{name}
{readonly ?
<>
{e.options.map((e,i) => {
return (
)
})}
This setting is defined as an env variable and is not editable.
>
:
{e.options.map((e,i) => {
return (
)
})}
}
{e.description == null &&