From 62c116189a1b66667f5843e3a069add05c2487f8 Mon Sep 17 00:00:00 2001 From: Nidboj132 <28981031+Nidboj132@users.noreply.github.com> Date: Sat, 23 Dec 2023 14:09:35 +0100 Subject: feat: discussion page (#137) --- frontend/src/components/pages/summary.js | 176 ++++++++++++++++++++++++++++++- 1 file changed, 175 insertions(+), 1 deletion(-) (limited to 'frontend/src/components/pages/summary.js') diff --git a/frontend/src/components/pages/summary.js b/frontend/src/components/pages/summary.js index 7c72c48..228ce01 100644 --- a/frontend/src/components/pages/summary.js +++ b/frontend/src/components/pages/summary.js @@ -21,6 +21,10 @@ const fakedata={} //for debug const [data, setData] = React.useState(null); React.useEffect(() => { setData(null) + setDiscussionThread(null) + setCreatePostState(0) + setSelectedRun(0) + setCatState(1) fetch(`https://lp.ardapektezol.com/api/v1/maps/${location.pathname.split('/')[2]}/summary`) .then(r => r.json()) .then(d => { @@ -40,6 +44,28 @@ const fakedata={} //for debug // eslint-disable-next-line }, [pageNumber,location.pathname]); + const [discussions,setDiscussions] = React.useState(null) + function fetchDiscussions() { + fetch(`https://lp.ardapektezol.com/api/v1/maps/${location.pathname.split('/')[2]}/discussions`) + .then(r=>r.json()) + .then(d=>setDiscussions(d.data.discussions)) + } + + React.useEffect(()=>{ + fetchDiscussions() + },[location.pathname]) + + + +const [discussionThread,setDiscussionThread] = React.useState(null) +function openDiscussion(x){ + fetch(`https://lp.ardapektezol.com/api/v1/maps/${location.pathname.split('/')[2]}/discussions/${x}`) + .then(r=>r.json()) + .then(d=>setDiscussionThread(d.data.discussion)) +} +const [discussionSearch, setDiscussionSearch] = React.useState("") + + const [navState, setNavState] = React.useState(0); // eslint-disable-next-line @@ -204,7 +230,9 @@ function YouTubeGetID(url){ } function TimeAgo(date) { - const seconds = Math.floor((new Date() - date) / 1000); + // const seconds = Math.floor((new Date() - date) / 1000); + + const seconds = Math.floor(((new Date(new Date() - (date.getTimezoneOffset()*-60000))) - date) / 1000); let interval = Math.floor(seconds / 31536000); if (interval > 1) {return interval + ' years ago';} @@ -239,6 +267,52 @@ function TicksToTime(ticks) { return `${hours===0?"":hours+":"}${minutes===0?"":hours>0?minutes.toString().padStart(2, '0')+":":(minutes+":")}${minutes>0?seconds.toString().padStart(2, '0'):seconds}.${milliseconds.toString().padStart(3, '0')} (${ticks})`; } +function PostComment() { + + fetch(`https://lp.ardapektezol.com/api/v1/maps/${location.pathname.split('/')[2]}/discussions/${discussionThread.id}`,{ + method:"POST", + headers:{authorization:token}, + body:JSON.stringify({"comment":document.querySelector("#discussion-send>input").value}) +}) +.then(r=>r.json()) +.then(d=>{ + document.querySelector("#discussion-send>input").value="" + openDiscussion(discussionThread.id) +}) +} + + +const [createPostState,setCreatePostState] = React.useState(0) +function CreatePost() { + + fetch(`https://lp.ardapektezol.com/api/v1/maps/${location.pathname.split('/')[2]}/discussions`,{ + method:"POST", + headers:{authorization:token}, + body:JSON.stringify({"title":document.querySelector("#discussion-create-title").value,"content":document.querySelector("#discussion-create-content").value}) + }) + .then(r=>r.json()) + .then(d=>{ + setCreatePostState(0) + fetchDiscussions() + }) +} + +function DeletePost(post) { +if(window.confirm(`Are you sure you want to remove post: ${post.title}?`)){ + console.log("deleted",post.id) + fetch(`https://lp.ardapektezol.com/api/v1/maps/${location.pathname.split('/')[2]}/discussions/${post.id}`,{ + method:"DELETE", + headers:{authorization:token}, + }) + .then(r=>r.json()) + .then(d=>{ + fetchDiscussions() + }) +} +} + + + if(data!==null){ return ( <> @@ -365,6 +439,8 @@ return ( + {/* Leaderboards */} + {lbData===null?"":lbData.success===false?(

Map is not available for competitive boards.

@@ -445,6 +521,104 @@ return (
)} + + {/* Discussions */} +
+ + {discussionThread === null ? ( + createPostState === 0 ? ( + discussions !== null ? ( + // Main screen + <> + + {discussions.filter(f=>f.title.includes(discussionSearch)).sort((a, b) => new Date(b.updated_at) - new Date(a.updated_at)) + .map((e, i) => ( +
+ + + ::} + {e.creator.user_name}: {e.content} + last updated: {TimeAgo(new Date(e.updated_at.replace("T"," ").replace("Z","")))} + +
+ ))} + + ):( + + // Main screen (no posts) + <> + + no discussions + + ) + ):( + // Creating post +
+ Create post + +
+ + +
+
+ +
+ +
+ + )):( + // Post screen +
+
+ {discussionThread.title} + +
+ +
+ +
+ {discussionThread.creator.user_name} + {TimeAgo(new Date(discussionThread.created_at.replace("T"," ").replace("Z","")))} + {discussionThread.content} +
+ {discussionThread.comments!==null? + discussionThread.comments.sort((a, b) => new Date(a.date) - new Date(b.date)) + .map(e=>( + <> + +
+ {e.user.user_name} + {TimeAgo(new Date(e.date.replace("T"," ").replace("Z","")))} + {e.comment} +
+ + + )):""} + + +
+
+ e.key==="Enter"?PostComment():""}/> +
+
+ +
+ + + )} + +
+ ) -- cgit v1.2.3