2022-03-13 01:40:28 -03:00
|
|
|
import styles from '@styles/Home.module.css'
|
2022-03-06 16:46:59 -08:00
|
|
|
|
2022-03-13 01:40:28 -03:00
|
|
|
import Header from '@components/header'
|
|
|
|
import MyPosts from '@components/my-posts'
|
2022-03-21 17:20:41 -07:00
|
|
|
import cookie from "cookie";
|
2022-03-21 20:30:45 -07:00
|
|
|
import type { GetServerSideProps } from 'next';
|
2022-03-22 20:06:15 -07:00
|
|
|
import { Post } from '@lib/types';
|
|
|
|
import { Page } from '@geist-ui/core';
|
2022-03-06 16:46:59 -08:00
|
|
|
|
2022-03-24 18:03:57 -07:00
|
|
|
const Home = ({ morePosts, posts, error }: { morePosts: boolean, posts: Post[]; error: boolean; }) => {
|
2022-03-06 16:46:59 -08:00
|
|
|
return (
|
2022-03-24 18:03:57 -07:00
|
|
|
<Page className={styles.wrapper}>
|
2022-03-22 20:06:15 -07:00
|
|
|
<Page.Content className={styles.main}>
|
2022-03-24 18:03:57 -07:00
|
|
|
<MyPosts morePosts={morePosts} error={error} posts={posts} />
|
2022-03-06 16:46:59 -08:00
|
|
|
</Page.Content>
|
|
|
|
</Page >
|
|
|
|
)
|
|
|
|
}
|
2022-03-21 17:20:41 -07:00
|
|
|
// get server side props
|
|
|
|
export const getServerSideProps: GetServerSideProps = async ({ req }) => {
|
|
|
|
const driftToken = cookie.parse(req.headers.cookie || '')[`drift-token`]
|
|
|
|
if (!driftToken) {
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
destination: '/',
|
|
|
|
permanent: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 00:05:05 -07:00
|
|
|
const posts = await fetch(process.env.API_URL + `/posts/mine`, {
|
2022-03-21 17:20:41 -07:00
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
2022-03-21 17:42:37 -07:00
|
|
|
"Authorization": `Bearer ${driftToken}`,
|
|
|
|
"x-secret-key": process.env.SECRET_KEY || ''
|
2022-03-21 17:20:41 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-03-24 19:25:02 -07:00
|
|
|
if (!posts.ok) {
|
2022-03-28 12:13:22 -07:00
|
|
|
console.error(await posts.json())
|
2022-03-21 17:42:37 -07:00
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
destination: '/',
|
|
|
|
permanent: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 18:03:57 -07:00
|
|
|
const data = await posts.json()
|
2022-03-21 17:20:41 -07:00
|
|
|
return {
|
|
|
|
props: {
|
2022-03-26 00:05:05 -07:00
|
|
|
posts: data.posts,
|
2022-03-21 17:20:41 -07:00
|
|
|
error: posts.status !== 200,
|
2022-03-26 00:05:05 -07:00
|
|
|
morePosts: data.hasMore,
|
2022-03-21 17:20:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-06 16:46:59 -08:00
|
|
|
|
|
|
|
export default Home
|