diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 5c1f6d9..3842a8b 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -2253,53 +2253,59 @@ function App() { } }; + const saveWorkspaceSettings = async () => { + const response = await apiFetch('/workspace', authToken, { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + ...workspaceForm, + billingPlan: workspaceForm.workspaceType === 'rescue' ? undefined : workspaceForm.billingPlan, + billingInterval: workspaceForm.workspaceType === 'rescue' ? undefined : workspaceForm.billingInterval, + }), + }); + + if (!response.ok) { + throw new Error(await readErrorMessage(response, 'Unable to save flock settings.')); + } + + const data = (await readJsonSafely<{ workspace?: Workspace }>(response)) ?? {}; + + if (!data.workspace) { + throw new Error('Unable to save flock settings.'); + } + + const savedWorkspace = data.workspace; + + setWorkspace(savedWorkspace); + setAuthSession((current) => + current + ? { + ...current, + activeWorkspace: savedWorkspace, + workspaces: current.workspaces.map((entry) => + entry.workspace.id === savedWorkspace.id ? { ...entry, workspace: savedWorkspace } : entry, + ), + } + : current, + ); + setWorkspaceForm({ + name: savedWorkspace.name, + workspaceType: savedWorkspace.workspaceType, + billingEmail: savedWorkspace.billingEmail ?? '', + billingPlan: isHouseholdPlan(savedWorkspace.billingPlan) ? savedWorkspace.billingPlan : 'household_basic', + billingInterval: savedWorkspace.billingInterval, + }); + + return savedWorkspace; + }; + const handleWorkspaceSubmit = async (event: React.FormEvent) => { event.preventDefault(); setError(''); setSavingWorkspace(true); try { - const response = await apiFetch('/workspace', authToken, { - method: 'PUT', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - ...workspaceForm, - billingPlan: workspaceForm.workspaceType === 'rescue' ? undefined : workspaceForm.billingPlan, - billingInterval: workspaceForm.workspaceType === 'rescue' ? undefined : workspaceForm.billingInterval, - }), - }); - - if (!response.ok) { - throw new Error(await readErrorMessage(response, 'Unable to save flock settings.')); - } - - const data = (await readJsonSafely<{ workspace?: Workspace }>(response)) ?? {}; - - if (!data.workspace) { - throw new Error('Unable to save flock settings.'); - } - - const savedWorkspace = data.workspace; - - setWorkspace(savedWorkspace); - setAuthSession((current) => - current - ? { - ...current, - activeWorkspace: savedWorkspace, - workspaces: current.workspaces.map((entry) => - entry.workspace.id === savedWorkspace.id ? { ...entry, workspace: savedWorkspace } : entry, - ), - } - : current, - ); - setWorkspaceForm({ - name: savedWorkspace.name, - workspaceType: savedWorkspace.workspaceType, - billingEmail: savedWorkspace.billingEmail ?? '', - billingPlan: isHouseholdPlan(savedWorkspace.billingPlan) ? savedWorkspace.billingPlan : 'household_basic', - billingInterval: savedWorkspace.billingInterval, - }); + await saveWorkspaceSettings(); } catch (workspaceError) { setError(workspaceError instanceof Error ? workspaceError.message : 'Unable to save flock settings.'); } finally { @@ -2406,12 +2412,17 @@ function App() { setError(''); setBillingRedirecting(true); + setSavingWorkspace(true); try { + const savedWorkspace = await saveWorkspaceSettings(); + const billingPlan = isHouseholdPlan(savedWorkspace.billingPlan) ? savedWorkspace.billingPlan : workspaceForm.billingPlan; + const billingInterval = savedWorkspace.billingInterval; + const response = await apiFetch('/billing/checkout-session', authToken, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ billingPlan: workspace.billingPlan, billingInterval: workspace.billingInterval }), + body: JSON.stringify({ billingPlan, billingInterval }), }); if (!response.ok) { @@ -2428,6 +2439,7 @@ function App() { } catch (billingError) { setError(billingError instanceof Error ? billingError.message : 'Unable to start Stripe checkout.'); setBillingRedirecting(false); + setSavingWorkspace(false); } }; @@ -3410,7 +3422,7 @@ function App() { {workspace?.workspaceType !== 'rescue' ? ( -
+