fix: routing issues (#590)

This commit is contained in:
Denys Konovalov 2023-02-26 15:25:27 +01:00 committed by GitHub
parent e793a50a92
commit 4bee8f2e0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 37 additions and 57 deletions

View File

@ -1,2 +0,0 @@
/* eslint-disable import/prefer-default-export */
export const createHashHistory = jest.fn();

View File

@ -1,18 +0,0 @@
import { history } from '../routing/history';
import { getCollectionUrl, getNewEntryUrl } from '../lib/urlHelper';
export function searchCollections(query: string, collection?: string) {
if (collection) {
history.push(`/collections/${collection}/search/${query}`);
} else {
history.push(`/search/${query}`);
}
}
export function showCollection(collectionName: string) {
history.push(getCollectionUrl(collectionName));
}
export function createNewEntry(collectionName: string) {
history.push(getNewEntryUrl(collectionName));
}

View File

@ -1,4 +1,5 @@
import isEqual from 'lodash/isEqual'; import isEqual from 'lodash/isEqual';
import { redirect } from 'react-router-dom';
import { currentBackend } from '../backend'; import { currentBackend } from '../backend';
import { import {
@ -48,7 +49,6 @@ import {
selectIsFetching, selectIsFetching,
selectPublishedSlugs, selectPublishedSlugs,
} from '../reducers/selectors/entries'; } from '../reducers/selectors/entries';
import { navigateToEntry } from '../routing/history';
import { addSnackbar } from '../store/slices/snackbars'; import { addSnackbar } from '../store/slices/snackbars';
import { createAssetProxy } from '../valueObjects/AssetProxy'; import { createAssetProxy } from '../valueObjects/AssetProxy';
import createEntry from '../valueObjects/createEntry'; import createEntry from '../valueObjects/createEntry';
@ -1040,7 +1040,7 @@ export function persistEntry(collection: Collection) {
} }
if (entry.slug !== newSlug) { if (entry.slug !== newSlug) {
await dispatch(loadEntry(collection, newSlug)); await dispatch(loadEntry(collection, newSlug));
navigateToEntry(collection.name, newSlug); redirect(`/collections/${collection.name}/entries/${newSlug}`);
} else { } else {
await dispatch(loadEntry(collection, newSlug, true)); await dispatch(loadEntry(collection, newSlug, true));
} }

View File

@ -4,7 +4,7 @@ import { styled } from '@mui/material/styles';
import React, { useCallback, useEffect, useMemo } from 'react'; import React, { useCallback, useEffect, useMemo } from 'react';
import { translate } from 'react-polyglot'; import { translate } from 'react-polyglot';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { Navigate, Route, Routes, useLocation, useParams } from 'react-router-dom'; import { Navigate, Route, Routes, useLocation, useNavigate, useParams } from 'react-router-dom';
import { ScrollSync } from 'react-scroll-sync'; import { ScrollSync } from 'react-scroll-sync';
import TopBarProgress from 'react-topbar-progress-indicator'; import TopBarProgress from 'react-topbar-progress-indicator';
@ -12,7 +12,6 @@ import { loginUser as loginUserAction } from '@staticcms/core/actions/auth';
import { discardDraft as discardDraftAction } from '@staticcms/core/actions/entries'; import { discardDraft as discardDraftAction } from '@staticcms/core/actions/entries';
import { currentBackend } from '@staticcms/core/backend'; import { currentBackend } from '@staticcms/core/backend';
import { colors, GlobalStyles } from '@staticcms/core/components/UI/styles'; import { colors, GlobalStyles } from '@staticcms/core/components/UI/styles';
import { history } from '@staticcms/core/routing/history';
import { getDefaultPath } from '../../lib/util/collection.util'; import { getDefaultPath } from '../../lib/util/collection.util';
import CollectionRoute from '../Collection/CollectionRoute'; import CollectionRoute from '../Collection/CollectionRoute';
import EditorRoute from '../Editor/EditorRoute'; import EditorRoute from '../Editor/EditorRoute';
@ -84,6 +83,8 @@ const App = ({
scrollSyncEnabled, scrollSyncEnabled,
discardDraft, discardDraft,
}: TranslatedProps<AppProps>) => { }: TranslatedProps<AppProps>) => {
const navigate = useNavigate();
const configError = useCallback( const configError = useCallback(
(error?: string) => { (error?: string) => {
return ( return (
@ -140,7 +141,7 @@ const App = ({
base_url={config.config.backend.base_url} base_url={config.config.backend.base_url}
authEndpoint={config.config.backend.auth_endpoint} authEndpoint={config.config.backend.auth_endpoint}
config={config.config} config={config.config}
clearHash={() => history.replace('/')} clearHash={() => navigate('/', { replace: true })}
t={t} t={t}
/> />
</div> </div>

View File

@ -12,13 +12,13 @@ import Toolbar from '@mui/material/Toolbar';
import React, { useCallback, useEffect, useMemo, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { translate } from 'react-polyglot'; import { translate } from 'react-polyglot';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { useNavigate } from 'react-router-dom';
import { logoutUser as logoutUserAction } from '@staticcms/core/actions/auth'; import { logoutUser as logoutUserAction } from '@staticcms/core/actions/auth';
import { createNewEntry } from '@staticcms/core/actions/collections';
import { openMediaLibrary as openMediaLibraryAction } from '@staticcms/core/actions/mediaLibrary'; import { openMediaLibrary as openMediaLibraryAction } from '@staticcms/core/actions/mediaLibrary';
import { checkBackendStatus as checkBackendStatusAction } from '@staticcms/core/actions/status'; import { checkBackendStatus as checkBackendStatusAction } from '@staticcms/core/actions/status';
import { buttons, colors } from '@staticcms/core/components/UI/styles'; import { buttons, colors } from '@staticcms/core/components/UI/styles';
import { stripProtocol } from '@staticcms/core/lib/urlHelper'; import { stripProtocol, getNewEntryUrl } from '@staticcms/core/lib/urlHelper';
import NavLink from '../UI/NavLink'; import NavLink from '../UI/NavLink';
import SettingsDropdown from '../UI/SettingsDropdown'; import SettingsDropdown from '../UI/SettingsDropdown';
@ -82,9 +82,7 @@ const Header = ({
setAnchorEl(null); setAnchorEl(null);
}, []); }, []);
const handleCreatePostClick = useCallback((collectionName: string) => { const navigate = useNavigate();
createNewEntry(collectionName);
}, []);
const creatableCollections = useMemo( const creatableCollections = useMemo(
() => () =>
@ -148,7 +146,7 @@ const Header = ({
{creatableCollections.map(collection => ( {creatableCollections.map(collection => (
<MenuItem <MenuItem
key={collection.name} key={collection.name}
onClick={() => handleCreatePostClick(collection.name)} onClick={() => navigate(getNewEntryUrl(collection.name))}
> >
{collection.label_singular || collection.label} {collection.label_singular || collection.label}
</MenuItem> </MenuItem>

View File

@ -10,8 +10,8 @@ import ListItemText from '@mui/material/ListItemText';
import Typography from '@mui/material/Typography'; import Typography from '@mui/material/Typography';
import React, { useMemo } from 'react'; import React, { useMemo } from 'react';
import { translate } from 'react-polyglot'; import { translate } from 'react-polyglot';
import { useNavigate } from 'react-router-dom';
import { searchCollections } from '@staticcms/core/actions/collections';
import { colors } from '@staticcms/core/components/UI/styles'; import { colors } from '@staticcms/core/components/UI/styles';
import { getAdditionalLinks, getIcon } from '@staticcms/core/lib/registry'; import { getAdditionalLinks, getIcon } from '@staticcms/core/lib/registry';
import NavLink from '../UI/NavLink'; import NavLink from '../UI/NavLink';
@ -48,6 +48,15 @@ const Sidebar = ({
t, t,
filterTerm, filterTerm,
}: TranslatedProps<SidebarProps>) => { }: TranslatedProps<SidebarProps>) => {
const navigate = useNavigate();
function searchCollections(query: string, collection?: string) {
if (collection) {
navigate(`/collections/${collection}/search/${query}`);
} else {
navigate(`/search/${query}`);
}
}
const collectionLinks = useMemo( const collectionLinks = useMemo(
() => () =>
Object.values(collections) Object.values(collections)

View File

@ -2,6 +2,8 @@ import debounce from 'lodash/debounce';
import React, { useCallback, useEffect, useMemo, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { translate } from 'react-polyglot'; import { translate } from 'react-polyglot';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { useNavigate } from 'react-router-dom';
import { createHashHistory } from 'history';
import { logoutUser as logoutUserAction } from '@staticcms/core/actions/auth'; import { logoutUser as logoutUserAction } from '@staticcms/core/actions/auth';
import { import {
@ -25,7 +27,6 @@ import {
import { selectFields } from '@staticcms/core/lib/util/collection.util'; import { selectFields } from '@staticcms/core/lib/util/collection.util';
import { useWindowEvent } from '@staticcms/core/lib/util/window.util'; import { useWindowEvent } from '@staticcms/core/lib/util/window.util';
import { selectEntry } from '@staticcms/core/reducers/selectors/entries'; import { selectEntry } from '@staticcms/core/reducers/selectors/entries';
import { history, navigateToCollection, navigateToNewEntry } from '@staticcms/core/routing/history';
import confirm from '../UI/Confirm'; import confirm from '../UI/Confirm';
import Loader from '../UI/Loader'; import Loader from '../UI/Loader';
import EditorInterface from './EditorInterface'; import EditorInterface from './EditorInterface';
@ -74,6 +75,10 @@ const Editor = ({
}: TranslatedProps<EditorProps>) => { }: TranslatedProps<EditorProps>) => {
const [version, setVersion] = useState(0); const [version, setVersion] = useState(0);
const history = createHashHistory();
const navigate = useNavigate();
const createBackup = useMemo( const createBackup = useMemo(
() => () =>
debounce(function (entry: Entry, collection: Collection) { debounce(function (entry: Entry, collection: Collection) {
@ -111,7 +116,7 @@ const Editor = ({
deleteBackup(); deleteBackup();
if (createNew) { if (createNew) {
navigateToNewEntry(collection.name); navigate(`/collections/${collection.name}/new`, { replace: true });
if (duplicate && entryDraft.entry) { if (duplicate && entryDraft.entry) {
createDraftDuplicateFromEntry(entryDraft.entry); createDraftDuplicateFromEntry(entryDraft.entry);
} }
@ -135,7 +140,7 @@ const Editor = ({
return; return;
} }
navigateToNewEntry(collection.name); navigate(`/collections/${collection.name}/new`, { replace: true });
createDraftDuplicateFromEntry(entryDraft.entry); createDraftDuplicateFromEntry(entryDraft.entry);
}, [collection.name, createDraftDuplicateFromEntry, entryDraft.entry]); }, [collection.name, createDraftDuplicateFromEntry, entryDraft.entry]);
@ -161,13 +166,13 @@ const Editor = ({
} }
if (!slug) { if (!slug) {
return navigateToCollection(collection.name); return navigate(`/collections/${collection.name}`);
} }
setTimeout(async () => { setTimeout(async () => {
await deleteEntry(collection, slug); await deleteEntry(collection, slug);
deleteBackup(); deleteBackup();
return navigateToCollection(collection.name); return navigate(`/collections/${collection.name}`);
}, 0); }, 0);
}, [collection, deleteBackup, deleteEntry, entryDraft.hasChanged, slug]); }, [collection, deleteBackup, deleteEntry, entryDraft.hasChanged, slug]);

View File

@ -21,7 +21,7 @@ const de: LocalePhrasesRoot = {
content: 'Inhalt', content: 'Inhalt',
workflow: 'Arbeitsablauf', workflow: 'Arbeitsablauf',
media: 'Medien', media: 'Medien',
quickAdd: 'Schnell-Erstellung', quickAdd: 'Schnellerstellung',
}, },
app: { app: {
errorHeader: 'Fehler beim Laden der CMS-Konfiguration.', errorHeader: 'Fehler beim Laden der CMS-Konfiguration.',
@ -36,8 +36,8 @@ const de: LocalePhrasesRoot = {
}, },
collection: { collection: {
sidebar: { sidebar: {
collections: 'Inhaltstypen', collections: 'Bereiche',
allCollections: 'Allen Inhaltstypen', allCollections: 'Allen Bereichen',
searchAll: 'Alles durchsuchen', searchAll: 'Alles durchsuchen',
searchIn: 'Suchen in', searchIn: 'Suchen in',
}, },
@ -231,6 +231,10 @@ const de: LocalePhrasesRoot = {
}, },
}, },
ui: { ui: {
common: {
yes: 'Ja',
no: 'Nein',
},
default: { default: {
goBackToSite: 'Zurück zur Seite', goBackToSite: 'Zurück zur Seite',
}, },

View File

@ -1,17 +0,0 @@
import { createHashHistory } from 'history';
const history = createHashHistory();
export function navigateToCollection(collectionName: string) {
return history.push(`/collections/${collectionName}`);
}
export function navigateToNewEntry(collectionName: string) {
return history.replace(`/collections/${collectionName}/new`);
}
export function navigateToEntry(collectionName: string, slug: string) {
return history.replace(`/collections/${collectionName}/entries/${slug}`);
}
export { history };