Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ibm
GitHub Repository: ibm/watson-machine-learning-samples
Path: blob/master/cloud/ai-service-apps/nextjs-carbon-react-ui/src/app/api/theme/route.js
6410 views
1
import { cookies } from "next/headers";
2
import { COOKIE_KEYS, THEME } from "../../../utils/constants";
3
import { NextResponse } from "next/server";
4
5
export async function GET() {
6
try {
7
const cookiesStorage = await cookies();
8
const theme = cookiesStorage.get(COOKIE_KEYS.THEME)?.value || THEME.SYSTEM;
9
10
return NextResponse.json({ theme }, { status: 200 });
11
} catch (err) {
12
return NextResponse.json(
13
{ error: `Error fetching theme cookie: ${err.message}` },
14
{ status: 500 }
15
);
16
}
17
}
18
19
export async function POST(req) {
20
try {
21
const { theme } = await req.json();
22
23
const cookiesStorage = await cookies();
24
cookiesStorage.set(COOKIE_KEYS.THEME, theme, {
25
path: "/",
26
maxAge: 2592000,
27
});
28
29
return NextResponse.json({ success: true }, { status: 200 });
30
} catch (err) {
31
return NextResponse.json(
32
{ error: `Error updating theme cookie: ${err.message}` },
33
{ status: 500 }
34
);
35
}
36
}
37
38