|
|
|
|
@ -11,18 +11,53 @@ import LeStorage |
|
|
|
|
struct UserCreationView: View { |
|
|
|
|
|
|
|
|
|
@State var username: String = "" |
|
|
|
|
@State var password: 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() |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TextField("Username", text: self.$username) |
|
|
|
|
.autocorrectionDisabled() |
|
|
|
|
TextField("Password", text: self.$password) |
|
|
|
|
.autocorrectionDisabled() |
|
|
|
|
TextField("Email", text: self.$email) |
|
|
|
|
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: { |
|
|
|
|
@ -33,10 +68,30 @@ struct UserCreationView: View { |
|
|
|
|
.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 |
|
|
|
|
} |
|
|
|
|
@ -44,10 +99,17 @@ struct UserCreationView: View { |
|
|
|
|
do { |
|
|
|
|
let userCreationForm = UserCreationForm( |
|
|
|
|
username: self.username, |
|
|
|
|
password: self.password, |
|
|
|
|
email: self.email) |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
// let _ = try await service.requestToken(username: self.username, password: self.password1) |
|
|
|
|
|
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
@ -55,6 +117,7 @@ struct UserCreationView: View { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#Preview { |
|
|
|
|
|