import React, { useState } from "react"; import useSWR from "swr"; import { cx } from "twind/core"; import { fetchApi, handleResponse } from "./apiClient.ts"; import { omitUndef } from "../utils/omitUndef.ts"; export function SettingsPage(props: { sessionId: string | null }) { const { sessionId } = props; const getSession = useSWR( sessionId ? ["/sessions/:sessionId", { params: { sessionId } }] as const : null, (args) => fetchApi(...args).then(handleResponse), ); const getUser = useSWR( getSession.data?.userId ? ["/users/:userId", { params: { userId: String( getSession.data.userId) } }] as const : null, (args) => fetchApi(...args).then(handleResponse), ); const getParams = useSWR( ["/settings/params", {}] as const, (args) => fetchApi(...args).then(handleResponse), ); const [newParams, setNewParams] = useState>({}); const [patchParamsError, setPatchParamsError] = useState(); return (
{ e.preventDefault(); getParams.mutate(() => fetchApi("/settings/params", { method: "PATCH", query: { sessionId: sessionId ?? "" }, body: omitUndef(newParams ?? {}), }).then(handleResponse) ) .then(() => setNewParams({})) .catch((e) => setPatchParamsError(String(e))); }} >