Browse Source

implement premium validation

AykutSarac 2 years ago
parent
commit
fce3e3c9b8
3 changed files with 20 additions and 4 deletions
  1. 1 0
      src/containers/Home/index.tsx
  2. 12 4
      src/pages/widget.tsx
  3. 7 0
      src/services/db/widget.ts

+ 1 - 0
src/containers/Home/index.tsx

@@ -134,6 +134,7 @@ const PreviewSection = () => {
         id="jcPreview"
         src={`${baseURL}/widget?json=63b73305c358219fbc421adf`}
         loading="eager"
+        data-partner="true"
       />
     </Styles.StyledPreviewSection>
   );

+ 12 - 4
src/pages/widget.tsx

@@ -4,10 +4,11 @@ import { useRouter } from "next/router";
 import toast from "react-hot-toast";
 import { baseURL } from "src/constants/data";
 import { darkTheme, lightTheme } from "src/constants/theme";
+import { Tools } from "src/containers/Editor/LiveEditor/Tools";
+import { getPartnerStatus } from "src/services/db/widget";
 import useGraph from "src/store/useGraph";
 import useJson from "src/store/useJson";
 import styled, { ThemeProvider } from "styled-components";
-import { Tools } from "src/containers/Editor/LiveEditor/Tools";
 
 const GraphCanvas = dynamic(
   () => import("src/containers/Editor/LiveEditor/GraphCanvas").then(c => c.GraphCanvas),
@@ -51,11 +52,16 @@ interface EmbedMessage {
 const WidgetPage = () => {
   const { query, push, isReady } = useRouter();
   const [theme, setTheme] = React.useState("dark");
+  const [isPremium, setIsPremium] = React.useState(false);
   const fetchJson = useJson(state => state.fetchJson);
   const setGraph = useGraph(state => state.setGraph);
 
   React.useEffect(() => {
     if (isReady) {
+      if (window.frameElement?.getAttribute("data-partner")) {
+        getPartnerStatus().then(r => r.data?.premium && setIsPremium(!!r.data.premium));
+      }
+
       fetchJson(query.json);
       if (!inIframe()) push("/");
     }
@@ -84,9 +90,11 @@ const WidgetPage = () => {
     <ThemeProvider theme={theme === "dark" ? darkTheme : lightTheme}>
       <Tools isWidget />
       <GraphCanvas isWidget />
-      <StyledAttribute href={`${baseURL}/editor`} target="_blank" rel="noreferrer">
-        jsoncrack.com
-      </StyledAttribute>
+      {!isPremium && (
+        <StyledAttribute href={`${baseURL}/editor`} target="_blank" rel="noreferrer">
+          jsoncrack.com
+        </StyledAttribute>
+      )}
     </ThemeProvider>
   );
 };

+ 7 - 0
src/services/db/widget.ts

@@ -0,0 +1,7 @@
+import { altogic, AltogicResponse } from "src/api/altogic";
+
+const getPartnerStatus = async (): Promise<AltogicResponse<{ premium: boolean }>> => {
+  return await altogic.endpoint.get("/partner");
+};
+
+export { getPartnerStatus };