mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 07:03:25 +00:00
Ajout de PrismaDB pour les catégories + liens
This commit is contained in:
@@ -5,11 +5,11 @@ export default function Categories({ categories, favorites, handleSelectCategory
|
|||||||
<div className={`${styles['block-wrapper']} ${styles['favorites']}`}>
|
<div className={`${styles['block-wrapper']} ${styles['favorites']}`}>
|
||||||
<h4>Favoris</h4>
|
<h4>Favoris</h4>
|
||||||
<ul className={styles['items']}>
|
<ul className={styles['items']}>
|
||||||
{favorites.map(({ name, link, category }, key) => {
|
{favorites.map((link, key) => {
|
||||||
const catName = categories.find(c => c.id === category).name;
|
const { name, url, categoryName } = link;
|
||||||
return <li key={key} className={styles['item']}>
|
return <li key={key} className={styles['item']}>
|
||||||
<a href={link} target={'_blank'} rel={'noreferrer'}>
|
<a href={url} target={'_blank'} rel={'noreferrer'}>
|
||||||
{name} <span className={styles['category']}>- {catName}</span>
|
{name} <span className={styles['category']}>- {categoryName}</span>
|
||||||
</a>
|
</a>
|
||||||
</li>;
|
</li>;
|
||||||
})}
|
})}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import styles from '../../styles/links.module.scss';
|
|||||||
export default function Link({ link }) {
|
export default function Link({ link }) {
|
||||||
return (
|
return (
|
||||||
<li className={styles['link']}>
|
<li className={styles['link']}>
|
||||||
<a href={link?.link} target={'_blank'} rel={'noreferrer'}>
|
<a href={link?.url} target={'_blank'} rel={'noreferrer'}>
|
||||||
<span className={styles['link-name']}>{link?.name}</span> <span className={styles['link-url']}>{link?.link}</span>
|
<span className={styles['link-name']}>{link?.name} <span className={styles['link-category']}>— {link?.categoryName}</span></span> <span className={styles['link-url']}>{link?.url}</span>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
"lint": "next lint"
|
"lint": "next lint"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@prisma/client": "^3.7.0",
|
||||||
"next": "12.0.7",
|
"next": "12.0.7",
|
||||||
"react": "17.0.2",
|
"react": "17.0.2",
|
||||||
"react-dom": "17.0.2",
|
"react-dom": "17.0.2",
|
||||||
@@ -16,6 +17,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "7",
|
"eslint": "7",
|
||||||
"eslint-config-next": "12.0.7"
|
"eslint-config-next": "12.0.7",
|
||||||
|
"prisma": "^3.7.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
import styles from '../styles/Home.module.scss';
|
import { createRef, useRef, useState } from 'react';
|
||||||
|
|
||||||
import Categories from '../components/Categories/Categories';
|
import Categories from '../components/Categories/Categories';
|
||||||
import Links from '../components/Links/Links';
|
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 }) {
|
export default function Home({ categories, favorites }) {
|
||||||
const [categoryActive, setCategoryActive] = useState(categories?.[0]?.id);
|
const [categoryActive, setCategoryActive] = useState(categories?.[0]?.id);
|
||||||
@@ -36,42 +40,30 @@ export default function Home({ categories, favorites }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getStaticProps(context) {
|
export async function getStaticProps(context) {
|
||||||
const categories = [];
|
const categories = await prisma.category.findMany({
|
||||||
for (let i = 0; i < 20; i++) {
|
include: {
|
||||||
const links = [];
|
links: true
|
||||||
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}`
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
categories.push({
|
|
||||||
id: i,
|
|
||||||
name: 'Catégorie #' + (i + 1),
|
|
||||||
links,
|
|
||||||
ref: createRef()
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
const favorites = [];
|
const favorites = [];
|
||||||
for (let i = 0; i < 5; i++) {
|
categories.map((category) => {
|
||||||
const category = categories[Math.floor(Math.random() * categories.length)];
|
category['ref'] = createRef();
|
||||||
const link = category.links[Math.floor(Math.random() * category.links.length)]
|
category['links'] = category.links.map((link) => {
|
||||||
|
if (link.favorite)
|
||||||
favorites.push(link);
|
favorites.push(link);
|
||||||
}
|
|
||||||
|
|
||||||
console.log(favorites);
|
link['categoryName'] = category.name;
|
||||||
|
return link;
|
||||||
|
});
|
||||||
|
|
||||||
|
return category;
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
categories,
|
categories: JSON.parse(JSON.stringify(categories)),
|
||||||
favorites
|
favorites: JSON.parse(JSON.stringify(favorites))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function randomIntFromInterval(min, max) {
|
|
||||||
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
||||||
}
|
|
||||||
@@ -6,12 +6,35 @@ generator client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
datasource db {
|
datasource db {
|
||||||
provider = "postgresql"
|
provider = "mysql"
|
||||||
url = env("DATABASE_URL")
|
url = env("DATABASE_URL")
|
||||||
}
|
}
|
||||||
|
|
||||||
model user {
|
model User {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
email String @unique
|
email String @unique
|
||||||
password String
|
password String
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
}
|
||||||
|
|
||||||
|
model Category {
|
||||||
|
id Int @id @default(autoincrement())
|
||||||
|
name String @unique
|
||||||
|
links Link[]
|
||||||
|
order Int
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
}
|
||||||
|
|
||||||
|
model Link {
|
||||||
|
id Int @id @default(autoincrement())
|
||||||
|
name String
|
||||||
|
url String
|
||||||
|
category Category @relation(fields: [categoryId], references: [id])
|
||||||
|
categoryId Int
|
||||||
|
order Int
|
||||||
|
favorite Boolean @default(false)
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@
|
|||||||
& .items .item {
|
& .items .item {
|
||||||
user-select: none;
|
user-select: none;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
padding: 10px;
|
padding: 5px 10px;
|
||||||
border: 1px solid #dadce0;
|
border: 1px solid #dadce0;
|
||||||
border-bottom: 2px solid #dadce0;
|
border-bottom: 2px solid #dadce0;
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
|
|||||||
@@ -39,16 +39,25 @@
|
|||||||
margin-bottom: 10px;
|
margin-bottom: 10px;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
transition: .15s;
|
transition: .05s;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
border-bottom-color: #3f88c5;
|
border-bottom-color: #3f88c5;
|
||||||
transform: scale(1.01);
|
transform: scale(1.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
& .link-url {
|
& .link-name .link-category {
|
||||||
color: #bbb;
|
color: #bbb;
|
||||||
font-size: .80em;
|
font-size: .9em;
|
||||||
|
}
|
||||||
|
|
||||||
|
& .link-url {
|
||||||
|
width: 100%;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
overflow: hidden;
|
||||||
|
color: #bbb;
|
||||||
|
font-size: .8em;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user