From d7335b3364c3927f9303e1e8446fff137b19f27b Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Thu, 5 Sep 2024 11:12:44 +0530 Subject: [PATCH] Refactor to use make zoom functionallity reusable --- src/CAREUI/interactive/Zoom.tsx | 97 ++++++++++++++++++++++++++++++++ src/CAREUI/misc/PrintPreview.tsx | 71 ++++++++--------------- src/Locale/en/Common.json | 3 +- 3 files changed, 121 insertions(+), 50 deletions(-) create mode 100644 src/CAREUI/interactive/Zoom.tsx diff --git a/src/CAREUI/interactive/Zoom.tsx b/src/CAREUI/interactive/Zoom.tsx new file mode 100644 index 00000000000..bf5747f9b91 --- /dev/null +++ b/src/CAREUI/interactive/Zoom.tsx @@ -0,0 +1,97 @@ +import { createContext, ReactNode, useContext, useState } from "react"; +import ButtonV2 from "../../Components/Common/components/ButtonV2"; +import CareIcon from "../icons/CareIcon"; + +type ProviderValue = { + scale: number; + zoomIn: () => void; + zoomOut: () => void; +}; + +const ZoomContext = createContext(null); + +type Props = { + initialScale?: number; + scaleRatio?: number; + children: ReactNode; +}; + +export const ZoomProvider = ({ + initialScale = 1, + scaleRatio = 1.25, + children, +}: Props) => { + const [scale, setScale] = useState(initialScale); + + return ( + setScale((scale) => scale * scaleRatio), + zoomOut: () => setScale((scale) => scale / scaleRatio), + }} + > + {children} + + ); +}; + +export const ZoomTransform = (props: { + children: ReactNode; + className?: string; +}) => { + const ctx = useContext(ZoomContext); + + if (ctx == null) { + throw new Error("Component must be used with ZoomProvider"); + } + + return ( +
+ {props.children} +
+ ); +}; + +export const ZoomControls = (props: { disabled?: boolean }) => { + const ctx = useContext(ZoomContext); + + if (ctx == null) { + throw new Error("Component must be used with ZoomProvider"); + } + + return ( +
+ + + + + {Math.round(ctx.scale * 100)}% + + + + +
+ ); +}; diff --git a/src/CAREUI/misc/PrintPreview.tsx b/src/CAREUI/misc/PrintPreview.tsx index ad0e0983bf7..a6a9250e4ec 100644 --- a/src/CAREUI/misc/PrintPreview.tsx +++ b/src/CAREUI/misc/PrintPreview.tsx @@ -1,9 +1,11 @@ -import { ReactNode, useState } from "react"; +import { ReactNode } from "react"; import ButtonV2 from "../../Components/Common/components/ButtonV2"; import CareIcon from "../icons/CareIcon"; import { classNames } from "../../Utils/utils"; import Page from "../../Components/Common/components/Page"; import useBreakpoints from "../../Common/hooks/useBreakpoints"; +import { useTranslation } from "react-i18next"; +import { ZoomControls, ZoomProvider, ZoomTransform } from "../interactive/Zoom"; type Props = { children: ReactNode; @@ -14,60 +16,31 @@ type Props = { export default function PrintPreview(props: Props) { const normalScale = useBreakpoints({ default: 0.44, md: 1 }); - const [scale, setScale] = useState(normalScale); + const { t } = useTranslation(); return ( -
-
- print()}> - - Print - -
- -
-
- {props.children} + +
+
+ + + {t("print")} +
-
-
- setScale((scale) => scale * 1.25)} - > - - - - {Math.round(scale * 100)}% - - setScale((scale) => scale / 1.25)} - > - - + +
+ {props.children} +
+
+ +
-
+ ); } diff --git a/src/Locale/en/Common.json b/src/Locale/en/Common.json index 24855e05d70..0d021ba2f9e 100644 --- a/src/Locale/en/Common.json +++ b/src/Locale/en/Common.json @@ -66,6 +66,7 @@ "contact_person_number": "Contact person number", "referral_letter": "Referral Letter", "close": "Close", + "print": "Print", "print_referral_letter": "Print Referral Letter", "date_of_positive_covid_19_swab": "Date of Positive Covid 19 Swab", "patient_no": "OP/IP No", @@ -201,4 +202,4 @@ "oxygen_information": "Oxygen Information", "deleted_successfully": "{{name}} deleted successfully", "delete_item": "Delete {{name}}" -} +} \ No newline at end of file