aboutsummaryrefslogtreecommitdiff
path: root/go-src
diff options
context:
space:
mode:
Diffstat (limited to 'go-src')
-rw-r--r--go-src/windows.go39
-rw-r--r--go-src/windowsHelpers.go36
2 files changed, 48 insertions, 27 deletions
diff --git a/go-src/windows.go b/go-src/windows.go
index 31c2fb0..5463cd2 100644
--- a/go-src/windows.go
+++ b/go-src/windows.go
@@ -1,7 +1,6 @@
package ctfc
import (
- "strconv"
"strings"
"gitlab.com/Syndamia/ctfc/go-src/csi"
@@ -254,7 +253,6 @@ func directMessagesWindow(values ...string) {
defer nextWindow()
}
lastLine = -2
-
}
func directMessageWindow(values ...string) {
@@ -341,31 +339,18 @@ func accountWindow(values ...string) {
)
if nextWindow == nil {
- userProp, _ := strconv.Atoi(input)
- inputs, updateSuccessful := []formInput{{currentPasswordInName, inputBackSpec, nil}}, false
-
- switch UserProp(userProp) {
- case passwordProp:
- values := formWindow(editPasswordNavTitle, accountWindow,
- append(inputs, formInput{newPasswordInName, passwordSpec, stringValidPassword}),
- )
- updateSuccessful = loggedInUser.UpdatePassword(values[0], values[1])
- case nameProp:
- values := formWindow(editNameNavTitle, accountWindow,
- append(inputs, formInput{newNameInName, nameSpec, stringValidName}),
- )
- updateSuccessful = loggedInUser.UpdateName(values[0], values[1])
- loggedInUser.Name = utils.If(updateSuccessful).String(values[1], loggedInUser.Name)
- default:
- defer showError(invalidCommand, accountWindow)
- return
- }
-
- if !updateSuccessful {
- defer showError(invalidArgument, accountWindow)
- } else {
- defer accountWindow()
- }
+ defer validatedMultiForm(input, accountWindow,
+ multiFormProp{int(passwordProp), editPasswordNavTitle,
+ formInput{newPasswordInName, passwordSpec, stringValidPassword},
+ func(values []string) bool { return loggedInUser.UpdatePassword(values[0], values[1]) },
+ nil,
+ },
+ multiFormProp{int(nameProp), editNameNavTitle,
+ formInput{newNameInName, nameSpec, stringValidName},
+ func(values []string) bool { return loggedInUser.UpdateName(values[0], values[1]) },
+ func(values []string) { loggedInUser.Name = values[1] },
+ },
+ )
} else {
defer nextWindow()
}
diff --git a/go-src/windowsHelpers.go b/go-src/windowsHelpers.go
index 761e0d2..f59f34f 100644
--- a/go-src/windowsHelpers.go
+++ b/go-src/windowsHelpers.go
@@ -161,6 +161,42 @@ func formWindow(boxTitle string, backWindow window, formInputs []formInput, curr
return
}
+type multiFormProp struct {
+ propInd int
+ editTitle string
+ formInp formInput
+ updateF func([]string) bool
+ postSucUpdateF func([]string)
+}
+
+func validatedMultiForm(input string, returnWindow window, props ...multiFormProp) {
+ propInd, _ := strconv.Atoi(input)
+ inputs, triedUpdate, updateSuccessful := []formInput{{currentPasswordInName, inputBackSpec, nil}}, false, false
+
+ for _, v := range props {
+ if v.propInd == propInd {
+ triedUpdate = true
+
+ values := formWindow(v.editTitle, returnWindow, append(inputs, v.formInp))
+ updateSuccessful = v.updateF(values)
+
+ if updateSuccessful && v.postSucUpdateF != nil {
+ v.postSucUpdateF(values)
+ }
+ break
+ }
+ }
+
+ if !triedUpdate {
+ defer showError(invalidCommand, returnWindow)
+ }
+ if !updateSuccessful {
+ defer showError(invalidArgument, returnWindow)
+ } else {
+ defer returnWindow()
+ }
+}
+
/* Error */
func showError(message string, callback window, callbackData ...string) {