Moved url updating to a helper function
This commit is contained in:
@@ -6,7 +6,7 @@ import { observer, inject } from 'mobx-react';
|
||||
import { withRouter, Prompt } from 'react-router';
|
||||
import Flex from 'components/Flex';
|
||||
import { color, layout } from 'styles/constants';
|
||||
import { collectionUrl } from 'utils/routeHelpers';
|
||||
import { collectionUrl, updateDocumentUrl } from 'utils/routeHelpers';
|
||||
|
||||
import Document from 'models/Document';
|
||||
import UiStore from 'stores/UiStore';
|
||||
@@ -92,8 +92,9 @@ type Props = {
|
||||
document.view();
|
||||
|
||||
// Update url to match the current one
|
||||
const urlParts = this.props.match.url.split('/');
|
||||
this.props.history.replace([document.url, urlParts.slice(3)].join('/'));
|
||||
this.props.history.replace(
|
||||
updateDocumentUrl(this.props.match.url, document.url)
|
||||
);
|
||||
} else {
|
||||
// Render 404 with search
|
||||
this.setState({ notFound: true });
|
||||
|
||||
@@ -38,3 +38,13 @@ export function searchUrl(query?: string): string {
|
||||
export function notFoundUrl(): string {
|
||||
return '/404';
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace full url's document part with the new one in case
|
||||
* the document slug has been updated
|
||||
*/
|
||||
export function updateDocumentUrl(oldUrl: string, newUrl: string): string {
|
||||
// Update url to match the current one
|
||||
const urlParts = oldUrl.split('/');
|
||||
return [newUrl, urlParts.slice(3)].join('/');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user