Skip to content

Commit

Permalink
fix: wrong api endpoint for upload notes
Browse files Browse the repository at this point in the history
  • Loading branch information
citruscai committed Jul 5, 2024
1 parent 1d9f813 commit 120b716
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
22 changes: 12 additions & 10 deletions app/api/notes/upload/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { NextRequest, NextResponse } from 'next/server';


export async function POST(req: NextRequest) {
try {
const formData = await req.formData();
const pdfBlob = formData.get('file') as Blob;
const fileName = formData.get('fileName') as string;
const { text, level } = await req.json();

console.log('Uploading PDF:', fileName);
if (!text) {
throw new Error('No text provided');
}

const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/worksheets/upload`, {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/notes/upload`, {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ text, level })
});

if (!response.ok) {
throw new Error(`Failed to upload PDF: ${response.statusText}`);
throw new Error(`Failed to upload notes: ${response.statusText}`);
}

const data = await response.json();
return NextResponse.json({ file_url: data.file_url });
return NextResponse.json(data);
} catch (error) {
console.error('Error uploading PDF:', error);
console.error('Error uploading notes:', error);
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@ const SelectLevelStep: React.FC<SelectLevelStepProps> = ({ prev }) => {
return text.replace(//g, "'").replace(//g, "'");
};


const uploadWorksheet = async (pdf: Blob, fileName: string): Promise<string> => {
const formData = new FormData();
formData.append('file', pdf, fileName);
const response = await fetch('/api/worksheets/upload', {

const response = await fetch('/api/worksheets/upload', {
method: 'POST',
body: formData,
});

if (!response.ok) {
throw new Error(`Failed to upload PDF: ${response.statusText}`);
}

const data = await response.json();
return data.file_url;
};

const submitData = async () => {
setIsSubmitting(true);
try {
Expand All @@ -53,13 +54,13 @@ const SelectLevelStep: React.FC<SelectLevelStepProps> = ({ prev }) => {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: formState.notes.text, level: selectedLevel })
});

if (!response.ok) {
throw new Error(`Failed to upload notes: ${response.statusText}`);
}

const jsonResponse = await response.json();

let parsedText;
try {
const sanitizedText = jsonResponse.text
Expand All @@ -72,28 +73,27 @@ const SelectLevelStep: React.FC<SelectLevelStepProps> = ({ prev }) => {
console.error('Error parsing text field:', parseError);
throw new Error('Invalid JSON structure in text field.');
}

// Always generate PDF using the simple template

const guidedNotesPdf = await generatePDF(parsedText, jsonResponse.level);
const solutionsPdf = await generatePDF(parsedText, jsonResponse.level, true);

const sanitizedTitle = parsedText.title
? parsedText.title.replace(/[^\w\s-]/g, '').replace(/\s+/g, '-').toLowerCase()
: 'guided-notes';

const guidedNotesUrl = await uploadWorksheet(guidedNotesPdf, `${sanitizedTitle}-guided-notes.pdf`);
const solutionsUrl = await uploadWorksheet(solutionsPdf, `${sanitizedTitle}-solutions.pdf`);

const saveResponse = await fetch('/api/worksheets/save-urls', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id: jsonResponse.id, guidedNotesUrl, solutionsUrl })
});

if (!saveResponse.ok) {
throw new Error('Failed to save PDF URLs');
}

router.push(`/guidednotes/${jsonResponse.id}`);
} catch (error) {
if (error instanceof Error) {
Expand All @@ -103,7 +103,7 @@ const SelectLevelStep: React.FC<SelectLevelStepProps> = ({ prev }) => {
setIsSubmitting(false);
}
};

return (
<div className="flex flex-col space-y-4 theme-custom">
<h1 className="text-2xl mb-4 text-primary">Select Level</h1>
Expand Down

0 comments on commit 120b716

Please sign in to comment.