Skip to content

Commit

Permalink
feat: 🔒 saving changes for later
Browse files Browse the repository at this point in the history
  • Loading branch information
WasiqB committed Dec 15, 2024
1 parent c178e8f commit 257d629
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 62 deletions.
12 changes: 9 additions & 3 deletions apps/web/app/(app)/loading/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ const LoadingPage = (): JSX.Element => {
try {
setProgress(25);

const response = await fetch('/api/get-formatted-data');
const contentType = response.headers.get('content-type');
const response = await fetch('/api/format-data', {
body: JSON.stringify({
value: localStorage.getItem('xml-data'),
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});

if (!response.ok) {
const errorData = await response.json();
Expand All @@ -39,7 +46,6 @@ const LoadingPage = (): JSX.Element => {
throw new Error('No data received from server');
}

// Store the data
localStorage.setItem('json-data', JSON.stringify(data));

setProgress(100);
Expand Down
1 change: 0 additions & 1 deletion apps/web/app/(app)/results/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const ResultsPage = (): JSX.Element => {
useEffect(() => {
const loadData = async () => {
try {
// First try to get data from localStorage
const resultData = localStorage.getItem('json-data');
if (resultData) {
const testResult: TestResultData[] = JSON.parse(resultData);
Expand Down
28 changes: 28 additions & 0 deletions apps/web/app/api/format-data/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getData } from '@ultra-reporter/ui/data';
import { getTestResults } from '@ultra-reporter/utils/xml-parser';
import { NextRequest, NextResponse } from 'next/server';

export async function POST(request: NextRequest) {
try {
const { value } = await request.json();

if (!value) {
return NextResponse.json(
{ error: 'Please upload a file first' },
{ status: 404 }
);
}

console.log(value);
const testResult = getTestResults(value);
const formattedData = getData(testResult);

return NextResponse.json(formattedData);
} catch (error) {
console.error('Error getting formatted data:', error);
return NextResponse.json(
{ error: 'Error retrieving formatted data' },
{ status: 500 }
);
}
}
36 changes: 0 additions & 36 deletions apps/web/app/api/get-formatted-data/route.ts

This file was deleted.

32 changes: 10 additions & 22 deletions apps/web/app/api/process-file/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { getData } from '@ultra-reporter/ui/data';
import {
convertToJson,
getTestResults,
} from '@ultra-reporter/utils/xml-parser';
import { convertToJson } from '@ultra-reporter/utils/xml-parser';
import { FileReader } from 'fs/promises';
import { NextRequest, NextResponse } from 'next/server';

export async function POST(request: NextRequest) {
Expand All @@ -21,7 +18,13 @@ export async function POST(request: NextRequest) {
);
}

const xmlContent = await file.text();
let xmlContent: string | null = null;
const reader = new FileReader();
reader.onload = async (e) => {
xmlContent = e.target?.result as string;
};
reader.readAsText(file);

if (!xmlContent) {
return NextResponse.json(
{ error: 'Empty file content' },
Expand All @@ -30,22 +33,7 @@ export async function POST(request: NextRequest) {
}

const jsonData = convertToJson(xmlContent);
const testResult = getTestResults(jsonData);
const processedData = getData(testResult);

// Create the response first
const response = NextResponse.json({ success: true });

// Set cookie in the response
response.cookies.set('test-results', JSON.stringify(processedData), {
path: '/',
maxAge: 3600,
httpOnly: false, // Set to false so client-side can access it
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
});

return response;
return NextResponse.json(jsonData);
} catch (error) {
console.error('Error processing file:', error);
return NextResponse.json(
Expand Down
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@next/third-parties": "^15.0.4",
"@tanstack/react-table": "^8.20.5",
"@ultra-reporter/analytics": "workspace:*",
"@ultra-reporter/kv": "workspace:*",
"@ultra-reporter/feature-toggle": "workspace:*",
"@ultra-reporter/ui": "workspace:*",
"@ultra-reporter/utils": "workspace:*",
Expand Down
21 changes: 21 additions & 0 deletions packages/kv/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@ultra-reporter/kv",
"description": "Ultra Reporter Redis and Rate limiter",
"version": "0.6.0",
"private": true,
"scripts": {
"lint": "eslint . --max-warnings 0"
},
"exports": {
"./redis": "./src/index.ts",
"./ratelimit": "./src/rate-limit.ts"
},
"devDependencies": {
"@ultra-reporter/typescript-config": "workspace:*"
},
"dependencies": {
"@upstash/ratelimit": "^2.0.5",
"@upstash/redis": "^1.34.3",
"server-only": "^0.0.1"
}
}
4 changes: 4 additions & 0 deletions packages/kv/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Redis } from '@upstash/redis';
import 'server-only';

export const redis = Redis.fromEnv();
10 changes: 10 additions & 0 deletions packages/kv/src/rate-limit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Ratelimit } from '@upstash/ratelimit';
import 'server-only';
import { redis } from './index';

export const ratelimit = new Ratelimit({
limiter: Ratelimit.slidingWindow(5, '10s'),
redis,
analytics: true,
timeout: 10000,
});
11 changes: 11 additions & 0 deletions packages/kv/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@ultra-reporter/typescript-config/react-library.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src"],
"exclude": ["dist", "build", "node_modules"]
}
2 changes: 2 additions & 0 deletions packages/ui/src/utils/file-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export const FileUpload = (): JSX.Element => {
throw new Error(error.error || 'Failed to process file');
}

localStorage.setItem('xml-data', JSON.stringify(await response.json()));

router.push('/loading');
} catch (error) {
console.error('Error processing file:', error);
Expand Down
54 changes: 54 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 257d629

Please sign in to comment.