- 룰렛 설정 완료 API 적용

- 룰렛 설정 완료 시 신호전송
This commit is contained in:
Yu Sung
2023-12-06 23:24:33 +09:00
parent a22e2e7a51
commit 2e8258a977
9 changed files with 100 additions and 5 deletions

View File

@@ -147,4 +147,52 @@ final class RouletteSettingsViewModel: ObservableObject {
isLoading = false
isShowPreview = true
}
func createOrUpdateRoulette(onSuccess: @escaping (Bool) -> Void) {
if !isLoading {
isLoading = true
var items = [RouletteItem]()
for option in options {
if option.title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
isLoading = false
errorMessage = "옵션은 빈칸일 수 없습니다."
isShowErrorPopup = true
return
}
items.append(RouletteItem(title: option.title, weight: option.weight))
}
let request = CreateOrUpdateRouletteRequest(can: can, isActive: isActive, items: items)
repository.createOrUpdateRoulette(request: request)
.sink { result in
switch result {
case .finished:
DEBUG_LOG("finish")
case .failure(let error):
ERROR_LOG(error.localizedDescription)
}
} receiveValue: { [unowned self] response in
self.isLoading = false
let responseData = response.data
do {
let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponseWithoutData.self, from: responseData)
if decoded.success {
onSuccess(isActive)
} else {
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowErrorPopup = true
}
} catch {
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowErrorPopup = true
}
}
.store(in: &subscription)
}
}
}