refactor: use data retrieval functions

This commit is contained in:
Sonny
2023-06-03 20:14:54 +02:00
parent a6f32e852c
commit 35829522c3
7 changed files with 33 additions and 97 deletions

2
src/constants/url.ts Normal file
View File

@@ -0,0 +1,2 @@
export const VALID_URL_REGEX =
/^(?:http(s)?:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.\%]+$/;

View File

@@ -13,10 +13,10 @@ import PATHS from "constants/paths";
import useModal from "hooks/useModal";
import { Category, Link, SearchItem } from "types";
import { BuildCategory } from "utils/front";
import getUserCategories from "lib/category/getUserCategories";
import getUser from "lib/user/getUser";
import { pushStateVanilla } from "utils/link";
import prisma from "utils/prisma";
import { getSessionOrThrow } from "utils/session";
import { getSession } from "utils/session";
interface HomePageProps {
categories: Category[];
@@ -187,29 +187,12 @@ function Home(props: HomePageProps) {
}
export async function getServerSideProps({ req, res, query }) {
const session = await getSessionOrThrow(req, res);
const session = await getSession(req, res);
const queryCategoryId = (query?.categoryId as string) || "";
const user = await prisma.user.findFirstOrThrow({
where: {
email: session.user.email,
},
});
const categoriesDB = await prisma.category.findMany({
include: {
links: {
where: {
authorId: user.id,
},
},
author: true,
},
where: {
authorId: user.id,
},
});
if (categoriesDB.length === 0) {
const user = await getUser(session);
const categories = await getUserCategories(user);
if (categories.length === 0) {
return {
redirect: {
destination: PATHS.CATEGORY.CREATE,
@@ -217,11 +200,9 @@ export async function getServerSideProps({ req, res, query }) {
};
}
const categories = categoriesDB.map(BuildCategory);
const currentCategory = categories.find(
({ id }) => id === Number(queryCategoryId)
);
return {
props: {
categories: JSON.parse(JSON.stringify(categories)),

View File

@@ -10,9 +10,11 @@ import Selector from "components/Selector";
import TextBox from "components/TextBox";
import useAutoFocus from "hooks/useAutoFocus";
import getUserCategories from "lib/category/getUserCategories";
import getUser from "lib/user/getUser";
import { Category, Link } from "types";
import { BuildCategory, HandleAxiosError, IsValidURL } from "utils/front";
import prisma from "utils/prisma";
import { HandleAxiosError, IsValidURL } from "utils/front";
import { getSession } from "utils/session";
import styles from "styles/create.module.scss";
@@ -110,14 +112,11 @@ function CreateLink({ categories }: { categories: Category[] }) {
CreateLink.authRequired = true;
export default CreateLink;
export async function getServerSideProps() {
const categoriesDB = await prisma.category.findMany({
include: { author: true },
});
const categories = categoriesDB.map((categoryDB) =>
BuildCategory(categoryDB)
);
export async function getServerSideProps({ req, res }) {
const session = await getSession(req, res);
const user = await getUser(session);
const categories = await getUserCategories(user);
if (categories.length === 0) {
return {
redirect: {

View File

@@ -8,9 +8,11 @@ import FormLayout from "components/FormLayout";
import PageTransition from "components/PageTransition";
import TextBox from "components/TextBox";
import getUserLink from "lib/link/getUserLink";
import getUser from "lib/user/getUser";
import { Link } from "types";
import { BuildLink, HandleAxiosError } from "utils/front";
import prisma from "utils/prisma";
import { HandleAxiosError } from "utils/front";
import { getSession } from "utils/session";
import styles from "styles/create.module.scss";
@@ -94,14 +96,14 @@ function RemoveLink({ link }: { link: Link }) {
RemoveLink.authRequired = true;
export default RemoveLink;
export async function getServerSideProps({ query }) {
export async function getServerSideProps({ req, res, query }) {
const { lid } = query;
const linkDB = await prisma.link.findFirst({
where: { id: Number(lid) },
include: { category: true, author: true },
});
if (!linkDB) {
const session = await getSession(req, res);
const user = await getUser(session);
const link = await getUserLink(user, Number(lid));
if (!link) {
return {
redirect: {
destination: "/",
@@ -109,10 +111,6 @@ export async function getServerSideProps({ query }) {
};
}
const link = BuildLink(linkDB, {
categoryId: linkDB.categoryId,
categoryName: linkDB.category.name,
});
return {
props: {
link: JSON.parse(JSON.stringify(link)),

View File

@@ -1,4 +1,3 @@
import { getServerSession } from "next-auth/next";
import { Provider } from "next-auth/providers";
import { getProviders, signIn } from "next-auth/react";
import { NextSeo } from "next-seo";
@@ -6,9 +5,9 @@ import Link from "next/link";
import MessageManager from "components/MessageManager/MessageManager";
import PATHS from "constants/paths";
import { getSession } from "utils/session";
import styles from "styles/login.module.scss";
import { authOptions } from "./api/auth/[...nextauth]";
interface SignInProps {
providers: Provider[];
@@ -39,7 +38,7 @@ export default function SignIn({ providers }: SignInProps) {
}
export async function getServerSideProps({ req, res }) {
const session = await getServerSession(req, res, authOptions);
const session = await getSession(req, res);
if (session) {
return {
redirect: {

2
src/types.d.ts vendored
View File

@@ -1,5 +1,7 @@
import { User } from "@prisma/client";
// TODO: extend @prisma/client type with Link[] instead of
// recreate interface (same for Link)
export interface Category {
id: number;
name: string;

View File

@@ -1,53 +1,8 @@
import axios from "axios";
import { Category, Link } from "types";
export function BuildCategory({
id,
name,
authorId,
author,
links = [],
createdAt,
updatedAt,
}): Category {
return {
id,
name,
links: links.map((link) =>
BuildLink(link, { categoryId: id, categoryName: name })
),
authorId,
author,
createdAt,
updatedAt,
};
}
export function BuildLink(
{ id, name, url, authorId, author, favorite, createdAt, updatedAt },
{ categoryId, categoryName }
): Link {
return {
id,
name,
url,
category: {
id: categoryId,
name: categoryName,
},
authorId,
author,
favorite,
createdAt,
updatedAt,
};
}
import { VALID_URL_REGEX } from "constants/url";
export function IsValidURL(url: string): boolean {
const regex = new RegExp(
/^(?:http(s)?:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.\%]+$/
);
const regex = new RegExp(VALID_URL_REGEX);
return url.match(regex) ? true : false;
}