From 7593b34b281a4b7630aa5332e8218044c61c248e Mon Sep 17 00:00:00 2001 From: FadhilP <fadhilpradipta0@gmail.com> Date: Sat, 9 Jan 2021 14:08:42 +0700 Subject: [PATCH 1/7] add CreateTopicForm component for testing purposes --- diskuy/src/CreateTopicForm.js | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 diskuy/src/CreateTopicForm.js diff --git a/diskuy/src/CreateTopicForm.js b/diskuy/src/CreateTopicForm.js new file mode 100644 index 0000000..027b318 --- /dev/null +++ b/diskuy/src/CreateTopicForm.js @@ -0,0 +1,37 @@ +import axios from "axios"; +import React from "react"; +import { useInput } from './hooks/input-hook'; +import './Form.css'; +import authHeader from './services/auth-header' + +const LINK = 'http://localhost:4000'; + +export default function CreateTopicForm(props){ + const { value: name, bind: bindName, reset: resetName } = useInput('') + + const handleSubmit = async (event) => { + event.preventDefault(); + + try { + await axios.post(`${LINK}/api/topics`, { + topic : { + name: name + } + }, + {headers: authHeader()}) + props.history.push(`/`) + } + catch(error){} + resetName(); + } + + return ( + <form onSubmit={handleSubmit}> + <div className="form_container"> + <label for="title">Name</label> + <input type='text' name="title" placeholder="Your topic name" required="true" {...bindName}/> + <input type="submit" value="Submit"/> + </div> + </form> + ) +} -- GitLab From 705b8e8522b541d59c043cee022ffab18a7edd98 Mon Sep 17 00:00:00 2001 From: FadhilP <fadhilpradipta0@gmail.com> Date: Sat, 9 Jan 2021 14:09:10 +0700 Subject: [PATCH 2/7] fix login argument after register --- diskuy/src/services/auth.service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diskuy/src/services/auth.service.js b/diskuy/src/services/auth.service.js index 05eb403..7077c0f 100644 --- a/diskuy/src/services/auth.service.js +++ b/diskuy/src/services/auth.service.js @@ -20,7 +20,7 @@ class AuthService { password } }) - this.login(username, password) + this.login(email, password) return response } -- GitLab From cb977b0a8d62f07a40389ab06b4722d8e62492d4 Mon Sep 17 00:00:00 2001 From: FadhilP <fadhilpradipta0@gmail.com> Date: Sat, 9 Jan 2021 14:27:39 +0700 Subject: [PATCH 3/7] add route for CreateTopicForm --- diskuy/src/App.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/diskuy/src/App.js b/diskuy/src/App.js index 0922e08..12227e3 100644 --- a/diskuy/src/App.js +++ b/diskuy/src/App.js @@ -8,6 +8,7 @@ import Form from './Form'; import LoginForm from './LoginForm' import TopicList from './TopicList'; import RegisterForm from './RegisterForm' +import CreateTopicForm from './CreateTopicForm' import { BrowserRouter as Router, Route, @@ -23,6 +24,7 @@ function App() { <Route exact path='/profile' component={Profile} /> <Route exact path='/login' component={LoginForm} /> <Route exact path='/register' component={RegisterForm} /> + <Route exact path='/create/topic' component={CreateTopicForm} /> <Route exact path='/topic/:topic/thread/create' component={Form} /> <Route exact path='/topic/:topic/:thread' component={Thread} /> <Route exact path='/topic/:topic' component={Topic} /> -- GitLab From 3824f92affcd8b17a670a402f93167fb4138b5fd Mon Sep 17 00:00:00 2001 From: FadhilP <fadhilpradipta0@gmail.com> Date: Sat, 9 Jan 2021 15:57:41 +0700 Subject: [PATCH 4/7] change route to create thread --- diskuy/src/App.js | 2 +- diskuy/src/Topic.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/diskuy/src/App.js b/diskuy/src/App.js index 12227e3..bea072e 100644 --- a/diskuy/src/App.js +++ b/diskuy/src/App.js @@ -25,7 +25,7 @@ function App() { <Route exact path='/login' component={LoginForm} /> <Route exact path='/register' component={RegisterForm} /> <Route exact path='/create/topic' component={CreateTopicForm} /> - <Route exact path='/topic/:topic/thread/create' component={Form} /> + <Route exact path='/create/:topic/thread' component={Form} /> <Route exact path='/topic/:topic/:thread' component={Thread} /> <Route exact path='/topic/:topic' component={Topic} /> <Route exact path='/threads' component={ListThreads} /> diff --git a/diskuy/src/Topic.js b/diskuy/src/Topic.js index 5728ae9..2f61644 100644 --- a/diskuy/src/Topic.js +++ b/diskuy/src/Topic.js @@ -29,7 +29,7 @@ export default function Topic(props){ <div> {loggedIn && ( - <Link to={`/topic/${topicParam}/thread/create`}> + <Link to={`/create/${topicParam}/thread`}> <button type="button" className="btn btn-primary btn_cancel newThreadButton">Create New Thread</button> </Link> )} -- GitLab From 01edf2226dfe7180b346d03860a65da467924f88 Mon Sep 17 00:00:00 2001 From: FadhilP <fadhilpradipta0@gmail.com> Date: Sat, 9 Jan 2021 15:58:19 +0700 Subject: [PATCH 5/7] add point system for threads --- diskuy/src/Threads/Post.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/diskuy/src/Threads/Post.js b/diskuy/src/Threads/Post.js index 38732d0..b023687 100644 --- a/diskuy/src/Threads/Post.js +++ b/diskuy/src/Threads/Post.js @@ -3,24 +3,39 @@ import './Comment.css'; import axios from 'axios'; import AuthService from '../services/auth.service' import authHeader from '../services/auth-header' +import React, { useState } from 'react' +import { loggedIn } from '../services/loggedInService' -const LINK = 'http://localhost:4000/'; +const LINK = 'http://localhost:4000/api'; export default function Post(props){ + const [points, setPoints] = useState(props.points) const deletePost = async () => { try { if(props.type === "thread") { - await axios.delete(`${LINK}api/threads/${props.id}` + await axios.delete(`${LINK}/threads/${props.id}` ,{headers: authHeader()}); props.redirect() } else { - await axios.delete(`${LINK}api/post/${props.id}` + await axios.delete(`${LINK}/post/${props.id}` ,{headers: authHeader()}); window.location.reload(); } } catch (error) {} } + const handleLike = async () => { + try { + await axios.get(`${LINK}/threads/checklike/${props.id}`, {headers: authHeader()}) + await axios.post(`${LINK}/threads/dislike/${props.id}`, {}, {headers: authHeader()}) + setPoints(points - 1) + } + catch(error) { + await axios.post(`${LINK}/threads/like/${props.id}`, {}, {headers: authHeader()}) + setPoints(points + 1) + } + } + return ( <div id="post"> <div id="postHeader"> @@ -38,7 +53,10 @@ export default function Post(props){ <div id="postContent"> <h1 id="judul">{props.header}</h1> <p id="isi">{props.text}</p> - <p className='inline'>{props.points}</p> + <p className='inline'>{points}</p> + {loggedIn && ( + <button onClick={handleLike}>like</button> + )} </div> </div> ) -- GitLab From e2248ba487277ad28f066bd1b16aa29a9d78436a Mon Sep 17 00:00:00 2001 From: FadhilP <fadhilpradipta0@gmail.com> Date: Sat, 9 Jan 2021 15:59:14 +0700 Subject: [PATCH 6/7] minor fix --- diskuy/src/RegisterForm.js | 1 - diskuy/src/Threads/Thread.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/diskuy/src/RegisterForm.js b/diskuy/src/RegisterForm.js index 6a12831..04c0dcc 100644 --- a/diskuy/src/RegisterForm.js +++ b/diskuy/src/RegisterForm.js @@ -28,7 +28,6 @@ export default function RegisterForm(props){ try { const response = AuthService.register(username, email, password) - setMessage(response.data.message) setSuccesful(true) props.history.push('/') window.location.reload() diff --git a/diskuy/src/Threads/Thread.js b/diskuy/src/Threads/Thread.js index 26b7d8a..838b801 100644 --- a/diskuy/src/Threads/Thread.js +++ b/diskuy/src/Threads/Thread.js @@ -95,7 +95,7 @@ export default function Thread(props){ text={thread.content} header={thread.title} user={thread.username} - points={thread.point} + points={thread.points} id={thread.id} user_id={thread.user_id} thread_id={thread.thread_id} -- GitLab From 3b3a286680fe763feb9a911a6d921c2c2b12483a Mon Sep 17 00:00:00 2001 From: FadhilP <fadhilpradipta0@gmail.com> Date: Sat, 9 Jan 2021 16:02:43 +0700 Subject: [PATCH 7/7] add point system for post --- diskuy/src/Threads/Post.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/diskuy/src/Threads/Post.js b/diskuy/src/Threads/Post.js index b023687..8169ebb 100644 --- a/diskuy/src/Threads/Post.js +++ b/diskuy/src/Threads/Post.js @@ -25,13 +25,14 @@ export default function Post(props){ } const handleLike = async () => { + const checkType = props.type === 'thread' ? 'threads' : 'post' try { - await axios.get(`${LINK}/threads/checklike/${props.id}`, {headers: authHeader()}) - await axios.post(`${LINK}/threads/dislike/${props.id}`, {}, {headers: authHeader()}) + await axios.get(`${LINK}/${checkType}/checklike/${props.id}`, {headers: authHeader()}) + await axios.post(`${LINK}/${checkType}/dislike/${props.id}`, {}, {headers: authHeader()}) setPoints(points - 1) } catch(error) { - await axios.post(`${LINK}/threads/like/${props.id}`, {}, {headers: authHeader()}) + await axios.post(`${LINK}/${checkType}/like/${props.id}`, {}, {headers: authHeader()}) setPoints(points + 1) } } -- GitLab