Skip to content

Commit

Permalink
fix qr code
Browse files Browse the repository at this point in the history
  • Loading branch information
ashik-75 committed Oct 14, 2023
1 parent 2da4289 commit 8ae6b61
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ui/src/components/General/ErrorComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ErrorComponent: React.FC<ErrorComponentProps> = ({
return (
<Card title={category}>
<Alert
message={"REASON"}
message={"Reason"}
description={
<>
{reasons.map((reason) => (
Expand Down
27 changes: 27 additions & 0 deletions ui/src/components/General/FallbackComponent/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Alert, Button, Card } from "antd";
import React from "react";

const FallbackComponent: React.FC = () => {
return (
<Card style={{ width: "500px" }}>
<Alert
message={"Something went wrong"}
description={
<>
<Button
onClick={() =>
window.location.assign(window.location.pathname)
}
>
Refresh
</Button>
</>
}
type="error"
showIcon
/>
</Card>
);
};

export default FallbackComponent;
3 changes: 2 additions & 1 deletion ui/src/components/Hoc/withPageTitle/withPageTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import usePageTitle from "./utils/hooks";
import { HeadProvider, Title, Link, Meta } from "react-head";
import { NO_PADDING, NO_TITLE } from "./utils/constants";
import { ErrorBoundary } from "react-error-boundary";
import FallbackComponent from "components/General/FallbackComponent";

const withPageTitle = <T extends object>(
WrappedComponent: React.ComponentType<T>
Expand Down Expand Up @@ -50,7 +51,7 @@ const withPageTitle = <T extends object>(
</div>
)}
</ErrorBoundary>
<ErrorBoundary fallback={<p>Something went wrong</p>}>
<ErrorBoundary FallbackComponent={FallbackComponent}>
<WrappedComponent {...props} />
</ErrorBoundary>
</div>
Expand Down
3 changes: 2 additions & 1 deletion ui/src/pages/Generator/QRcode/QRcode.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
}

&__output {
height: 623px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
overflow: hidden;
overflow-y: auto;
}
}
19 changes: 19 additions & 0 deletions ui/src/pages/Generator/QRcode/QRcode.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, test } from "vitest";
import QRcode from ".";

describe("QRCODE", () => {
test("render component without crash", () => {
render(<QRcode />);
});

test("textarea input providation", () => {
render(<QRcode />);

const textarea = screen.getByPlaceholderText("Enter input");

fireEvent.change(textarea, { target: { value: "Test text" } });

expect(textarea).toHaveValue("Test text");
});
});
43 changes: 43 additions & 0 deletions ui/src/pages/Generator/QRcode/components/QRCodeErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import ErrorComponent from "components/General/ErrorComponent";
import { Component, ReactNode } from "react";

type ErrorBoundaryProps = {
children: ReactNode;
};

type ErrorBoundaryState = {
hasError: boolean;
};

class QRCodeErrorBoundary extends Component<
ErrorBoundaryProps,
ErrorBoundaryState
> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { hasError: false };
}

componentDidCatch() {
this.setState({ hasError: true });
}

render() {
if (this.state.hasError) {
return (
<ErrorComponent
category="Input Error"
reasons={["Input text is too long"]}
solutions={[
"Input should be small",
"Compressing the data before generating the QR code",
"truncate or split the text and generate multiple QR Code",
]}
/>
);
}
return this.props.children;
}
}

export default QRCodeErrorBoundary;
25 changes: 15 additions & 10 deletions ui/src/pages/Generator/QRcode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { detectData } from "pages/Tools/Sorting/utils/helper";
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";

const { TextArea } = Input;

Expand Down Expand Up @@ -50,6 +52,7 @@ const QRcode: React.FC = () => {
rows={7}
onChange={(e) => setValue(e.target.value)}
data-gramm={false}
placeholder="Enter input"
allowClear
/>
</Form.Item>
Expand Down Expand Up @@ -110,23 +113,25 @@ const QRcode: React.FC = () => {
</Form.Item>
</Form>
</Card>
<Card className={style.qrcode__output}>
<Card className={classNames(style.qrcode__output, "qrcode")}>
{value.length > 0 ? (
<Space
direction="vertical"
align="center"
size={"large"}
id="myqrcode"
>
<QRCode
value={value}
color={color}
bgColor={bgColor}
bordered={bordered}
size={size}
iconSize={iconSize}
icon={icon}
/>
<QRCodeErrorBoundary>
<QRCode
value={value}
color={color}
bgColor={bgColor}
bordered={bordered}
size={size}
iconSize={iconSize}
icon={icon}
/>
</QRCodeErrorBoundary>
<DropdownDownloadButton
handleDownload={downloadQRCode}
/>
Expand Down
1 change: 1 addition & 0 deletions ui/src/styles/antd-overrides.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ h5 {
.ant-layout-content,
.notification-container,
.search_container,
.qrcode,
.ant-menu {
&::-webkit-scrollbar {
width: var(--bt-size-8);
Expand Down

0 comments on commit 8ae6b61

Please sign in to comment.