Fix
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { STUDENT_REGISTRATION, STUDENT_REGISTRATION_ORDER } from '@/enums'
|
||||
|
||||
const STORAGE_KEY = 'student-registration'
|
||||
|
||||
const emptyState = () => ({
|
||||
isProfileCompleted: false,
|
||||
personal: {
|
||||
avatar: null,
|
||||
avatarId: '',
|
||||
firstName: '',
|
||||
lastName: '',
|
||||
birthDate: '',
|
||||
nationalCode: '',
|
||||
maritalStatus: '',
|
||||
gender: '',
|
||||
phoneNumber: '',
|
||||
provinceId: null,
|
||||
cityId: null,
|
||||
address: '',
|
||||
},
|
||||
educational: {
|
||||
educationStatus: '',
|
||||
seminaryLevel: '',
|
||||
universityLevel: '',
|
||||
universityName: '',
|
||||
fieldOfStudy: '',
|
||||
workExperienceSummary: '',
|
||||
activitySummary: '',
|
||||
},
|
||||
professional: {
|
||||
propagationExperienceYears: 0,
|
||||
propagationMethodDescription: '',
|
||||
specializedTopics: '',
|
||||
propagationPlatforms: [],
|
||||
},
|
||||
skill1: {
|
||||
responseToLowSatisfaction: '',
|
||||
responseToLowSatisfactionDescription: '',
|
||||
},
|
||||
skill2: {
|
||||
responseToSessionCancellation: '',
|
||||
responseToSessionCancellationDescription: '',
|
||||
},
|
||||
skill3: {
|
||||
responseToAudienceConflict: '',
|
||||
responseToAudienceConflictDescription: '',
|
||||
},
|
||||
skill4: {
|
||||
responseToCompetingPropagator: '',
|
||||
responseToCompetingPropagatorDescription: '',
|
||||
},
|
||||
skill5: {
|
||||
responseToUnqualifiedAdvisors: '',
|
||||
relevantCertificates: '',
|
||||
hijabApproach: '',
|
||||
},
|
||||
skill6: {
|
||||
faithProductionAudio: null,
|
||||
faithProductionId: '',
|
||||
},
|
||||
skill7: {
|
||||
leaderMessageVideo: null,
|
||||
leaderMessageId: '',
|
||||
},
|
||||
currentStep: STUDENT_REGISTRATION.PERSONAL_INFORMATION,
|
||||
completedSteps: [],
|
||||
registrationMedia: {},
|
||||
})
|
||||
|
||||
const readPersisted = () => {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY)
|
||||
if (!raw) return null
|
||||
return JSON.parse(raw)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export const useStudentRegistrationStore = defineStore('studentRegistration', () => {
|
||||
const persisted = readPersisted() || {}
|
||||
const initial = { ...emptyState(), ...persisted }
|
||||
|
||||
const isProfileCompleted = ref(initial.isProfileCompleted)
|
||||
const personal = ref({ ...emptyState().personal, ...persisted.personal })
|
||||
const educational = ref({ ...emptyState().educational, ...persisted.educational })
|
||||
const professional = ref({ ...emptyState().professional, ...persisted.professional })
|
||||
const skill1 = ref({ ...emptyState().skill1, ...persisted.skill1 })
|
||||
const skill2 = ref({ ...emptyState().skill2, ...persisted.skill2 })
|
||||
const skill3 = ref({ ...emptyState().skill3, ...persisted.skill3 })
|
||||
const skill4 = ref({ ...emptyState().skill4, ...persisted.skill4 })
|
||||
const skill5 = ref({ ...emptyState().skill5, ...persisted.skill5 })
|
||||
const skill6 = ref({ ...emptyState().skill6, ...persisted.skill6 })
|
||||
const skill7 = ref({ ...emptyState().skill7, ...persisted.skill7 })
|
||||
const currentStep = ref(initial.currentStep)
|
||||
const completedSteps = ref(initial.completedSteps || [])
|
||||
const registrationMedia = ref(initial.registrationMedia || {})
|
||||
|
||||
const snapshot = computed(() => ({
|
||||
isProfileCompleted: isProfileCompleted.value,
|
||||
personal: personal.value,
|
||||
educational: educational.value,
|
||||
professional: professional.value,
|
||||
skill1: skill1.value,
|
||||
skill2: skill2.value,
|
||||
skill3: skill3.value,
|
||||
skill4: skill4.value,
|
||||
skill5: skill5.value,
|
||||
skill6: skill6.value,
|
||||
skill7: skill7.value,
|
||||
currentStep: currentStep.value,
|
||||
completedSteps: completedSteps.value,
|
||||
registrationMedia: registrationMedia.value,
|
||||
}))
|
||||
|
||||
watch(
|
||||
snapshot,
|
||||
(value) => {
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(value))
|
||||
} catch {
|
||||
/* storage may be unavailable */
|
||||
}
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
|
||||
const markStepAsCompleted = (stepKey) => {
|
||||
if (!completedSteps.value.includes(stepKey)) {
|
||||
completedSteps.value = [...completedSteps.value, stepKey]
|
||||
}
|
||||
}
|
||||
|
||||
const updateCurrentStep = (stepKey) => {
|
||||
if (stepKey) currentStep.value = stepKey
|
||||
}
|
||||
|
||||
const goToNextStep = () => {
|
||||
const idx = STUDENT_REGISTRATION_ORDER.indexOf(currentStep.value)
|
||||
if (idx >= 0 && idx < STUDENT_REGISTRATION_ORDER.length - 1) {
|
||||
currentStep.value = STUDENT_REGISTRATION_ORDER[idx + 1]
|
||||
}
|
||||
}
|
||||
|
||||
const goToPrevStep = () => {
|
||||
const idx = STUDENT_REGISTRATION_ORDER.indexOf(currentStep.value)
|
||||
if (idx > 0) currentStep.value = STUDENT_REGISTRATION_ORDER[idx - 1]
|
||||
}
|
||||
|
||||
const canGoToStep = (stepKey) => {
|
||||
const currentIndex = STUDENT_REGISTRATION_ORDER.indexOf(currentStep.value)
|
||||
const targetIndex = STUDENT_REGISTRATION_ORDER.indexOf(stepKey)
|
||||
return targetIndex <= currentIndex + 1
|
||||
}
|
||||
|
||||
const buildCompletePayload = () => ({
|
||||
...personal.value,
|
||||
...educational.value,
|
||||
...professional.value,
|
||||
...skill1.value,
|
||||
...skill2.value,
|
||||
...skill3.value,
|
||||
...skill4.value,
|
||||
...skill5.value,
|
||||
...skill6.value,
|
||||
...skill7.value,
|
||||
avatarId: personal.value.avatar?.uploadId
|
||||
? Number.parseInt(personal.value.avatar.uploadId)
|
||||
: personal.value.avatarId,
|
||||
})
|
||||
|
||||
const reset = () => {
|
||||
const fresh = emptyState()
|
||||
isProfileCompleted.value = fresh.isProfileCompleted
|
||||
personal.value = fresh.personal
|
||||
educational.value = fresh.educational
|
||||
professional.value = fresh.professional
|
||||
skill1.value = fresh.skill1
|
||||
skill2.value = fresh.skill2
|
||||
skill3.value = fresh.skill3
|
||||
skill4.value = fresh.skill4
|
||||
skill5.value = fresh.skill5
|
||||
skill6.value = fresh.skill6
|
||||
skill7.value = fresh.skill7
|
||||
currentStep.value = fresh.currentStep
|
||||
completedSteps.value = fresh.completedSteps
|
||||
registrationMedia.value = fresh.registrationMedia
|
||||
try {
|
||||
localStorage.removeItem(STORAGE_KEY)
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
isProfileCompleted,
|
||||
personal,
|
||||
educational,
|
||||
professional,
|
||||
skill1,
|
||||
skill2,
|
||||
skill3,
|
||||
skill4,
|
||||
skill5,
|
||||
skill6,
|
||||
skill7,
|
||||
currentStep,
|
||||
completedSteps,
|
||||
registrationMedia,
|
||||
markStepAsCompleted,
|
||||
updateCurrentStep,
|
||||
goToNextStep,
|
||||
goToPrevStep,
|
||||
canGoToStep,
|
||||
buildCompletePayload,
|
||||
reset,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user