|
@@ -7,14 +7,71 @@
|
7
|
7
|
|
8
|
8
|
import SwiftUI
|
9
|
9
|
|
|
10
|
+class RegisterViewModel: ObservableObject {
|
|
11
|
+ @Published var response: Bool?
|
|
12
|
+
|
|
13
|
+ func changeResponse(response: Bool) {
|
|
14
|
+ self.response = response
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ func register(data: Registration) {
|
|
18
|
+
|
|
19
|
+ let service = "signup.php"
|
|
20
|
+ guard let encodedData = try? JSONEncoder().encode(data)
|
|
21
|
+ else {
|
|
22
|
+ print("Failed to encode register request")
|
|
23
|
+ return
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ print("Reg JSON to send:", String(data: encodedData, encoding: .utf8)!)
|
|
27
|
+
|
|
28
|
+ let url = URL(string: serverAddress + service)!
|
|
29
|
+ var request = URLRequest(url: url)
|
|
30
|
+ request.setValue("appregister/json", forHTTPHeaderField: "Content-Type")
|
|
31
|
+ request.httpMethod = "POST"
|
|
32
|
+ request.httpBody = encodedData
|
|
33
|
+
|
|
34
|
+ URLSession.shared.dataTask(with: request) {data, response, error in
|
|
35
|
+
|
|
36
|
+ //handle result here
|
|
37
|
+ guard let data = data
|
|
38
|
+ else {
|
|
39
|
+ print("No data in response: \(error?.localizedDescription ?? "Unknown error").")
|
|
40
|
+ return
|
|
41
|
+ }
|
|
42
|
+ print(response ?? "No response")
|
|
43
|
+ print("Recieved data:", data)
|
|
44
|
+ print(error ?? "No error")
|
|
45
|
+
|
|
46
|
+ if let httpResponse = response as? HTTPURLResponse {
|
|
47
|
+ DispatchQueue.main.async {
|
|
48
|
+ if httpResponse.statusCode == 200 {
|
|
49
|
+ self.changeResponse(response: true)
|
|
50
|
+ } else {
|
|
51
|
+ self.changeResponse(response: false)
|
|
52
|
+ }
|
|
53
|
+ }
|
|
54
|
+ }
|
|
55
|
+ }.resume()
|
|
56
|
+ }
|
|
57
|
+}
|
|
58
|
+
|
10
|
59
|
struct RegisterView: View {
|
11
|
|
- @State var firstName: String = ""
|
12
|
|
- @State var lastName: String = ""
|
|
60
|
+ @State var name: String = ""
|
13
|
61
|
@State var phoneNumber: String = ""
|
14
|
62
|
@State var email: String = ""
|
15
|
63
|
@State var password: String = ""
|
|
64
|
+ @State var urb: String = ""
|
|
65
|
+ @State var city: String = ""
|
|
66
|
+ @State var street: String = ""
|
|
67
|
+ @State var org: String = ""
|
|
68
|
+ @State var position: String = ""
|
|
69
|
+ @State var zip: String = ""
|
16
|
70
|
@Environment(\.colorScheme) var colorScheme
|
17
|
71
|
|
|
72
|
+ @ObservedObject var registerVM = RegisterViewModel()
|
|
73
|
+ @State private var showingAlert = false
|
|
74
|
+
|
18
|
75
|
var body: some View {
|
19
|
76
|
VStack {
|
20
|
77
|
Text("Details")
|
|
@@ -23,36 +80,90 @@ struct RegisterView: View {
|
23
|
80
|
.frame(width: 350, height: 25, alignment: .topLeading)
|
24
|
81
|
.padding(.bottom, 20)
|
25
|
82
|
|
26
|
|
- TextField("First Name", text: $firstName)
|
27
|
|
- .padding()
|
28
|
|
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
29
|
|
- .cornerRadius(5.0)
|
30
|
|
- .padding(.bottom, 5)
|
31
|
|
- TextField("Last Name", text: $lastName)
|
32
|
|
- .padding()
|
33
|
|
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
34
|
|
- .cornerRadius(5.0)
|
35
|
|
- .padding(.bottom, 5)
|
36
|
|
- TextField("Phone Number", text: $phoneNumber)
|
37
|
|
- .keyboardType(.numberPad)
|
38
|
|
- .padding()
|
39
|
|
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
40
|
|
- .cornerRadius(5.0)
|
41
|
|
- .padding(.bottom, 5)
|
42
|
|
- TextField("Email", text: $email)
|
43
|
|
- .autocapitalization(.none)
|
44
|
|
- .padding()
|
45
|
|
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
46
|
|
- .cornerRadius(5.0)
|
47
|
|
- .padding(.bottom, 5)
|
48
|
|
- SecureField("Password", text: $password)
|
49
|
|
- .autocapitalization(.none)
|
50
|
|
- .padding()
|
51
|
|
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
52
|
|
- .cornerRadius(5.0)
|
53
|
|
- .padding(.bottom, 20)
|
|
83
|
+ ScrollView {
|
|
84
|
+ Group {
|
|
85
|
+ TextField("Full Name", text: $name)
|
|
86
|
+ .padding()
|
|
87
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
88
|
+ .cornerRadius(5.0)
|
|
89
|
+ .padding(.bottom, 5)
|
|
90
|
+ TextField("Email", text: $email)
|
|
91
|
+ .autocapitalization(.none)
|
|
92
|
+ .padding()
|
|
93
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
94
|
+ .cornerRadius(5.0)
|
|
95
|
+ .padding(.bottom, 5)
|
|
96
|
+ SecureField("Password", text: $password)
|
|
97
|
+ .autocapitalization(.none)
|
|
98
|
+ .padding()
|
|
99
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
100
|
+ .cornerRadius(5.0)
|
|
101
|
+ .padding(.bottom, 5)
|
|
102
|
+ TextField("Phone Number", text: $phoneNumber)
|
|
103
|
+ .keyboardType(.numberPad)
|
|
104
|
+ .padding()
|
|
105
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
106
|
+ .cornerRadius(5.0)
|
|
107
|
+ .padding(.bottom, 5)
|
|
108
|
+ TextField("Urbanization", text: $urb)
|
|
109
|
+ .padding()
|
|
110
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
111
|
+ .cornerRadius(5.0)
|
|
112
|
+ .padding(.bottom, 5)
|
|
113
|
+ TextField("Street", text: $street)
|
|
114
|
+ .padding()
|
|
115
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
116
|
+ .cornerRadius(5.0)
|
|
117
|
+ .padding(.bottom, 5)
|
|
118
|
+ TextField("City", text: $city)
|
|
119
|
+ .padding()
|
|
120
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
121
|
+ .cornerRadius(5.0)
|
|
122
|
+ .padding(.bottom, 5)
|
|
123
|
+ TextField("Zip Code", text: $zip)
|
|
124
|
+ .keyboardType(.numberPad)
|
|
125
|
+ .padding()
|
|
126
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
127
|
+ .cornerRadius(5.0)
|
|
128
|
+ .padding(.bottom, 5)
|
|
129
|
+ TextField("Organization (optional)", text: $org)
|
|
130
|
+ .padding()
|
|
131
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
132
|
+ .cornerRadius(5.0)
|
|
133
|
+ .padding(.bottom, 5)
|
|
134
|
+ TextField("Position (optional)", text: $position)
|
|
135
|
+ .padding()
|
|
136
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
137
|
+ .cornerRadius(5.0)
|
|
138
|
+ //.padding(.bottom, 5)
|
|
139
|
+
|
|
140
|
+ }.padding(5)
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ if self.registerVM.response == false {
|
|
144
|
+ Text("Registration failed, please try again")
|
|
145
|
+ //.offset(y: -5)
|
|
146
|
+ .foregroundColor(.red)
|
|
147
|
+ } else if self.registerVM.response == true {
|
|
148
|
+ Text("Registration successful 🎉")
|
|
149
|
+ //.offset(y: -5)
|
|
150
|
+ .foregroundColor(.green)
|
|
151
|
+ }
|
|
152
|
+
|
54
|
153
|
|
55
|
|
- Button(action: {print("register tapped")}) {
|
|
154
|
+ Button(action: {
|
|
155
|
+ print("register tapped")
|
|
156
|
+
|
|
157
|
+ let registrationData = Registration(email: email, password: password, name: name, urb: urb, street: street, city: city, zip: zip, phone: phoneNumber, org: org, position: position)
|
|
158
|
+
|
|
159
|
+ if registrationData.isComplete() {
|
|
160
|
+ self.registerVM.register(data: registrationData)
|
|
161
|
+ } else {
|
|
162
|
+ // Alert here
|
|
163
|
+ self.showingAlert = true
|
|
164
|
+ }
|
|
165
|
+
|
|
166
|
+ }) {
|
56
|
167
|
Text("Register")
|
57
|
168
|
.font(.headline)
|
58
|
169
|
.foregroundColor(.white)
|
|
@@ -64,7 +175,9 @@ struct RegisterView: View {
|
64
|
175
|
}
|
65
|
176
|
|
66
|
177
|
|
67
|
|
- }.padding()
|
|
178
|
+ }.alert(isPresented: $showingAlert) {
|
|
179
|
+ Alert(title: Text("Fields incomplete"), message: Text("Please enter a all required information"), dismissButton: .default(Text("OK")))}
|
|
180
|
+ .padding()
|
68
|
181
|
}
|
69
|
182
|
}
|
70
|
183
|
|