58 lines
1.4 KiB
Swift
58 lines
1.4 KiB
Swift
//
|
|
// SignupViewModel.swift
|
|
// medmap
|
|
//
|
|
|
|
import Foundation
|
|
import Combine
|
|
import SwiftUI
|
|
@MainActor
|
|
final class SignupViewModel: ViewModel {
|
|
|
|
@Published var email = ""
|
|
@Published var password = ""
|
|
@Published var confirmationPassword = ""
|
|
@Published private(set) var errorMessage: String?
|
|
|
|
private let api: MedMapAPI = DIContainer.shared.resolve()
|
|
|
|
|
|
|
|
var canSubmit: Bool {
|
|
!email.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty &&
|
|
password.count >= 8 &&
|
|
password == confirmationPassword &&
|
|
!isLoading
|
|
}
|
|
|
|
func signup() async {
|
|
guard canSubmit else { return }
|
|
|
|
errorMessage = nil
|
|
isLoading = true
|
|
defer { isLoading = false }
|
|
|
|
do {
|
|
let response = try await api.signup(
|
|
email: email.trimmingCharacters(in: .whitespacesAndNewlines),
|
|
password: password,
|
|
role: .patient
|
|
)
|
|
|
|
guard response.role == .patient else {
|
|
errorMessage = "Nao foi possivel criar uma conta de doente."
|
|
return
|
|
}
|
|
|
|
appStateManager.navigateTo(state: .dashboard)
|
|
} catch {
|
|
errorMessage = "Nao foi possivel criar a conta. Confirme os dados ou tente outro email."
|
|
fluxlog("Patient signup failed: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
|
|
func showLogin() {
|
|
appStateManager.navigateBack()
|
|
}
|
|
}
|