How to prevent reload and show custom popup? - reactjs

I have a form, when my user tries to reload, I need to show the popup "If you reload the page you cannot use this form".
the page is reloaded using a button in browsers
How to prevent reload and show custom popup ?

The browsers are designed to ask for confirmation with the native popup https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
And this cannot be customized (which would require async handling)
If the latter was possible people would get stuck on porn pages and would never have the chance to leave them.

Related

How to trigger a delayed function call after you have navigated away from the page in React

So I have a really ask but the implementation is eluding me.
I want to simulate the completion of a task in the background in our product demo.
The User Journey :
Arrive at the home page
Click button on the home page -> Button goes into loading state (button replaced by loading animation)
We continue on with the demo and navigate away
We want to come back to the home page after "execution complete" .
Well how do we know if execution is complete ? Well that's where this delayed alert I'm trying to implement comes in. I'd like an alert to be sent out , regardless of what page I am on in the app after some x minutes/seconds of clicking that button.
Any ideas on how I would go about doing this ?
I am trying with redux-toolkit but so far it's only working on the page itself and
if I return to the page after navigating away.
This can be done in two ways,
elevate the state to the point which covers both home page and all the navigation pages, Use react context API to pass data and use useEffect hook to track task completion and do the need full.
Use native javascript to trigger custom event and add an event listner to it Read more about curtom events here
Although this is all just theory, I can help more after viewing the code.

Web Page Refreshing on keyboard pop

I have implemented a login page in React and Material UI. When using in mobile, on certain phones the page refreshes automatically as soon as focus from one textfield goes away, and in another scenario, as soon as the keyboard pops up it refreshes. Anyone who has faced a similar issue, or any clue why this could be happening.
I tried calling the preventDefault method for onBlur event, but no change. Couldn't think of anything else, since observed the issue on some devices and not on others

Detect when a user leaves the single page app

I want to pop a modal with "Cancel" and "Yes" buttons when a user attempts to leave the single-page app.
To provide some context, I am working on a single page app that appears on a larger site that is not part of the single page app (so, the header and footer is part of the larger site, and single-page-app is the content). When a user attempts to leave to another part of the site (by clicking somewhere in the nav for instance) that is not part of the single page app, I want a warning modal to display with a warning. A user can close or refresh the page as much as they want, and it shouldn't pop a modal. Same thing if a user hard-codes a url (say, google.com in the browser), the modal should not pop.
So far the other similar threads I found on stackoverflow either uses Prompt component, or window.onbeforeunload. Both of these have the issue of them popping a default text box with a warning, and not a custom modal, which is an important problem, but that isn't all of the issue.
Prompt, to my understanding triggers whenever a user redirects within the single page app, which is not the behavior that I want.
window.onbeforeunload, to my undertstanding triggers even when refresh or close, which is not the behavior I want.
Maybe this isn't such a common problem, since I can't seem to find an easy solution for this, but any and all ideas are greatly appreciated.

Redirecting to previous page without reloading

I have a question that suppose, I have an Admin Dashboard page, on which I have given a list of users. clicking upon a particular user the details of corresponding user is shown on a different page (say on userDetails.jsp). Now I have a back button on userDetails page, Clicking of which I need to redirect admin to the last visited page that is Admin Dashboard page, I have one solution that I can "Reload" the previos page on clicking back button, but then I feel that there is the overhead of calling a server side API, retrieving all the data, Again redraw the whole admin dashboard page even when there is no change made in the userDetails[because it is read only page, modification is not allowed there]. Is there anyway that I can make user of cache or anything else so that I will not need to go to the server to fetch all the data again. I am using spring MVC, angularjs, Thank you.
On your button's click event add the following code:
$window.history.back();
make sure to use $window rather than window since you're using AngularJS

How should I use route&view

Background:
I am using backbone.js & Twitter Bootstrap in my client-end page.
On clicking the logout button on header, a confirmation dialog should open.
The question is that
should I use router such as /logout to change to logoutView ?
If click No in the dialog, how could I show the main content with data before the dialog is opened.
Thanks!
Yes, you can use a router and you should.
First thing to know, is you have to render application's layout before dispatching any route, because the layout is rendered and needed for every action, so it's independant, right ?
Second you create a "logout" route in your router and give it the "#logout" hash, then in your "logout" action you open the modal.
Don't use router for such thing. Just fire the modal directly because:
On changing the router, you are gonna push that to the History. Hitting the browser's back button shouldn't really open a modal window.
URLs should be crafted in a way to be bookmarked. You don't want a URL that would open a popup or a modal window!
It's much simpler just to start the modal than to create a variable to hold the previous view and to fall back to it when clicking No
I have build client-side apps using different MVC frameworks like AngularJS and Backbone.js. Every time I faced the same situation you are talking about and found that the easiest and most accurate way is to just show the modal.
UPDATE
Please watch this. This is Jeremy Ashkenas the author of backbone.js stating exactly your situation about how should URLs be used and weather if they should be used to open a pop up or not.

Resources