From 3c91f2dddb2c4b3be52b9f5683f4d218fddc0a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awek=20Zatorski?= Date: Wed, 6 Nov 2024 14:44:34 +0100 Subject: [PATCH] jako tako --- .../registration-update.component.ts | 155 ++++++++++++++---- .../registration/registration-update.vue | 86 +++------- .../webapp/app/entities/user/user.service.ts | 17 +- src/main/webapp/app/main.ts | 5 + src/main/webapp/app/router/index.ts | 3 +- 5 files changed, 169 insertions(+), 97 deletions(-) diff --git a/src/main/webapp/app/entities/registration/registration-update.component.ts b/src/main/webapp/app/entities/registration/registration-update.component.ts index c74812f..84be65b 100644 --- a/src/main/webapp/app/entities/registration/registration-update.component.ts +++ b/src/main/webapp/app/entities/registration/registration-update.component.ts @@ -8,78 +8,160 @@ import { useDateFormat, useValidation } from '@/shared/composables'; import { useAlertService } from '@/shared/alert/alert.service'; import UserService from '@/entities/user/user.service'; +import AccountService from '@/account/account.service'; import EventService from '@/entities/event/event.service'; import { type IEvent } from '@/shared/model/event.model'; import { type IRegistration, Registration } from '@/shared/model/registration.model'; +import type LoginService from '@/account/login.service'; +import { type IUser, User } from '@/shared/model/user.model'; export default defineComponent({ compatConfig: { MODE: 3 }, name: 'RegistrationUpdate', - setup() { + props: { + eventId: { + type: [Number, String], + required: false, + default: null, + }, + }, + setup(props) { const registrationService = inject('registrationService', () => new RegistrationService()); const alertService = inject('alertService', () => useAlertService(), true); + const userService = inject('userService', () => new UserService()); + const eventService = inject('eventService', () => new EventService()); + const loginService = inject('loginService'); + const accountService = inject('accountService'); + const isAdmin = inject('isAdmin'); + const currentUserFullName = ref(''); //inject('currentUserFullName'); + const currentUsername = ref(''); // nject('currentUsername'); const registration: Ref = ref(new Registration()); - const userService = inject('userService', () => new UserService()); const users: Ref> = ref([]); - - const eventService = inject('eventService', () => new EventService()); - const events: Ref = ref([]); const isSaving = ref(false); const currentLanguage = inject('currentLanguage', () => computed(() => navigator.language ?? 'en'), true); + const eventName = ref(''); + const currentDateTime = ref(new Date().toISOString().slice(0, 16)); const route = useRoute(); const router = useRouter(); + const loggedUser: Ref = ref(new User()); const previousState = () => router.go(-1); - const retrieveRegistration = async registrationId => { + // const setCurrentUser = async () => { + // const userResponse = await userService().retrieve(); + // users.value = userResponse.data; + // + // console.log('1users:', users.value); + // console.log('2currentUsername:', currentUsername); + // console.log('4users:', users.value); + // console.log('5currentUsername:', currentUsername); + // console.log('6currentUserFullName2:', currentUserFullName2); + // + // } + + const initRelationships = async () => { try { - const res = await registrationService().find(registrationId); - res.dateTime = new Date(res.dateTime); - registration.value = res; + const userResponse = await userService().retrieve(); + users.value = userResponse.data; } catch (error) { - alertService.showHttpError(error.response); + console.error('Error fetching users:', error); } - }; + try { + const eventResponse = await eventService().retrieve(); + events.value = eventResponse.data; - if (route.params?.registrationId) { - retrieveRegistration(route.params.registrationId); - } + console.log('events.value:', events.value); // Sprawdź czy dane wydarzeń zostały poprawnie pobrane + console.log('props.eventId:', props.eventId); // Sprawdź czy eventId jest przekazywany - const initRelationships = () => { - userService() - .retrieve() - .then(res => { - users.value = res.data; - }); - eventService() - .retrieve() - .then(res => { - events.value = res.data; - }); + // // Find and set the event name for the eventId from props + // const event = events.value.find((e: IEvent) => String(e.id) === props.eventId); + // + // console.log('Found event:', event); // Sprawdź czy event został znaleziony + // + // if (event) { + // eventName.value = event.name; + // registration.value.event = event; + // console.log('Event name set:', eventName.value); // Sprawdź czy nazwa wydarzenia została ustawiona + // } else { + // console.log('No event found for id:', props.eventId); // Jeśli event nie został znaleziony + // } + } catch (error) { + console.error('Error fetching events:', error); + } }; initRelationships(); + // // Fetch user details from the /api/account endpoint + // userService().fetchAccountDetails().then(account => { + // currentUserFullName.value = `${account.firstName} ${account.lastName}`; + // registration.value.user = account; // Assuming that account includes the user details + // }); + + const retrieveRegistration = async (registrationId: string) => { + try { + const res = await registrationService().find(registrationId); + res.dateTime = new Date(res.dateTime); + registration.value = res; + // Assuming you can retrieve event details here + eventName.value = '' + registration.value.event?.name; // Set this from the event details + } catch (error) { + alertService.showHttpError(error.response); + } + }; + const initEvent = async (eventId: number) => { + console.log('init'); + if (eventId) { + const event = await eventService().find(eventId); + registration.value.event = event; + eventName.value = '' + registration.value.event?.name; + } + registration.value.active = true; + registration.value.dateTime = new Date(); + userService() + .fetchAccountDetails() + .then(account => { + currentUserFullName.value = `${account.firstName} ${account.lastName}`; + currentUsername.value = `${account.id}`; + userService() + .retrieve() + .then(users => { + const user = users.data.find((e: IUser) => e.id === currentUsername.value); + console.log('7user:', user); + if (user) { + loggedUser.value = user; + } + registration.value.user = loggedUser.value; + }); + console.log('resetting player'); + registration.value.playerName = currentUserFullName.value; + }); + }; + + if (route.params?.registrationId) { + retrieveRegistration(route.params.registrationId as string); + } else { + if (route.params?.eventId) { + initEvent(Number(route.params?.eventId)); + } + } + const dataUtils = useDataUtils(); - const validations = useValidation(); + const { required } = useValidation(); const validationRules = { - dateTime: { - required: validations.required('This field is required.'), + playerName: { + // required: required('This field is required.'), }, active: { - required: validations.required('This field is required.'), + // required: required('This field is required.'), }, - playerName: {}, comment: {}, - user: {}, - event: {}, }; - const v$ = useVuelidate(validationRules, registration as any); - v$.value.$validate(); + const v$ = useVuelidate(validationRules, registration); return { registrationService, @@ -93,9 +175,12 @@ export default defineComponent({ ...dataUtils, v$, ...useDateFormat({ entityRef: registration }), + eventName, + currentUserFullName, + currentDateTime, + isAdmin, }; }, - created(): void {}, methods: { save(): void { this.isSaving = true; diff --git a/src/main/webapp/app/entities/registration/registration-update.vue b/src/main/webapp/app/entities/registration/registration-update.vue index a57cbac..ccd0958 100644 --- a/src/main/webapp/app/entities/registration/registration-update.vue +++ b/src/main/webapp/app/entities/registration/registration-update.vue @@ -6,28 +6,35 @@ Create or edit a Registration
-
- - +
+ +
- -
- -
-
- {{ error.$message }} -
+ + +
+
+ + +
+
+ +
@@ -45,18 +52,6 @@ {{ error.$message }}
-
- - -
-
- - -
-
- - -