diff --git a/Sources/WishKit/Configuration/Configuration.swift b/Sources/WishKit/Configuration/Configuration.swift index b5b9a1b..21eced5 100644 --- a/Sources/WishKit/Configuration/Configuration.swift +++ b/Sources/WishKit/Configuration/Configuration.swift @@ -28,6 +28,8 @@ public struct Configuration { public var emailField: EmailField = .optional public var commentSection: Display = .show + + public var allowUndoVote: Bool = false init( statusBadgeDisplay: Display = .hide, diff --git a/Sources/WishKit/SwiftUI/WishView.swift b/Sources/WishKit/SwiftUI/WishView.swift index 7256cac..baa79cd 100644 --- a/Sources/WishKit/SwiftUI/WishView.swift +++ b/Sources/WishKit/SwiftUI/WishView.swift @@ -26,9 +26,6 @@ struct WishView: View { @Binding private var voteCount: Int - @State - private var hasVoted = false - private let wishResponse: WishResponse private let voteActionCompletion: () -> Void @@ -164,12 +161,21 @@ struct WishView: View { } private func voteAction() { - if wishResponse.state == .implemented { alertModel.alertReason = .alreadyImplemented alertModel.showAlert = true return } + + let userUUID = UUIDManager.getUUID() + + let hasVoted = wishResponse.votingUsers.contains(where: { user in user.uuid == userUUID }) + + if (hasVoted) && WishKit.config.allowUndoVote == false { + alertModel.alertReason = .alreadyVoted + alertModel.showAlert = true + return + } let request = VoteWishRequest(wishId: wishResponse.id) @@ -192,7 +198,7 @@ struct WishView: View { extension WishView { var arrowColor: Color { let userUUID = UUIDManager.getUUID() - if wishResponse.votingUsers.contains(where: { user in user.uuid == userUUID }) || hasVoted { + if wishResponse.votingUsers.contains(where: { user in user.uuid == userUUID }) { return WishKit.theme.primaryColor }