-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
2,480 additions
and
1,037 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
'use client' | ||
import { useEffect, useState } from "react"; | ||
import Desktop from "../components/desktop"; | ||
import Window from "../components/window" | ||
import { FetchBlogPosts, UploadBlogPost } from "../database"; | ||
import { ParseText } from "../text-parser" | ||
import parse from 'html-react-parser'; | ||
import { BlogPost } from "../components/database-parse-type"; | ||
import axios from "axios"; | ||
import { useSession } from "next-auth/react"; | ||
import { redirect } from 'next/navigation' | ||
import { useRouter, usePathname, useSearchParams } from 'next/navigation' | ||
import Head from "next/head"; | ||
|
||
function formatDate(date: Date): string { | ||
const options: Intl.DateTimeFormatOptions = { | ||
year: 'numeric', | ||
month: 'long', // 'short' for abbreviated month names | ||
day: 'numeric', | ||
hour12: true // Set to false for 24-hour format | ||
}; | ||
|
||
return date.toLocaleString('en-US', options); | ||
} | ||
|
||
export default function Page() { | ||
|
||
const [blogPosts, setBlogPosts] = useState<BlogPost[]>([]); | ||
const [userID, setUserID] = useState<null | string>(null); | ||
const [uploadedBlogPost, uploadBlogPost] = useState<BlogPost>(); | ||
|
||
const { data: discordData, status } = useSession() | ||
const searchParams = useSearchParams(); | ||
const router = useRouter() | ||
|
||
useEffect(() => { | ||
async function grabBlogPosts() { | ||
setBlogPosts((await FetchBlogPosts()).reverse()); | ||
} | ||
|
||
grabBlogPosts(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
async function ubp() { | ||
await UploadBlogPost(uploadedBlogPost as BlogPost); | ||
} | ||
|
||
ubp(); | ||
|
||
}, [uploadedBlogPost]) | ||
|
||
useEffect(() => { | ||
const login = async () => { | ||
|
||
try { | ||
|
||
if (!discordData) { // If discordData is null, then user has not signed in to the website. | ||
return; | ||
} | ||
|
||
if (!discordData?.access_token) { return } // If there's no access token then we don't need to do anything yet. | ||
|
||
const response = await axios.get('https://discord.com/api/users/@me', { headers: { Authorization: `Bearer ${discordData.access_token}` } }) | ||
|
||
setUserID(response.data.id) | ||
} | ||
catch (error) { | ||
console.error('Error fetching data from discord:', error); | ||
} | ||
} | ||
|
||
login(); | ||
|
||
}, [discordData]); | ||
|
||
function ButtonPane({post, id} : {post: BlogPost, id: number}) { | ||
|
||
const handleClick = () => { | ||
redirect(`/blog?id=${id}`); | ||
}; | ||
|
||
return ( | ||
<div className="window text-black min-w-[5px] mb-5" onClick={() => router.push(`/blog?id=${id}`)}> | ||
<p className="text-lg mx-3">{post.title}</p> | ||
<p className="text-sm mx-3">{post.description}</p> | ||
<p className="text-gray-500 text-right text-xs mx-3">{formatDate(post.datetime)}</p> | ||
</div> | ||
); | ||
} | ||
|
||
function DisplayBlogPost({id} : {id: number}) { | ||
|
||
const post = blogPosts.filter((p) => p.post_id == id)[0]; | ||
|
||
if (post == undefined) { | ||
return( | ||
<div className=" text-black min-w-[5px]"> | ||
<p className="text-2xl">...</p><br/> | ||
<div className='window mb-5'> | ||
<div className="text-sm mx-5 my-2">...</div><br/> | ||
<p className="text-xs text-right m-2 my-[-10px]">...</p><br/> | ||
</div> | ||
<button onClick={() => router.push('/blog')}>Back To Posts</button> | ||
</div> | ||
) | ||
} | ||
|
||
return ( | ||
<div className=" text-black min-w-[5px]"> | ||
<Head> | ||
<title>The World Machine</title> | ||
<meta name="description" content={post.description} /> | ||
<meta property="og:title" content={post.title} /> | ||
<meta property="og:description" content={post.description} /> | ||
<meta property="og:url" content="https://www.theworldmachine.xyz/" /> | ||
</Head> | ||
<div className='window mb-5'> | ||
<div className="text-sm mx-5 my-2"> | ||
<p className="text-2xl mt-5 text-center">{post.title}</p><br/> | ||
<ParseText>{post.content}</ParseText> | ||
</div><br/> | ||
<p className="text-xs text-right m-2 my-[-10px]">Posted: {formatDate(post.datetime)}</p><br/> | ||
</div> | ||
<button onClick={() => router.push('/blog')}>Back To Posts</button> | ||
</div> | ||
) | ||
} | ||
|
||
function CreateBlogPost() { | ||
|
||
const blog: BlogPost = { | ||
title: '', | ||
content: '', | ||
description: '', | ||
datetime: new Date(Date.now()), | ||
post_id: 0 | ||
} | ||
|
||
function Upload() { | ||
|
||
blog.datetime = new Date(Date.now()); | ||
blog.post_id = blogPosts.length; | ||
|
||
uploadBlogPost(blog); | ||
} | ||
|
||
return( | ||
<div className=" text-black min-w-[5px]"> | ||
<textarea placeholder="Blog title" onChange={(title) => (blog.title = title.target.value)}></textarea><br/> | ||
<textarea className="w-1/2" placeholder="Small description goes here" onChange={(description) => (blog.description = description.target.value)}></textarea><br/> | ||
<textarea className="w-full" placeholder="Blog content goes here" onChange={(content) => (blog.content = content.target.value)}></textarea> | ||
<button className="mb-5" onClick={Upload}>Post</button> | ||
</div> | ||
) | ||
} | ||
|
||
// ----------------------------------- | ||
|
||
if (blogPosts.length == 0) { | ||
return( | ||
<Desktop> | ||
<Window title='Blog Posts' className="max-w-[800px]"> | ||
{userID !== null && userID == "302883948424462346" && <CreateBlogPost/>} | ||
<div className='text-black'>Loading...</div> | ||
</Window> | ||
</Desktop> | ||
) | ||
} | ||
|
||
if (searchParams.size > 0) { | ||
|
||
return( | ||
<Desktop> | ||
<Window title='Blog Posts' className="max-w-[800px]"> | ||
<DisplayBlogPost id={Number(searchParams.get('id') || '0')} /> | ||
</Window> | ||
</Desktop> | ||
) | ||
} | ||
|
||
return( | ||
<Desktop> | ||
<Window title='Blog Posts' className="max-w-[800px]"> | ||
{userID !== null && userID == "302883948424462346" && <CreateBlogPost/>} | ||
{blogPosts.map((post) => ( | ||
<ButtonPane post={post} id={post.post_id}/> | ||
))} | ||
</Window> | ||
</Desktop> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.