Moved url updating to a helper function

This commit is contained in:
Jori Lallo
2017-09-12 20:14:14 -07:00
parent 5c43e12218
commit 76afaacc6e
2 changed files with 14 additions and 3 deletions

View File

@@ -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 });

View File

@@ -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('/');
}