Ajout de PrismaDB pour les catégories + liens

This commit is contained in:
Sonny
2022-01-09 23:45:27 +01:00
parent 877b3d3806
commit 568b56dff0
8 changed files with 2399 additions and 2313 deletions

View File

@@ -1,8 +1,12 @@
import styles from '../styles/Home.module.scss';
import { createRef, useRef, useState } from 'react';
import Categories from '../components/Categories/Categories';
import Links from '../components/Links/Links';
import { createRef, useRef, useState } from 'react';
import styles from '../styles/Home.module.scss';
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
export default function Home({ categories, favorites }) {
const [categoryActive, setCategoryActive] = useState(categories?.[0]?.id);
@@ -36,42 +40,30 @@ export default function Home({ categories, favorites }) {
}
export async function getStaticProps(context) {
const categories = [];
for (let i = 0; i < 20; i++) {
const links = [];
for (let y = 0; y < randomIntFromInterval(5, 25); y++) {
links.push({
id: y,
name: 'Lien #' + (y + 1),
category: i,
link: `https://google.com/search?q=${y}`
});
const categories = await prisma.category.findMany({
include: {
links: true
}
categories.push({
id: i,
name: 'Catégorie #' + (i + 1),
links,
ref: createRef()
});
}
});
const favorites = [];
for (let i = 0; i < 5; i++) {
const category = categories[Math.floor(Math.random() * categories.length)];
const link = category.links[Math.floor(Math.random() * category.links.length)]
favorites.push(link);
}
categories.map((category) => {
category['ref'] = createRef();
category['links'] = category.links.map((link) => {
if (link.favorite)
favorites.push(link);
link['categoryName'] = category.name;
return link;
});
return category;
});
console.log(favorites);
return {
props: {
categories,
favorites
categories: JSON.parse(JSON.stringify(categories)),
favorites: JSON.parse(JSON.stringify(favorites))
}
}
}
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}