<!-- src/components/EmploymentHistoryForm.vue --> <template> <div class="pt-3…
- Usercode
- Assistant
<div dir="ltr" class="p-6 bg-bg min-h-screen">
<div v-if="request.doc" class="max-w-7xl mx-auto bg-bg shadow-md rounded-lg p-6">
<!-- رسائل الأخطاء -->
<div v-if="errors.length > 0" class="mb-4 space-y-2">
<ErrorMessage
v-for="(error, index) in errors"
:key="index"
:message="error"
/>
</div>
<!-- أقسام تاريخ التوظيف -->
<div class="space-y-4">
<div
v-for="(employment, index) in request.doc.employment_history"
:key="employment.id"
class="border border-light_gray rounded-md"
>
<!-- حاوية زر التبديل والحذف -->
<div class="flex justify-between items-center px-4 py-2 bg-more_lighter_gray hover:bg-bg rounded-t-md">
<!-- زر التبديل -->
<button
:id="'employment-section-button-' + employment.id"
@click="toggleSection(employment.id)"
:aria-expanded="isOpen(employment.id)"
:aria-controls="'employment-section-content-' + employment.id"
class="flex items-center space-x-2 focus:outline-none"
>
<span class="text-dark_gray">{{ employment.company || 'New Employment Record' }}</span>
<svg
:class="{'transform rotate-180': isOpen(employment.id)}"
class="w-5 h-5 transition-transform duration-200"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
</svg>
</button>
<!-- زر الحذف -->
<button
@click="confirmRemove(employment.id)"
class="bg-warn hover:bg-warn_hover text-white px-2 py-1 rounded-md"
title="Delete Record"
>
Delete
</button>
</div>
<!-- محتوى القسم -->
<transition name="accordion">
<div
v-show="isOpen(employment.id)"
class="p-4 space-y-4"
:id="'employment-section-content-' + employment.id"
role="region"
:aria-labelledby="'employment-section-button-' + employment.id"
>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<!-- Company -->
<div>
<label class="block text-dark_gray">Company</label>
<input
v-model="employment.company"
placeholder="Company"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
<!-- Continuous -->
<div class="flex items-center">
<input
type="checkbox"
v-model="employment.continuous"
:id="'continuous-' + employment.id"
class="mr-2"
/>
<label :for="'continuous-' + employment.id" class="text-dark_gray">Continuous</label>
</div>
<!-- Country -->
<div>
<label class="block text-dark_gray">Country</label>
<select
v-model="employment.country"
@change="employment.selectedCity = ''"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
>
<option value="">Select Country</option>
<option
v-for="country in optionCountry"
:key="country.value"
:value="country.value"
>
{{ country.label }}
</option>
</select>
</div>
<!-- City -->
<div>
<label class="block text-dark_gray">City</label>
<select
v-model="employment.city"
@change="employment.governorate = ''"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
>
<option value="">Select City</option>
<option
v-for="city in getOptionCity(employment.country)"
:key="city.value"
:value="city.value"
>
{{ city.label }}
</option>
</select>
</div>
<!-- Governorate -->
<div>
<label class="block text-dark_gray">Governorate</label>
<select
v-model="employment.governorate"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
>
<option value="">Select Governorate</option>
<option
v-for="governorate in getOptionGovernorate(employment.city)"
:key="governorate.value"
:value="governorate.value"
>
{{ governorate.label }}
</option>
</select>
</div>
<!-- From Date -->
<div>
<label class="block text-dark_gray">From Date</label>
<input
type="date"
v-model="employment.from_date"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
<!-- To Date -->
<div>
<label class="block text-dark_gray">To Date</label>
<input
type="date"
v-model="employment.to_date"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
<!-- Name of Your Employer -->
<div>
<label class="block text-dark_gray">Name of Your Employer</label>
<input
v-model="employment.name_of_your_employer"
placeholder="Name of Your Employer"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
<!-- Phone -->
<FieldContainer>
<div>
<label class="block text-dark_gray">Phone</label>
<input
type="tel"
v-model="employment.phone"
placeholder="Phone"
pattern="[0-9]{10,15}"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
</FieldContainer>
<!-- Extension -->
<div>
<label class="block text-dark_gray">Extension</label>
<input
v-model="employment.Ext"
placeholder="Extension"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
<!-- Type of Employment -->
<div>
<label class="block text-dark_gray">Type of Employment</label>
<select
v-model="employment.type_of_employment"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
>
<option value="">Select Type</option>
<option value="Full-Time">Full-Time</option>
<option value="Part-Time">Part-Time</option>
<option value="Contract">Contract</option>
<option value="Internship">Internship</option>
<!-- أضف الخيارات الأخرى حسب الحاجة -->
</select>
</div>
<!-- Permission to Contact Current Employer -->
<div class="flex items-center">
<input
type="checkbox"
v-model="employment.do_we_have_permission_to_contact_this_current_employer"
:id="'permission-contact-' + employment.id"
class="mr-2"
/>
<label :for="'permission-contact-' + employment.id" class="text-dark_gray">Do we have permission to contact this current employer?</label>
</div>
<!-- Issuing Salary -->
<div class="flex items-center">
<input
type="checkbox"
v-model="employment.issuing_salary"
:id="'issuing-salary-' + employment.id"
class="mr-2"
/>
<label :for="'issuing-salary-' + employment.id" class="text-dark_gray">Issuing Salary</label>
</div>
<!-- Activity Has Stopped -->
<div class="flex items-center">
<input
type="checkbox"
v-model="employment.activity_has_stopped"
:id="'activity-stopped-' + employment.id"
class="mr-2"
/>
<label :for="'activity-stopped-' + employment.id" class="text-dark_gray">Activity Has Stopped</label>
</div>
<!-- The Company Has Different Names -->
<div class="flex items-center">
<input
type="checkbox"
v-model="employment.the_company_has_different_names"
:id="'different-names-' + employment.id"
class="mr-2"
/>
<label :for="'different-names-' + employment.id" class="text-dark_gray">The company has different names</label>
</div>
<!-- Different Company Names -->
<div v-if="employment.the_company_has_different_names">
<label class="block text-dark_gray">Different Company Names</label>
<input
v-model="employment.different_company_names"
placeholder="Different Company Names"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
<!-- Name of the Last Position You Held -->
<div>
<label class="block text-dark_gray">Name of the Last Position You Held</label>
<input
v-model="employment.name_of_the_last_position_you_held"
placeholder="Last Position Held"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
<!-- You Have a Nickname -->
<div class="flex items-center">
<input
type="checkbox"
v-model="employment.you_have_a_nicknamecx"
:id="'nickname-' + employment.id"
class="mr-2"
/>
<label :for="'nickname-' + employment.id" class="text-dark_gray">You have a nickname</label>
</div>
<!-- Nickname -->
<div v-if="employment.you_have_a_nicknamecx">
<label class="block text-dark_gray">Nickname</label>
<input
v-model="employment.nickname"
placeholder="Nickname"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
<!-- File Attachment -->
<!-- <div>
<label class="block text-dark_gray">Attach File</label>
<input
type="file"
@change="handleFileUpload($event, employment)"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
<div v-if="employment.file">
<a :href="employment.file.url" target="_blank" class="text-blue-500 underline">
View Attached File
</a>
</div>
</div> -->
</div>
</div>
</transition>
</div>
</div>
<!-- أزرار الإضافة والحفظ -->
<div class="flex justify-center items-center mt-6 space-x-4">
<button
@click="addEmployment"
class="bg-primary hover:bg-primary_hover text-white px-4 py-2 rounded-md"
>
Add Employment Record
</button>
<button
@click="saveRequest"
:disabled="isSaving"
class="bg-secondary hover:bg-secondary_hover text-white px-4 py-2 rounded-md"
>
<span v-if="!isSaving">Save Request</span>
<span v-else>Saving...</span>
</button>
</div>
</div>
<div v-else class="flex justify-center items-center h-full">
<p class="text-dark_gray">Loading...</p>
</div>
</div>
Your Vue.js component for the Employment History form is quite comprehensive and well-structured. However, there are a few areas where improvements can be made to enhance functionality, maintainability, and user experience. Below is a detailed review along with suggestions:
Issue:
addEmployment function adds a new employment record without setting a unique id. This can cause issues when trying to manage specific records (e.g., toggling sections, deleting records).Solution:
uuidv4 function to assign a unique id to each new employment record.Implementation:
import { v4 as uuidv4 } from 'uuid' // Ensure uuid is installed
const addEmployment = () => {
const newEmployment = {
id: uuidv4(), // Assign a unique ID
company: '',
continuous: false,
country: '',
city: '',
governorate: '',
from_date: '',
to_date: '',
name_of_your_employer: '',
phone: '',
Ext: '',
type_of_employment: '',
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, // Consider renaming
nickname: '',
// file: null // For file attachment
}
request.doc.employment_history.push(newEmployment)
nextTick(() => {
openSections[newEmployment.id] = true
})
}
Note: Ensure you have uuid installed in your project:
npm install uuid
Issue:
you_have_a_nicknamecx seems to have a typographical error (nicknamecx instead of nickname).Solution:
you_have_a_nicknamecx to you_have_a_nickname for consistency and clarity.Implementation:
const addEmployment = () => {
const newEmployment = {
// ... other properties
you_have_a_nickname: false, // Corrected property name
nickname: '',
// ... other properties
}
// Update usage in the template and validation accordingly
}
Template Updates:
<!-- You Have a Nickname -->
<div class="flex items-center">
<input
type="checkbox"
v-model="employment.you_have_a_nickname"
:id="'nickname-' + employment.id"
class="mr-2"
/>
<label :for="'nickname-' + employment.id" class="text-dark_gray">You have a nickname</label>
</div>
<!-- Nickname -->
<div v-if="employment.you_have_a_nickname">
<label class="block text-dark_gray">Nickname</label>
<input
v-model="employment.nickname"
placeholder="Nickname"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
Validation Update:
if (employment.you_have_a_nickname && !employment.nickname) {
errors.value.push(`Nickname is required in record number ${index + 1} when "You have a nickname" is checked.`)
}
Issue:
Solution:
Implementation Steps:
<!-- File Attachment -->
<div>
<label class="block text-dark_gray">Attach File</label>
<input
type="file"
@change="handleFileUpload($event, employment)"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
<div v-if="employment.file">
<a :href="employment.file.url" target="_blank" class="text-blue-500 underline">
View Attached File
</a>
</div>
</div>
handleFileUpload Function:const handleFileUpload = async (event, employment) => {
const file = event.target.files[0]
if (file) {
try {
// Assuming you have a Frappe API endpoint to handle file uploads
const formData = new FormData()
formData.append('file', file)
const response = await fetch('/api/method/upload_file', { // Update the endpoint
method: 'POST',
body: formData
})
const data = await response.json()
if (data.file_url) {
employment.file = {
name: file.name,
url: data.file_url // URL returned from the server
}
toast.success('File uploaded successfully!')
} else {
throw new Error('File upload failed')
}
} catch (error) {
toast.error(`File upload error: ${error.message}`)
}
}
}
Notes:
/api/method/upload_file with your actual file upload endpoint.Issue:
Solution:
Implementation:
// Phone pattern validation
const phonePattern = /^[0-9]{10,15}$/
if (employment.phone && !phonePattern.test(employment.phone)) {
errors.value.push(`Invalid phone number in record number ${index + 1}.`)
}
Enhancements:
Issue:
Solution:
Implementation:
Add a validationErrors object to track errors per employment record and field.
const validationErrors = reactive({})
const saveRequest = async () => {
// Reset previous errors
validationErrors.value = {}
let hasErrors = false
// Validate data
request.doc.employment_history.forEach((employment, index) => {
validationErrors[index] = {}
if (!employment.company) {
validationErrors[index].company = 'Company is required.'
hasErrors = true
}
// Repeat for other fields...
if (employment.you_have_a_nickname && !employment.nickname) {
validationErrors[index].nickname = 'Nickname is required.'
hasErrors = true
}
// Phone validation
if (employment.phone && !phonePattern.test(employment.phone)) {
validationErrors[index].phone = 'Invalid phone number.'
hasErrors = true
}
})
if (hasErrors) {
// Optionally scroll to the first error
return
}
// Proceed with saving
}
<!-- Example for the Company Field -->
<div>
<label class="block text-dark_gray">Company</label>
<input
v-model="employment.company"
placeholder="Company"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
<span v-if="validationErrors[index]?.company" class="text-red-500 text-sm">
{{ validationErrors[index].company }}
</span>
</div>
Benefits:
Issue:
aria-expanded and aria-controls are used, ensuring complete accessibility requires additional considerations.Solution:
Implementation Tips:
Keyboard Navigation:
Tab key.ARIA Attributes:
role="button" where necessary and ensure ARIA labels are descriptive.Color Contrast:
Issue:
Solution:
Implementation Considerations:
Virtualization Libraries:
vue-virtual-scroll-list to render only visible records.Lazy Loading:
Issue:
Solution:
Implementation Steps:
<!-- src/components/EmploymentSection.vue -->
<template>
<div class="border border-light_gray rounded-md">
<!-- Header with Toggle and Delete Buttons -->
<div class="flex justify-between items-center px-4 py-2 bg-more_lighter_gray hover:bg-bg rounded-t-md">
<button
:id="'employment-section-button-' + employment.id"
@click="toggleSection"
:aria-expanded="isOpen"
:aria-controls="'employment-section-content-' + employment.id"
class="flex items-center space-x-2 focus:outline-none"
>
<span class="text-dark_gray">{{ employment.company || 'New Employment Record' }}</span>
<svg
:class="{'transform rotate-180': isOpen}"
class="w-5 h-5 transition-transform duration-200"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
</svg>
</button>
<!-- Delete Button -->
<button
@click="$emit('delete', employment.id)"
class="bg-warn hover:bg-warn_hover text-white px-2 py-1 rounded-md"
title="Delete Record"
>
Delete
</button>
</div>
<!-- Section Content -->
<transition name="accordion">
<div
v-show="isOpen"
class="p-4 space-y-4"
:id="'employment-section-content-' + employment.id"
role="region"
:aria-labelledby="'employment-section-button-' + employment.id"
>
<!-- Employment Form Fields -->
<slot :employment="employment" :index="index"></slot>
</div>
</transition>
</div>
</template>
<script setup>
import { ref } from 'vue'
defineProps({
employment: {
type: Object,
required: true
},
index: {
type: Number,
required: true
}
})
const isOpen = ref(false)
const toggleSection = () => {
isOpen.value = !isOpen.value
}
</script>
<style scoped>
.accordion-enter-active, .accordion-leave-active {
transition: max-height 0.3s ease;
}
.accordion-enter-from, .accordion-leave-to {
max-height: 0;
overflow: hidden;
}
</style>
<template>
<!-- ... other parts of the template -->
<!-- Employment Sections -->
<div class="space-y-4">
<EmploymentSection
v-for="(employment, index) in request.doc.employment_history"
:key="employment.id"
:employment="employment"
:index="index"
@delete="confirmRemove"
>
<!-- Pass the form fields as a slot -->
<template #default="{ employment, index }">
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<!-- Company -->
<div>
<label class="block text-dark_gray">Company</label>
<input
v-model="employment.company"
placeholder="Company"
class="w-full px-3 py-2 border-light_gray rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
/>
<!-- Inline error -->
<span v-if="validationErrors[index]?.company" class="text-red-500 text-sm">
{{ validationErrors[index].company }}
</span>
</div>
<!-- Repeat similar for other fields with inline errors -->
</div>
</template>
</EmploymentSection>
</div>
<!-- ... rest of the template -->
</template>
<script setup>
import EmploymentSection from '../../components/EmploymentSection.vue'
// ... existing script
</script>
Benefits:
Issue:
Solution:
vue-i18n to manage translations.Implementation Steps:
npm install vue-i18n
// src/i18n.js
import { createI18n } from 'vue-i18n'
const messages = {
en: {
employmentHistory: 'Employment History',
addEmployment: 'Add Employment Record',
saveRequest: 'Save Request',
// ... other translations
},
ar: {
employmentHistory: 'تاريخ التوظيف',
addEmployment: 'إضافة سجل توظيف',
saveRequest: 'حفظ الطلب',
// ... other translations
}
}
const i18n = createI18n({
locale: 'en', // Set default locale
messages,
})
export default i18n
// src/main.js
import { createApp } from 'vue'
import App from './App.vue'
import i18n from './i18n'
const app = createApp(App)
app.use(i18n)
app.mount('#app')
<h3 class="text-lg font-medium mb-3 text-black">{{ $t('employmentHistory') }}</h3>
<!-- Add Employment Button -->
<button
@click="addEmployment"
class="bg-primary hover:bg-primary_hover text-white px-4 py-2 rounded-md"
>
{{ $t('addEmployment') }}
</button>
<!-- Save Request Button -->
<button
@click="saveRequest"
:disabled="isSaving"
class="bg-secondary hover:bg-secondary_hover text-white px-4 py-2 rounded-md"
>
<span v-if="!isSaving">{{ $t('saveRequest') }}</span>
<span v-else>{{ $t('saving') }}</span>
</button>
<!-- Other labels and buttons should use $t('key') -->
Benefits:
Suggestion:
Implementation Considerations:
Autosave Intervals:
Local Storage:
localStorage to preserve data between sessions.Explicit Save Draft Option:
Example Autosave Implementation:
import { onMounted, onBeforeUnmount } from 'vue'
const autosaveInterval = 5000 // 5 seconds
const autosave = () => {
try {
localStorage.setItem('employment_history_draft', JSON.stringify(request.doc.employment_history))
toast.info('Draft saved automatically.')
} catch (error) {
console.error('Autosave failed:', error)
}
}
onMounted(() => {
const savedDraft = localStorage.getItem('employment_history_draft')
if (savedDraft) {
request.doc.employment_history = JSON.parse(savedDraft)
}
// Start autosave interval
const intervalId = setInterval(autosave, autosaveInterval)
onBeforeUnmount(() => {
clearInterval(intervalId)
})
})
Benefits:
Recommendations:
Input Sanitization:
File Upload Security:
Authentication and Authorization:
Error Handling:
Suggestion:
Tools:
Example Unit Test with Jest:
// EmploymentHistoryForm.spec.js
import { mount } from '@vue/test-utils'
import EmploymentHistoryForm from '@/components/EmploymentHistoryForm.vue'
import { createDocumentResource } from 'frappe-ui'
jest.mock('frappe-ui', () => ({
createDocumentResource: jest.fn(() => ({
doc: {
employment_history: []
},
setValue: {
submit: jest.fn().mockResolvedValue({ success: true })
}
})),
ErrorMessage: { /* Mock component */ }
}))
describe('EmploymentHistoryForm.vue', () => {
it('renders the form correctly', () => {
const wrapper = mount(EmploymentHistoryForm)
expect(wrapper.find('h3').text()).toBe('Employment History')
expect(wrapper.find('button', { text: 'Add Employment Record' })).toBeTruthy()
})
it('adds a new employment record', async () => {
const wrapper = mount(EmploymentHistoryForm)
await wrapper.find('button', { text: 'Add Employment Record' }).trigger('click')
expect(wrapper.vm.request.doc.employment_history.length).toBe(1)
})
// Add more tests for validation, saving, deleting, etc.
})
Benefits:
Your component is well on its way to being a robust and user-friendly form for managing employment history. By addressing the points above, you can enhance its functionality, maintainability, accessibility, and security. Implementing these suggestions will lead to a more polished and reliable application.
If you have any specific questions or encounter particular issues while implementing these changes, feel free to ask!
Not yet included in a published release.