feat: new dropdown menu for output file format selection + dynamic rendering of the information table

This commit is contained in:
theoleuthardt 2025-02-16 17:53:19 +01:00
parent 43abb6d5fd
commit f149472975
3 changed files with 193 additions and 144 deletions

View file

@ -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<File | null>(null);
const [downloadUrl, setDownloadUrl] = useState<string>("");
const [loading, setLoading] = useState(false);
const [tableOpen, setTableOpen] = useState(false);
const [filteredOptions, setFilteredOptions] = useState<string[]>([]);
const [selectedOutputFormat, setSelectedOutputFormat] = useState<string | "">(
"",
);
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
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() {
<Navbar renderHomeLink={true} />
<div className="w-screen h-auto min-h-[calc(100vh-80px-60px)] flex flex-1 flex-col items-center justify-center">
<h2 className="text-5xl font-bold text-white mb-16">doc-converter</h2>
<div className="h-36 flex flex-row items-center justify-center gap-20">
<div className="h-36 flex flex-row items-center justify-center gap-4">
<input
type="file"
className="h-14 border-2 border-white p-3 rounded-xl text-center text-white"
className="h-16 border-2 border-white p-3 rounded-xl text-2xl text-center text-white"
id="documentUpload"
onChange={handleFileChange}
/>
{/* TODO: Fix Dropdown menu placement */}
<Dropdown content="output format" className="h-14" />
<Dropdown
content={
file
? `Convert to: ${selectedOutputFormat}`
: "output file format"
}
options={filteredOptions.length > 0 ? filteredOptions : [""]}
onClick={(event) => {
const selectedFormat =
event.currentTarget.textContent?.trim() || "";
setSelectedOutputFormat(selectedFormat);
}}
/>
</div>
<div className={"flex flex-row items-center gap-4 mt-4 mb-16"}>
{downloadUrl ? (
@ -121,108 +158,16 @@ export default function DocConverter() {
>
<table className="w-full h-auto text-left border-collapse border-white">
<tbody>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.doc (MS Word)
</td>
<td className="min-w-96 px-6 py-4">
.pdf, .docx, .odt, .txt, .rtf, .html, .epub
</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.docx (MS Word)
</td>
<td className="min-w-96 px-6 py-4">
.pdf, .odt, .txt, .rtf, .html, .epub
</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.odt (OpenDocument Text)
</td>
<td className="min-w-96 px-6 py-4">
.pdf, .doc, .docx, .txt, .rtf, .html, .epub
</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.rtf (Rich Text Format)
</td>
<td className="min-w-96 px-6 py-4">
.pdf, .doc, .docx, .odt, .txt, .html
</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">.txt (Text)</td>
<td className="min-w-96 px-6 py-4">
.pdf, .doc, .docx, .odt, .txt, .html
</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.html (Webseite)
</td>
<td className="min-w-96 px-6 py-4">
.pdf, .doc, .docx, .odt, .rtf, .txt
</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.epub (E-Book)
</td>
<td className="min-w-96 px-6 py-4">
.pdf, .doc, .docx, .odt, .rtf, .txt
</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.xls (MS Excel)
</td>
<td className="min-w-96 px-6 py-4">
.pdf, .xlsx, .ods, .csv
</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.xlsx (MS Excel)
</td>
<td className="min-w-96 px-6 py-4">.pdf, .xls, .ods, .csv</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.ods (OpenDocument Spreadsheet)
</td>
<td className="min-w-96 px-6 py-4">
.pdf, .xls, .xlsx, .csv
</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.csv (Comma-Separated Values)
</td>
<td className="min-w-96 px-6 py-4">
.pdf, .xls, .xlsx, .ods
</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.ppt (MS PowerPoint)
</td>
<td className="min-w-96 px-6 py-4">.pdf, .pptx, .odp</td>
</tr>
<tr className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
.pptx (MS PowerPoint)
</td>
<td className="min-w-96 px-6 py-4">.pdf, .ppt, .odp</td>
</tr>
<tr className="pb-10">
<td className="min-w-96 px-6 py-4 border-r">
.odp (OpenDocument Presentation)
</td>
<td className="min-w-96 px-6 py-4">.pdf, .ppt, .pptx</td>
</tr>
{FileFormatsTable.map((format) => (
<tr key={format.input} className="border-b">
<td className="min-w-96 px-6 py-4 border-r">
{format.input}
</td>
<td className="min-w-96 px-6 py-4">
{format.output.join(", ")}
</td>
</tr>
))}
</tbody>
</table>
</div>

View file

@ -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<HTMLAnchorElement>) => void;
}
const Dropdown = (props: DropdownProps) => {
@ -17,16 +16,17 @@ const Dropdown = (props: DropdownProps) => {
setIsOpen(!isOpen);
};
const closeDropdown = () => {
const closeDropdown = (event: React.MouseEvent<HTMLAnchorElement>) => {
setIsOpen(false);
if (props.onClick) props.onClick(event);
};
return (
<div className={`w-full py-6 pb-8 ${props.className}`}>
<div className="relative inline-block">
<div className={`w-full ${props.className}`}>
<div className="relative">
<button
type="button"
className="px-4 py-2 text-white bg-black rounded-lg text-2xl inline-flex items-center
className="h-16 px-4 py-2 text-white bg-black rounded-lg text-2xl inline-flex items-center
border-white border-2 hover:text-blue-400 hover:border-blue-400"
onClick={toggleDropdown}
>
@ -48,41 +48,27 @@ const Dropdown = (props: DropdownProps) => {
</svg>
</button>
<div
className={`origin-top-right absolute right-0 mt-1 rounded-lg shadow-lg ring-1 ring-white
ring-opacity-5 border-2 border-white transition-all duration-500 ease-in-out ${isOpen ? "opacity-100" : "opacity-0"}`}
className={`absolute left-0 mt-2 w-full rounded-lg shadow-lg ring-1 ring-white
ring-opacity-5 border-2 border-white transition-all duration-500 ease-in-out text-center
${isOpen ? "opacity-100 scale-100 visible" : "opacity-0 scale-95 invisible"}`}
>
<ul
role="menu"
aria-orientation="vertical"
aria-labelledby="options-menu"
className="max-h-[164px] w-full overflow-y-auto"
>
<li>
<Link
href="#"
className="block px-4 py-2 rounded-lg hover:text-blue-400 hover:border-2 hover:border-blue-400"
onClick={closeDropdown}
>
Option 1
</Link>
</li>
<li>
<Link
href="#"
className="block px-4 py-2 rounded-lg hover:text-blue-400 hover:border-2 hover:border-blue-400"
onClick={closeDropdown}
>
Option 2
</Link>
</li>
<li>
<Link
href="#"
className="block px-4 py-2 rounded-lg hover:text-blue-400 hover:border-2 hover:border-blue-400"
onClick={closeDropdown}
>
Option 3
</Link>
</li>
{props.options?.map((option) => (
<li key={option}>
<Link
href="#"
className="block px-4 py-2 rounded-lg hover:text-blue-400 hover:border-2 hover:border-blue-400"
onClick={closeDropdown}
>
{option}
</Link>
</li>
))}
</ul>
</div>
</div>

View file

@ -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"],
},
];