From 5a5ef110e7994dec1eb3b85774cb838b9186ee8f Mon Sep 17 00:00:00 2001 From: theBGuy <60308670+theBGuy@users.noreply.github.com> Date: Tue, 20 Feb 2024 01:25:22 -0500 Subject: [PATCH] Add confirmation action method (#6) - Add `ConfirmAction` method that uses the `runtime.MessageDialog` component for confirming an action - Use `ConfirmAction` before deleting an organization as this is a destructive action, and should be sure the user meant to do so --- app.go | 15 +++++++++++++++ frontend/src/App.tsx | 30 +++++++++++++++++++----------- frontend/wailsjs/go/main/App.d.ts | 2 ++ frontend/wailsjs/go/main/App.js | 4 ++++ 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/app.go b/app.go index 6b27959..29b06b9 100644 --- a/app.go +++ b/app.go @@ -223,6 +223,21 @@ func (a *App) ShowWindow() { runtime.WindowShow(a.ctx) } +// Display a confirmation dialog +func (a *App) ConfirmAction(title string, message string) bool { + selection, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ + Type: runtime.QuestionDialog, + Title: title, + Message: message, + DefaultButton: "No", + }) + if err != nil { + fmt.Println(err) + return false + } + return selection == "Yes" +} + // GetWorkTime returns the total seconds worked func (a *App) GetWorkTime(date string, organization string) (seconds int, err error) { if date == "" || organization == "" { diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 4bb2b04..bc03eb2 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -54,7 +54,8 @@ import { GetMonthlyWorkTime, GetVersion, UpdateAvailable, - ShowWindow + ShowWindow, + ConfirmAction } from "../wailsjs/go/main/App"; function App() { @@ -215,17 +216,24 @@ function App() { }; const handleDeleteOrganization = () => { - // TODO: Are you sure you want to delete this organization? This is a destructive action we should double check with the user - handleMenuClose(); - if (organizations.length === 1) { - toast.error("You cannot delete the last organization"); - return; - } - DeleteOrganization(selectedOrganization).then(() => { - setOrganizations(orgs => orgs.filter(org => org !== selectedOrganization)); - setSelectedOrganization(organizations[0]); + // if (window.confirm("Are you sure you want to delete this organization?") === false) { + // return; + // } + ConfirmAction(`Delete ${selectedOrganization}`, "Are you sure you want to delete this organization?").then((confirmed) => { + if (confirmed === false) { + return; + } + handleMenuClose(); + if (organizations.length === 1) { + toast.error("You cannot delete the last organization"); + return; + } + DeleteOrganization(selectedOrganization).then(() => { + setOrganizations(orgs => orgs.filter(org => org !== selectedOrganization)); + setSelectedOrganization(organizations[0]); + }); + SetOrganization(selectedOrganization); }); - SetOrganization(selectedOrganization); }; const handleUpdateSettings = () => { diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index 1a0d80f..a90bef8 100644 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -2,6 +2,8 @@ // This file is automatically generated. DO NOT EDIT import {time} from '../models'; +export function ConfirmAction(arg1:string,arg2:string):Promise; + export function DeleteOrganization(arg1:string):Promise; export function ExportCSVByMonth(arg1:string,arg2:number,arg3:time.Month):Promise; diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js index 21fdf8c..21b14c8 100644 --- a/frontend/wailsjs/go/main/App.js +++ b/frontend/wailsjs/go/main/App.js @@ -2,6 +2,10 @@ // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // This file is automatically generated. DO NOT EDIT +export function ConfirmAction(arg1, arg2) { + return window['go']['main']['App']['ConfirmAction'](arg1, arg2); +} + export function DeleteOrganization(arg1) { return window['go']['main']['App']['DeleteOrganization'](arg1); }