generated from jhackett1/railsy-nextjs-prisma-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.jsx
56 lines (49 loc) · 1.54 KB
/
settings.jsx
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
import { useState } from "react"
import SettingsForm from "../components/SettingsForm"
import { Router, useRouter } from "next/router"
import { useSession, getSession } from "next-auth/client"
import useSWR, { mutate } from "swr"
import { useEffect } from "react"
const Settings = () => {
const router = useRouter()
const [session, setSession] = useState(false)
useEffect(async () => {
// explicitly grab a fresh session on page load
setSession(await getSession())
}, [])
const { data: team } = useSWR(
`${process.env.NEXT_PUBLIC_API_HOST}/api/settings`
)
const handleSubmit = async (id, values) => {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_HOST}/api/settings`,
{
method: "POST",
body: JSON.stringify({
teamId: id,
...values,
}),
}
)
mutate(`${process.env.NEXT_PUBLIC_API_HOST}/api/settings`)
router.push("/")
}
return (
<div className="govuk-grid-row">
<div className="govuk-grid-column-two-thirds">
<h1 className="lbh-heading-h1 govuk-!-margin-bottom-8">Settings</h1>
<SettingsForm
onSubmit={values => handleSubmit(team.id, values)}
initialValues={{
useSignature: session?.user?.useSignature,
signature: session?.user?.signature,
outOfHoursAutoreply: team?.outOfHoursAutoreply,
outOfHoursMessage: team?.outOfHoursMessage,
messageTemplates: team?.messageTemplates,
}}
/>
</div>
</div>
)
}
export default Settings