sendou.ink/app/modules/theme/action.server.ts
2022-09-04 15:23:59 +03:00

26 lines
726 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 (!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() } }
);
};