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'; 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 } } ucfirst(string) { return string.charAt(0).toUpperCase() + string.slice(1); } update = () => { var url = 'api/settings/bulk'; var data = []; var settings = this.state.settings; settings.forEach(e => { 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'); 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}

{description}

{title} {settings.map((e,i) => { var name = e.obj.name.split('_'); name[0] = this.ucfirst(name[0]); name = name.join(' '); if(e.type == 'checkbox') { return (

{e.obj.description}

); } else if(e.type == 'number') { return ( {name}

{e.obj.description}

); } else if(e.type == 'select') { return ( {name} {e.options.map((e,i) => { return ( ) })}

{e.obj.description}

) } })}
); } } if (document.getElementById('Setting')) { ReactDOM.render(, document.getElementById('Setting')); }