diff --git a/diskuy/src/App.js b/diskuy/src/App.js index 82e2ffeff1206ca6792b07f06d44ba6c3264bad8..330dd412e2fc61516ed3d184c022326da1a222fb 100644 --- a/diskuy/src/App.js +++ b/diskuy/src/App.js @@ -3,11 +3,13 @@ import Thread from './Thread'; import Topic from './Topic'; import Navbar from './Navbar'; import Profile from './Profile'; +import Form from './Form'; +import TopicList from './TopicList'; import { BrowserRouter as Router, Route, } from "react-router-dom" -import TopicList from './TopicList'; + function App() { @@ -16,6 +18,7 @@ function App() { <Navbar /> <Route exact path='/' component={TopicList} /> <Route exact path='/profile' component={Profile} /> + <Route exact path='/:topic/thread/create' component={Form} /> <Route exact path='/:topic/:thread' component={Thread} /> <Route exact path='/:topic' component={Topic} /> </Router> diff --git a/diskuy/src/Form.js b/diskuy/src/Form.js index d9bec8d7dc21f36d01c2edfeda25ee22465893a7..a569fc90a36b7573bfed0e20776245c28335bbbd 100644 --- a/diskuy/src/Form.js +++ b/diskuy/src/Form.js @@ -1,13 +1,35 @@ +import axios from "axios"; import React from "react"; import { useInput } from './hooks/input-hook'; +const LINK = 'http://localhost:4000'; + export default function Form(props){ const { value: title, bind: bindTitle, reset: resetTitle } = useInput('') const { value: body, bind: bindBody, reset: resetBody } = useInput('') - const handleSubmit = (event) => { + const topicParam = props.match.params.topic + + const handleSubmit = async (event) => { event.preventDefault(); - alert(`${title}${body}`); + + try { + const responseTopics = await axios.get(`${LINK}/api/topics`) + const topic = responseTopics.data.data.find(topic => topic.name === topicParam).id + const response = await axios.post(`${LINK}/api/threads`, { + thread: { + content : body, + points : 0, + title : title, + topic_id : topic, + user_id : 1 + } + }) + props.history.push(`/${topicParam}`) + } + catch(error){ + + } resetBody(); resetTitle(); }