2022-03-12 23:40:28 -05:00
|
|
|
import styles from '@styles/Home.module.css'
|
2022-03-06 19:46:59 -05:00
|
|
|
import { Page } from '@geist-ui/core'
|
|
|
|
|
2022-03-12 23:40:28 -05:00
|
|
|
import Header from '@components/header'
|
|
|
|
import MyPosts from '@components/my-posts'
|
2022-03-21 20:20:41 -04:00
|
|
|
import cookie from "cookie";
|
|
|
|
import { GetServerSideProps } from 'next';
|
|
|
|
import { ThemeProps } from '@lib/types';
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-03-21 20:20:41 -04:00
|
|
|
const Home = ({ posts, error, theme, changeTheme }: ThemeProps & { posts: any; error: any; }) => {
|
2022-03-06 19:46:59 -05:00
|
|
|
return (
|
2022-03-07 21:36:36 -05:00
|
|
|
<Page className={styles.container} width="100%">
|
2022-03-06 19:46:59 -05:00
|
|
|
<Page.Header>
|
|
|
|
<Header theme={theme} changeTheme={changeTheme} />
|
|
|
|
</Page.Header>
|
2022-03-09 04:33:22 -05:00
|
|
|
<Page.Content paddingTop={"var(--gap)"} width={"var(--main-content-width)"} margin="0 auto" className={styles.main}>
|
2022-03-21 20:20:41 -04:00
|
|
|
<MyPosts error={error} posts={posts} />
|
2022-03-06 19:46:59 -05:00
|
|
|
</Page.Content>
|
|
|
|
</Page >
|
|
|
|
)
|
|
|
|
}
|
2022-03-21 20:20:41 -04: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-21 20:42:37 -04:00
|
|
|
const posts = await fetch(process.env.API_URL + `/posts/mine`, {
|
2022-03-21 20:20:41 -04:00
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
2022-03-21 20:42:37 -04:00
|
|
|
"Authorization": `Bearer ${driftToken}`,
|
|
|
|
"x-secret-key": process.env.SECRET_KEY || ''
|
2022-03-21 20:20:41 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-03-21 20:42:37 -04:00
|
|
|
if (!posts.ok || posts.status !== 200) {
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
destination: '/',
|
|
|
|
permanent: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 20:20:41 -04:00
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
posts: await posts.json(),
|
|
|
|
error: posts.status !== 200,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-06 19:46:59 -05:00
|
|
|
|
|
|
|
export default Home
|