Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Reinstate unsaved changes dialog #1458

Conversation

GuySartorelli
Copy link
Member

@GuySartorelli GuySartorelli commented Feb 22, 2023

This broke when upgrading to react-router 6.4 - they've added a new API for it in 6.7

I've added behat tests for this in asset-admin: silverstripe/silverstripe-asset-admin#1335

Notes

  • There is a pre-existing issue you'll run into while testing this: STATE: Edit form still marked as dirty after closing it silverstripe-asset-admin#1334
    • Fixing that is way out of scope for this PR given it's a pre-existing issue from CMS 4.
  • There is no custom message for navigating outside of the react context (i.e. refreshing the page, clicking a link in the left navigation panel, etc). That dialog box comes natively with the browser as part of the beforeunload event. Adding a custom message to it is not supported by modern browsers.

Parent issue

@GuySartorelli
Copy link
Member Author

GuySartorelli commented Feb 22, 2023

UPDATE: The problem mentioned in this comment is resolved. Leaving here for historical context only.

I've got this to a state where it technically meets the acceptance criteria - except after the initial "yes continue I want to navigate away" it keeps prompting you every time you go somewhere until you go back to the original form. It seems the state of the form is stuck as dirty until you prove (by navigating back to it) that you don't have unsaved changes there anymore.

There's a console error which I think is part of the problem when you accept the prompt the first time:

Warning: Cannot update a component (`re`) while rendering a different component (`c`). To locate the bad setState() call inside `c`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render c@http://react-unsaved-changes_09.local/_resources/vendor/silverstripe/admin/client/dist/js/bundle.js?m=1677039314:1:219846 div ./client/src/containers/App/App.js/d<@http://react-unsaved-changes_09.local/_resources/vendor/silverstripe/admin/client/dist/js/bundle.js?m=1677039314:1:219676 s@http://react-unsaved-changes_09.local/_resources/vendor/silverstripe/admin/client/dist/js/bundle.js?m=1677039314:38:11650 F@http://react-unsaved-changes_09.local/_resources/vendor/silverstripe/admin/client/dist/js/vendor.js?m=1677035706:1:1672740 N@http://react-unsaved-changes_09.local/_resources/vendor/silverstripe/admin/client/dist/js/vendor.js?m=1677035706:1:1672136 ue@http://react-unsaved-changes_09.local/_resources/vendor/silverstripe/admin/client/dist/js/vendor.js?m=1677035706:1:1678678 le@http://react-unsaved-changes_09.local/_resources/vendor/silverstripe/admin/client/dist/js/vendor.js?m=1677035706:1:1677857 re@http://react-unsaved-changes_09.local/_resources/vendor/silverstripe/admin/client/dist/js/vendor.js?m=1677035706:1:1675895 ./node_modules/react-redux/es/components/Provider.js/t.default@http://react-unsaved-changes_09.local/_resources/vendor/silverstripe/admin/client/dist/js/vendor.js?m=1677035706:1:1626487 i@http://react-unsaved-changes_09.local/_resources/vendor/silverstripe/admin/client/dist/js/vendor.js?m=1677035706:1:2510593

I'm pretty sure react isn't able to update the form state at the same time as the blocker state, and for whatever reason the blocker state gets prioritised, so yes the blocker lets you continue, but each time it asks "should I block this new navigation?" the app tells it "yes, the form is still dirty"

This broke when upgrading to react-router 6.4 - they've added a new API
for it in 6.7
import { BrowserRouter } from 'react-router-dom';
import { createBrowserRouter, createRoutesFromElements, RouterProvider } from 'react-router-dom';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use createBrowserRouter() and createRoutesFromElements() instead of adding the BrowserRouter and Routes components ourselves because the RouterProvider doesn't like us doing it ourself - and we have to use the RouterProvider because this instantiates a "data router" which is necessary to use the new useBlocker hook.

Comment on lines -38 to -39
this.handleBeforeRoute = this.handleBeforeRoute.bind(this);
this.handleBeforeUnload = this.handleBeforeUnload.bind(this);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither of these functions is passed into downstream components anymore, so we don't need explicit binds.

Comment on lines -112 to 137
const routes = reactRouteRegister.getChildRoutes().map((route) => (
<Route key={route.path} path={route.path} element={<route.component />} />
));
const router = createBrowserRouter(
createRoutesFromElements(
<Route
path={rootRoute.path}
element={
<rootRoute.component>
<NavigationBlocker shouldBlockFn={this.shouldConfirmBeforeUnload} blockMessage={this.getUnsavedChangesMessage()} />
</rootRoute.component>
}
>
{reactRouteRegister.getChildRoutes().map(
(route) => (
<Route key={route.path} path={route.path} element={<route.component />} />
)
)}
</Route>
),
{ basename: joinUrlPaths(Config.get('baseUrl'), Config.get('adminUrl')) }
);

ReactDOM.createRoot(document.getElementsByClassName('cms-content')[0]).render(
<ApolloProvider client={this.client}>
<ReduxProvider store={this.store}>
<BrowserRouter basename={joinUrlPaths(Config.get('baseUrl'), Config.get('adminUrl'))}>
<Routes>
<Route path={rootRoute.path} element={<rootRoute.component />}>{routes}</Route>
</Routes>
</BrowserRouter>
<RouterProvider router={router} />
</ReduxProvider>
</ApolloProvider>
Copy link
Member Author

@GuySartorelli GuySartorelli Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately the only differences here are:

  • We now have a RouterProvider (which is necessary for the new useBlocker hook)
  • We're passing a component in as a child of the App component (rootRoute.component)

See #1458 (comment) for the reason we had to change the way we're doing the browserrouter etc.

Comment on lines -221 to -236
handleBeforeUnload(content, callback) {
if (this.shouldConfirmBeforeUnload()) {
return callback(confirm(i18n._t('Admin.CONFIRMUNSAVEDSHORT', 'WARNING: Your changes have not been saved.')));
}

return callback(true);
}

handleBeforeRoute(content, callback) {
if (this.shouldConfirmBeforeUnload()) {
return callback(confirm(i18n._t('Admin.CONFIRMUNSAVED', `Are you sure you want to navigate away
from this page?\n\nWARNING: Your changes have not been saved.\n\n
Press OK to continue, or Cancel to stay on the current page.`)));
}

return callback(true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither of these is needed anymore


export default function NavigationBlocker({ shouldBlockFn, blockMessage }) {
const blocker = useBlocker(shouldBlockFn);
useEffect(() => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has to be in an effect to avoid the console error I mentioned in #1458 (comment)

Comment on lines +9 to +10
const App = ({ children }) => (
<div className="app">{children}<Outlet /></div>
Copy link
Member Author

@GuySartorelli GuySartorelli Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to have the blocker as a child of the root component because it has to be a child of the RouteProvider and this is the only component we know for sure will be in there.
But because this replaceable via the reactRouteRegister we don't even know that the root component will specifically be App so we can't just slap it directly here, we have to pass it in as a child prop.

Comment on lines -74 to +75
"react-router": "^6.4.2",
"react-router-dom": "^6.4.2",
"react-router": "^6.7",
"react-router-dom": "^6.7",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new useBlocker hook was only introduced in 6.7 (I'll update the changelog as well to reflect the new dependency constraint)

Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally, works good, have confirmed it seems to behave the same was it did in 4.12

@emteknetnz emteknetnz merged commit 2cfc401 into silverstripe:2.0 Feb 27, 2023
@emteknetnz emteknetnz deleted the pulls/2.0/unsaved-changes-dialog branch February 27, 2023 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants