Skip to content

Commit

Permalink
Redirect if evaluated route changes on update (#232).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Sep 9, 2024
1 parent 3280a19 commit 4107658
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
18 changes: 11 additions & 7 deletions defaults/core/cms/post_local.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ export async function postLocal(commitList, shadowContent, action, encoding) {
body: JSON.stringify(body),
});
if (response.ok) {
if (commitList.length === 1 && action === 'create') {
let evaluatedRoute = evaluateRoute(commitList[0]);
history.pushState({
isNew: true,
route: evaluatedRoute
}, '', evaluatedRoute);
}
if (action === 'create' || action === 'update') {
shadowContent?.onSave?.();
// Make sure saving single content file, not list of media items
if (commitList.length === 1 && commitList[0].file.lastIndexOf('.json') > 0) {
let evaluatedRoute = evaluateRoute(commitList[0]);
// Redirect only if new route is being created
if (evaluatedRoute !== location.pathname) {
history.pushState({
isNew: true,
route: evaluatedRoute
}, '', evaluatedRoute);
}
}
}
if (action === 'delete') {
shadowContent?.onDelete?.();
Expand Down
18 changes: 11 additions & 7 deletions defaults/core/cms/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,19 @@ export async function publish(commitList, shadowContent, action, encoding, user)
body: JSON.stringify(payload),
});
if (response.ok) {
if (commitList.length === 1 && action === 'create') {
let evaluatedRoute = evaluateRoute(commitList[0]);
history.pushState({
isNew: true,
route: evaluatedRoute
}, '', evaluatedRoute);
}
if (action === 'create' || action === 'update') {
shadowContent?.onSave?.();
// Make sure saving single content file, not list of media items
if (commitList.length === 1 && commitList[0].file.lastIndexOf('.json') > 0) {
let evaluatedRoute = evaluateRoute(commitList[0]);
// Redirect only if new route is being created
if (evaluatedRoute !== location.pathname) {
history.pushState({
isNew: true,
route: evaluatedRoute
}, '', evaluatedRoute);
}
}
}
if (action === 'delete') {
shadowContent?.onDelete?.();
Expand Down
5 changes: 3 additions & 2 deletions defaults/core/cms/route_eval.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { env } from '../../generated/env.js';

export default function evaluateRoute(commitItem) {
let content_type = window.location.hash.split("/")[1];
let filename = window.location.hash.split("/")[2];
let content_type = commitItem.file.split("/")[1];
let filenameWithExt = commitItem.file.split("/")[2]; // Should probably account for nested content folders
let filename = filenameWithExt.substring(0, filenameWithExt.lastIndexOf('.json'));
let filepath = commitItem.file;
let fields = JSON.parse(commitItem.contents);
let default_route = "/" + content_type + "/" + filename;
Expand Down

0 comments on commit 4107658

Please sign in to comment.