Skip to content

Commit

Permalink
fix: running dynamic page should now work
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopr committed Jun 8, 2023
1 parent 089f96b commit 21734ce
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 74 deletions.
25 changes: 25 additions & 0 deletions apps/client/src/hooks/use-is-dynamic-running.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable no-return-assign */
import * as React from "react";
import { useRouter } from "next/router";
import { useSession } from "next-auth/react";

import { getRunningDynamic } from "@app/queries/useRunningDynamic";

// @see https://usehooks.com/useLockBodyScroll.
export function useIsDynamicRunning() {
const { data } = useSession();
const router = useRouter();

async function checkAndRedirect() {
if (data && data.user) {
const running = await getRunningDynamic(data.user.username);

if (running.status === "running") {
router.push("/dynamic/running");
}
}
}
React.useLayoutEffect(() => {
checkAndRedirect();
}, []);
}
28 changes: 3 additions & 25 deletions apps/client/src/pages/dynamic/acpype.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PageLoadingIndicator } from "@app/components/Loading/PageLoadingIndicat
import { SEO } from "@app/components/SEO";
import { H1 } from "@app/components/typography/headings";
import { withSSRAuth } from "@app/hocs/withSSRAuth";
import { getRunningDynamic } from "@app/queries/useRunningDynamic";
import { useIsDynamicRunning } from "@app/hooks/use-is-dynamic-running";

const ACPYPEForm = dynamic(
() => import("@app/components/Forms/ACPYPE").then((mod) => mod.ACPYPEForm),
Expand All @@ -16,32 +16,10 @@ const ACPYPEForm = dynamic(
}
);

export const getServerSideProps = withSSRAuth(async (_, session) => {
try {
if (session) {
const data = await getRunningDynamic(session.user.username);

if (data?.status === "running") {
return {
redirect: {
destination: "/dynamic/running",
permanent: false
}
};
}
}
} catch {
return {
props: {}
};
}

return {
props: {}
};
});
export const getServerSideProps = withSSRAuth();

export default function ACPYPEDynamic({ user }: { user: User }) {
useIsDynamicRunning();
const { t } = useTranslation();

return (
Expand Down
27 changes: 3 additions & 24 deletions apps/client/src/pages/dynamic/apo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PageLoadingIndicator } from "@app/components/Loading/PageLoadingIndicat
import { SEO } from "@app/components/SEO";
import { H1 } from "@app/components/typography/headings";
import { withSSRAuth } from "@app/hocs/withSSRAuth";
import { getRunningDynamic } from "@app/queries/useRunningDynamic";
import { useIsDynamicRunning } from "@app/hooks/use-is-dynamic-running";

const APOForm = dynamic(
() => import("@app/components/Forms/APO").then((mod) => mod.APOForm),
Expand All @@ -16,31 +16,10 @@ const APOForm = dynamic(
}
);

export const getServerSideProps = withSSRAuth(async (_, session) => {
try {
if (session) {
const data = await getRunningDynamic(session.user.username);

if (data?.status === "running") {
return {
redirect: {
destination: "/dynamic/running",
permanent: false
}
};
}
}
} catch {
return {
props: {}
};
}
return {
props: {}
};
});
export const getServerSideProps = withSSRAuth();

export default function APODynamic({ user }: { user: User }) {
useIsDynamicRunning();
const { t } = useTranslation();

return (
Expand Down
28 changes: 3 additions & 25 deletions apps/client/src/pages/dynamic/prodrg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PageLoadingIndicator } from "@app/components/Loading/PageLoadingIndicat
import { SEO } from "@app/components/SEO";
import { H1 } from "@app/components/typography/headings";
import { withSSRAuth } from "@app/hocs/withSSRAuth";
import { getRunningDynamic } from "@app/queries/useRunningDynamic";
import { useIsDynamicRunning } from "@app/hooks/use-is-dynamic-running";

const PRODRGForm = dynamic(
() => import("@app/components/Forms/PRODRG").then((mod) => mod.PRODRGForm),
Expand All @@ -16,32 +16,10 @@ const PRODRGForm = dynamic(
}
);

export const getServerSideProps = withSSRAuth(async (_, session) => {
try {
if (session) {
const data = await getRunningDynamic(session.user.username);

if (data?.status === "running") {
return {
redirect: {
destination: "/dynamic/running",
permanent: false
}
};
}
}
} catch {
return {
props: {}
};
}

return {
props: {}
};
});
export const getServerSideProps = withSSRAuth();

export default function PRODRGDynamic({ user }: { user: User }) {
useIsDynamicRunning();
const { t } = useTranslation();

return (
Expand Down

0 comments on commit 21734ce

Please sign in to comment.