diff --git a/frontend/src/app/doc-converter/page.tsx b/frontend/src/app/doc-converter/page.tsx index 469e550..a09ee3d 100644 --- a/frontend/src/app/doc-converter/page.tsx +++ b/frontend/src/app/doc-converter/page.tsx @@ -1,5 +1,4 @@ "use client"; - import React, { useState } from "react"; import Navbar from "../../components/Navbar"; import Footer from "../../components/Footer"; @@ -7,17 +6,41 @@ import Button from "../../components/Button"; import Link from "next/link"; import { ChevronDown, ChevronUp } from "lucide-react"; import Dropdown from "@/components/Dropdown"; +import { FileFormatsTable, outputFileFormats } from "@/constants"; export default function DocConverter() { const [file, setFile] = useState(null); const [downloadUrl, setDownloadUrl] = useState(""); const [loading, setLoading] = useState(false); const [tableOpen, setTableOpen] = useState(false); + const [filteredOptions, setFilteredOptions] = useState([]); + const [selectedOutputFormat, setSelectedOutputFormat] = useState( + "", + ); const handleFileChange = (event: React.ChangeEvent) => { if (event.target.files && event.target.files.length > 0) { - setFile(event.target.files[0]); + const selectedFile = event.target.files[0]; + const fileExtension = selectedFile.name.split(".").pop()?.toLowerCase(); + + const isSupported = outputFileFormats.some((format) => + format.input.toLowerCase().includes(fileExtension || ""), + ); + + if (!isSupported) { + console.error("Not supported file uploaded!"); + alert("File format not supported!"); + event.target.value = ""; + return; + } + + setFile(selectedFile); setDownloadUrl(""); + + const matchedFormat = outputFileFormats.find((format) => + format.input.toLowerCase().includes(fileExtension || ""), + ); + setFilteredOptions(matchedFormat ? matchedFormat.output : []); } }; @@ -29,6 +52,7 @@ export default function DocConverter() { const formData = new FormData(); formData.append("file", file); + formData.append("outputFormat", selectedOutputFormat); setLoading(true); @@ -46,7 +70,9 @@ export default function DocConverter() { } const blob = await response.blob(); + console.log("Blob:", blob); const url = window.URL.createObjectURL(blob); + console.log("Download URL:", url); setDownloadUrl(url); } catch (error) { console.error("Error while converting:", error); @@ -61,15 +87,26 @@ export default function DocConverter() {

doc-converter

-
+
- {/* TODO: Fix Dropdown menu placement */} - + 0 ? filteredOptions : [""]} + onClick={(event) => { + const selectedFormat = + event.currentTarget.textContent?.trim() || ""; + setSelectedOutputFormat(selectedFormat); + }} + />
{downloadUrl ? ( @@ -121,108 +158,16 @@ export default function DocConverter() { > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + {FileFormatsTable.map((format) => ( + + + + + ))}
- .doc (MS Word) - - .pdf, .docx, .odt, .txt, .rtf, .html, .epub -
- .docx (MS Word) - - .pdf, .odt, .txt, .rtf, .html, .epub -
- .odt (OpenDocument Text) - - .pdf, .doc, .docx, .txt, .rtf, .html, .epub -
- .rtf (Rich Text Format) - - .pdf, .doc, .docx, .odt, .txt, .html -
.txt (Text) - .pdf, .doc, .docx, .odt, .txt, .html -
- .html (Webseite) - - .pdf, .doc, .docx, .odt, .rtf, .txt -
- .epub (E-Book) - - .pdf, .doc, .docx, .odt, .rtf, .txt -
- .xls (MS Excel) - - .pdf, .xlsx, .ods, .csv -
- .xlsx (MS Excel) - .pdf, .xls, .ods, .csv
- .ods (OpenDocument Spreadsheet) - - .pdf, .xls, .xlsx, .csv -
- .csv (Comma-Separated Values) - - .pdf, .xls, .xlsx, .ods -
- .ppt (MS PowerPoint) - .pdf, .pptx, .odp
- .pptx (MS PowerPoint) - .pdf, .ppt, .odp
- .odp (OpenDocument Presentation) - .pdf, .ppt, .pptx
+ {format.input} + + {format.output.join(", ")} +
diff --git a/frontend/src/components/Dropdown.tsx b/frontend/src/components/Dropdown.tsx index 8a9d93c..23e0951 100644 --- a/frontend/src/components/Dropdown.tsx +++ b/frontend/src/components/Dropdown.tsx @@ -2,12 +2,11 @@ import React, { useState } from "react"; import Link from "next/link"; -// TODO: Adjust the width of the option list to match the width of the button -// TODO: Dynamic rendering of options - interface DropdownProps { content: string; + options: string[]; className?: string; + onClick?: (event: React.MouseEvent) => void; } const Dropdown = (props: DropdownProps) => { @@ -17,16 +16,17 @@ const Dropdown = (props: DropdownProps) => { setIsOpen(!isOpen); }; - const closeDropdown = () => { + const closeDropdown = (event: React.MouseEvent) => { setIsOpen(false); + if (props.onClick) props.onClick(event); }; return ( -
-
+
+
    -
  • - - Option 1 - -
  • -
  • - - Option 2 - -
  • -
  • - - Option 3 - -
  • + {props.options?.map((option) => ( +
  • + + {option} + +
  • + ))}
diff --git a/frontend/src/constants/index.ts b/frontend/src/constants/index.ts index 62ea718..344ac59 100644 --- a/frontend/src/constants/index.ts +++ b/frontend/src/constants/index.ts @@ -36,3 +36,121 @@ export const toolLinks = [ link: "/pomodoro-timer", }, ]; + +export const outputFileFormats = [ + { + input: ".doc", + output: [".pdf", ".docx", ".odt", ".txt", ".rtf", ".html", ".epub"], + }, + { + input: ".docx", + output: [".pdf", ".odt", ".txt", ".rtf", ".html", ".epub"], + }, + { + input: ".odt", + output: [".pdf", ".doc", ".docx", ".txt", ".rtf", ".html", ".epub"], + }, + { + input: ".rtf", + output: [".pdf", ".doc", ".docx", ".odt", ".txt", ".html"], + }, + { + input: ".txt", + output: [".pdf", ".doc", ".docx", ".odt", ".txt", ".html"], + }, + { + input: ".html", + output: [".pdf", ".doc", ".docx", ".odt", ".rtf", ".txt"], + }, + { + input: ".epub", + output: [".pdf", ".doc", ".docx", ".odt", ".rtf", ".txt"], + }, + { + input: ".xls", + output: [".pdf", ".xlsx", ".ods", ".csv"], + }, + { + input: ".xlsx", + output: [".pdf", ".xls", ".ods", ".csv"], + }, + { + input: ".ods", + output: [".pdf", ".xls", ".xlsx", ".csv"], + }, + { + input: ".csv", + output: [".pdf", ".xls", ".xlsx", ".ods"], + }, + { + input: ".ppt", + output: [".pdf", ".pptx", ".odp"], + }, + { + input: ".pptx", + output: [".pdf", ".ppt", ".odp"], + }, + { + input: ".odp", + output: [".pdf", ".ppt", ".pptx"], + }, +]; + +export const FileFormatsTable = [ + { + input: ".doc (MS Word)", + output: [".pdf", ".docx", ".odt", ".txt", ".rtf", ".html", ".epub"], + }, + { + input: ".docx (MS Word)", + output: [".pdf", ".odt", ".txt", ".rtf", ".html", ".epub"], + }, + { + input: ".odt (OpenDocument Text)", + output: [".pdf", ".doc", ".docx", ".txt", ".rtf", ".html", ".epub"], + }, + { + input: ".rtf (Rich Text Format)", + output: [".pdf", ".doc", ".docx", ".odt", ".txt", ".html"], + }, + { + input: ".txt (Text)", + output: [".pdf", ".doc", ".docx", ".odt", ".txt", ".html"], + }, + { + input: ".html (Webseite)", + output: [".pdf", ".doc", ".docx", ".odt", ".rtf", ".txt"], + }, + { + input: ".epub (E-Book)", + output: [".pdf", ".doc", ".docx", ".odt", ".rtf", ".txt"], + }, + { + input: ".xls (MS Excel)", + output: [".pdf", ".xlsx", ".ods", ".csv"], + }, + { + input: ".xlsx (MS Excel)", + output: [".pdf", ".xls", ".ods", ".csv"], + }, + { + input: ".ods (OpenDocument Spreadsheet)", + output: [".pdf", ".xls", ".xlsx", ".csv"], + }, + { + input: ".csv (Comma-Separated Values)", + output: [".pdf", ".xls", ".xlsx", ".ods"], + }, + { + input: ".ppt (MS PowerPoint)", + output: [".pdf", ".pptx", ".odp"], + }, + { + input: ".pptx (MS PowerPoint)", + output: [".pdf", ".ppt", ".odp"], + }, + { + input: ".odp (OpenDocument Presentation)", + output: [".pdf", ".ppt", ".pptx"], + }, +];