Skip to content

Commit

Permalink
[feat] #84 닉네임 수정 API 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
stealmh committed Aug 9, 2024
1 parent eb320a1 commit 846f275
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
import Foundation
/// 닉네임 수정 API Request
public struct NicknameEditRequest: Encodable {
let nickname: String
public let nickname: String

public init(nickname: String) {
self.nickname = nickname
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import Util
public struct NickNameSettingFeature {
/// - Dependency
@Dependency(\.dismiss) var dismiss
@Dependency(\.userClient) var userClient
@Dependency(\.mainQueue) var mainQueue
/// - State
@ObservableState
public struct State: Equatable {
Expand All @@ -22,6 +24,8 @@ public struct NickNameSettingFeature {
get { self.domain.nickname }
set { self.domain.nickname = newValue }
}

var textfieldState: PokitInputStyle.State = .default
var buttonState: PokitButtonStyle.State = .disable

public init() {}
Expand All @@ -42,9 +46,14 @@ public struct NickNameSettingFeature {
case saveButtonTapped
}

public enum InnerAction: Equatable { case doNothing }
public enum InnerAction: Equatable {
case textChanged
case 닉네임_중복_체크_네트워크_결과(Bool)
}

public enum AsyncAction: Equatable { case doNothing }
public enum AsyncAction: Equatable {
case 닉네임_중복_체크_네트워크
}

public enum ScopeAction: Equatable { case doNothing }

Expand Down Expand Up @@ -78,7 +87,7 @@ public struct NickNameSettingFeature {
return handleDelegateAction(delegateAction, state: &state)
}
}

public enum CancelID { case response }
/// - Reducer body
public var body: some ReducerOf<Self> {
BindingReducer(action: \.view)
Expand All @@ -90,6 +99,15 @@ private extension NickNameSettingFeature {
/// - View Effect
func handleViewAction(_ action: Action.View, state: inout State) -> Effect<Action> {
switch action {
case .binding(\.text):
state.buttonState = .disable
return .run { send in
await send(.inner(.textChanged))
}
.debounce(
id: CancelID.response,
for: 3.0, scheduler: mainQueue
)
case .binding:
// - MARK: 목업 데이터 조회
state.domain.isDuplicate = NicknameCheckResponse.mock.toDomain()
Expand All @@ -99,18 +117,47 @@ private extension NickNameSettingFeature {
return .run { _ in await dismiss() }

case .saveButtonTapped:
return .none
return .run { [nickName = state.text] send in
let request = NicknameEditRequest(nickname: nickName)
let _ = try await userClient.닉네임_수정(request)
await dismiss()
}
}
}

/// - Inner Effect
func handleInnerAction(_ action: Action.InnerAction, state: inout State) -> Effect<Action> {
return .none
switch action {
case .textChanged:
if state.text.isEmpty || state.text.count > 10 {
state.buttonState = .disable
return .none
} else {
return .run { send in
await send(.async(.닉네임_중복_체크_네트워크))
}
}
case let .닉네임_중복_체크_네트워크_결과(isDuplicate):
if isDuplicate {
state.textfieldState = .error
state.buttonState = .disable
} else {
state.textfieldState = .active
state.buttonState = .filled(.primary)
}
return .none
}
}

/// - Async Effect
func handleAsyncAction(_ action: Action.AsyncAction, state: inout State) -> Effect<Action> {
return .none
switch action {
case .닉네임_중복_체크_네트워크:
return .run { [nickName = state.text] send in
let result = try await userClient.닉네임_중복_체크(nickName)
await send(.inner(.닉네임_중복_체크_네트워크_결과(result.isDuplicate)))
}
}
}

/// - Scope Effect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@ public extension NickNameSettingView {
VStack(spacing: 0) {
PokitTextInput(
text: $store.text,
state: .constant(.active),
state: $store.textfieldState,
errorMessage: "사용중인 닉네임입니다.",
info: "한글, 영어, 숫자로만 입력이 가능합니다.",
focusState: $isFocused,
equals: true
)
Spacer()
}
.overlay(alignment: .bottom) {
PokitBottomButton(
"저장",
state: store.buttonState,
action: { send(.saveButtonTapped) }
)
.padding(.top, 16)
.setKeyboardHeight()
}
.padding(.top, 16)
.padding(.horizontal, 20)
Expand Down

0 comments on commit 846f275

Please sign in to comment.