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