You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
3.9 KiB
135 lines
3.9 KiB
//
|
|
// UserCreationView.swift
|
|
// PadelClub
|
|
//
|
|
// Created by Laurent Morvillier on 13/02/2024.
|
|
//
|
|
|
|
import SwiftUI
|
|
import LeStorage
|
|
|
|
struct UserCreationView: View {
|
|
|
|
@State var username: String = ""
|
|
@State var password1: String = ""
|
|
@State var password2: String = ""
|
|
@State var email: String = ""
|
|
|
|
@State var firstName: String = ""
|
|
@State var lastName: String = ""
|
|
@State var phone: String = ""
|
|
|
|
@State var showUnmatchingPasswordView = false
|
|
@State var selectedCountryIndex = 0
|
|
|
|
let countries: [String] = Locale.countries()
|
|
|
|
@State var isLoading = false
|
|
@State var showLoginScreen: Bool = false
|
|
|
|
var body: some View {
|
|
|
|
Form {
|
|
|
|
Section {
|
|
TextField("Username", text: self.$username)
|
|
.autocorrectionDisabled()
|
|
.textInputAutocapitalization(.never)
|
|
TextField("Email", text: self.$email)
|
|
.keyboardType(.emailAddress)
|
|
.textInputAutocapitalization(.never)
|
|
|
|
}
|
|
|
|
Section {
|
|
SecureField("Password", text: self.$password1)
|
|
SecureField("Confirm password", text: self.$password2)
|
|
}
|
|
|
|
Section {
|
|
TextField("First Name", text: self.$firstName)
|
|
.autocorrectionDisabled()
|
|
TextField("Last Name", text: self.$lastName)
|
|
.autocorrectionDisabled()
|
|
TextField("Phone", text: self.$phone)
|
|
.autocorrectionDisabled()
|
|
Picker("Select a country", selection: $selectedCountryIndex) {
|
|
ForEach(0..<self.countries.count, id: \.self) { index in
|
|
Text(self.countries[index]).tag(index)
|
|
}
|
|
}
|
|
.pickerStyle(DefaultPickerStyle())
|
|
.padding()
|
|
}
|
|
|
|
Section {
|
|
Button(action: {
|
|
self._create()
|
|
}, label: {
|
|
if self.isLoading {
|
|
ProgressView()
|
|
} else {
|
|
Text("Create")
|
|
}
|
|
})
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
}
|
|
.onAppear {
|
|
self._selectCountry()
|
|
}
|
|
.alert("Password do not match", isPresented: self.$showUnmatchingPasswordView, actions: {
|
|
Button("Ok", action: {})
|
|
} )
|
|
.navigationTitle("Create user")
|
|
}
|
|
|
|
fileprivate func _selectCountry() {
|
|
guard let regionCode = Locale.current.region?.identifier, let country = Locale.current.localizedString(forRegionCode: regionCode) else {
|
|
return
|
|
}
|
|
|
|
self.selectedCountryIndex = self.countries.firstIndex(of: country) ?? 0
|
|
}
|
|
|
|
fileprivate func _create() {
|
|
|
|
guard self.password1 == self.password2 else {
|
|
self.showUnmatchingPasswordView = true
|
|
return
|
|
}
|
|
|
|
guard let service = Store.main.service else {
|
|
return
|
|
}
|
|
|
|
self.isLoading = true
|
|
|
|
Task {
|
|
do {
|
|
let userCreationForm = UserCreationForm(
|
|
username: self.username,
|
|
password: self.password1,
|
|
firstName: self.firstName,
|
|
lastName: self.lastName,
|
|
email: self.email,
|
|
phone: self.phone,
|
|
country: self.countries[self.selectedCountryIndex])
|
|
|
|
let _: User = try await service.createAccount(user: userCreationForm)
|
|
|
|
|
|
|
|
} catch {
|
|
self.isLoading = false
|
|
Logger.error(error)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#Preview {
|
|
UserCreationView()
|
|
}
|
|
|