import React from 'react'; import superagent from 'superagent'; import EventsWrapper from './EventsWrapper.jsx'; import Messages from '../Util/Messages.jsx'; import { Accordion, Segment, Dropdown, Grid, Header, Form, Image, Message, Icon, Button, Table, Divider} from 'semantic-ui-react'; export default class TopicEditor extends React.Component { constructor(props) { super(props); this.state = { loading: false, topic: { id: null, name: "", title: "", description: "" } }; } async componentDidMount() { if (this.props.match.params.topicId) { this.setState({ loading: true }); const res = await superagent.get(`/api/admin/topic/${this.props.match.params.topicId}`).accept('json'); this.setState({ topic: res.body, loading: false }); } } async onSaveTopic() { try { this.setState({ loading: true }); if (this.state.topic.id) { const res = await superagent.put(`/api/admin/topic`).send(this.state.topic); this.setState({ loading: false }); } else { const res = await superagent.post(`/api/admin/topic`).send(this.state.topic); this.props.history.push({pathname: '/events/topics'}); // HACK HACK HACK - reload the window to force express to refresh cookies. window.location.reload(); } } catch (e) { if (e.response.status === 401) { this.setState({ loading: false, messages: [{ msg: e.response.body.message }] }); } else if (e.response.status === 422) { this.setState({ loading: false, messages: e.response.body }); } else { this.setState({ loading: false, messages: [{msg: "Login failed!"}] }) } } } async onChange(e, { name, value }) { const topic = this.state.topic; topic[name] = value; this.setState({ topic }); } render() { if (!this.state.topic) { return
; } if (this.state.topic.id) { return ( {(this.state.topic.id) &&
Edit Topic
}
{(this.state.topic.id) && } {(this.state.topic.id) &&