// // CreateAccountView.swift // JobTrackerPro // // Created by Jake Steirer on 12/30/23. // import SwiftUI struct CreateAccountView: View { @ObservedObject var viewModel: JobApplicationViewModel @State private var email: String = "" @State private var password: String = "" @State private var confirmPassword: String = "" @State private var storedEmail: String = "" // Separate variable to hold the email after editing ends @State private var storedPassword: String = "" // Separate variable for the password @State private var storedConfirmPassword: String = "" // Separate variable for the confirm password @State private var isShowingAlert = false @State private var alertMessage = "" @FocusState private var isInputActive: Bool // For managing keyboard focus var body: some View { NavigationView { VStack { TextField("Email", text: $email) .autocapitalization(.none) .keyboardType(.emailAddress) .disableAutocorrection(true) .padding() .border(Color(UIColor.separator)) .focused($isInputActive) // Binding focus state .onChange(of: email) { [email] in storedEmail = email } .onSubmit { storedEmail = email // Store the email when submit the field } SecureField("Password", text: $password) .padding() .border(Color(UIColor.separator)) .focused($isInputActive) // Binding focus state .onChange(of: password) { [password] in storedPassword = password } .onSubmit { storedPassword = password // Store the password when submit the field } SecureField("Confirm Password", text: $confirmPassword) .padding() .border(Color(UIColor.separator)) .focused($isInputActive) // Binding focus state .onChange(of: confirmPassword) { [confirmPassword] in storedConfirmPassword = confirmPassword } .onSubmit { storedConfirmPassword = confirmPassword // Store the confirm password when submit the field } Button("Create Account") { isInputActive = false // Dismiss the keyboard before account creation // Perform input validation, check for matching passwords, etc. guard storedPassword == storedConfirmPassword else { alertMessage = "Passwords do not match." isShowingAlert = true return } // Call the create account function in the view model viewModel.createAccount(email: storedEmail, password: storedPassword) { success, message in if !success { alertMessage = message isShowingAlert = true } } } .padding() .alert(isPresented: $isShowingAlert) { Alert(title: Text("Account Creation Failed"), message: Text(alertMessage), dismissButton: .default(Text("OK"))) } } .navigationBarTitle("Create Account", displayMode: .large) .padding() } } }