|
|
|
@@ -16,6 +16,7 @@ import { ModalStatus } from "@app/_helpers/modal.states"; |
|
|
|
import {firstValueFrom, Observable} from 'rxjs'; |
|
|
|
import { ListColDefinition } from '@app/_components/list/list-col-definition'; |
|
|
|
import { SearchSelectComponent } from '@app/_components/search-select/search-select.component'; |
|
|
|
import {map} from "rxjs/operators"; |
|
|
|
|
|
|
|
@Component({ |
|
|
|
selector: 'app-trip-detail', |
|
|
|
@@ -104,7 +105,52 @@ export class TripDetailComponent implements OnInit, AfterViewInit { |
|
|
|
} |
|
|
|
|
|
|
|
getUsers = (page: number, pageSize: number, term?: string): Observable<any> => { |
|
|
|
return this.userService.usersGetCollection(page, pageSize, undefined, term); |
|
|
|
// Sammeln Sie alle aktuell ausgewählten Benutzer-IDs (sowohl gespeicherte als auch nicht gespeicherte) |
|
|
|
const assignedUserIds: string[] = []; |
|
|
|
|
|
|
|
// IDs aus bestehenden UserTrips (mit gültiger ID) |
|
|
|
this.userTrips |
|
|
|
.filter(ut => ut.user && ut.user.id) |
|
|
|
.forEach(ut => assignedUserIds.push(this.appHelperService.extractId(ut.user.id!))); |
|
|
|
|
|
|
|
// IDs aus aktuellen Formularwerten (auch für noch nicht gespeicherte Einträge) |
|
|
|
this.userForms.forEach(form => { |
|
|
|
const userValue = form.get('user')?.value; |
|
|
|
if (userValue) { |
|
|
|
// Wenn es ein Objekt ist (z.B. vollständiges UserJsonld-Objekt) |
|
|
|
if (typeof userValue === 'object' && userValue.id) { |
|
|
|
assignedUserIds.push(this.appHelperService.extractId(userValue.id)); |
|
|
|
} |
|
|
|
// Wenn es nur die ID als String ist |
|
|
|
else if (typeof userValue === 'string') { |
|
|
|
assignedUserIds.push(userValue); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
// Filtere Benutzer, die bereits ausgewählt sind (gespeichert oder nicht) |
|
|
|
return this.userService.usersGetCollection(page, pageSize, undefined, term).pipe( |
|
|
|
map(response => { |
|
|
|
response.member = response.member.filter(user => |
|
|
|
!assignedUserIds.includes(this.appHelperService.extractId(user.id!)) |
|
|
|
); |
|
|
|
return response; |
|
|
|
}) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
onUserSelectChange(index: number) { |
|
|
|
// Erzwingen Sie eine Aktualisierung aller anderen SearchSelect-Komponenten |
|
|
|
setTimeout(() => { |
|
|
|
if (this.searchSelects) { |
|
|
|
this.searchSelects.forEach((component, i) => { |
|
|
|
// Überspringen Sie das aktuelle SearchSelect (es muss nicht aktualisiert werden) |
|
|
|
if (i !== index) { |
|
|
|
component.ngAfterViewInit(); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
onFormUpdate(event: FormSubmitEvent<TripJsonld>) { |
|
|
|
@@ -173,12 +219,12 @@ export class TripDetailComponent implements OnInit, AfterViewInit { |
|
|
|
} |
|
|
|
|
|
|
|
addNewUserTrip() { |
|
|
|
// Create a new empty user trip |
|
|
|
// Create a new empty user trip with an empty user object as a placeholder |
|
|
|
const newUserTrip: UserTripJsonld = { |
|
|
|
trip: { |
|
|
|
'id': this.trip.id |
|
|
|
} as TripJsonld, |
|
|
|
// No default user set |
|
|
|
user: {} as UserJsonld // Empty user object as placeholder |
|
|
|
}; |
|
|
|
|
|
|
|
this.userTrips.push(newUserTrip); |
|
|
|
@@ -311,21 +357,20 @@ export class TripDetailComponent implements OnInit, AfterViewInit { |
|
|
|
'id': userFormValue |
|
|
|
} as UserJsonld; |
|
|
|
} |
|
|
|
} else { |
|
|
|
// If no user is set, show an error |
|
|
|
return; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
// Filter out user trips that have no user set |
|
|
|
const validUserTrips = this.userTrips.filter(ut => ut.user); |
|
|
|
// Check if there are any invalid user trips (without a proper user selection) |
|
|
|
const invalidEntries = this.userTrips.filter(ut => !ut.user || !ut.user.id); |
|
|
|
|
|
|
|
if (validUserTrips.length === 0) { |
|
|
|
if (invalidEntries.length > 0) { |
|
|
|
// Show alert to the user |
|
|
|
window.alert('Please select a user for all entries or remove unused entries before saving.'); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// Save all user trips |
|
|
|
const savePromises = validUserTrips.map(userTrip => { |
|
|
|
// At this point, all entries are valid |
|
|
|
const savePromises = this.userTrips.map(userTrip => { |
|
|
|
if (userTrip.id) { |
|
|
|
// Update existing |
|
|
|
return firstValueFrom(this.userTripService.userTripsIdPatch( |
|
|
|
@@ -345,6 +390,7 @@ export class TripDetailComponent implements OnInit, AfterViewInit { |
|
|
|
}) |
|
|
|
.catch(error => { |
|
|
|
console.error('Error saving user trips:', error); |
|
|
|
window.alert('An error occurred while saving user assignments. Please try again.'); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |