CoastalCommitsPastes/client/pages/mine.tsx

64 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-03-12 23:40:28 -05:00
import styles from '@styles/Home.module.css'
2022-03-06 19:46:59 -05:00
2022-03-12 23:40:28 -05:00
import Header from '@components/header'
import MyPosts from '@components/my-posts'
import cookie from "cookie";
import type { GetServerSideProps } from 'next';
import { Post } from '@lib/types';
import { Page } from '@geist-ui/core';
2022-03-06 19:46:59 -05:00
2022-03-24 21:03:57 -04:00
const Home = ({ morePosts, posts, error }: { morePosts: boolean, posts: Post[]; error: boolean; }) => {
2022-03-06 19:46:59 -05:00
return (
2022-03-24 21:03:57 -04:00
<Page className={styles.wrapper}>
2022-03-06 19:46:59 -05:00
<Page.Header>
<Header />
2022-03-06 19:46:59 -05:00
</Page.Header>
<Page.Content className={styles.main}>
2022-03-24 21:03:57 -04:00
<MyPosts morePosts={morePosts} error={error} posts={posts} />
2022-03-06 19:46:59 -05:00
</Page.Content>
</Page >
)
}
// 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-24 21:03:57 -04:00
const posts = await fetch(process.env.API_URL + `/posts/mine?page=1`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${driftToken}`,
"x-secret-key": process.env.SECRET_KEY || ''
}
})
2022-03-24 22:25:02 -04:00
if (!posts.ok) {
return {
redirect: {
destination: '/',
permanent: false,
}
}
}
2022-03-24 21:03:57 -04:00
const data = await posts.json()
return {
props: {
2022-03-24 21:03:57 -04:00
posts: data,
error: posts.status !== 200,
2022-03-24 21:03:57 -04:00
morePosts: data.length > 10,
}
}
}
2022-03-06 19:46:59 -05:00
export default Home