Skip to content

Commit

Permalink
feat: add confirmation alert when attempting to nav away from geocodi…
Browse files Browse the repository at this point in the history
…ng page
  • Loading branch information
stdavis authored and steveoh committed Oct 12, 2021
1 parent 64a0d38 commit f6842ae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
14 changes: 13 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { app, BrowserWindow, ipcMain, shell } = require('electron');
const { app, BrowserWindow, dialog, ipcMain, shell } = require('electron');
const path = require('path');
require('./services/config');
require('./services/csv');
Expand Down Expand Up @@ -38,6 +38,18 @@ const createWindow = () => {
if (process.env.NODE_ENV === 'development') {
mainWindow.webContents.openDevTools();
}

ipcMain.handle('getUserConfirmation', (_, message) => {
const buttonIndex = dialog.showMessageBoxSync(mainWindow, {
message,
type: 'warning',
buttons: ['Yes', 'Cancel'],
defaultId: 1,
cancelId: 1,
});

return buttonIndex === 0;
});
};

// This method will be called when Electron has finished
Expand Down
19 changes: 11 additions & 8 deletions src/pages/Geocoding.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect, useRef } from 'react';
import { Link } from 'react-router-dom';
import { Link, Prompt } from 'react-router-dom';
import humanizeDuration from 'humanize-duration';
import { DocumentTextIcon } from '@heroicons/react/outline';
import { useGeocodeContext } from '../components/GeocodeContext.js';
Expand Down Expand Up @@ -108,13 +108,16 @@ export default function Geocoding() {
)}
</section>
{stats.status === 'running' ? (
<button
type="button"
onClick={cancel}
disabled={stats.rowsProcessed > 0 && stats.totalRows === stats.rowsProcessed}
>
Cancel
</button>
<>
<button
type="button"
onClick={cancel}
disabled={stats.rowsProcessed > 0 && stats.totalRows === stats.rowsProcessed}
>
Cancel
</button>
<Prompt message="Navigating to a different page will cancel the current geocoding process. Are you sure?" />
</>
) : null}
{stats.status === 'cancelled' ? (
<section className="flex flex-col justify-center p-6 bg-red-100 border border-red-200 rounded shadow">
Expand Down
5 changes: 4 additions & 1 deletion src/pages/Routes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { Chrome, Header, Footer } from '../components/PageElements';
export default function Routes() {
return (
<GeocodeContextProvider>
<Router defaultRoute>
<Router
defaultRoute
getUserConfirmation={(message, callback) => window.ugrc.getUserConfirmation(message).then(callback)}
>
<Header />
<Chrome>
<ScrollToTop />
Expand Down
1 change: 1 addition & 0 deletions src/services/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ contextBridge.exposeInMainWorld('ugrc', {
checkApiKey: (content) => ipcRenderer.invoke('checkApiKey', content),
getAppVersion: (content) => ipcRenderer.invoke('getAppVersion', content),
getAppInfo: (content) => ipcRenderer.invoke('getAppInfo', content),
getUserConfirmation: (content) => ipcRenderer.invoke('getUserConfirmation', content),
});

0 comments on commit f6842ae

Please sign in to comment.