fix(web): wire GDPR data-export buttons to the real backend API
Three user-facing "export my data" entry points (settings/privacy, settings/data, backup/export) all called endpoints that never existed on the backend: /v1/settings/data and /v1/compliance/export-data both 404. Every click silently failed or showed a fake "queued, check your email" message with nothing ever sent. A fully working, real GDPR export pipeline already exists at /api/v1/data-export (request -> async generate -> poll status -> download), just never wired to any UI. Added a shared requestAndDownloadDataExport flow (web/utils/dataExportFlow.ts) that requests the export, polls status until ready, then streams and downloads the file, and pointed all three pages plus userService's mismatched getExportStatus (was missing the required exportId path param) at it. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
125baca2db
commit
842be56e80
@ -1,8 +1,8 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import api from '@/services/api'
|
||||
import { useToast } from '@/components/ui/toast'
|
||||
import { requestAndDownloadDataExport, DataExportTimeoutError } from '@/utils/dataExportFlow'
|
||||
|
||||
export default function ExportDataPage() {
|
||||
const { showToast } = useToast()
|
||||
@ -11,17 +11,18 @@ export default function ExportDataPage() {
|
||||
const handleExport = async () => {
|
||||
setExporting(true)
|
||||
try {
|
||||
const response = await api.get('/v1/compliance/export-data')
|
||||
const blob = new Blob([JSON.stringify(response.data, null, 2)], { type: 'application/json' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = `gitgram-data-export-${new Date().toISOString()}.json`
|
||||
a.click()
|
||||
URL.revokeObjectURL(url)
|
||||
await requestAndDownloadDataExport((phase) => {
|
||||
if (phase === 'processing') showToast('Preparing your export… this can take a couple of minutes.', 'info')
|
||||
})
|
||||
showToast('Your data export downloaded successfully.', 'success')
|
||||
} catch (error) {
|
||||
console.error('Export data error:', error)
|
||||
showToast('Failed to export data', 'error')
|
||||
showToast(
|
||||
error instanceof DataExportTimeoutError
|
||||
? 'Still preparing your export. Please check back in a few minutes.'
|
||||
: 'Failed to export data',
|
||||
'error',
|
||||
)
|
||||
} finally {
|
||||
setExporting(false)
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ import { useState, useEffect } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import api from '@/services/api'
|
||||
import { requestAndDownloadDataExport, DataExportTimeoutError } from '@/utils/dataExportFlow'
|
||||
|
||||
export default function DataSettingsPage() {
|
||||
const router = useRouter()
|
||||
@ -14,6 +15,7 @@ export default function DataSettingsPage() {
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [exporting, setExporting] = useState(false)
|
||||
const [exportMessage, setExportMessage] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
loadSettings()
|
||||
@ -46,17 +48,22 @@ export default function DataSettingsPage() {
|
||||
|
||||
const handleExport = async () => {
|
||||
setExporting(true)
|
||||
setExportMessage(null)
|
||||
try {
|
||||
const response = await api.get('/v1/compliance/export-data')
|
||||
const blob = new Blob([JSON.stringify(response.data, null, 2)], { type: 'application/json' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = `gitgram-data-export-${new Date().toISOString()}.json`
|
||||
a.click()
|
||||
URL.revokeObjectURL(url)
|
||||
await requestAndDownloadDataExport((phase) => {
|
||||
if (phase === 'processing') setExportMessage('Preparing your export… this can take a couple of minutes.')
|
||||
if (phase === 'downloading') setExportMessage('Export ready — starting download…')
|
||||
})
|
||||
setExportMessage('Your data export downloaded successfully.')
|
||||
} catch (error) {
|
||||
console.error('Export data error:', error)
|
||||
setExportMessage(
|
||||
error instanceof DataExportTimeoutError
|
||||
? 'Still preparing your export. Please check back in a few minutes.'
|
||||
: error instanceof Error
|
||||
? error.message
|
||||
: 'Failed to export data. Please try again.',
|
||||
)
|
||||
} finally {
|
||||
setExporting(false)
|
||||
}
|
||||
@ -88,6 +95,9 @@ export default function DataSettingsPage() {
|
||||
>
|
||||
{exporting ? 'Exporting...' : 'Export Data'}
|
||||
</button>
|
||||
{exportMessage && (
|
||||
<p className="mt-3 text-sm text-foreground/70">{exportMessage}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="bg-surface rounded-lg p-6">
|
||||
|
||||
@ -4,6 +4,7 @@ import { useState, useEffect, useCallback } from 'react'
|
||||
import { useAuthStore } from '@/store/authStore'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { settingsService } from '@/services/settingsService'
|
||||
import { requestAndDownloadDataExport, DataExportTimeoutError } from '@/utils/dataExportFlow'
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@ -325,13 +326,19 @@ export default function PrivacySettingsPage() {
|
||||
setExportLoading(true)
|
||||
setExportMessage(null)
|
||||
try {
|
||||
const response = await settingsService.getDataSettings()
|
||||
setExportMessage(
|
||||
(response as Record<string, unknown>).message as string
|
||||
?? 'Your data export has been queued. You will receive an email with the download link within 24 hours.',
|
||||
)
|
||||
await requestAndDownloadDataExport((phase) => {
|
||||
if (phase === 'processing') setExportMessage('Preparing your export… this can take a couple of minutes.')
|
||||
if (phase === 'downloading') setExportMessage('Export ready — starting download…')
|
||||
})
|
||||
setExportMessage('Your data export downloaded successfully.')
|
||||
} catch (err: unknown) {
|
||||
setExportMessage('Failed to request data export. Please try again.')
|
||||
setExportMessage(
|
||||
err instanceof DataExportTimeoutError
|
||||
? 'Still preparing your export. Please check back in a few minutes.'
|
||||
: err instanceof Error
|
||||
? `Failed: ${err.message}`
|
||||
: 'Failed to request data export. Please try again.',
|
||||
)
|
||||
} finally {
|
||||
setExportLoading(false)
|
||||
}
|
||||
@ -713,7 +720,7 @@ export default function PrivacySettingsPage() {
|
||||
<div style={{ flex: 1, minWidth: '200px' }}>
|
||||
<p style={{ margin: 0, fontSize: '13px', color: '#6b7280', lineHeight: 1.6 }}>
|
||||
Includes your posts, followers, messages, repositories, and account metadata.
|
||||
You will receive a download link via email within 24 hours.
|
||||
Preparation takes a couple of minutes; the file downloads automatically when ready.
|
||||
</p>
|
||||
{exportMessage && (
|
||||
<p
|
||||
|
||||
@ -158,14 +158,22 @@ export const userService = {
|
||||
return data
|
||||
},
|
||||
|
||||
// Data export
|
||||
requestDataExport: async () => {
|
||||
// Data export (GDPR). Backend flow is async: request -> poll status -> download
|
||||
// once ready. See backend/src/routes/dataExport.ts.
|
||||
requestDataExport: async (): Promise<{ success: boolean; data: { exportId: string; estimatedTime: string; createdAt: string } }> => {
|
||||
const { data } = await api.post('/v1/data-export/request')
|
||||
return data
|
||||
},
|
||||
|
||||
getExportStatus: async () => {
|
||||
const { data } = await api.get('/v1/data-export/status')
|
||||
getExportStatus: async (
|
||||
exportId: string
|
||||
): Promise<{ success: boolean; data: { exportId: string; status: 'processing' | 'ready' | 'expired' | 'failed'; progress: number } }> => {
|
||||
const { data } = await api.get(`/v1/data-export/${exportId}/status`)
|
||||
return data
|
||||
},
|
||||
|
||||
downloadDataExport: async (exportId: string): Promise<Blob> => {
|
||||
const { data } = await api.get(`/v1/data-export/${exportId}/download`, { responseType: 'blob' })
|
||||
return data
|
||||
},
|
||||
|
||||
|
||||
77
gitgram/web/utils/dataExportFlow.ts
Normal file
77
gitgram/web/utils/dataExportFlow.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import { userService } from '@/services/userService'
|
||||
|
||||
/**
|
||||
* Drives the full GDPR data-export flow against the real backend
|
||||
* (backend/src/routes/dataExport.ts): request -> poll status -> download.
|
||||
*
|
||||
* The backend generates the export asynchronously (2-5 min estimate), so this
|
||||
* polls until the job is 'ready' (or fails/expires), then streams the file
|
||||
* and triggers a browser download. Used by both settings/privacy and
|
||||
* settings/data, which previously called endpoints that never existed on the
|
||||
* backend (`/v1/settings/data`, `/v1/compliance/export-data`) and always 404'd.
|
||||
*/
|
||||
|
||||
const POLL_INTERVAL_MS = 3000
|
||||
const MAX_POLL_ATTEMPTS = 60 // ~3 minutes
|
||||
|
||||
export type DataExportPhase = 'requesting' | 'processing' | 'downloading' | 'done'
|
||||
|
||||
export interface DataExportFlowResult {
|
||||
exportId: string
|
||||
}
|
||||
|
||||
export class DataExportTimeoutError extends Error {
|
||||
constructor(public exportId: string) {
|
||||
super('Export is taking longer than expected. Check back in a few minutes.')
|
||||
this.name = 'DataExportTimeoutError'
|
||||
}
|
||||
}
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
function triggerBlobDownload(blob: Blob, filename: string): void {
|
||||
const url = URL.createObjectURL(blob)
|
||||
const anchor = document.createElement('a')
|
||||
anchor.href = url
|
||||
anchor.download = filename
|
||||
document.body.appendChild(anchor)
|
||||
anchor.click()
|
||||
anchor.remove()
|
||||
URL.revokeObjectURL(url)
|
||||
}
|
||||
|
||||
export async function requestAndDownloadDataExport(
|
||||
onPhaseChange?: (phase: DataExportPhase) => void
|
||||
): Promise<DataExportFlowResult> {
|
||||
onPhaseChange?.('requesting')
|
||||
const requested = await userService.requestDataExport()
|
||||
const exportId = requested.data.exportId
|
||||
|
||||
onPhaseChange?.('processing')
|
||||
for (let attempt = 0; attempt < MAX_POLL_ATTEMPTS; attempt += 1) {
|
||||
const statusResult = await userService.getExportStatus(exportId)
|
||||
const { status } = statusResult.data
|
||||
|
||||
if (status === 'ready') {
|
||||
onPhaseChange?.('downloading')
|
||||
const blob = await userService.downloadDataExport(exportId)
|
||||
triggerBlobDownload(blob, `gitgram-data-export-${new Date().toISOString()}.json`)
|
||||
onPhaseChange?.('done')
|
||||
return { exportId }
|
||||
}
|
||||
|
||||
if (status === 'failed') {
|
||||
throw new Error('Data export failed. Please try again.')
|
||||
}
|
||||
|
||||
if (status === 'expired') {
|
||||
throw new Error('Data export expired before it could be downloaded. Please request a new one.')
|
||||
}
|
||||
|
||||
await sleep(POLL_INTERVAL_MS)
|
||||
}
|
||||
|
||||
throw new DataExportTimeoutError(exportId)
|
||||
}
|
||||
Reference in New Issue
Block a user