Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor: use a single switch component

thecodrr863b6ffd4e36
diff --git a/apps/web/src/components/dialogs/buydialog.js b/apps/web/src/components/dialogs/buydialog.js
index 93df4a9cb..0acb80ed3 100644
--- a/apps/web/src/components/dialogs/buydialog.js
+++ b/apps/web/src/components/dialogs/buydialog.js
@@ -7,9 +7,7 @@ import { getCouponData, upgrade } from "../../common/checkout";
import getSymbolFromCurrency from "currency-symbol-map";
import { ANALYTICS_EVENTS, trackEvent } from "../../utils/analytics";
import { navigate } from "../../navigation";
-import ReactToggle from "react-toggle";
-import "react-toggle/style.css";
-import "../properties/toggle.css";
+import Switch from "../switch";
const premiumDetails = [
{
@@ -171,28 +169,20 @@ function BuyDialog(props) {
</>
) : (
<>
- <Button
- variant="anchor"
- display="flex"
- justifyContent="center"
- alignItems="center"
- onClick={() => {
- setPlan((s) => (s === "monthly" ? "yearly" : "monthly"));
- }}
- >
+ <Flex justifyContent="center" alignItems="center">
<Text variant="body" mr={1}>
Monthly
</Text>
- <ReactToggle
- size={20}
- onChange={() => {}}
+ <Switch
checked={plan !== "monthly"}
- icons={false}
+ onClick={() => {
+ setPlan((s) => (s === "monthly" ? "yearly" : "monthly"));
+ }}
/>
<Text variant="body" ml={1}>
Yearly
</Text>
- </Button>
+ </Flex>
{coupon ? (
<Flex mb={1} alignItems="center" justifyContent="space-between">
<Flex alignItems="center" justifyContent="center">
diff --git a/apps/web/src/components/properties/toggle.js b/apps/web/src/components/properties/toggle.js
index 9c6bf3d46..aecb1a3eb 100644
--- a/apps/web/src/components/properties/toggle.js
+++ b/apps/web/src/components/properties/toggle.js
@@ -1,8 +1,6 @@
import { Flex, Text } from "rebass";
import { useStore } from "../../stores/editor-store";
-import ReactToggle from "react-toggle";
-import "react-toggle/style.css";
-import "./toggle.css";
+import Switch from "../switch";
function Toggle(props) {
const { icon: ToggleIcon, label, onToggle, toggleKey } = props;
@@ -24,7 +22,7 @@ function Toggle(props) {
<ToggleIcon size={13} sx={{ flexShrink: 0, mr: 1 }} />
{label}
</Text>
- <ReactToggle size={20} onChange={() => {}} checked={isOn} icons={false} />
+ <Switch onClick={() => onToggle(!isOn)} checked={isOn} />
</Flex>
);
}
diff --git a/apps/web/src/components/switch/index.js b/apps/web/src/components/switch/index.js
new file mode 100644
index 000000000..c50a38dd3
--- /dev/null
+++ b/apps/web/src/components/switch/index.js
@@ -0,0 +1,16 @@
+import ReactToggle from "react-toggle";
+import "react-toggle/style.css";
+import "./style.css";
+
+function Switch({ checked, onClick }) {
+ return (
+ <ReactToggle
+ size={20}
+ onChange={() => {}}
+ onClick={onClick}
+ checked={checked}
+ icons={false}
+ />
+ );
+}
+export default Switch;
diff --git a/apps/web/src/components/properties/toggle.css b/apps/web/src/components/switch/style.css
similarity index 100%
rename from apps/web/src/components/properties/toggle.css
rename to apps/web/src/components/switch/style.css
diff --git a/apps/web/src/components/toggle/index.js b/apps/web/src/components/toggle/index.js
index d431fc657..5959095b2 100644
--- a/apps/web/src/components/toggle/index.js
+++ b/apps/web/src/components/toggle/index.js
@@ -1,26 +1,25 @@
-import React from "react";
+import React, { useCallback } from "react";
import { showBuyDialog } from "../../common/dialog-controller";
import Tip from "../tip";
import { isUserPremium } from "../../hooks/use-is-user-premium";
import { Flex } from "rebass";
-import ReactToggle from "react-toggle";
-import "react-toggle/style.css";
-import "../properties/toggle.css";
+import Switch from "../switch";
function Toggle(props) {
const { title, onTip, offTip, isToggled, onToggled, onlyIf, premium } = props;
+ const onClick = useCallback(async () => {
+ if (isUserPremium() || !premium) onToggled();
+ else {
+ await showBuyDialog();
+ }
+ }, [onToggled, premium]);
if (onlyIf === false) return null;
return (
<Flex
justifyContent="space-between"
alignItems="center"
- onClick={async () => {
- if (isUserPremium() || !premium) onToggled();
- else {
- await showBuyDialog();
- }
- }}
+ onClick={onClick}
py={2}
sx={{
cursor: "pointer",
@@ -30,18 +29,7 @@ function Toggle(props) {
}}
>
<Tip text={title} tip={isToggled ? onTip : offTip} />
- <ReactToggle
- size={20}
- onClick={async () => {
- if (isUserPremium() || !premium) onToggled();
- else {
- await showBuyDialog();
- }
- }}
- onChange={() => {}}
- checked={isToggled}
- icons={false}
- />
+ <Switch onClick={onClick} checked={isToggled} />
</Flex>
);
}