<template> <div class="pt-3 container"> <h3 class="text-lg font-medium mb-3…
- Usercode
- Assistant
<FieldContainer>
<StyledInput
labelText="Company"
:isMandatory="true"
infoText="Company"
inputType="text"
:name="company"
:id="company"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
<FieldContainer>
<StyledInput
labelText="Name of Your Employer"
:isMandatory="true"
infoText="Name of Your Employer"
inputType="text"
:name="name_of_your_employer"
:id="name_of_your_employer"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
<FieldContainer>
<CheckBox name="continuous" id="continuous" @input-change="handleInput">
Continuous
</CheckBox>
</FieldContainer>
<FieldContainer>
<Autocomp
labelText="country"
:isMandatory="true"
infoText="country"
inputType="text"
name="country"
id="country"
:options="optionCountry"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
<FieldContainer>
<Autocomp
labelText="city"
:isMandatory="true"
infoText="city"
inputType="text"
name="city"
id="city"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
<FieldContainer>
<Autocomp
labelText="governorate"
:isMandatory="true"
infoText="governorate"
inputType="text"
name="governorate"
id="governorate"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
<FieldContainer>
<StyledInput
id="from_date"
name="from_date"
labelText="From"
isMandatory="true"
infoText="from_date"
inputType="date"
@input-change="" />
</FieldContainer>
<FieldContainer>
<StyledInput
id="to_date"
name="to_date"
labelText="From"
isMandatory="true"
infoText="to_date"
inputType="date"
@input-change="" />
</FieldContainer>
<FieldContainer>
<StyledInput
labelText="Phone"
:isMandatory="true"
infoText="+999-77885951"
inputType="text"
:name="phone"
:id="phone"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
<FieldContainer>
<Select
id="type_of_employment"
name="type_of_employment"
labelText="Type of Employment"
isMandatory="true"
infoText="Type of Employment"
:options="optionsTypeEmployment"
@input-change="handleChange"
/>
</FieldContainer>
<FieldContainer>
<StyledInput
labelText="Extension"
:isMandatory="true"
infoText="Extension"
inputType="text"
:name="ext"
:id="ext"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
<FieldContainer>
<CheckBox
name="do_we_have_permission_to_contact_this_current_employer"
id="do_we_have_permission_to_contact_this_current_employer"
@input-change="handleInput"
>
Do we have permission to contact this current employer?
</CheckBox>
</FieldContainer>
<FieldContainer>
<CheckBox
name="issuing_salary"
id="issuing_salary"
@input-change="handleInput"
>
Issuing Salary
</CheckBox>
</FieldContainer>
<FieldContainer>
<CheckBox
name="activity_has_stopped"
id="activity_has_stopped"
@input-change="handleInput"
>
Activity Has Stopped
</CheckBox>
</FieldContainer>
<FieldContainer>
<CheckBox
name="the_company_has_different_names"
id="the_company_has_different_names"
@input-change="handleInput"
>
The company has different names
</CheckBox>
</FieldContainer>
<FieldContainer>
<StyledInput
labelText="Different Company Names"
:isMandatory="true"
infoText="Different Company Names"
inputType="text"
:name="different_company_names"
:id="different_company_names"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
<FieldContainer>
<StyledInput
labelText="Name of the Last Position You Held"
:isMandatory="true"
infoText="Name of the Last Position You Held"
inputType="text"
:name="name_of_the_last_position_you_held"
:id="name_of_the_last_position_you_held"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
<FieldContainer>
<CheckBox
name="you_have_a_nicknamecx"
id="you_have_a_nicknamecx"
@input-change="handleInput"
>
You have a nickname?
</CheckBox>
</FieldContainer>
<FieldContainer>
<StyledInput
labelText="Nickname"
:isMandatory="true"
infoText="Nickname"
inputType="text"
:name="name_of_the_last_position_you_held"
:id="name_of_the_last_position_you_held"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
</div>
<FileUpload
:name="`file-${n}`"
:id="`file-${n}`"
/>
</FieldsToggleContainer>
<div class="flex justify-center py-3">
<Button level="secondary" @clicked="addCompany">+ add employment history</Button>
</div>
You've shared a Vue.js component intended to manage a dynamic employment history form. While the structure is comprehensive, there are several areas that can be improved to ensure functionality, maintainability, and adherence to best practices. Below, I'll outline the key issues found in your code and provide recommendations for each.
Inconsistent Data Binding Between Template and Script
v-for loop based on companies, which is a ref initialized to 1, implying it's a single number. However, in your script, you have an array employment_history within request.doc where the employment records are stored. This inconsistency can lead to synchronization issues between the UI and the underlying data.Improper Use of :key in v-for
:key="n" where n is a number (from the companies ref) may not provide unique identifiers, especially as entries are added or removed. This can cause rendering issues and unexpected behavior.Incorrect Binding of name and id Attributes
:name="company" and :id="company". If company is not a defined variable within the scope, Vue will treat them as dynamic bindings, which is likely unintended.Static Strings Bound as Props
:validationMessage="'you can edit message'" use binding (:) for static strings. This is unnecessary and can be simplified.Misspelled or Incorrect Property Names
name and id are incorrectly bound to name_of_the_last_position_you_held instead of nickname.Lack of Unique Identifiers for Employment Entries
id) to manage state effectively, especially for operations like remove or toggle.Handling of handleInput Method
handleInput method is attempting to mutate props.stepData, which might not correctly reflect changes in request.doc.employment_history.File Upload Naming Convention
FileUpload component uses n from the companies loop for name and id, which may not correlate with the actual employment entries.v-for with employment_history ArrayIssue: You're using a separate companies ref to manage the count, while employment records are stored in request.doc.employment_history.
Solution: Directly loop through the employment_history array to ensure consistency between the UI and data.
Implementation:
<template>
<div class="pt-3 container">
<h3 class="text-lg font-medium mb-3 text-black">Employment History</h3>
</div>
<div class="space-y-2">
<FieldsToggleContainer
v-for="(employment, index) in request.doc.employment_history"
:key="employment.id"
:title="'Company ' + (index + 1)"
>
<!-- Employment Form Fields -->
<div class="lg:grid grid-cols-2 lg:gap-2">
<!-- ... all your FieldContainers ... -->
</div>
<FileUpload
:name="`file-${employment.id}`"
:id="`file-${employment.id}`"
/>
<Button level="danger" @clicked="() => confirmRemove(employment.id)">
Remove
</Button>
</FieldsToggleContainer>
<div class="flex justify-center py-3">
<Button level="secondary" @clicked="addEmployment">+ Add Employment History</Button>
</div>
</div>
</template>
Issue: Employment entries lack unique identifiers, complicating operations like removal or toggling.
Solution: Assign a unique id to each employment entry, such as using Date.now() or a UUID.
Implementation:
import { v4 as uuidv4 } from 'uuid'; // Install uuid package if not already
const addEmployment = () => {
const newEmployment = {
id: uuidv4(),
company: '',
name_of_your_employer: '',
type_of_employment: '',
continuous: false,
country: '',
city: '',
governorate: '',
from_date: '',
to_date: '',
phone: '',
ext: '',
do_we_have_permission_to_contact_this_current_employer: false,
issuing_salary: false,
activity_has_stopped: false,
the_company_has_different_names: false,
different_company_names: '',
name_of_the_last_position_you_held: '',
you_have_a_nicknamecx: false,
nickname: ''
};
request.doc.employment_history.push(newEmployment);
nextTick(() => {
openSections[newEmployment.id] = true;
});
};
:name and :id BindingsIssue: Some name and id attributes are dynamically bound to variables that may not exist or are incorrect.
Solution: Bind them correctly based on the employment object, using unique identifiers or indexes.
Implementation:
<StyledInput
labelText="Company"
:isMandatory="true"
infoText="Company"
inputType="text"
:name="`company-${employment.id}`"
:id="`company-${employment.id}`"
:isValid="false"
validationMessage="You can edit this message"
/>
Repeat similar corrections for other fields, ensuring each name and id is unique and correctly references the employment object.
Issue: Static strings are bound using : which is unnecessary.
Solution: Remove the : for static attributes.
Implementation:
<StyledInput
labelText="Company"
isMandatory="true"
infoText="Company"
inputType="text"
name="company"
id="company"
:isValid="false"
validationMessage="You can edit this message"
/>
Issue: The "Nickname" input incorrectly binds to name_of_the_last_position_you_held.
Solution: Update the name and id to reflect the correct property.
Implementation:
<StyledInput
labelText="Nickname"
isMandatory="true"
infoText="Nickname"
inputType="text"
:name="`nickname-${employment.id}`"
:id="`nickname-${employment.id}`"
:isValid="false"
validationMessage="You can edit this message"
/>
handleInput Method to Reflect the Correct Data StructureIssue: The current handleInput method modifies props.stepData, which doesn't align with the employment_history array.
Solution: Update the method to target the specific employment entry being modified.
Implementation:
First, pass the employment.id to the method from the template:
<CheckBox
name="continuous"
:id="`continuous-${employment.id}`"
@input-change="value => handleInput(employment.id, 'continuous', value)"
>
Continuous
</CheckBox>
Then, modify the handleInput method accordingly:
const handleInput = (id, field, value) => {
const employment = request.doc.employment_history.find(emp => emp.id === id);
if (employment) {
employment[field] = value;
}
};
Issue: File uploads use n from companies, which may not align with the employment_history entries.
Solution: Use the unique employment.id instead.
Implementation:
<FileUpload
:name="`file-${employment.id}`"
:id="`file-${employment.id}`"
/>
companies Ref if Not NeededIssue: The companies ref is no longer necessary as we're directly managing the employment_history array.
Solution: Remove the companies ref and related logic.
Implementation:
// Remove this line
// const companies = ref(1);
// Remove the addCompany method
// const addCompany = function () {
// companies.value++
// }
Update the template's v-for accordingly as shown in Recommendation 1.
Issue: Both "From" and "To" date fields have the label "From".
Solution: Update the labelText for the "to_date" field.
Implementation:
<StyledInput
id="to_date"
name="to_date"
labelText="To"
isMandatory="true"
infoText="to_date"
inputType="date"
@input-change="value => handleInput(employment.id, 'to_date', value)"
/>
Issue: All inputs have :isValid="false", which might not be the intended behavior for validation.
Solution: Implement dynamic validation logic that updates the isValid and validationMessage based on user input.
Implementation:
Consider using a computed property or a watcher to validate each field and update its state accordingly.
// Example: Simple validation for the 'company' field
watch(() => request.doc.employment_history, (newHistory) => {
newHistory.forEach(emp => {
if (!emp.company.trim()) {
emp.isValid = false;
emp.validationMessage = 'Company name is required.';
} else {
emp.isValid = true;
emp.validationMessage = '';
}
// Repeat for other fields as necessary
});
}, { deep: true });
Ensure each StyledInput binds to the correct isValid and validationMessage properties.
<StyledInput
labelText="Company"
isMandatory="true"
infoText="Company"
inputType="text"
:name="`company-${employment.id}`"
:id="`company-${employment.id}`"
:isValid="employment.isValid"
:validationMessage="employment.validationMessage"
/>
Combining all the recommendations, here's a revised version of your component:
Form Validation: Implement comprehensive validation for all required fields. Consider using a validation library like Vuelidate or Vue Use for more advanced scenarios.
User Experience Enhancements:
v-if and v-show to display fields like "Nickname" only when necessary.Accessibility Considerations: Ensure all form elements are accessible, with proper aria attributes and labels.
Styling Consistency: Maintain consistent styling across all components. Utilize CSS classes or a utility-first framework like Tailwind CSS for uniformity.
Error Handling: Provide user-friendly error messages and highlight fields with validation errors to guide users in correcting them.
Performance Optimization: If the employment history list becomes extensive, consider implementing pagination or virtualization to enhance performance.
By addressing the issues outlined above and following these recommendations, your component will be more robust, maintainable, and user-friendly.
Not yet included in a published release.
<template>
<div class="pt-3 container">
<h3 class="text-lg font-medium mb-3 text-black">Employment History</h3>
</div>
<div class="space-y-2">
<FieldsToggleContainer
v-for="(employment, index) in request.doc.employment_history"
:key="employment.id"
:title="'Company ' + (index + 1)"
>
<div class="lg:grid grid-cols-2 lg:gap-2">
<FieldContainer>
<StyledInput
labelText="Company"
isMandatory="true"
infoText="Company"
inputType="text"
:name="`company-${employment.id}`"
:id="`company-${employment.id}`"
:isValid="employment.isValid"
:validationMessage="employment.validationMessage"
v-model="employment.company"
/>
</FieldContainer>
<FieldContainer>
<StyledInput
labelText="Name of Your Employer"
isMandatory="true"
infoText="Name of Your Employer"
inputType="text"
:name="`name_of_your_employer-${employment.id}`"
:id="`name_of_your_employer-${employment.id}`"
:isValid="employment.isValid"
:validationMessage="employment.validationMessage"
v-model="employment.name_of_your_employer"
/>
</FieldContainer>
<!-- Repeat similar structure for other fields -->
<FieldContainer>
<CheckBox
:name="`continuous-${employment.id}`"
:id="`continuous-${employment.id}`"
@input-change="value => handleInput(employment.id, 'continuous', value)"
v-model="employment.continuous"
>
Continuous
</CheckBox>
</FieldContainer>
<FieldContainer>
<Autocomp
labelText="Country"
isMandatory="true"
infoText="Country"
inputType="text"
:name="`country-${employment.id}`"
:id="`country-${employment.id}`"
:options="optionCountry"
:isValid="employment.isValid"
:validationMessage="employment.validationMessage"
v-model="employment.country"
/>
</FieldContainer>
<!-- Continue updating all other fields similarly -->
<FieldContainer>
<StyledInput
id="from_date"
:name="`from_date-${employment.id}`"
labelText="From"
isMandatory="true"
infoText="From Date"
inputType="date"
@input-change="value => handleInput(employment.id, 'from_date', value)"
v-model="employment.from_date"
/>
</FieldContainer>
<FieldContainer>
<StyledInput
id="to_date"
:name="`to_date-${employment.id}`"
labelText="To"
isMandatory="true"
infoText="To Date"
inputType="date"
@input-change="value => handleInput(employment.id, 'to_date', value)"
v-model="employment.to_date"
/>
</FieldContainer>
<!-- Phone Field -->
<FieldContainer>
<StyledInput
labelText="Phone"
isMandatory="true"
infoText="+999-77885951"
inputType="text"
:name="`phone-${employment.id}`"
:id="`phone-${employment.id}`"
:isValid="employment.isValid"
:validationMessage="employment.validationMessage"
v-model="employment.phone"
/>
</FieldContainer>
<!-- Type of Employment -->
<FieldContainer>
<Select
:id="`type_of_employment-${employment.id}`"
:name="`type_of_employment-${employment.id}`"
labelText="Type of Employment"
isMandatory="true"
infoText="Type of Employment"
:options="optionsTypeEmployment"
@input-change="value => handleInput(employment.id, 'type_of_employment', value)"
v-model="employment.type_of_employment"
/>
</FieldContainer>
<!-- Extension -->
<FieldContainer>
<StyledInput
labelText="Extension"
isMandatory="true"
infoText="Extension"
inputType="text"
:name="`ext-${employment.id}`"
:id="`ext-${employment.id}`"
:isValid="employment.isValid"
:validationMessage="employment.validationMessage"
v-model="employment.ext"
/>
</FieldContainer>
<!-- Permissions and Flags -->
<FieldContainer>
<CheckBox
:name="`do_we_have_permission_to_contact_this_current_employer-${employment.id}`"
:id="`do_we_have_permission_to_contact_this_current_employer-${employment.id}`"
@input-change="value => handleInput(employment.id, 'do_we_have_permission_to_contact_this_current_employer', value)"
v-model="employment.do_we_have_permission_to_contact_this_current_employer"
>
Do we have permission to contact this current employer?
</CheckBox>
</FieldContainer>
<!-- Repeat for other CheckBoxes -->
<!-- Different Company Names -->
<FieldContainer>
<StyledInput
labelText="Different Company Names"
isMandatory="false"
infoText="Different Company Names"
inputType="text"
:name="`different_company_names-${employment.id}`"
:id="`different_company_names-${employment.id}`"
:isValid="employment.isValid"
:validationMessage="employment.validationMessage"
v-model="employment.different_company_names"
/>
</FieldContainer>
<!-- Last Position -->
<FieldContainer>
<StyledInput
labelText="Name of the Last Position You Held"
isMandatory="true"
infoText="Name of the Last Position You Held"
inputType="text"
:name="`name_of_the_last_position_you_held-${employment.id}`"
:id="`name_of_the_last_position_you_held-${employment.id}`"
:isValid="employment.isValid"
:validationMessage="employment.validationMessage"
v-model="employment.name_of_the_last_position_you_held"
/>
</FieldContainer>
<!-- Nickname -->
<FieldContainer>
<CheckBox
:name="`you_have_a_nickname-${employment.id}`"
:id="`you_have_a_nickname-${employment.id}`"
@input-change="value => handleInput(employment.id, 'you_have_a_nicknamecx', value)"
v-model="employment.you_have_a_nicknamecx"
>
You have a nickname?
</CheckBox>
</FieldContainer>
<FieldContainer v-if="employment.you_have_a_nicknamecx">
<StyledInput
labelText="Nickname"
isMandatory="false"
infoText="Nickname"
inputType="text"
:name="`nickname-${employment.id}`"
:id="`nickname-${employment.id}`"
:isValid="employment.isValid"
:validationMessage="employment.validationMessage"
v-model="employment.nickname"
/>
</FieldContainer>
</div>
<FileUpload
:name="`file-${employment.id}`"
:id="`file-${employment.id}`"
/>
<Button level="danger" @clicked="() => confirmRemove(employment.id)">
Remove
</Button>
</FieldsToggleContainer>
<div class="flex justify-center py-3">
<Button level="secondary" @clicked="addEmployment">+ Add Employment History</Button>
</div>
</div>
</template>
<script setup>
import Button from '../../components/button.vue';
import CheckBox from '../../components/checkBox.vue';
import FieldContainer from '../../components/fieldContainer.vue';
import FieldsToggleContainer from '../../components/fieldsToggleContainer.vue';
import FileUpload from '../../components/fileUpload.vue';
import Select from '../../components/select.vue';
import StyledInput from '../../components/styledInput.vue';
import Autocomp from '../../components/autocomp.vue';
import { ref, computed, watch, reactive, nextTick } from 'vue';
import { createDocumentResource } from 'frappe-ui';
import { location } from '../../data/useAddressLogic';
import { v4 as uuidv4 } from 'uuid'; // Ensure you have installed uuid: npm install uuid
const optionsTypeEmployment = [
{ value: "Full-Time", label: "Full-Time" },
{ value: "Part-Time", label: "Part-Time" },
{ value: "Contract", label: "Contract" },
{ value: "Internship", label: "Internship" },
];
const request = createDocumentResource({
doctype: 'Verification Instructions Request',
name: 'VIR-2024-26-09-000007',
});
const optionCountry = computed(() => {
return location.data
.filter(loc => loc.location_type === 'Country')
.map(loc => ({
label: loc.location_name,
value: loc.location_name
}));
});
const getOptionCity = (country) => {
if (!country) return [];
return location.data
.filter(loc => loc.location_type === 'City' && loc.parent_location === country)
.map(loc => ({
label: loc.location_name,
value: loc.location_name
}));
};
const getOptionGovernorate = (city) => {
if (!city) return [];
return location.data
.filter(loc => loc.location_type === 'Governorate' && loc.parent_location === city)
.map(loc => ({
label: loc.location_name,
value: loc.location_name
}));
};
watch(() => request.doc, (newDoc) => {
if (newDoc && !newDoc.employment_history) {
newDoc.employment_history = [];
}
}, { immediate: true });
const openSections = reactive({});
const toggleSection = (id) => {
openSections[id] = !openSections[id];
};
const isOpen = (id) => {
return openSections[id];
};
const addEmployment = () => {
const newEmployment = {
id: uuidv4(),
company: '',
name_of_your_employer: '',
type_of_employment: '',
continuous: false,
country: '',
city: '',
governorate: '',
from_date: '',
to_date: '',
phone: '',
ext: '',
do_we_have_permission_to_contact_this_current_employer: false,
issuing_salary: false,
activity_has_stopped: false,
the_company_has_different_names: false,
different_company_names: '',
name_of_the_last_position_you_held: '',
you_have_a_nicknamecx: false,
nickname: '',
isValid: true,
validationMessage: ''
};
request.doc.employment_history.push(newEmployment);
nextTick(() => {
openSections[newEmployment.id] = true;
});
};
const confirmRemove = (id) => {
if (confirm('Are you sure you want to delete this employment record?')) {
removeEmployment(id);
}
};
const removeEmployment = (id) => {
const index = request.doc.employment_history.findIndex(emp => emp.id === id);
if (index !== -1) {
request.doc.employment_history.splice(index, 1);
delete openSections[id];
}
};
const handleInput = (id, field, value) => {
const employment = request.doc.employment_history.find(emp => emp.id === id);
if (employment) {
employment[field] = value;
validateField(employment, field);
}
};
const validateField = (employment, field) => {
switch(field) {
case 'company':
case 'name_of_your_employer':
case 'type_of_employment':
case 'country':
case 'city':
case 'governorate':
case 'from_date':
case 'phone':
case 'ext':
case 'name_of_the_last_position_you_held':
if (!employment[field] || (typeof employment[field] === 'string' && !employment[field].trim())) {
employment.isValid = false;
employment.validationMessage = `${field.replace(/_/g, ' ')} is required.`;
} else {
employment.isValid = true;
employment.validationMessage = '';
}
break;
// Add more cases as needed for other fields
default:
break;
}
};
const saveRequest = async () => {
try {
await request.setValue.submit({
employment_history: request.doc.employment_history,
});
toast.success('Request has been successfully saved!');
} catch (error) {
if (!errors.global) {
errors.global = [];
}
errors.global.push(`An error occurred while saving the request: ${error.message}`);
}
};
// Props
const props = defineProps({
stepData: {
type: Object,
required: true,
},
});
</script>