feat: use route management system for collections

This commit is contained in:
Sonny
2024-05-16 17:59:05 +02:00
committed by Sonny
parent 19b96650e9
commit 905f0ba1c7
4 changed files with 49 additions and 35 deletions

View File

@@ -1,4 +1,3 @@
import PATHS from '#constants/paths';
import Collection from '#models/collection'; import Collection from '#models/collection';
import User from '#models/user'; import User from '#models/user';
import { import {
@@ -12,7 +11,7 @@ export default class CollectionsController {
async index({ auth, inertia, request, response }: HttpContext) { async index({ auth, inertia, request, response }: HttpContext) {
const collections = await this.getCollectionsByAuthorId(auth.user!.id); const collections = await this.getCollectionsByAuthorId(auth.user!.id);
if (collections.length === 0) { if (collections.length === 0) {
return response.redirect('/collections/create'); return response.redirectToNamedRoute('collection.create-form');
} }
const activeCollectionId = request.qs()?.collectionId ?? ''; const activeCollectionId = request.qs()?.collectionId ?? '';
@@ -21,7 +20,7 @@ export default class CollectionsController {
); );
if (!activeCollection && !!activeCollectionId) { if (!activeCollection && !!activeCollectionId) {
return response.redirect('/dashboard'); return response.redirectToNamedRoute('dashboard');
} }
return inertia.render('dashboard', { return inertia.render('dashboard', {
@@ -51,7 +50,7 @@ export default class CollectionsController {
async showEditPage({ auth, request, inertia, response }: HttpContext) { async showEditPage({ auth, request, inertia, response }: HttpContext) {
const collectionId = request.qs()?.collectionId; const collectionId = request.qs()?.collectionId;
if (!collectionId) { if (!collectionId) {
return response.redirect('/dashboard'); return response.redirectToNamedRoute('dashboard');
} }
const collection = await this.getCollectionById( const collection = await this.getCollectionById(
@@ -105,6 +104,8 @@ export default class CollectionsController {
response: HttpContext['response'], response: HttpContext['response'],
collectionId: Collection['id'] collectionId: Collection['id']
) { ) {
return response.redirect(`${PATHS.DASHBOARD}?collectionId=${collectionId}`); return response.redirectToNamedRoute('dashboard', {
qs: { collectionId },
});
} }
} }

View File

@@ -13,12 +13,13 @@ const CollectionNameWrapper = styled.div({
alignItems: 'center', alignItems: 'center',
}); });
const CollectionName = styled.div({ const CollectionName = styled.div(({ theme }) => ({
minWidth: 0, minWidth: 0,
color: theme.colors.primary,
textOverflow: 'ellipsis', textOverflow: 'ellipsis',
whiteSpace: 'nowrap', whiteSpace: 'nowrap',
overflow: 'hidden', overflow: 'hidden',
}); }));
const LinksCount = styled.div(({ theme }) => ({ const LinksCount = styled.div(({ theme }) => ({
minWidth: 'fit-content', minWidth: 'fit-content',

View File

@@ -1,12 +1,14 @@
import PATHS from '#constants/paths';
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { route } from '@izzyjs/route/client';
import { AiOutlineFolderAdd } from 'react-icons/ai'; import { AiOutlineFolderAdd } from 'react-icons/ai';
import { BsGear } from 'react-icons/bs'; import { BsGear } from 'react-icons/bs';
import { IoAdd } from 'react-icons/io5'; import { IoAdd } from 'react-icons/io5';
import { MdOutlineAdminPanelSettings } from 'react-icons/md'; import { MdOutlineAdminPanelSettings } from 'react-icons/md';
import FavoriteList from '~/components/dashboard/side_nav/favorite/favorite_list'; import FavoriteList from '~/components/dashboard/side_nav/favorite/favorite_list';
import { ItemLink } from '~/components/dashboard/side_nav/nav_item'; import { Item, ItemLink } from '~/components/dashboard/side_nav/nav_item';
import UserCard from '~/components/dashboard/side_nav/user_card'; import UserCard from '~/components/dashboard/side_nav/user_card';
import useActiveCollection from '~/hooks/use_active_collection';
import { appendCollectionId } from '~/lib/navigation';
const SideMenu = styled.nav({ const SideMenu = styled.nav({
height: '100%', height: '100%',
@@ -15,11 +17,11 @@ const SideMenu = styled.nav({
flexDirection: 'column', flexDirection: 'column',
}); });
const AdminButton = styled(ItemLink)(({ theme }) => ({ const AdminButton = styled(Item)(({ theme }) => ({
color: theme.colors.lightRed, color: theme.colors.lightRed,
})); }));
const SettingsButton = styled(ItemLink)(({ theme }) => ({ const SettingsButton = styled(Item)(({ theme }) => ({
color: theme.colors.grey, color: theme.colors.grey,
})); }));
@@ -27,26 +29,32 @@ const AddButton = styled(ItemLink)(({ theme }) => ({
color: theme.colors.primary, color: theme.colors.primary,
})); }));
const SideNavigation = () => ( export default function SideNavigation() {
<SideMenu> const { activeCollection } = useActiveCollection();
<div css={{ paddingInline: '10px' }}> return (
<UserCard /> <SideMenu>
<AdminButton href="/admin"> <div css={{ paddingInline: '10px' }}>
<MdOutlineAdminPanelSettings /> Administrator <UserCard />
</AdminButton> <AdminButton>
<SettingsButton href="/settings"> <MdOutlineAdminPanelSettings /> Administrator
<BsGear /> </AdminButton>
Settings <SettingsButton>
</SettingsButton> <BsGear />
<AddButton href={PATHS.LINK.CREATE}> Settings
<IoAdd /> Create link </SettingsButton>
</AddButton> <AddButton
<AddButton href={PATHS.COLLECTION.CREATE}> href={appendCollectionId(
<AiOutlineFolderAdd /> Create collection route('link.create-form').url,
</AddButton> activeCollection?.id
</div> )}
<FavoriteList /> >
</SideMenu> <IoAdd /> Create link
); </AddButton>
<AddButton href={route('collection.create-form').url}>
export default SideNavigation; <AiOutlineFolderAdd /> Create collection
</AddButton>
</div>
<FavoriteList />
</SideMenu>
);
}

View File

@@ -4,6 +4,7 @@ import type Collection from '#models/collection';
import Link from '#models/link'; import Link from '#models/link';
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { router } from '@inertiajs/react'; import { router } from '@inertiajs/react';
import { route } from '@izzyjs/route/client';
import { ReactNode, useEffect, useMemo, useState } from 'react'; import { ReactNode, useEffect, useMemo, useState } from 'react';
import { useHotkeys } from 'react-hotkeys-hook'; import { useHotkeys } from 'react-hotkeys-hook';
import { useSwipeable } from 'react-swipeable'; import { useSwipeable } from 'react-swipeable';
@@ -18,6 +19,7 @@ import FavoritesContext from '~/contexts/favorites_context';
import GlobalHotkeysContext from '~/contexts/global_hotkeys_context'; import GlobalHotkeysContext from '~/contexts/global_hotkeys_context';
import { useMediaQuery } from '~/hooks/use_media_query'; import { useMediaQuery } from '~/hooks/use_media_query';
import useToggle from '~/hooks/use_modal'; import useToggle from '~/hooks/use_modal';
import { appendCollectionId } from '~/lib/navigation';
interface DashboardPageProps { interface DashboardPageProps {
collections: Collection[]; collections: Collection[];
@@ -78,8 +80,10 @@ function DashboardProviders(
props.activeCollection || collections?.[0] props.activeCollection || collections?.[0]
); );
const handleChangeCollection = (collection: Collection) => const handleChangeCollection = (collection: Collection) => {
setActiveCollection(collection); setActiveCollection(collection);
router.visit(appendCollectionId(route('dashboard').url, collection.id));
};
const favorites = useMemo<Link[]>( const favorites = useMemo<Link[]>(
() => () =>