<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-${index}`"
:options="optionCountry"
v-model="employment.country"
@input-change="handleCountryChange(index)"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
<FieldContainer>
<Autocomp
labelText="City"
:isMandatory="true"
infoText="City"
inputType="text"
name="city"
:id="`city-${index}`"
:options="getOptionCity(employment.country)"
v-model="employment.city"
@input-change="handleCityChange(index)"
:isValid="false"
:validationMessage="'you can edit message'"
/>
</FieldContainer>
<FieldContainer>
<Autocomp
labelText="Governorate"
:isMandatory="true"
infoText="Governorate"
inputType="text"
name="governorate"
:id="`governorate-${index}`"
:options="getOptionGovernorate(employment.city)"
v-model="employment.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>
Your Vue component for managing Employment History is quite comprehensive, but there are several areas where improvements can be made to enhance functionality, maintainability, and adherence to Vue best practices. Below, I'll outline key observations, potential issues, and recommendations to optimize your component.
companies as a Counter:
companies as a ref initialized to 1 to control the number of employment history forms displayed. This approach can lead to synchronization issues between the UI and the actual data in request.doc.employment_history.No Unique Identifier for Employment Entries:
addEmployment function, you push a new employment object without an id, but you're trying to access newEmployment.id when opening sections. This will result in undefined keys in openSections.Use Employment History Array Directly:
companies counter, utilize the employment_history array within request.doc. This ensures that the UI accurately reflects the underlying data.Assign Unique IDs:
key in v-for and managing open sections.import { v4 as uuidv4 } from 'uuid' // Install uuid via npm
const addEmployment = () => {
const newEmployment = {
id: uuidv4(), // Assign a unique ID
company: '',
name_of_your_employer: '',
// ... rest of the fields
}
request.doc.employment_history.push(newEmployment)
nextTick(() => {
openSections[newEmployment.id] = true
})
}
Incorrect v-for Usage:
companies, which is a number, but you should loop over the employment_history array.Static :title Attribute:
:title="Company" uses Company as a string instead of a dynamic property. If it's intended to be dynamic, it should reference the actual company name from the data.Undefined index:
`country-${index}` and similar expressions, but index isn't defined in the v-for. This will lead to errors.Duplicate labelText and name Attributes:
name_of_the_last_position_you_held for name and id.Proper v-for Iteration:
request.doc.employment_history to bind each form to its respective data object.Dynamic Titles and IDs:
id attributes are unique.Fix Duplicate Attributes:
name and id attributes corresponding to their purpose.Two-Way Binding with v-model:
v-model.Event Emission and Handling:
<CheckBox> and <Select> have event listeners (e.g., @input-change) but their handlers may not correctly update the data.Consistent Use of v-model:
v-model for two-way data binding wherever possible to simplify state management.Update Event Handlers:
const handleInput = (employmentId, fieldName, value) => {
const employment = request.doc.employment_history.find(emp => emp.id === employmentId)
if (employment) {
employment[fieldName] = value
}
}
const handleCountryChange = (index) => {
// Logic to handle country change
}
const handleCityChange = (index) => {
// Logic to handle city change
}
const handleCheckBoxChange = (employmentId, fieldName, checked) => {
handleInput(employmentId, fieldName, checked)
}
<!-- Example Checkbox Usage -->
<CheckBox
name="continuous"
:id="`continuous-${employment.id}`"
v-model="employment.continuous"
>
Continuous
</CheckBox>
Static Validation Messages:
'you can edit message', which isn't informative for users.isValid Prop:
false for all inputs, which might prevent proper validation feedback.Dynamic Validation:
Manage Validation States:
isValid and validationMessage accordingly.// Example validation logic
const validateEmployment = (employment) => {
const errors = {}
if (!employment.company) {
errors.company = "Company name is required."
}
if (!employment.name_of_your_employer) {
errors.name_of_your_employer = "Employer name is required."
}
// ...additional validations
return errors
}
const employmentErrors = computed(() => {
return request.doc.employment_history.map(emp => validateEmployment(emp))
})
<!-- Example Input with Dynamic Validation -->
<StyledInput
labelText="Company"
:isMandatory="true"
infoText="Company"
inputType="text"
name="company"
:id="`company-${employment.id}`"
v-model="employment.company"
:isValid="!employmentErrors[index]?.company"
:validationMessage="employmentErrors[index]?.company"
/>
FieldsGroup, List, StyledtextArea, SupportingText, Toggle, and Info are not used in the template.// Remove unused imports
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 { v4 as uuidv4 } from 'uuid' // For unique IDs
// ... other necessary imports
Here's a refined version of your <script setup> considering the above recommendations:
By implementing the above recommendations, your Vue component will become more robust, maintainable, and aligned with best practices. Here's a summary of the key improvements:
v-for loop, dynamically set titles and IDs, and ensure all bindings are accurate.v-model for two-way data binding and refine event handlers to work with the correct data scope.Feel free to adjust the validation logic and component structure further based on your specific requirements and the behavior of child components like StyledInput, Autocomp, and others.
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="employment.company || `Employment ${index + 1}`"
>
<div class="lg:grid grid-cols-2 lg:gap-2">
<!-- Company Field -->
<FieldContainer>
<StyledInput
labelText="Company"
:isMandatory="true"
infoText="Company"
inputType="text"
name="company"
:id="`company-${employment.id}`"
v-model="employment.company"
:isValid="false"
validationMessage="You can edit the message"
/>
</FieldContainer>
<!-- Name of Employer Field -->
<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-${employment.id}`"
v-model="employment.name_of_your_employer"
:isValid="false"
validationMessage="You can edit the message"
/>
</FieldContainer>
<!-- Other Fields... -->
<!-- Country Autocomplete -->
<FieldContainer>
<Autocomp
labelText="Country"
:isMandatory="true"
infoText="Country"
inputType="text"
name="country"
:id="`country-${employment.id}`"
:options="optionCountry"
v-model="employment.country"
@input-change="handleCountryChange(index)"
:isValid="false"
validationMessage="You can edit the message"
/>
</FieldContainer>
<!-- City Autocomplete -->
<FieldContainer>
<Autocomp
labelText="City"
:isMandatory="true"
infoText="City"
inputType="text"
name="city"
:id="`city-${employment.id}`"
:options="getOptionCity(employment.country)"
v-model="employment.city"
@input-change="handleCityChange(index)"
:isValid="false"
validationMessage="You can edit the message"
/>
</FieldContainer>
<!-- More Fields... -->
<!-- Nickname Field Fix -->
<FieldContainer>
<StyledInput
labelText="Nickname"
:isMandatory="false" <!-- Assuming nickname is optional -->
infoText="Nickname"
inputType="text"
name="nickname"
:id="`nickname-${employment.id}`"
v-model="employment.nickname"
:isValid="false"
validationMessage="You can edit the message"
/>
</FieldContainer>
<!-- File Upload -->
<FileUpload
:name="`file-${employment.id}`"
:id="`file-${employment.id}`"
v-model="employment.file"
/>
</div>
</FieldsToggleContainer>
<!-- Add Employment Button -->
<div class="flex justify-center py-3">
<Button level="secondary" @clicked="addEmployment">+ Add Employment History</Button>
</div>
</div>
</template>
<script setup>
import { ref, computed, watch, reactive, nextTick } from 'vue'
import { createDocumentResource } from 'frappe-ui'
import { useToast } from "vue-toastification"
import { v4 as uuidv4 } from 'uuid' // Ensure to install via npm
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'
// Reactive Variables
const toast = useToast()
const optionsTypeEmployment = reactive([
{ 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 }))
}
// Initialize employment_history
watch(() => request.doc, (newDoc) => {
if (newDoc && !newDoc.employment_history) {
newDoc.employment_history = []
}
}, { immediate: true })
// Section Management
const openSections = reactive({})
const toggleSection = (id) => {
openSections[id] = !openSections[id]
}
const isOpen = (id) => openSections[id]
// Add Employment Entry
const addEmployment = () => {
const newEmployment = {
id: uuidv4(), // Unique identifier
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: '',
file: null, // Assuming file upload
}
request.doc.employment_history.push(newEmployment)
nextTick(() => {
openSections[newEmployment.id] = true
})
}
// Remove Employment Entry
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]
}
}
// Save Request
const saveRequest = async () => {
try {
await request.setValue.submit({
employment_history: request.doc.employment_history,
})
toast.success('Request has been successfully saved!')
} catch (error) {
toast.error('Failed to save the request.')
}
}
// Data
const handleInput = (employmentId, fieldName, value) => {
const employment = request.doc.employment_history.find(emp => emp.id === employmentId)
if (employment) {
employment[fieldName] = value
}
}
const handleCountryChange = (index) => {
// Customize as needed
}
const handleCityChange = (index) => {
// Customize as needed
}
</script>