From a731739178d72ff628813375b836301ce707a265 Mon Sep 17 00:00:00 2001 From: TBS093A Date: Wed, 15 Jan 2020 19:15:07 +0100 Subject: [PATCH] Add all forum functionality --- src/components/forum/forumSubjects.js | 6 +- src/components/forum/indexForum.js | 104 ++++++++++++++++++++--- src/components/forum/indexForumUpdate.js | 95 +++++++++++++++++++++ src/stores/threads/duck/operations.js | 52 ++++++++++++ src/styles/general.scss | 4 + 5 files changed, 246 insertions(+), 15 deletions(-) create mode 100644 src/components/forum/indexForumUpdate.js diff --git a/src/components/forum/forumSubjects.js b/src/components/forum/forumSubjects.js index 36e85ec..c86a138 100644 --- a/src/components/forum/forumSubjects.js +++ b/src/components/forum/forumSubjects.js @@ -128,9 +128,9 @@ const ForumSubjects = ({
- +
) diff --git a/src/components/forum/indexForum.js b/src/components/forum/indexForum.js index a37bf3e..a18bb48 100644 --- a/src/components/forum/indexForum.js +++ b/src/components/forum/indexForum.js @@ -1,16 +1,49 @@ -import React, { useEffect } from 'react' +import React, { useEffect, useState } from 'react' import { connect } from 'react-redux' -import { getAllThreads, getThreadSubjects } from '../../stores/threads/duck/operations' +import { getAllThreads, getThreadSubjects, addThread, deleteThread } from '../../stores/threads/duck/operations' import '../../styles/indexForum.scss' import ForumSubjects from './forumSubjects' +import ForumThreadUpdate from './indexForumUpdate' -const IndexForum = ({ user, threads, getAllThreads, getThreadSubjects }) => { +const IndexForum = ({ + user, + threads, getAllThreads, getThreadSubjects, addThread, deleteThread }) => { useEffect( () => { getAllThreads() }, [] ) + const [formDiv, setFormDiv] = useState(false) + const [updateFormDiv, setUpdateFormDiv] = useState( { isActive: false, thread_id: -1 } ) + + const [titleText, setTitleText] = useState(0) + + const addThreadTitle = React.createRef() + + const addNewThread = (event) => { + event.preventDefault() + if ( addThreadTitle.current.value !== '' ) { + let newThread = { + name: addThreadTitle.current.value, + user_id: user.id, + token: user.token + } + addThread(newThread) + addThreadTitle.current.value = '' + } + } + + const deleteOldThread = (thread) => { + if( user.privilige === 3 ) { + let delSubject = { + token: user.token, + thread_id: thread.id + } + deleteThread(delSubject) + } + } + if (threads.isActive === false) { return (
@@ -24,18 +57,63 @@ const IndexForum = ({ user, threads, getAllThreads, getThreadSubjects }) => {
{ threads.threadsList.map( thread => -
getThreadSubjects(thread) }> -

{thread.name}

-

moderator: {thread.moderator}

+
+
+

getThreadSubjects(thread) }>{thread.name}

+ { (user.id === thread.user_id || + user.privilige === 3) ? ( +
+ + { user.privilige === 3 ? ( + + ) : ( +
+ ) } + +

{ thread.moderator }

+
+ ) : ( +
+ +

{ thread.moderator }

+
+ ) + } +
+
) }
+
+
+ setTitleText(e.target.value.length) }> + +

Title: {titleText}/30

+ +
+
- Foot + { user.privilige === 3 ? ( + + ) : ( +
+ ) }
@@ -64,8 +142,10 @@ const mapStateToProps = state => ({ }) const mapDispatchToProps = dispatch => ({ + addThread: threads => dispatch( addThread(threads) ), + deleteThread: threads => dispatch( deleteThread(threads) ), getAllThreads: () => dispatch( getAllThreads() ), - getThreadSubjects: thread => dispatch( getThreadSubjects(thread) ) + getThreadSubjects: threads => dispatch( getThreadSubjects(threads) ) }) export default connect(mapStateToProps, mapDispatchToProps)(IndexForum) diff --git a/src/components/forum/indexForumUpdate.js b/src/components/forum/indexForumUpdate.js new file mode 100644 index 0000000..b6d3b08 --- /dev/null +++ b/src/components/forum/indexForumUpdate.js @@ -0,0 +1,95 @@ +import React, { useState } from 'react' +import { connect } from 'react-redux' + +import { updateThread } from '../../stores/threads/duck/operations' + +import '../../styles/indexForum.scss' + +const IndexForumUpdate = ({ + user, + threads, getAllThreads, getThreadSubjects, updateThread, + thread, thisThread }) => { + + const updateThreadTitle = React.createRef() + const [threadTitleText, setThreadTitleText] = useState(0) + + const [selectThreadModeratorID, setSelectThreadModeratorID] = useState(-1) + + const updateOldThread = (event) => { + event.preventDefault() + if ( updateThreadTitle.current.value !== '' && selectThreadModeratorID === -1 && user.privilige >= 2) { + let putThread = { + id: thread.id, + name: updateThreadTitle.current.value, + user_id: thread.user_id, + token: user.token + } + updateThread(putThread) + updateThreadTitle.current.value = '' + } else if ( updateThreadTitle.current.value !== '' && user.privilige === 3 ) { + let putThread = { + id: thread.id, + name: updateThreadTitle.current.value, + user_id: selectThreadModeratorID === -1 ? thread.user_id : selectThreadModeratorID, + token: user.token + } + updateThread(putThread) + updateThreadTitle.current.value = '' + } + } + + if ( thisThread.isActive === true && thisThread.thread_id === thread.id ) { + return ( +
+
+ setThreadTitleText( e.target.value.length ) }> + + { (user.privilige === 3) ? ( +
+ +
+ ) : ( +
+ ) + } +

{threadTitleText}/30

+ +
+
+ ) + } else { + return ( +
+
+ ) + } +} + +const mapStateToProps = state => ({ + user: state.user, + threads: state.threads +}) + +const mapDispatchToProps = dispatch => ({ + updateThread: threads => dispatch( updateThread(threads) ) +}) + +export default connect(mapStateToProps, mapDispatchToProps)(IndexForumUpdate) diff --git a/src/stores/threads/duck/operations.js b/src/stores/threads/duck/operations.js index cd882a5..4138c0c 100644 --- a/src/stores/threads/duck/operations.js +++ b/src/stores/threads/duck/operations.js @@ -22,6 +22,43 @@ const fetchGetSubjects = async (threadID) => { return response.json() } +const fetchAddThread = async (data) => { + const response = await + fetch( + 'http://localhost:8001/index/thread', { + method: 'POST', + credential: 'same-origin', + body: JSON.stringify(data) + } + ) + return response.json() +} + +const fetchUpdateThread = async (data) => { + const response = await + fetch( + 'http://localhost:8001/index/thread/' + data.id, { + method: 'PUT', + credential: 'same-origin', + body: JSON.stringify(data) + } + ) + return response.json() +} + +const fetchDeleteThread = async (data) => { + const response = await + fetch( + 'http://localhost:8001/index/thread/' + data.thread_id, { + method: 'DELETE', + credential: 'same-origin', + body: JSON.stringify(data) + } + ) + return response.json() +} + + export const getAllThreads = () => async (dispatch) => { const allThreads = await fetchGetAll() @@ -41,3 +78,18 @@ export const getThreadSubjects = (data) => dispatch( actions.activate(data) ) dispatch( actions.getThreadSubjects(subjects) ) } + +export const addThread = (data) => + async (dispatch) => { + const subjects = await fetchAddThread(data) + } + +export const updateThread = (data) => + async (dispatch) => { + const subjects = await fetchUpdateThread(data) + } + +export const deleteThread = (data) => + async (dispatch) => { + const subjects = await fetchDeleteThread(data) + } diff --git a/src/styles/general.scss b/src/styles/general.scss index b5fe1a1..5c13fce 100644 --- a/src/styles/general.scss +++ b/src/styles/general.scss @@ -2,7 +2,11 @@ body { margin: 0 auto; + overflow-y: hidden; + ::-webkit-scrollbar { + display: none; + } input { @include inputStyle }