説明なし

RegisterView.swift 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // RegisterView.swift
  3. // Comedores Sociales
  4. //
  5. // Created by Hector Carrion on 10/24/20.
  6. //
  7. import SwiftUI
  8. struct RegisterView: View {
  9. @EnvironmentObject var registerVM: RegisterViewModel
  10. @State var name: String = ""
  11. @State var phoneNumber: String = ""
  12. @State var email: String = ""
  13. @State var password: String = ""
  14. @State var passwordConf: String = ""
  15. @State var urb: String = ""
  16. @State var city: String = ""
  17. @State var street: String = ""
  18. @State var org: String = ""
  19. @State var position: String = ""
  20. @State var zip: String = ""
  21. @Environment(\.colorScheme) var colorScheme
  22. @State private var showingAlert = false
  23. @State private var passwordsAlert = false
  24. var body: some View {
  25. VStack {
  26. Text("Details")
  27. .font(.largeTitle)
  28. .fontWeight(.semibold)
  29. .frame(width: 350, height: 25, alignment: .topLeading)
  30. .padding(.bottom, 20)
  31. ScrollView {
  32. Group {
  33. Group {
  34. TextField("Full Name", text: $name)
  35. .autocapitalization(.words)
  36. .padding()
  37. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  38. .cornerRadius(5.0)
  39. .padding(.bottom, 5)
  40. TextField("Email", text: $email)
  41. .disableAutocorrection(true)
  42. .autocapitalization(.none)
  43. .padding()
  44. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  45. .cornerRadius(5.0)
  46. .padding(.bottom, 5)
  47. SecureField("Password", text: $password)
  48. .autocapitalization(.none)
  49. .padding()
  50. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  51. .cornerRadius(5.0)
  52. .padding(.bottom, 5)
  53. SecureField("Password (confirmation)", text: $passwordConf)
  54. .autocapitalization(.none)
  55. .padding()
  56. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  57. .cornerRadius(5.0)
  58. .padding(.bottom, 5)
  59. }
  60. TextField("Phone Number", text: $phoneNumber)
  61. .keyboardType(.numberPad)
  62. .padding()
  63. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  64. .cornerRadius(5.0)
  65. .padding(.bottom, 5)
  66. TextField("Urbanization", text: $urb)
  67. .padding()
  68. .autocapitalization(.words)
  69. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  70. .cornerRadius(5.0)
  71. .padding(.bottom, 5)
  72. TextField("Street", text: $street)
  73. .autocapitalization(.words)
  74. .padding()
  75. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  76. .cornerRadius(5.0)
  77. .padding(.bottom, 5)
  78. TextField("City", text: $city)
  79. .autocapitalization(.words)
  80. .padding()
  81. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  82. .cornerRadius(5.0)
  83. .padding(.bottom, 5)
  84. TextField("Zip Code", text: $zip)
  85. .keyboardType(.numberPad)
  86. .padding()
  87. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  88. .cornerRadius(5.0)
  89. .padding(.bottom, 5)
  90. TextField("Organization (optional)", text: $org)
  91. .autocapitalization(.words)
  92. .padding()
  93. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  94. .cornerRadius(5.0)
  95. .padding(.bottom, 5)
  96. TextField("Position (optional)", text: $position)
  97. .padding()
  98. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  99. .cornerRadius(5.0)
  100. //.padding(.bottom, 5)
  101. }.padding(5)
  102. }
  103. if registerVM.response == false {
  104. Text("Registration failed, please try again")
  105. //.offset(y: -5)
  106. .foregroundColor(.red)
  107. } else if registerVM.response == true {
  108. Text("Registration successful 🎉")
  109. //.offset(y: -5)
  110. .foregroundColor(.green)
  111. }
  112. Button(action: {
  113. print("register tapped")
  114. let registrationData = Registration(email: email, password: password, name: name, urb: urb, street: street, city: city, zip: zip, phone: phoneNumber, org: org, position: position)
  115. if password == passwordConf {
  116. if registrationData.isComplete() {
  117. registerVM.register(data: registrationData)
  118. } else {
  119. // Alert here
  120. self.showingAlert = true
  121. }
  122. } else {
  123. // Alert here
  124. self.passwordsAlert = true
  125. }
  126. }) {
  127. Text("Register")
  128. .font(.headline)
  129. .foregroundColor(.white)
  130. .padding()
  131. .frame(width: 220, height: 50)
  132. .background(Color.blue)
  133. .cornerRadius(5)
  134. .padding(.bottom, 5)
  135. }
  136. }.alert(isPresented: $showingAlert) {
  137. Alert(title: Text("Fields incomplete"), message: Text("Please enter a all required information"), dismissButton: .default(Text("OK")))}
  138. .alert(isPresented: $passwordsAlert) {
  139. Alert(title: Text("Passwords do not match"), message: Text("Please retype your password and try again"), dismissButton: .default(Text("OK")))}
  140. .padding()
  141. }
  142. }
  143. struct RegisterView_Previews: PreviewProvider {
  144. static var previews: some View {
  145. RegisterView()
  146. }
  147. }