generated from jhackett1/railsy-nextjs-prisma-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.jsx
70 lines (63 loc) · 1.92 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import Head from "next/head"
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, setStatus) => {
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_HOST}/api/settings`,
{
method: "POST",
body: JSON.stringify({
teamId: id,
...values,
}),
}
)
const data = await res.json()
if (data.error) throw data.error
mutate(`${process.env.NEXT_PUBLIC_API_HOST}/api/settings`)
router.push("/")
} catch (e) {
setStatus(e.toString())
}
}
return (
<div className="govuk-grid-row">
<Head>
<title>Settings | SMS | Hackney Council</title>
</Head>
<div className="govuk-grid-column-two-thirds">
<h1 className="lbh-heading-h1 govuk-!-margin-bottom-8">
Settings and preferences
</h1>
<SettingsForm
onSubmit={(values, { setStatus }) =>
handleSubmit(team.id, values, setStatus)
}
initialValues={{
useSignature: session?.user?.useSignature,
signature: session?.user?.signature || "",
outOfHoursAutoreply: team?.outOfHoursAutoreply,
outOfHoursMessage: team?.outOfHoursMessage || "",
messageTemplates: team?.messageTemplates,
}}
/>
</div>
</div>
)
}
export default Settings