Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update QRcode file uploader #367

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 60 additions & 27 deletions ui/src/pages/Generator/QRcode/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { Card, Form, QRCode, Input, Badge, Space, Checkbox } from "antd";
import {
Card,
Form,
QRCode,
Input,
Badge,
Space,
Checkbox,
Upload,
Button,
} from "antd";
import PageGrid from "components/Layouts/PageGrid";
import React, { useEffect, useState } from "react";
import { downloadQRCode } from "./utils/helper";
Expand All @@ -7,7 +17,6 @@ import DropdownDownloadButton from "components/General/DropdownDownloadButton";
import Warning from "components/General/Warning";
import ColorPickerWithInput from "components/General/ColorPickerWithInput";
import { ResponsiveInputWithLabel } from "components/General/FormComponents";
import { handleImageUpload } from "utils/helper-functions/files";
import QRCodeErrorBoundary from "./components/QRCodeErrorBoundary";
import { classNames } from "utils/helper-functions/string";
import { DataDetection } from "utils/helper-classes/ DataDetection";
Expand All @@ -31,6 +40,18 @@ const QRcode: React.FC = () => {
setDataType(detection.detect());
}, [value]);

const handleUpload = (file: File) => {
const reader = new FileReader();

reader.onload = () => {
if (typeof reader.result === "string") {
setIcon(reader.result);
}
};

reader.readAsDataURL(file);
};

return (
<PageGrid>
<Card>
Expand Down Expand Up @@ -59,15 +80,7 @@ const QRcode: React.FC = () => {
allowClear
/>
</Form.Item>
<Form.Item tooltip="Add border to QR code">
<Checkbox
type="checkbox"
value="Border"
onChange={(e) => setBordered(e.target.checked)}
>
Border
</Checkbox>
</Form.Item>

<PageGrid>
<ColorPickerWithInput
value={color}
Expand Down Expand Up @@ -95,25 +108,45 @@ const QRcode: React.FC = () => {
/>
</Form.Item>

<Form.Item>
<ResponsiveInputWithLabel
label="Icon Size"
placeholder="Height"
value={iconSize}
onChange={(val) => val && setIconSize(val)}
min={0}
type="number"
/>
<Form.Item
label="Show Border"
tooltip="Add border to QR code"
>
<Checkbox
type="checkbox"
value="Border"
onChange={(e) => setBordered(e.target.checked)}
>
Border
</Checkbox>
</Form.Item>
</PageGrid>

<Form.Item label="Upload Iocn">
<Input
type="file"
accept="image/*"
onChange={(e) => handleImageUpload(e, setIcon)}
/>
</Form.Item>
<PageGrid>
<Form.Item label="Icon">
<Upload
customRequest={({ onSuccess }) => {
onSuccess && onSuccess("OK");
}}
beforeUpload={handleUpload}
listType="picture"
>
<Button disabled={false}>Upload </Button>
</Upload>
</Form.Item>
{icon && (
<Form.Item>
<ResponsiveInputWithLabel
label="Icon Size"
placeholder="Height"
value={iconSize}
onChange={(val) => val && setIconSize(val)}
min={0}
type="number"
/>
</Form.Item>
)}
</PageGrid>
</Form>
</Card>
<Card className={classNames(style.qrcode__output, "qrcode")}>
Expand Down
22 changes: 0 additions & 22 deletions ui/src/utils/helper-functions/files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { saveAs } from "file-saver";
import { marked } from "marked";
import { ChangeEvent } from "react";

const downloadFile = (fileContent: string, fileName: string, type: string) => {
saveAs(
Expand All @@ -26,26 +25,6 @@ const downloadTextFile = (fileContent: string, fileName: string) => {
downloadFile(fileContent, fileName, "text/plain;charset=utf-8");
};

type ImageCallback = (dataUrl: string) => void;

const handleImageUpload = (
e: ChangeEvent<HTMLInputElement>,
callback: ImageCallback
) => {
if (e.target.files && e.target.files.length > 0) {
const file = e.target.files[0];
const reader = new FileReader();

reader.onload = () => {
if (typeof reader.result === "string") {
callback(reader.result);
}
};

reader.readAsDataURL(file);
}
};

const getFileExtension = (fileName: string) => {
const ext = fileName.split(".").pop();
if (ext) {
Expand All @@ -66,7 +45,6 @@ export {
downloadFile,
downloadPDFFile,
downloadTextFile,
handleImageUpload,
getFileExtension,
removeFileExtension,
};
Loading