web: ssr redirects

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-12-31 13:35:11 +01:00
parent 1ec95ca242
commit 31a4e1a277
4 changed files with 67 additions and 18 deletions

View File

@@ -13,7 +13,9 @@ import {
} from '@/components/ui/dropdown-menu';
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
import { useOrganizationParams } from '@/hooks/useOrganizationParams';
import { db } from '@/server/db';
import { createServerSideProps } from '@/server/getServerSideProps';
import { getDashboardBySlug } from '@/server/services/dashboard.service';
import type { IChartRange } from '@/types';
import { api, handleError } from '@/utils/api';
import { cn } from '@/utils/cn';
@@ -22,7 +24,27 @@ import { getRangeLabel } from '@/utils/getRangeLabel';
import { ChevronRight, MoreHorizontal, Trash } from 'lucide-react';
import Link from 'next/link';
export const getServerSideProps = createServerSideProps();
export const getServerSideProps = createServerSideProps(async (context) => {
const projectSlug = context.params?.project as string;
const dashboardSlug = context.params?.dashboard as string;
try {
await db.dashboard.findFirstOrThrow({
select: {
id: true,
},
where: {
slug: dashboardSlug,
project: {
slug: projectSlug,
},
},
});
} catch (error) {
return {
notFound: true,
};
}
});
export default function Dashboard() {
const params = useOrganizationParams();

View File

@@ -3,14 +3,30 @@ import { Container } from '@/components/Container';
import { MainLayout } from '@/components/layouts/MainLayout';
import { PageTitle } from '@/components/PageTitle';
import { useOrganizationParams } from '@/hooks/useOrganizationParams';
import { useRefetchActive } from '@/hooks/useRefetchActive';
import { pushModal } from '@/modals';
import { db } from '@/server/db';
import { createServerSideProps } from '@/server/getServerSideProps';
import { api, handleError } from '@/utils/api';
import { Pencil, Plus, Trash } from 'lucide-react';
import Link from 'next/link';
export const getServerSideProps = createServerSideProps();
export const getServerSideProps = createServerSideProps(async (context) => {
const projectSlug = context.params?.project as string;
try {
await db.project.findFirstOrThrow({
select: {
id: true,
},
where: {
slug: projectSlug,
},
});
} catch (error) {
return {
notFound: true,
};
}
});
export default function Home() {
const params = useOrganizationParams();

View File

@@ -1,22 +1,9 @@
import { useEffect } from 'react';
import { MainLayout } from '@/components/layouts/MainLayout';
import { createServerSideProps } from '@/server/getServerSideProps';
import { api } from '@/utils/api';
import { useRouter } from 'next/router';
export const getServerSideProps = createServerSideProps();
export default function Home() {
const router = useRouter();
const query = api.organization.first.useQuery();
const organization = query.data ?? null;
useEffect(() => {
if (organization) {
router.replace(`/${organization.slug}`);
}
}, [organization, router]);
return (
<MainLayout>
<div />

View File

@@ -21,7 +21,7 @@ export function createServerSideProps(
}
if (context.params?.organization) {
const organization = await db.user.findFirst({
const user = await db.user.findFirst({
where: {
id: session.user.id,
organization: {
@@ -30,11 +30,35 @@ export function createServerSideProps(
},
});
if (!organization) {
if (!user) {
return {
notFound: true,
};
}
} else {
const user = await db.user.findFirst({
where: {
id: session.user.id,
},
include: {
organization: true,
},
});
if (!user) {
return {
notFound: true,
};
}
if (user.organization) {
return {
redirect: {
destination: `/${user.organization.slug}`,
permanent: false,
},
};
}
}
const res = await (typeof cb === 'function'