Certainly! Below is the refactored edit-role-bonus-config.vue component using the Composition API with defineComponent from @vue/composition-api, modeled after the medical-org-structure-view.vue file you provided.
Refactored edit-role-bonus-config.vue
vue
<template class="lg12">
<v-container style="margin: 0; max-width: 400px" fixed>
<v-form v-model="isFormValid" lazy-validation>
<div style="max-width: 400px">
<v-row>
<v-col cols="7">
<v-subheader>Funkcjonalność bonusów aktywna dla wybranej roli</v-subheader>
</v-col>
<v-col cols="5">
<v-switch
v-model="bonusConfig.isActive"
:readonly="readonly"
@change="onBonusConfigChange" />
</v-col>
</v-row>
<v-row>
<v-col cols="7">
<v-subheader>Minimalna Wartość Bonusa</v-subheader>
</v-col>
<v-col cols="5">
<v-text-field
v-model.number="bonusConfig.minValue"
label="Kwota"
:rules="amountRules"
:readonly="readonly || !bonusConfig.isActive"
suffix="zł"
@input="onBonusConfigChange" />
</v-col>
</v-row>
<v-row>
<v-col cols="7">
<v-subheader>Maksymalna Wartość Bonusa</v-subheader>
</v-col>
<v-col cols="5">
<v-text-field
v-model.number="bonusConfig.maxValue"
label="Kwota"
:rules="amountRules"
:readonly="readonly || !bonusConfig.isActive"
suffix="zł"
@input="onBonusConfigChange" />
</v-col>
</v-row>
<v-row>
<v-col cols="7">
<v-subheader>Roczna podwyżka bonusa</v-subheader>
</v-col>
<v-col cols="5">
<v-text-field
v-model.number="bonusConfig.yearlyIncreaseValue"
label="Kwota"
:rules="amountRules"
:readonly="readonly || !bonusConfig.isActive"
suffix="zł"
@input="onBonusConfigChange" />
</v-col>
</v-row>
<v-row>
<v-col cols="7">
<v-subheader>Maksymalna Wartość Bonusa Dodatkowego</v-subheader>
</v-col>
<v-col cols="5">
<v-text-field
v-model.number="bonusConfig.maxExtraBonusValue"
label="Kwota"
:rules="amountRules"
:readonly="readonly || !bonusConfig.isActive"
suffix="zł"
@input="onBonusConfigChange" />
</v-col>
</v-row>
<v-row>
<v-col cols="7">
<v-subheader>Pomniejszenie w wyniku ZLA</v-subheader>
</v-col>
<v-col cols="5">
<v-text-field
v-model.number="bonusConfig.sickLeaveDecrement"
label="Kwota"
:rules="amountRules"
:readonly="readonly || !bonusConfig.isActive"
suffix="zł"
@input="onBonusConfigChange" />
</v-col>
</v-row>
<v-row>
<v-col cols="7">
<v-subheader>Pomniejszenie w wyniku opieki nad dzieckiem</v-subheader>
</v-col>
<v-col cols="5">
<v-text-field
v-model.number="bonusConfig.childCareDecrement"
label="Kwota"
:rules="amountRules"
:readonly="readonly || !bonusConfig.isActive"
suffix="zł"
@input="onBonusConfigChange" />
</v-col>
</v-row>
<v-row>
<v-col cols="7">
<v-subheader>Odroczenie Bonusa</v-subheader>
</v-col>
<v-col cols="5">
<v-switch
v-model="bonusConfig.isDeferred"
:readonly="readonly"
@change="onBonusConfigChange" />
</v-col>
</v-row>
<v-row v-if="bonusConfig.isDeferred">
<v-col cols="7">
<v-subheader>Ilość miesięcy do odroczenia</v-subheader>
</v-col>
<v-col cols="5">
<v-text-field
v-model.number="bonusConfig.deferralMonths"
label="Miesiące"
:rules="[v => v >= 0 || 'Liczba miesięcy musi być nieujemna']"
:readonly="readonly"
type="number"
min="0"
@input="onBonusConfigChange" />
</v-col>
</v-row>
</div>
</v-form>
</v-container>
</template>
<script lang="ts">
import { defineComponent, ref, reactive, watch, computed } from "@vue/composition-api";
import type { PropType } from "vue";
import BonusConfig from "@/store/entities/bonus-config";
import Role from "@/store/entities/role";
import Util from "@/lib/util";
import ValidatePolish from "@/lib/validate-polish";
export default defineComponent({
name: "EditRoleBonusConfig",
props: {
readonly: {
type: Boolean,
required: true
},
role: {
type: Object as PropType<Role | null>,
required: true
}
},
setup(props) {
// Reactive state
const isFormValid = ref(false);
const isModified = ref(false);
const bonusConfig = reactive(new BonusConfig());
// Watcher for role changes
watch(
() => props.role,
(newRole: Role | null) => {
if (newRole && newRole.bonusRoleConfig) {
Util.deepCloneObject(newRole.bonusRoleConfig, bonusConfig);
} else {
// Reset to default if role is null
Object.assign(bonusConfig, new BonusConfig());
}
},
{ immediate: true, deep: true }
);
// Validation rules
const amountRules = [
(v: number | null) => v !== null && v !== undefined || "Pole jest wymagane",
(v: number) => (ValidatePolish.amount(v) && v >= 0 && v < 100000) || "Niepoprawna wartość"
];
// Computed property for consistency error
const consistencyError = computed(() => {
if (bonusConfig.minValue > bonusConfig.maxValue) {
return "Maksymalna wartość bonusa musi być większa od minimalnej";
}
if (bonusConfig.yearlyIncreaseValue > (bonusConfig.maxValue - bonusConfig.minValue)) {
return "Roczna podwyżka bonusa nie może być większa niż różnica między wartością minimalną a maksymalną";
}
return null;
});
// Methods
const onBonusConfigChange = () => {
isModified.value = true;
};
const getDataIfValid = (): BonusConfig | null => {
if (!isFormValid.value) {
(window as any).abp.message.error("Nie wszystkie pola konfiguracji bonusów są wypełnione poprawnie");
return null;
}
if (consistencyError.value) {
(window as any).abp.message.error(consistencyError.value);
return null;
}
isModified.value = false;
return bonusConfig;
};
return {
isFormValid,
isModified,
bonusConfig,
amountRules,
consistencyError,
getDataIfValid,
onBonusConfigChange
};
}
});
</script>
<style scoped>
/* Add any component-specific styles here */
</style>
Explanation of Changes
Imports and Dependencies:
Imported necessary functions from @vue/composition-api such as defineComponent, ref, reactive, watch, and computed.
Imported types and utilities like BonusConfig, Role, Util, and ValidatePolish.
Props Definition:
Defined readonly and role props using the Composition API's props option with proper TypeScript typings.
Reactive State:
Replaced class-based data properties with ref and reactive to manage reactive state:
isFormValid and isModified are initialized as ref since they hold primitive values.
bonusConfig is made reactive using reactive() as it’s an object with nested properties.
Watchers:
Set up a watch to observe changes to the role prop. When role changes, it deep clones the bonusRoleConfig into bonusConfig. If role is null, it resets bonusConfig to a new instance.
Validation Rules:
Maintained the amountRules for validating monetary inputs. Adjusted the first rule to check for null or undefined values since we're using v-model.number.
Computed Properties:
Created a consistencyError computed property to encapsulate the logic for consistency checks between different bonus configuration fields.
Methods and Event Handlers:
Replaced class methods with regular functions inside the setup to handle events and validations:
onBonusConfigChange: Sets isModified to true whenever any bonus configuration field changes.
Additional Notes
Global Objects: The abp object is accessed via window to avoid TypeScript errors. Ensure that abp is globally available in your project. If you're using TypeScript and have typings for abp, you can adjust the access accordingly.
Utility Functions: The Util.deepCloneObject method is used to clone the bonusRoleConfig. Ensure this utility function correctly handles deep cloning without losing reactivity.
Validation Integration: The v-model.number modifier is used to automatically convert input values to numbers. This aligns with the amountRules which expect numerical values.
Error Handling: The getDataIfValid method includes logic to display error messages if the form is invalid or if there are consistency issues. Ensure that this method is called appropriately in your component's workflow, such as before saving the configuration.
Component Registration: Since the original class-based component might have inherited from AbpBaseComponent, ensure that any necessary functionalities from AbpBaseComponent are re-implemented or integrated within the Composition API setup. If AbpBaseComponent provides essential methods or reactive states, consider using Composition API features or mixins to incorporate them.
By following the above refactored structure, the edit-role-bonus-config.vue component now leverages the Composition API for better organization, scalability, and reusability within Vue.js applications.
getDataIfValid: Validates the form and consistency, showing error messages using abp.message.error if any issues are found.
Template Adjustments:
Updated event bindings to use the new methods:
Replaced @input="isModified = true" and @change="isModified = true" with @input="onBonusConfigChange" and @change="onBonusConfigChange" respectively for better readability and maintainability.
TypeScript Enhancements:
Added TypeScript typings to ensure type safety across the component, especially for props and reactive states.
Scoped Styles:
Retained the option for component-specific scoped styles, which can be added as needed.