123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- //
- // RegisterView.swift
- // Comedores Sociales
- //
- // Created by Hector Carrion on 10/24/20.
- //
-
- import SwiftUI
-
- class RegisterViewModel: ObservableObject {
- @Published var response: Bool?
-
- func changeResponse(response: Bool) {
- self.response = response
- }
-
- func register(data: Registration) {
-
- let service = "signup.php"
- guard let encodedData = try? JSONEncoder().encode(data)
- else {
- print("Failed to encode register request")
- return
- }
-
- print("Reg JSON to send:", String(data: encodedData, encoding: .utf8)!)
-
- let url = URL(string: serverAddress + service)!
- var request = URLRequest(url: url)
- request.setValue("appregister/json", forHTTPHeaderField: "Content-Type")
- request.httpMethod = "POST"
- request.httpBody = encodedData
-
- URLSession.shared.dataTask(with: request) {data, response, error in
-
- //handle result here
- guard let data = data
- else {
- print("No data in response: \(error?.localizedDescription ?? "Unknown error").")
- return
- }
- print(response ?? "No response")
- print("Recieved data:", data)
- print(error ?? "No error")
-
- if let httpResponse = response as? HTTPURLResponse {
- DispatchQueue.main.async {
- if httpResponse.statusCode == 200 {
- self.changeResponse(response: true)
- } else {
- self.changeResponse(response: false)
- }
- }
- }
- }.resume()
- }
- }
-
- struct RegisterView: View {
- @State var name: String = ""
- @State var phoneNumber: String = ""
- @State var email: String = ""
- @State var password: String = ""
- @State var urb: String = ""
- @State var city: String = ""
- @State var street: String = ""
- @State var org: String = ""
- @State var position: String = ""
- @State var zip: String = ""
- @Environment(\.colorScheme) var colorScheme
-
- @ObservedObject var registerVM = RegisterViewModel()
- @State private var showingAlert = false
-
- var body: some View {
- VStack {
- Text("Details")
- .font(.largeTitle)
- .fontWeight(.semibold)
- .frame(width: 350, height: 25, alignment: .topLeading)
- .padding(.bottom, 20)
-
- ScrollView {
- Group {
- TextField("Full Name", text: $name)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- .padding(.bottom, 5)
- TextField("Email", text: $email)
- .autocapitalization(.none)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- .padding(.bottom, 5)
- SecureField("Password", text: $password)
- .autocapitalization(.none)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- .padding(.bottom, 5)
- TextField("Phone Number", text: $phoneNumber)
- .keyboardType(.numberPad)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- .padding(.bottom, 5)
- TextField("Urbanization", text: $urb)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- .padding(.bottom, 5)
- TextField("Street", text: $street)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- .padding(.bottom, 5)
- TextField("City", text: $city)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- .padding(.bottom, 5)
- TextField("Zip Code", text: $zip)
- .keyboardType(.numberPad)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- .padding(.bottom, 5)
- TextField("Organization (optional)", text: $org)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- .padding(.bottom, 5)
- TextField("Position (optional)", text: $position)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- //.padding(.bottom, 5)
-
- }.padding(5)
- }
-
- if self.registerVM.response == false {
- Text("Registration failed, please try again")
- //.offset(y: -5)
- .foregroundColor(.red)
- } else if self.registerVM.response == true {
- Text("Registration successful 🎉")
- //.offset(y: -5)
- .foregroundColor(.green)
- }
-
-
- Button(action: {
- print("register tapped")
-
- let registrationData = Registration(email: email, password: password, name: name, urb: urb, street: street, city: city, zip: zip, phone: phoneNumber, org: org, position: position)
-
- if registrationData.isComplete() {
- self.registerVM.register(data: registrationData)
- } else {
- // Alert here
- self.showingAlert = true
- }
-
- }) {
- Text("Register")
- .font(.headline)
- .foregroundColor(.white)
- .padding()
- .frame(width: 220, height: 50)
- .background(Color.blue)
- .cornerRadius(5)
- .padding(.bottom, 5)
- }
-
-
- }.alert(isPresented: $showingAlert) {
- Alert(title: Text("Fields incomplete"), message: Text("Please enter a all required information"), dismissButton: .default(Text("OK")))}
- .padding()
- }
- }
-
- struct RegisterView_Previews: PreviewProvider {
- static var previews: some View {
- RegisterView()
- }
- }
|