personal-site/pages/blog/index.jsx

202 lines
7.0 KiB
React
Raw Normal View History

2022-06-10 07:49:12 +00:00
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
import React from "react";
import Head from "next/head";
2022-06-20 05:09:28 +00:00
2022-06-20 05:14:22 +00:00
// const contentful = require('contentful');
2022-08-06 18:48:47 +00:00
const https = require("https")
2022-06-10 07:49:12 +00:00
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
import GeneralLayout from "../../layouts/general_layout/GeneralLayout";
import TextShuffler from "../../components/actions/TextShuffler";
2022-07-27 17:16:37 +00:00
// import httpFetch from "../../functions/backend/httpFetch";
2022-06-10 07:49:12 +00:00
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* ==============================================================================
* Main Component { Functional }
* ==============================================================================
* @param {Object} props - Server props
*/
export default function BlogIndex(props) {
2022-06-20 05:09:28 +00:00
/**
* Get Contexts
*
* @abstract { React.useContext }
*/
2022-06-10 07:49:12 +00:00
/** ********************************************** */
2022-06-20 05:09:28 +00:00
/**
* Javascript Variables
*
* @abstract Non hook variables and functions
*/
2022-06-10 07:49:12 +00:00
/** ********************************************** */
2022-06-20 05:09:28 +00:00
/**
* React Hooks
*
* @abstract { useState, useEffect, useRef, etc ... }
*/
2022-06-10 07:49:12 +00:00
/** ********************************************** */
2022-06-20 05:09:28 +00:00
/**
* Function Return
*
* @abstract Main Function Return
*/
2022-06-10 07:49:12 +00:00
return (
<React.Fragment>
<Head>
<title>Blog | Tben.me</title>
<meta name="description" content="Tech talks" />
</Head>
<GeneralLayout>
<h1 className="mb-8"><TextShuffler textInput="My Blog" /></h1>
<div className="flex flex-col items-start w-full gap-4">
{ props.blogPosts.map(post =>
<a
href={ `/blog/${post.slug}` }
className="flex flex-col items-start gap-2 w-full hover:bg-blue-600 border border-solid border-white/20 p-8 transition-all"
2022-08-06 18:48:47 +00:00
key={ post.slug }
2022-06-10 07:49:12 +00:00
>
<h2 className="m-0"><TextShuffler textInput={ post.title } /></h2>
2022-08-06 18:48:47 +00:00
<span className="opacity-80"><TextShuffler textInput={ post.excerpt } /></span>
<span className="text-sm opacity-50"><TextShuffler textInput={ post.date_created.substring(0, 24) } /></span>
2022-06-10 07:49:12 +00:00
</a>
) }
</div>
</GeneralLayout>
</React.Fragment>
);
2022-06-20 05:09:28 +00:00
2022-06-10 07:49:12 +00:00
/** ********************************************** */
};
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* ==============================================================================
* Server Side Props or Static Props
* ==============================================================================
* @param {Object} req - http incoming request object
* @param {Object} res - http response object
* @param {Object} query - queries attached to the url
*/
2022-06-20 05:09:28 +00:00
export async function getStaticProps({ req, res, query }) {
/**
* User Auth
*
* @abstract grab user
*/
// const contentfulClient = contentful.createClient({
// space: process.env.CONTENTFUL_SPACE_ID,
// accessToken: process.env.CONTENTFUL_API_KEY,
// });
2022-06-10 07:49:12 +00:00
2022-06-20 05:09:28 +00:00
// const posts = await contentfulClient.getEntries()
2022-06-10 07:49:12 +00:00
2022-06-20 05:09:28 +00:00
// console.log(posts);
2022-06-10 07:49:12 +00:00
/** ********************************************** */
2022-06-20 05:09:28 +00:00
/**
* Data fetching
*
* @abstract fetch date from the server or externnal source
*/
// let blogPosts = await httpFetch({
// options: {
// method:"DELETE",
// path: "/api/tben-blogs"
// },
// paradigm:"https"
// })
// console.log(blogPosts.data);
2022-08-06 18:48:47 +00:00
const postsResponse = await new Promise((resolve, reject) => {
https
.get(
/** ********************* Get Options object */
{
host: "datasquirel.tben.me",
path: `/api/query/get?db=tbenme&query=select+title,slug,excerpt,date_created+from+blog_posts+limit+10`,
headers: {
Authorization: process.env.DATASQUIREL_API_KEY,
},
},
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
/** ********************* Callback function */
(response) => {
var str = "";
// ## another chunk of data has been received, so append it to `str`
response.on("data", function (chunk) {
str += chunk;
});
// ## the whole response has been received, so we just print it out here
response.on("end", function () {
resolve(JSON.parse(str))
});
response.on("error", (err) => {
console.log(err);
});
}
)
.end();
});
if (!postsResponse.success) {
return {
redirect: {
destination: "/",
permanent: false
}
}
}
const posts = postsResponse.payload;
2022-06-10 07:49:12 +00:00
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
// ## Server Props Return
return {
props: {
2022-07-27 17:16:20 +00:00
blogPosts: posts,
2022-06-10 07:49:12 +00:00
},
2022-08-06 18:48:47 +00:00
revalidate: 1000
2022-06-10 07:49:12 +00:00
};
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
}