This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
71 lines (65 loc) · 2.24 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Habit, UserSettings } from '@prisma/client'
import HabitCard from 'components/habit/habit-card'
import NewHabitCard from 'components/habit/new-habit-card'
import Layout from 'components/layout/layout'
import Nav from 'components/layout/nav'
import safeJsonStringify from 'fast-safe-stringify'
import { GetServerSidePropsContext } from 'next'
import { Session } from 'next-auth'
import { unstable_getServerSession } from 'next-auth/next'
import { useRouter } from 'next/router'
import { authOptions } from 'pages/api/auth/[...nextauth]'
import { getHabitsForUser } from 'pages/api/habits'
import { getSettingsForUser } from 'pages/api/user/settings'
import { Col, Container, Row } from 'react-bootstrap'
export default function Home({
session,
userSettings,
habits,
}: {
session: Session
userSettings: UserSettings
habits: Habit[]
}) {
const router = useRouter()
const refreshData = () => {
router.replace(router.asPath, undefined, { scroll: false })
}
return (
<Layout>
<Nav session={session}></Nav>
<Container className="mt-3">
<Row xs={2} md={3}>
{habits.map((habit: Habit) => (
<Col className="pt-3" key={habit.habitId}>
<HabitCard habit={habit} userSettings={userSettings} habitChanged={refreshData} />
</Col>
))}
<Col className="pt-3">
<NewHabitCard habitChanged={refreshData} />
</Col>
</Row>
</Container>
</Layout>
)
}
export async function getServerSideProps(context: GetServerSidePropsContext) {
const session = await unstable_getServerSession(context.req, context.res, authOptions)
if (!session) {
return {
redirect: {
destination: '/',
permanent: false,
},
}
}
const userSettings = JSON.parse(safeJsonStringify(await getSettingsForUser(session.user.id)))
const habits = JSON.parse(safeJsonStringify(await getHabitsForUser(session.user.id)))
return {
props: {
session,
userSettings,
habits,
},
}
}