Skip to content

Commit

Permalink
[#73] 회원가입 API연동 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
00yhsp committed Feb 9, 2024
1 parent d2f94f3 commit 4a5c28a
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 28 deletions.
8 changes: 8 additions & 0 deletions Spon-us.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
DF90A5BE2B6E39A600BC54D0 /* RegisterPWView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF90A5BD2B6E39A600BC54D0 /* RegisterPWView.swift */; };
DF90A5C02B6E534500BC54D0 /* SelectUserTypeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF90A5BF2B6E534500BC54D0 /* SelectUserTypeView.swift */; };
DF91F4042B4864330021291A /* Portfolio.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF91F4032B4864330021291A /* Portfolio.swift */; };
DFB22ED72B766FF3007903DF /* JoinModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DFB22ED62B766FF3007903DF /* JoinModel.swift */; };
DFB22ED92B767003007903DF /* JoinViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DFB22ED82B767003007903DF /* JoinViewModel.swift */; };
DFCD247C2B54D08100B00515 /* PopupView in Frameworks */ = {isa = PBXBuildFile; productRef = DFCD247B2B54D08100B00515 /* PopupView */; };
DFCD24822B5A67FC00B00515 /* DocumentPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = DFCD24812B5A67FC00B00515 /* DocumentPicker.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -140,6 +142,8 @@
DF90A5BD2B6E39A600BC54D0 /* RegisterPWView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegisterPWView.swift; sourceTree = "<group>"; };
DF90A5BF2B6E534500BC54D0 /* SelectUserTypeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectUserTypeView.swift; sourceTree = "<group>"; };
DF91F4032B4864330021291A /* Portfolio.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Portfolio.swift; sourceTree = "<group>"; };
DFB22ED62B766FF3007903DF /* JoinModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JoinModel.swift; sourceTree = "<group>"; };
DFB22ED82B767003007903DF /* JoinViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JoinViewModel.swift; sourceTree = "<group>"; };
DFCD24812B5A67FC00B00515 /* DocumentPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentPicker.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -174,6 +178,8 @@
children = (
100A1E372B734DB200AAC1E8 /* EmailModel.swift */,
100A1E392B734DDE00AAC1E8 /* EmailViewModel.swift */,
DFB22ED62B766FF3007903DF /* JoinModel.swift */,
DFB22ED82B767003007903DF /* JoinViewModel.swift */,
);
path = Onboarding;
sourceTree = "<group>";
Expand Down Expand Up @@ -502,6 +508,7 @@
100D38B32B4EF15600498977 /* HomeView.swift in Sources */,
10C8AC632B5C34C400B40547 /* CompanyPostView.swift in Sources */,
DF3AD3DF2B61626300DBFCCC /* OnBoardingView.swift in Sources */,
DFB22ED92B767003007903DF /* JoinViewModel.swift in Sources */,
100A1E302B71637A00AAC1E8 /* MyProfileView.swift in Sources */,
3BFC8D1F2B5597C9000D6006 /* ProfileView.swift in Sources */,
3B36F09C2B6FEBA00000ACFB /* ChargerInfoViewTest.swift in Sources */,
Expand All @@ -524,6 +531,7 @@
3B36F0A82B6FEC3C0000ACFB /* PubData.swift in Sources */,
3B36F09A2B6FEB800000ACFB /* PaymentSettingView.swift in Sources */,
DFCD24822B5A67FC00B00515 /* DocumentPicker.swift in Sources */,
DFB22ED72B766FF3007903DF /* JoinModel.swift in Sources */,
3B36F0A32B6FEC0B0000ACFB /* PortOneViewModel.swift in Sources */,
3B36F09E2B6FEBBC0000ACFB /* PaymentView.swift in Sources */,
80FA316D2B5EBA6B0076453B /* SearchOfferView.swift in Sources */,
Expand Down
18 changes: 18 additions & 0 deletions Spon-us/Model/Onboarding/JoinModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// JoinModel.swift
// Spon-us
//
// Created by 박현수 on 2/9/24.
//

import Foundation

struct JoinContent: Codable {
let id: Int
let email, name: String
}

struct JoinModel: Codable {
let statusCode, message: String
let content: JoinContent
}
38 changes: 38 additions & 0 deletions Spon-us/Model/Onboarding/JoinViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// JoinViewModel.swift
// Spon-us
//
// Created by 박현수 on 2/9/24.
//

import Foundation
import Combine
import Moya

class JoinViewModel: ObservableObject {
@Published var join: JoinModel?
@Published var isButtonEnabled = false
private let provider = MoyaProvider<SponusAPI>(plugins: [NetworkLoggerPlugin()])

func postJoin(name: String, email: String, password: String, orgType: OrgType, subOrgType: SubOrgType?) {
provider.request(.postJoin(name: name, email: email, password: password, orgType: orgType, subOrgType: subOrgType)) { result in
switch result {
case let .success(response):
do {
if response.statusCode == 200 {
self.isButtonEnabled = true
}
print(self.isButtonEnabled)
print(try response.mapJSON())
let joinResponse = try response.map(JoinModel.self)
self.join = joinResponse
} catch {
print("Error parsing response: \(error)")
}

case let .failure(error):
print("Network request failed: \(error)")
}
}
}
}
51 changes: 50 additions & 1 deletion Spon-us/SponusAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,25 @@ import Foundation
import Moya
import KeychainSwift

enum OrgType {
case student, company
}

enum SubOrgType {
case studentCouncil, studentClub
}

struct JoinRequestBody: Codable {
let name: String
let email: String
let password: String
let organizationType: String
let suborganizationType: String?
}

enum SponusAPI {
case postEmail(email: String)
case postJoin(name: String, email: String, password: String, orgType: OrgType, subOrgType: SubOrgType?)
}

extension SponusAPI: TargetType {
Expand All @@ -20,22 +37,37 @@ extension SponusAPI: TargetType {

var path: String {
switch self {
case .postEmail(let email):
case .postEmail:
return "/api/v1/organizations/email"
case .postJoin:
return "/api/v1/organizations/join"
}
}

var method: Moya.Method {
switch self {
case .postEmail:
return .post
case .postJoin:
return .post
}
}

var sampleData: Data {
switch self {
case .postEmail:
return Data()
case .postJoin:
let response: [String : Any] = [
"statusCode": "OK",
"message": "OK",
"content": [
"id": 0,
"email": "[email protected]",
"name": "test"
]
]
return try! JSONSerialization.data(withJSONObject: response, options: .prettyPrinted)
}
}

Expand All @@ -44,13 +76,30 @@ extension SponusAPI: TargetType {
case .postEmail(let email):
let parameters: [String: Any] = ["email": email]
return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
case .postJoin(let name, let email, let password, let orgType, let subOrgType):
switch orgType {
case .student:
switch subOrgType! {
case .studentCouncil:
let requestBody = JoinRequestBody(name: name, email: email, password: password, organizationType: "STUDENT", suborganizationType: "STUDENT_COUNCIL")
return .requestJSONEncodable(requestBody)
case .studentClub:
let requestBody = JoinRequestBody(name: name, email: email, password: password, organizationType: "STUDENT", suborganizationType: "STUDENT_CLUB")
return .requestJSONEncodable(requestBody)
}
case .company:
let requestBody = JoinRequestBody(name: name, email: email, password: password, organizationType: "STUDENT", suborganizationType: nil)
return .requestJSONEncodable(requestBody)
}
}
}

var headers: [String: String]? {
switch self {
case .postEmail:
return nil
case .postJoin:
return nil
/*
case .postLike:
return ["Content-Type": "application/json",
Expand Down
4 changes: 2 additions & 2 deletions Spon-us/View/Onboarding/RegisterIDView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ struct RegisterIDView: View {
Spacer()

NavigationLink {
RegisterPWView()
RegisterPWView(userID: userID)
} label: {
Text("다음").font(.Body04).frame(maxWidth: .infinity).frame(height: 56).foregroundStyle(.sponusWhite).background(!isAuthenticated ? .sponusGrey600 : .sponusPrimary).padding(.bottom, 16)
}.disabled(!isAuthenticated)
}//.disabled(!isAuthenticated)
}.navigationBarTitleDisplayMode(.inline)
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: CustomBackButton())
Expand Down
5 changes: 3 additions & 2 deletions Spon-us/View/Onboarding/RegisterPWView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import SwiftUI

struct RegisterPWView: View {
@State var userID: String
@State private var userPW = ""
@State private var userPWConfirm = ""
@State private var isPWSecure = true
Expand Down Expand Up @@ -98,7 +99,7 @@ struct RegisterPWView: View {
}
Spacer()
NavigationLink {
SelectUserTypeView()
SelectUserTypeView(userID: userID, userPW: userPW)
} label: {
Text("다음").font(.Body04).frame(maxWidth: .infinity).frame(height: 56).foregroundStyle(.sponusWhite).background(!isValidPW ? .sponusGrey600 : .sponusPrimary).padding(.bottom, 16)
}.disabled(!isValidPW)
Expand All @@ -110,5 +111,5 @@ struct RegisterPWView: View {
}

#Preview {
RegisterPWView()
RegisterPWView(userID: "temp")
}
51 changes: 28 additions & 23 deletions Spon-us/View/Onboarding/SelectUserTypeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

import SwiftUI

enum GroupType {
case studentCouncil, studentClub, company
}

struct SelectUserTypeView: View {
@State var userID: String
@State var userPW: String
@State var isStudent = false
@State var isCompany = false

Expand Down Expand Up @@ -48,10 +46,10 @@ struct SelectUserTypeView: View {
Spacer()
NavigationLink {
if isStudent {
StudentTypeView()
StudentTypeView(userID: userID, userPW: userPW)
}
else {
fillInGroupNameView(groupType: .company)
fillInGroupNameView(userID: userID, userPW: userPW, orgType: .company, subOrgType: nil)
}
} label: {
Text("다음").font(.Body04).frame(maxWidth: .infinity).frame(height: 56).foregroundStyle(.sponusWhite).background(!(isStudent || isCompany) ? .sponusGrey600 : .sponusPrimary).padding(.bottom, 16)
Expand All @@ -64,6 +62,8 @@ struct SelectUserTypeView: View {
}

struct StudentTypeView: View {
@State var userID: String
@State var userPW: String
@State var isCouncil = false
@State var isClub = false

Expand Down Expand Up @@ -100,10 +100,10 @@ struct StudentTypeView: View {
Spacer()
NavigationLink {
if isCouncil {
fillInGroupNameView(groupType: .studentCouncil)
fillInGroupNameView(userID: userID, userPW: userPW, orgType: .student, subOrgType: .studentCouncil)
}
else {
fillInGroupNameView(groupType: .studentClub)
fillInGroupNameView(userID: userID, userPW: userPW, orgType: .student, subOrgType: .studentClub)
}
} label: {
Text("다음").font(.Body04).frame(maxWidth: .infinity).frame(height: 56).foregroundStyle(.sponusWhite).background(!(isCouncil || isClub) ? .sponusGrey600 : .sponusPrimary).padding(.bottom, 16)
Expand All @@ -117,7 +117,10 @@ struct StudentTypeView: View {


struct fillInGroupNameView: View {
@State var groupType: GroupType
@State var userID: String
@State var userPW: String
@State var orgType: OrgType
@State var subOrgType: SubOrgType?
@State var groupName = ""
@FocusState private var isTextFieldFocused: Bool
var body: some View {
Expand All @@ -140,14 +143,7 @@ struct fillInGroupNameView: View {

Spacer()
NavigationLink {
switch groupType {
case .company:
OnboardingCompletedView(groupType: .company, groupName: groupName)
case .studentCouncil:
OnboardingCompletedView(groupType: .studentCouncil, groupName: groupName)
case .studentClub:
OnboardingCompletedView(groupType: .studentClub, groupName: groupName)
}
OnboardingCompletedView(userID: userID, userPW: userPW, orgType: orgType, subOrgType: subOrgType, groupName: groupName)
} label: {
Text("다음").font(.Body04).frame(maxWidth: .infinity).frame(height: 56).foregroundStyle(.sponusWhite).background(groupName.isEmpty ? .sponusGrey600 : .sponusPrimary).padding(.bottom, 16)
}.disabled(groupName.isEmpty)
Expand All @@ -159,9 +155,15 @@ struct fillInGroupNameView: View {
}

struct OnboardingCompletedView: View {
@State var groupType: GroupType
@StateObject var joinViewModel = JoinViewModel()

@State var userID: String
@State var userPW: String
@State var orgType: OrgType
@State var subOrgType: SubOrgType?
@State var groupName: String
@State var goToContentView = false

var body: some View {
VStack(spacing: 0) {
Spacer()
Expand All @@ -181,15 +183,18 @@ struct OnboardingCompletedView: View {
Button {
goToContentView = true
} label: {
Text("시작하기").font(.Body04).frame(maxWidth: .infinity).frame(height: 56).foregroundStyle(.sponusWhite).background( .sponusPrimary).padding(.bottom, 16)
}.fullScreenCover(isPresented: $goToContentView, content: {
Text("시작하기").font(.Body04).frame(maxWidth: .infinity).frame(height: 56).foregroundStyle(.sponusWhite).background(joinViewModel.isButtonEnabled ? .sponusPrimary : .sponusGrey600).padding(.bottom, 16)
}.disabled(!joinViewModel.isButtonEnabled).fullScreenCover(isPresented: $goToContentView, content: {
ContentView()
})
}.padding(.horizontal, 20)
.toolbar(.hidden, for: .navigationBar)
.onAppear() {
joinViewModel.postJoin(name: groupName, email: userID, password: userPW, orgType: orgType, subOrgType: subOrgType)
}
}
}

#Preview {
OnboardingCompletedView(groupType: .company, groupName: "")
}
//#Preview {
// OnboardingCompletedView(userID: "temp", userPW: "temp", orgType: .student, subOrgType: nil, groupName: "temp")
//}

0 comments on commit 4a5c28a

Please sign in to comment.