Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invitation error handling #24

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions Sources/Screens/Invitation/InvitationCreate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ import Foundation
import SwiftUI
import VoltaserveCore

struct InvitationCreate: View {
struct InvitationCreate: View, FormValidatable, TokenDistributing, ErrorPresentable {
@EnvironmentObject private var tokenStore: TokenStore
@StateObject private var invitationStore = InvitationStore()
@Environment(\.dismiss) private var dismiss
@State private var commaSeparated = ""
@State private var emails: [String] = []
@State private var isSending = false
@State private var showError = false
@State private var errorTitle: String?
@State private var errorMessage: String?
private let organizationID: String

init(_ organizationID: String) {
Expand Down Expand Up @@ -66,7 +63,7 @@ struct InvitationCreate: View {
}
}
}
.voErrorAlert(isPresented: $showError, title: errorTitle, message: errorMessage)
.voErrorSheet(isPresented: $errorIsPresented, message: errorMessage)
.onAppear {
invitationStore.organizationID = organizationID
if let token = tokenStore.token {
Expand All @@ -80,10 +77,6 @@ struct InvitationCreate: View {
}
}

private func assignTokenToStores(_ token: VOToken.Value) {
invitationStore.token = token
}

private func parseEmails() {
var values: [String] = []
for item in commaSeparated.split(separator: ",") {
Expand All @@ -103,15 +96,27 @@ struct InvitationCreate: View {
} success: {
dismiss()
} failure: { message in
errorTitle = "Error: Sending Invitation"
errorMessage = message
showError = true
errorIsPresented = true
} anyways: {
isSending = false
}
}

private func isValid() -> Bool {
// MARK: - ErrorPresentable

@State var errorIsPresented: Bool = false
@State var errorMessage: String?

// MARK: - FormValidatable

func isValid() -> Bool {
!emails.isEmpty
}

// MARK: - TokenDistributing

func assignTokenToStores(_ token: VOToken.Value) {
invitationStore.token = token
}
}
84 changes: 54 additions & 30 deletions Sources/Screens/Invitation/InvitationIncomingList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,48 @@
import SwiftUI
import VoltaserveCore

struct InvitationIncomingList: View {
struct InvitationIncomingList: View, ViewDataProvider, LoadStateProvider, TimerLifecycle, TokenDistributing,
ListItemScrollable
{
@EnvironmentObject private var tokenStore: TokenStore
@StateObject private var invitationStore = InvitationStore()
@State private var showInfo = false
@State private var invitation: VOInvitation.Entity?

var body: some View {
VStack {
if let entities = invitationStore.entities {
Group {
if entities.isEmpty {
Text("There are no invitations.")
} else {
List {
ForEach(entities, id: \.id) { invitation in
NavigationLink {
InvitationOverview(
invitation,
invitationStore: invitationStore,
isAcceptableDeclinable: true
)
} label: {
InvitationIncomingRow(invitation)
.onAppear {
onListItemAppear(invitation.id)
}
if isLoading {
ProgressView()
} else if let error {
VOErrorMessage(error)
} else {
if let entities = invitationStore.entities {
Group {
if entities.isEmpty {
Text("There are no invitations.")
} else {
List {
ForEach(entities, id: \.id) { invitation in
NavigationLink {
InvitationOverview(
invitation,
invitationStore: invitationStore,
isAcceptableDeclinable: true
)
} label: {
InvitationIncomingRow(invitation)
.onAppear {
onListItemAppear(invitation.id)
}
}
}
}
}
}
.refreshable {
invitationStore.fetchNextPage(replace: true)
}
}
.refreshable {
invitationStore.fetchNextPage(replace: true)
}
} else {
ProgressView()
}
}
.navigationBarTitleDisplayMode(.inline)
Expand Down Expand Up @@ -76,27 +82,45 @@ struct InvitationIncomingList: View {
}
}

private func onAppearOrChange() {
// MARK: - LoadStateProvider

var isLoading: Bool {
invitationStore.entitiesIsLoadingFirstTime
}

var error: String? {
invitationStore.entitiesError
}

// MARK: - ViewDataProvider

func onAppearOrChange() {
fetchData()
}

private func fetchData() {
func fetchData() {
invitationStore.fetchNextPage(replace: true)
}

private func startTimers() {
// MARK: - TimerLifecycle

func startTimers() {
invitationStore.startTimer()
}

private func stopTimers() {
func stopTimers() {
invitationStore.stopTimer()
}

private func assignTokenToStores(_ token: VOToken.Value) {
// MARK: - TokenDistributing

func assignTokenToStores(_ token: VOToken.Value) {
invitationStore.token = token
}

private func onListItemAppear(_ id: String) {
// MARK: - ListItemScrollable

func onListItemAppear(_ id: String) {
if invitationStore.isEntityThreshold(id) {
invitationStore.fetchNextPage()
}
Expand Down
84 changes: 54 additions & 30 deletions Sources/Screens/Invitation/InvitationOutgoingList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import SwiftUI
import VoltaserveCore

struct InvitationOutgoingList: View {
struct InvitationOutgoingList: View, ViewDataProvider, LoadStateProvider, TimerLifecycle, TokenDistributing,
ListItemScrollable
{
@EnvironmentObject private var tokenStore: TokenStore
@StateObject private var invitationStore = InvitationStore()
@StateObject private var organizationStore = OrganizationStore()
Expand All @@ -25,34 +27,38 @@ struct InvitationOutgoingList: View {

var body: some View {
VStack {
if let entities = invitationStore.entities {
Group {
if entities.isEmpty {
Text("There are no invitations.")
} else {
List {
ForEach(entities, id: \.id) { invitation in
NavigationLink {
InvitationOverview(
invitation,
invitationStore: invitationStore,
isDeletable: true
)
} label: {
InvitationOutgoingRow(invitation)
.onAppear {
onListItemAppear(invitation.id)
}
if isLoading {
ProgressView()
} else if let error {
VOErrorMessage(error)
} else {
if let entities = invitationStore.entities {
Group {
if entities.isEmpty {
Text("There are no invitations.")
} else {
List {
ForEach(entities, id: \.id) { invitation in
NavigationLink {
InvitationOverview(
invitation,
invitationStore: invitationStore,
isDeletable: true
)
} label: {
InvitationOutgoingRow(invitation)
.onAppear {
onListItemAppear(invitation.id)
}
}
}
}
}
}
.refreshable {
invitationStore.fetchNextPage(replace: true)
}
}
.refreshable {
invitationStore.fetchNextPage(replace: true)
}
} else {
ProgressView()
}
}
.navigationTitle("Invitations")
Expand Down Expand Up @@ -92,30 +98,48 @@ struct InvitationOutgoingList: View {
}
}

private func onAppearOrChange() {
// MARK: - LoadStateProvider

var isLoading: Bool {
invitationStore.entitiesIsLoadingFirstTime
}

var error: String? {
invitationStore.entitiesError
}

// MARK: - ViewDataProvider

func onAppearOrChange() {
fetchData()
}

private func fetchData() {
func fetchData() {
invitationStore.fetchNextPage(replace: true)
}

private func startTimers() {
// MARK: - TimerLifecycle

func startTimers() {
invitationStore.startTimer()
organizationStore.startTimer()
}

private func stopTimers() {
func stopTimers() {
invitationStore.stopTimer()
organizationStore.stopTimer()
}

private func assignTokenToStores(_ token: VOToken.Value) {
// MARK: - TokenDistributing

func assignTokenToStores(_ token: VOToken.Value) {
invitationStore.token = token
organizationStore.token = token
}

private func onListItemAppear(_ id: String) {
// MARK: - ListItemScrollable

func onListItemAppear(_ id: String) {
if invitationStore.isEntityThreshold(id) {
invitationStore.fetchNextPage()
}
Expand Down
Loading