mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 22:53:25 +00:00
feat: add category visibility
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `category` ADD COLUMN `visibility` ENUM('private', 'public') NOT NULL DEFAULT 'private';
|
||||||
@@ -29,10 +29,11 @@ model User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model Category {
|
model Category {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
name String @db.VarChar(255)
|
name String @db.VarChar(255)
|
||||||
description String? @db.VarChar(255)
|
description String? @db.VarChar(255)
|
||||||
links Link[]
|
links Link[]
|
||||||
|
visibility Visibility @default(private)
|
||||||
|
|
||||||
author User @relation(fields: [authorId], references: [id])
|
author User @relation(fields: [authorId], references: [id])
|
||||||
authorId Int
|
authorId Int
|
||||||
@@ -63,3 +64,8 @@ model Link {
|
|||||||
|
|
||||||
@@map("link")
|
@@map("link")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Visibility {
|
||||||
|
private
|
||||||
|
public
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
"name": "Category name",
|
"name": "Category name",
|
||||||
"description": "Category description",
|
"description": "Category description",
|
||||||
"no-description": "No description",
|
"no-description": "No description",
|
||||||
|
"visibility": "Public",
|
||||||
"create": "Create a category",
|
"create": "Create a category",
|
||||||
"edit": "Edit a category",
|
"edit": "Edit a category",
|
||||||
"remove": "Delete a category",
|
"remove": "Delete a category",
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
"category": "Catégorie",
|
"category": "Catégorie",
|
||||||
"name": "Nom de la catégorie",
|
"name": "Nom de la catégorie",
|
||||||
"description": "Description de la catégorie",
|
"description": "Description de la catégorie",
|
||||||
|
"visibility": "Public",
|
||||||
"no-description": "Aucune description",
|
"no-description": "Aucune description",
|
||||||
"create": "Créer une catégorie",
|
"create": "Créer une catégorie",
|
||||||
"edit": "Modifier une catégorie",
|
"edit": "Modifier une catégorie",
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import Footer from 'components/Footer/Footer';
|
|||||||
import CreateItem from 'components/QuickActions/CreateItem';
|
import CreateItem from 'components/QuickActions/CreateItem';
|
||||||
import EditItem from 'components/QuickActions/EditItem';
|
import EditItem from 'components/QuickActions/EditItem';
|
||||||
import RemoveItem from 'components/QuickActions/RemoveItem';
|
import RemoveItem from 'components/QuickActions/RemoveItem';
|
||||||
|
import VisibilityBadge from 'components/Visibility/Visibility';
|
||||||
import { motion } from 'framer-motion';
|
import { motion } from 'framer-motion';
|
||||||
import useActiveCategory from 'hooks/useActiveCategory';
|
import useActiveCategory from 'hooks/useActiveCategory';
|
||||||
import { useTranslation } from 'next-i18next';
|
import { useTranslation } from 'next-i18next';
|
||||||
@@ -45,12 +46,16 @@ export default function Links({
|
|||||||
<RxHamburgerMenu size={'1.5em'} />
|
<RxHamburgerMenu size={'1.5em'} />
|
||||||
</ButtonLink>
|
</ButtonLink>
|
||||||
)}
|
)}
|
||||||
<span className={styles['category-name']}>
|
<div className={styles['category-name-wrapper']}>
|
||||||
{name}
|
<div className={styles['category-name']}>{name}</div>
|
||||||
{links.length > 0 && (
|
{links.length > 0 && (
|
||||||
<span className={styles['links-count']}> — {links.length}</span>
|
<span className={styles['links-count']}> — {links.length}</span>
|
||||||
)}
|
)}
|
||||||
</span>
|
<VisibilityBadge
|
||||||
|
label={t('common:category.visibility')}
|
||||||
|
visibility={activeCategory.visibility}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
<span className={styles['category-controls']}>
|
<span className={styles['category-controls']}>
|
||||||
<CreateItem
|
<CreateItem
|
||||||
type='link'
|
type='link'
|
||||||
|
|||||||
@@ -43,9 +43,15 @@
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
|
|
||||||
& .category-name {
|
& .category-name-wrapper {
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
display: flex;
|
||||||
|
flex: 1;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
& .category-name {
|
||||||
|
min-width: 0;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
@@ -57,7 +63,7 @@
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
& svg {
|
& > svg {
|
||||||
display: flex;
|
display: flex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
import VisibilityBadge from 'components/Visibility/Visibility';
|
||||||
import PATHS from 'constants/paths';
|
import PATHS from 'constants/paths';
|
||||||
import { motion } from 'framer-motion';
|
import { motion } from 'framer-motion';
|
||||||
import useActiveCategory from 'hooks/useActiveCategory';
|
import useActiveCategory from 'hooks/useActiveCategory';
|
||||||
import useCategories from 'hooks/useCategories';
|
import useCategories from 'hooks/useCategories';
|
||||||
import sortCategoriesByNextId from 'lib/category/sortCategoriesByNextId';
|
import sortCategoriesByNextId from 'lib/category/sortCategoriesByNextId';
|
||||||
import { makeRequest } from 'lib/request';
|
import { makeRequest } from 'lib/request';
|
||||||
|
import { useTranslation } from 'next-i18next';
|
||||||
import { useCallback, useEffect, useRef } from 'react';
|
import { useCallback, useEffect, useRef } from 'react';
|
||||||
import { useDrag, useDrop } from 'react-dnd';
|
import { useDrag, useDrop } from 'react-dnd';
|
||||||
import { AiFillFolderOpen, AiOutlineFolder } from 'react-icons/ai';
|
import { AiFillFolderOpen, AiOutlineFolder } from 'react-icons/ai';
|
||||||
@@ -28,6 +30,7 @@ export default function CategoryItem({
|
|||||||
}: Readonly<CategoryItemProps>): JSX.Element {
|
}: Readonly<CategoryItemProps>): JSX.Element {
|
||||||
const { activeCategory, setActiveCategory } = useActiveCategory();
|
const { activeCategory, setActiveCategory } = useActiveCategory();
|
||||||
const { categories, setCategories } = useCategories();
|
const { categories, setCategories } = useCategories();
|
||||||
|
const { t } = useTranslation('common');
|
||||||
|
|
||||||
const ref = useRef<HTMLLIElement>();
|
const ref = useRef<HTMLLIElement>();
|
||||||
|
|
||||||
@@ -155,8 +158,16 @@ export default function CategoryItem({
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
<div className={styles['content']}>
|
<div className={styles['content']}>
|
||||||
<span className={styles['name']}>{category.name}</span>
|
<div className={styles['name-wrapper']}>
|
||||||
<span className={styles['links-count']}>— {category.links.length}</span>
|
<span className={styles['name']}>{category.name}</span>
|
||||||
|
<span className={styles['links-count']}>
|
||||||
|
— {category.links.length}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<VisibilityBadge
|
||||||
|
label={t('common:category.visibility')}
|
||||||
|
visibility={category.visibility}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</motion.li>
|
</motion.li>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -37,13 +37,22 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
& .content {
|
& .content {
|
||||||
width: calc(100% - 42px);
|
width: 100%;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex: 1;
|
gap: 0.35em;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|
||||||
|
& .name-wrapper {
|
||||||
|
min-width: 0;
|
||||||
|
width: 100%;
|
||||||
|
display: flex;
|
||||||
|
gap: 0.35em;
|
||||||
|
flex: 1;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
& .name {
|
& .name {
|
||||||
margin-right: 5px;
|
min-width: 0;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
|
|||||||
18
src/components/Visibility/Visibility.tsx
Normal file
18
src/components/Visibility/Visibility.tsx
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { Visibility } from '@prisma/client';
|
||||||
|
import { IoEarthOutline } from 'react-icons/io5';
|
||||||
|
import styles from './visibility.module.scss';
|
||||||
|
|
||||||
|
const VisibilityBadge = ({
|
||||||
|
label,
|
||||||
|
visibility,
|
||||||
|
}: {
|
||||||
|
label: string;
|
||||||
|
visibility: Visibility;
|
||||||
|
}) =>
|
||||||
|
visibility === Visibility.public && (
|
||||||
|
<span className={styles['visibility']}>
|
||||||
|
{label} <IoEarthOutline size='1em' />
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
|
||||||
|
export default VisibilityBadge;
|
||||||
14
src/components/Visibility/visibility.module.scss
Normal file
14
src/components/Visibility/visibility.module.scss
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
@import 'styles/colors.scss';
|
||||||
|
|
||||||
|
.visibility {
|
||||||
|
font-weight: 300;
|
||||||
|
font-size: 0.6em;
|
||||||
|
color: rgba($color: $blue, $alpha: 0.75);
|
||||||
|
border: 1px solid rgba($color: $blue, $alpha: 0.75);
|
||||||
|
border-radius: 50px;
|
||||||
|
padding: 0.15em 0.65em;
|
||||||
|
margin-left: 0.5em;
|
||||||
|
display: flex;
|
||||||
|
gap: 0.35em;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { number, object, string } from 'yup';
|
import { Visibility } from '@prisma/client';
|
||||||
|
import { mixed, number, object, string } from 'yup';
|
||||||
|
|
||||||
const CategoryBodySchema = object({
|
const CategoryBodySchema = object({
|
||||||
name: string()
|
name: string()
|
||||||
@@ -6,6 +7,7 @@ const CategoryBodySchema = object({
|
|||||||
.required('Category name is required')
|
.required('Category name is required')
|
||||||
.max(128, 'Category name is too long'),
|
.max(128, 'Category name is too long'),
|
||||||
description: string().trim().max(255, 'Category description is too long'),
|
description: string().trim().max(255, 'Category description is too long'),
|
||||||
|
visibility: mixed<Visibility>().oneOf(Object.values(Visibility)).required(),
|
||||||
nextId: number().required().nullable(),
|
nextId: number().required().nullable(),
|
||||||
}).typeError('Missing request Body');
|
}).typeError('Missing request Body');
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,8 @@ export default apiHandler({
|
|||||||
|
|
||||||
async function editCategory({ req, res, user }) {
|
async function editCategory({ req, res, user }) {
|
||||||
const { cid } = await CategoryQuerySchema.validate(req.query);
|
const { cid } = await CategoryQuerySchema.validate(req.query);
|
||||||
const { name, description, nextId } = await CategoryBodySchema.validate(
|
const { name, description, visibility, nextId } =
|
||||||
req.body,
|
await CategoryBodySchema.validate(req.body);
|
||||||
);
|
|
||||||
const userId = user.id as User['id'];
|
const userId = user.id as User['id'];
|
||||||
|
|
||||||
const category = await getUserCategory(user, cid);
|
const category = await getUserCategory(user, cid);
|
||||||
@@ -107,6 +106,7 @@ async function editCategory({ req, res, user }) {
|
|||||||
data: {
|
data: {
|
||||||
name,
|
name,
|
||||||
description,
|
description,
|
||||||
|
visibility,
|
||||||
nextId: category.nextId,
|
nextId: category.nextId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -18,7 +18,9 @@ async function getCategories({ res, user }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createCategory({ req, res, user }) {
|
async function createCategory({ req, res, user }) {
|
||||||
const { name, description } = await CategoryBodySchema.validate(req.body);
|
const { name, description, visibility } = await CategoryBodySchema.validate(
|
||||||
|
req.body,
|
||||||
|
);
|
||||||
|
|
||||||
const category = await getUserCategoryByName(user, name);
|
const category = await getUserCategoryByName(user, name);
|
||||||
if (category) {
|
if (category) {
|
||||||
@@ -36,7 +38,7 @@ async function createCategory({ req, res, user }) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const categoryCreated = await prisma.category.create({
|
const categoryCreated = await prisma.category.create({
|
||||||
data: { name, description, authorId: user.id },
|
data: { name, description, visibility, authorId: user.id },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (lastCategory) {
|
if (lastCategory) {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { Visibility } from '@prisma/client';
|
||||||
|
import Checkbox from 'components/Checkbox';
|
||||||
import FormLayout from 'components/FormLayout';
|
import FormLayout from 'components/FormLayout';
|
||||||
import PageTransition from 'components/PageTransition';
|
import PageTransition from 'components/PageTransition';
|
||||||
import TextBox from 'components/TextBox';
|
import TextBox from 'components/TextBox';
|
||||||
@@ -24,6 +26,7 @@ export default function PageCreateCategory({
|
|||||||
|
|
||||||
const [name, setName] = useState<string>('');
|
const [name, setName] = useState<string>('');
|
||||||
const [description, setDescription] = useState<string>('');
|
const [description, setDescription] = useState<string>('');
|
||||||
|
const [visibility, setVisibility] = useState<Visibility>(Visibility.private);
|
||||||
|
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [submitted, setSubmitted] = useState<boolean>(false);
|
const [submitted, setSubmitted] = useState<boolean>(false);
|
||||||
@@ -41,7 +44,7 @@ export default function PageCreateCategory({
|
|||||||
makeRequest({
|
makeRequest({
|
||||||
url: PATHS.API.CATEGORY,
|
url: PATHS.API.CATEGORY,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: { name, description, nextId: null },
|
body: { name, description, visibility, nextId: null },
|
||||||
})
|
})
|
||||||
.then((data) =>
|
.then((data) =>
|
||||||
router.push(`${PATHS.APP}?categoryId=${data?.categoryId}`),
|
router.push(`${PATHS.APP}?categoryId=${data?.categoryId}`),
|
||||||
@@ -78,6 +81,14 @@ export default function PageCreateCategory({
|
|||||||
fieldClass={styles['input-field']}
|
fieldClass={styles['input-field']}
|
||||||
placeholder={t('common:category.description')}
|
placeholder={t('common:category.description')}
|
||||||
/>
|
/>
|
||||||
|
<Checkbox
|
||||||
|
name='visibility'
|
||||||
|
isChecked={visibility === Visibility.public}
|
||||||
|
onChangeCallback={(value) =>
|
||||||
|
setVisibility(!!value ? Visibility.public : Visibility.private)
|
||||||
|
}
|
||||||
|
label={t('common:category.visibility')}
|
||||||
|
/>
|
||||||
</FormLayout>
|
</FormLayout>
|
||||||
</PageTransition>
|
</PageTransition>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { Visibility } from '@prisma/client';
|
||||||
|
import Checkbox from 'components/Checkbox';
|
||||||
import FormLayout from 'components/FormLayout';
|
import FormLayout from 'components/FormLayout';
|
||||||
import PageTransition from 'components/PageTransition';
|
import PageTransition from 'components/PageTransition';
|
||||||
import TextBox from 'components/TextBox';
|
import TextBox from 'components/TextBox';
|
||||||
@@ -24,16 +26,27 @@ export default function PageEditCategory({
|
|||||||
|
|
||||||
const [name, setName] = useState<string>(category.name);
|
const [name, setName] = useState<string>(category.name);
|
||||||
const [description, setDescription] = useState<string>(category.description);
|
const [description, setDescription] = useState<string>(category.description);
|
||||||
|
const [visibility, setVisibility] = useState<Visibility>(category.visibility);
|
||||||
|
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [submitted, setSubmitted] = useState<boolean>(false);
|
const [submitted, setSubmitted] = useState<boolean>(false);
|
||||||
|
|
||||||
const canSubmit = useMemo<boolean>(() => {
|
const canSubmit = useMemo<boolean>(() => {
|
||||||
const isFormEdited =
|
const isFormEdited =
|
||||||
name !== category.name || description !== category.description;
|
name !== category.name ||
|
||||||
|
description !== category.description ||
|
||||||
|
visibility !== category.visibility;
|
||||||
const isFormValid = name !== '';
|
const isFormValid = name !== '';
|
||||||
return isFormEdited && isFormValid && !submitted;
|
return isFormEdited && isFormValid && !submitted;
|
||||||
}, [category.description, category.name, description, name, submitted]);
|
}, [
|
||||||
|
category.description,
|
||||||
|
category.name,
|
||||||
|
category.visibility,
|
||||||
|
description,
|
||||||
|
name,
|
||||||
|
submitted,
|
||||||
|
visibility,
|
||||||
|
]);
|
||||||
|
|
||||||
const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
|
const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@@ -43,7 +56,7 @@ export default function PageEditCategory({
|
|||||||
makeRequest({
|
makeRequest({
|
||||||
url: `${PATHS.API.CATEGORY}/${category.id}`,
|
url: `${PATHS.API.CATEGORY}/${category.id}`,
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: { name, description, nextId: category.nextId },
|
body: { name, description, visibility, nextId: category.nextId },
|
||||||
})
|
})
|
||||||
.then((data) =>
|
.then((data) =>
|
||||||
router.push(`${PATHS.APP}?categoryId=${data?.categoryId}`),
|
router.push(`${PATHS.APP}?categoryId=${data?.categoryId}`),
|
||||||
@@ -78,6 +91,14 @@ export default function PageEditCategory({
|
|||||||
fieldClass={styles['input-field']}
|
fieldClass={styles['input-field']}
|
||||||
placeholder={t('common:category.description')}
|
placeholder={t('common:category.description')}
|
||||||
/>
|
/>
|
||||||
|
<Checkbox
|
||||||
|
name='visibility'
|
||||||
|
isChecked={visibility === Visibility.public}
|
||||||
|
onChangeCallback={(value) =>
|
||||||
|
setVisibility(!!value ? Visibility.public : Visibility.private)
|
||||||
|
}
|
||||||
|
label={t('common:category.visibility')}
|
||||||
|
/>
|
||||||
</FormLayout>
|
</FormLayout>
|
||||||
</PageTransition>
|
</PageTransition>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user