You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.3 KiB
66 lines
2.3 KiB
//
|
|
// CreateClubView.swift
|
|
// PadelClub
|
|
//
|
|
// Created by Razmig Sarkissian on 20/03/2024.
|
|
//
|
|
|
|
import SwiftUI
|
|
import LeStorage
|
|
|
|
struct CreateClubView: View {
|
|
@Bindable var club: Club
|
|
@EnvironmentObject var dataStore: DataStore
|
|
@Environment(\.dismiss) var dismiss
|
|
|
|
init() {
|
|
self.club = Club.newEmptyInstance()
|
|
}
|
|
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
ClubDetailView(club: club, displayContext: .addition)
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Annuler", role: .cancel) {
|
|
dismiss()
|
|
}
|
|
}
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
ButtonValidateView {
|
|
|
|
let existingOrCreatedClub = Club.findOrCreate(name: club.name, code: club.code, city: club.city, zipCode: club.zipCode)
|
|
|
|
//update existing club if rights ok / freshly created club with data input from user
|
|
if existingOrCreatedClub.hasBeenCreated(by: dataStore.user.id) {
|
|
existingOrCreatedClub.update(fromClub: club)
|
|
do {
|
|
try dataStore.clubs.addOrUpdate(instance: existingOrCreatedClub)
|
|
} catch {
|
|
Logger.error(error)
|
|
}
|
|
}
|
|
|
|
//save into user
|
|
do {
|
|
if dataStore.user.clubs?.contains(where: { $0 == existingOrCreatedClub.id }) == false {
|
|
dataStore.user.clubs?.append(existingOrCreatedClub.id)
|
|
try dataStore.userStorage.update()
|
|
}
|
|
} catch {
|
|
Logger.error(error)
|
|
}
|
|
dismiss()
|
|
}
|
|
.disabled(club.isValid == false)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
CreateClubView()
|
|
.environmentObject(DataStore.shared)
|
|
}
|
|
|