sendou.ink/app/modules/theme/action.server.ts
2022-12-08 21:29:51 +02:00

33 lines
872 B
TypeScript

import type { ActionFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import { getThemeSession } from "./session.server";
import { isTheme } from "./provider";
export const action: ActionFunction = async ({ request }) => {
const themeSession = await getThemeSession(request);
const requestText = await request.text();
const form = new URLSearchParams(requestText);
const theme = form.get("theme");
if (theme === "auto") {
return json(
{ success: true },
{ headers: { "Set-Cookie": await themeSession.destroy() } }
);
}
if (!isTheme(theme)) {
return json({
success: false,
message: `theme value of ${theme ?? "null"} is not a valid theme`,
});
}
themeSession.setTheme(theme);
return json(
{ success: true },
{ headers: { "Set-Cookie": await themeSession.commit() } }
);
};