Files
sasiedzi/src/main/webapp/app/entities/user-account/user-account-update.component.ts
T
2024-11-13 14:52:04 +01:00

113 lines
3.3 KiB
TypeScript

import { type Ref, computed, defineComponent, inject, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useVuelidate } from '@vuelidate/core';
import UserAccountService from './user-account.service';
import { useValidation } from '@/shared/composables';
import { useAlertService } from '@/shared/alert/alert.service';
import UserService from '@/entities/user/user.service';
import { type IUserAccount, UserAccount } from '@/shared/model/user-account.model';
export default defineComponent({
compatConfig: { MODE: 3 },
name: 'UserAccountUpdate',
setup() {
const userAccountService = inject('userAccountService', () => new UserAccountService());
const alertService = inject('alertService', () => useAlertService(), true);
const userAccount: Ref<IUserAccount> = ref(new UserAccount());
const userService = inject('userService', () => new UserService());
const users: Ref<Array<any>> = ref([]);
const isSaving = ref(false);
const currentLanguage = inject('currentLanguage', () => computed(() => navigator.language ?? 'en'), true);
const route = useRoute();
const router = useRouter();
const previousState = () => router.go(-1);
const retrieveUserAccount = async userAccountId => {
try {
const res = await userAccountService().find(userAccountId);
userAccount.value = res;
} catch (error) {
alertService.showHttpError(error.response);
}
};
if (route.params?.userAccountId) {
retrieveUserAccount(route.params.userAccountId);
}
const initRelationships = () => {
userService()
.retrieve()
.then(res => {
users.value = res.data;
});
};
initRelationships();
const validations = useValidation();
const validationRules = {
name: {},
users: {},
};
const v$ = useVuelidate(validationRules, userAccount as any);
v$.value.$validate();
return {
userAccountService,
alertService,
userAccount,
previousState,
isSaving,
currentLanguage,
users,
v$,
};
},
created(): void {
this.userAccount.users = [];
},
methods: {
save(): void {
this.isSaving = true;
if (this.userAccount.id) {
this.userAccountService()
.update(this.userAccount)
.then(param => {
this.isSaving = false;
this.previousState();
this.alertService.showInfo(`A UserAccount is updated with identifier ${param.id}`);
})
.catch(error => {
this.isSaving = false;
this.alertService.showHttpError(error.response);
});
} else {
this.userAccountService()
.create(this.userAccount)
.then(param => {
this.isSaving = false;
this.previousState();
this.alertService.showSuccess(`A UserAccount is created with identifier ${param.id}`);
})
.catch(error => {
this.isSaving = false;
this.alertService.showHttpError(error.response);
});
}
},
getSelected(selectedVals, option, pkField = 'id'): any {
if (selectedVals) {
return selectedVals.find(value => option[pkField] === value[pkField]) ?? option;
}
return option;
},
},
});