恢复后台表单弹窗草稿
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest'
|
||||
import { buildFormModalDraftKey, readFormModalDraft, removeFormModalDraft, writeFormModalDraft } from '@/utils/form-modal-draft'
|
||||
import type { UserInfo } from '@/types/common'
|
||||
|
||||
interface EmployeeDraftForm {
|
||||
nickname: string
|
||||
phone: string
|
||||
}
|
||||
|
||||
const buildEmployeeDraftForm = (): EmployeeDraftForm => ({
|
||||
nickname: '王女士',
|
||||
phone: '13800138000'
|
||||
})
|
||||
|
||||
describe('form modal draft storage', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.clear()
|
||||
window.sessionStorage.clear()
|
||||
})
|
||||
|
||||
it('scopes draft by user, shop and modal name', () => {
|
||||
const userInfo: UserInfo = { id: 10, shopId: 20 }
|
||||
|
||||
expect(buildFormModalDraftKey(userInfo, 'employee-create')).toBe('managerFormModalDraft:10:20:employee-create')
|
||||
})
|
||||
|
||||
it('writes, reads and removes modal draft', () => {
|
||||
const userInfo: UserInfo = { id: 10, shopId: 20 }
|
||||
const form = buildEmployeeDraftForm()
|
||||
|
||||
writeFormModalDraft(userInfo, 'employee-create', form, true)
|
||||
expect(readFormModalDraft<EmployeeDraftForm>(userInfo, 'employee-create')?.form).toEqual(form)
|
||||
expect(readFormModalDraft<EmployeeDraftForm>(userInfo, 'employee-create')?.open).toBe(true)
|
||||
|
||||
removeFormModalDraft(userInfo, 'employee-create')
|
||||
expect(readFormModalDraft<EmployeeDraftForm>(userInfo, 'employee-create')).toBeNull()
|
||||
})
|
||||
|
||||
it('rejects invalid draft names', () => {
|
||||
const userInfo: UserInfo = { id: 10, shopId: 20 }
|
||||
|
||||
expect(() => buildFormModalDraftKey(userInfo, ' ')).toThrow('弹窗草稿名称不能为空')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { UserInfo } from '@/types/common'
|
||||
import { readJson, removeStorage, writeJson } from '@/utils/storage'
|
||||
|
||||
const FORM_MODAL_DRAFT_PREFIX = 'managerFormModalDraft'
|
||||
|
||||
export interface FormModalDraft<TForm extends object> {
|
||||
form: TForm
|
||||
open: boolean
|
||||
savedAt: number
|
||||
}
|
||||
|
||||
export const buildFormModalDraftKey = (userInfo: UserInfo | null, modalName: string): string => {
|
||||
const normalizedModalName = modalName.trim()
|
||||
if (!normalizedModalName) {
|
||||
throw new Error('弹窗草稿名称不能为空')
|
||||
}
|
||||
return `${FORM_MODAL_DRAFT_PREFIX}:${userInfo?.id || 'guest'}:${userInfo?.shopId || 'default'}:${normalizedModalName}`
|
||||
}
|
||||
|
||||
const isObjectRecord = (value: unknown): value is Record<string, unknown> => {
|
||||
return typeof value === 'object' && value !== null
|
||||
}
|
||||
|
||||
const isFormModalDraft = <TForm extends object>(value: unknown): value is FormModalDraft<TForm> => {
|
||||
if (!isObjectRecord(value)) {
|
||||
return false
|
||||
}
|
||||
return isObjectRecord(value.form) && typeof value.open === 'boolean' && typeof value.savedAt === 'number'
|
||||
}
|
||||
|
||||
export const readFormModalDraft = <TForm extends object>(userInfo: UserInfo | null, modalName: string): FormModalDraft<TForm> | null => {
|
||||
const draft = readJson<unknown>(buildFormModalDraftKey(userInfo, modalName), null)
|
||||
if (!isFormModalDraft<TForm>(draft)) {
|
||||
return null
|
||||
}
|
||||
return draft
|
||||
}
|
||||
|
||||
export const writeFormModalDraft = <TForm extends object>(userInfo: UserInfo | null, modalName: string, form: TForm, open: boolean): void => {
|
||||
writeJson(buildFormModalDraftKey(userInfo, modalName), { form, open, savedAt: Date.now() })
|
||||
}
|
||||
|
||||
export const removeFormModalDraft = (userInfo: UserInfo | null, modalName: string): void => {
|
||||
removeStorage(buildFormModalDraftKey(userInfo, modalName))
|
||||
}
|
||||
@@ -31,7 +31,12 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<BaseModal v-model="productModalVisible" title="选择预约作品" :has-unsaved-changes="manualProductChanged">
|
||||
<BaseModal
|
||||
:model-value="productModalVisible"
|
||||
title="选择预约作品"
|
||||
:has-unsaved-changes="manualProductChanged"
|
||||
@update:model-value="updateProductModalVisible"
|
||||
>
|
||||
<div class="product-modal">
|
||||
<input v-model.trim="productKeyword" class="search-input" placeholder="搜索作品" @input="filterProducts" />
|
||||
<div class="list">
|
||||
@@ -56,7 +61,11 @@
|
||||
</div>
|
||||
</BaseModal>
|
||||
|
||||
<BaseModal v-model="timeSheetVisible" title="选择可约时间">
|
||||
<BaseModal
|
||||
:model-value="timeSheetVisible"
|
||||
title="选择可约时间"
|
||||
@update:model-value="updateTimeSheetVisible"
|
||||
>
|
||||
<div class="time-sheet">
|
||||
<LoadingState v-if="timeLoading" text="正在计算可约时间" />
|
||||
<EmptyState v-else-if="availableTimeGroups.length === 0" title="暂无可约时间" :description="timeSelectTip" />
|
||||
@@ -75,7 +84,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive, ref } from 'vue'
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import BaseModal from '@/components/BaseModal.vue'
|
||||
import EmptyState from '@/components/EmptyState.vue'
|
||||
@@ -94,6 +103,7 @@ import { formatDate, minutesOfDay, timeFromMinutes } from '@/utils/date'
|
||||
import { buildAvailableTimeGroups, type AvailableTimeGroup } from '@/utils/available-times'
|
||||
import { buildClientRequestId, normalizeDuration } from '@/utils/planning'
|
||||
import { readDraftForm, removeDraftForm, writeDraftForm } from '@/utils/appointment-draft'
|
||||
import { readFormModalDraft, removeFormModalDraft, writeFormModalDraft } from '@/utils/form-modal-draft'
|
||||
import { markPlanningRefresh } from '@/utils/storage'
|
||||
|
||||
interface ProductCard extends Product {
|
||||
@@ -103,11 +113,19 @@ interface ProductCard extends Product {
|
||||
artistText: string
|
||||
}
|
||||
|
||||
interface AppointmentModalState {
|
||||
productModalVisible: boolean
|
||||
timeSheetVisible: boolean
|
||||
}
|
||||
|
||||
const APPOINTMENT_MODAL_STATE_DRAFT_NAME = 'appointment-modal-state'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const session = useSessionStore()
|
||||
const toast = useToastStore()
|
||||
const baseForm = readDraftForm(session.userInfo)
|
||||
const modalStateDraft = readFormModalDraft<AppointmentModalState>(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||||
const form = reactive<AppointmentForm>({
|
||||
customerUserId: '',
|
||||
customerName: String(route.query.customerName || baseForm?.customerName || ''),
|
||||
@@ -133,10 +151,10 @@ const customerTimer = ref<number | null>(null)
|
||||
const products = ref<ProductCard[]>([])
|
||||
const filteredProducts = ref<ProductCard[]>([])
|
||||
const productKeyword = ref('')
|
||||
const productModalVisible = ref(false)
|
||||
const productModalVisible = ref(Boolean(modalStateDraft?.open && modalStateDraft.form.productModalVisible))
|
||||
const artists = ref<Artist[]>([])
|
||||
const selectedProduct = ref<ProductCard | null>(null)
|
||||
const timeSheetVisible = ref(false)
|
||||
const timeSheetVisible = ref(Boolean(modalStateDraft?.open && modalStateDraft.form.timeSheetVisible))
|
||||
const availableTimeGroups = ref<AvailableTimeGroup[]>([])
|
||||
const timeLoading = ref(false)
|
||||
const timeSelectTip = ref('请先选择美甲师并填写时长')
|
||||
@@ -151,6 +169,57 @@ const manualProductChanged = computed(() => {
|
||||
|| form.manualProductDescription
|
||||
)
|
||||
})
|
||||
const appointmentFormChanged = computed(() => {
|
||||
return Boolean(
|
||||
form.customerUserId
|
||||
|| form.customerName
|
||||
|| form.phone
|
||||
|| form.appointmentTime
|
||||
|| form.productId
|
||||
|| form.manualProductTitle
|
||||
|| form.manualProductPrice
|
||||
|| form.manualProductDescription
|
||||
|| form.manualProductTags
|
||||
|| form.manualProductTargetAudience
|
||||
|| form.manualProductPrecautions
|
||||
|| form.manualProductImageFileIds
|
||||
|| form.manualProductImageUrl
|
||||
|| form.durationMinutes
|
||||
|| form.artistId
|
||||
|| form.remark
|
||||
)
|
||||
})
|
||||
|
||||
const syncAppointmentDraft = (): void => {
|
||||
if (!appointmentFormChanged.value) {
|
||||
return
|
||||
}
|
||||
writeDraftForm(session.userInfo, { ...form })
|
||||
if (!draftSavedAtText.value) {
|
||||
draftSavedAtText.value = '草稿已自动保存到本机'
|
||||
}
|
||||
}
|
||||
|
||||
const syncAppointmentModalState = (): void => {
|
||||
if (!productModalVisible.value && !timeSheetVisible.value) {
|
||||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||||
return
|
||||
}
|
||||
writeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME, {
|
||||
productModalVisible: productModalVisible.value,
|
||||
timeSheetVisible: timeSheetVisible.value
|
||||
}, true)
|
||||
}
|
||||
|
||||
const updateProductModalVisible = (value: boolean): void => {
|
||||
productModalVisible.value = value
|
||||
syncAppointmentModalState()
|
||||
}
|
||||
|
||||
const updateTimeSheetVisible = (value: boolean): void => {
|
||||
timeSheetVisible.value = value
|
||||
syncAppointmentModalState()
|
||||
}
|
||||
|
||||
const selectedProductTitle = computed(() => {
|
||||
if (selectedProduct.value) {
|
||||
@@ -231,7 +300,7 @@ const selectProduct = (product: ProductCard): void => {
|
||||
form.manualProductTitle = ''
|
||||
form.manualProductPrice = ''
|
||||
form.durationMinutes = String(Number.parseInt(product.approximateDuration || '', 10) || product.serviceDurationMinutes || form.durationMinutes)
|
||||
productModalVisible.value = false
|
||||
updateProductModalVisible(false)
|
||||
void refreshAvailableTimes()
|
||||
}
|
||||
|
||||
@@ -244,7 +313,7 @@ const useManualProduct = (): void => {
|
||||
}
|
||||
form.productId = ''
|
||||
selectedProduct.value = null
|
||||
productModalVisible.value = false
|
||||
updateProductModalVisible(false)
|
||||
}
|
||||
|
||||
const refreshAvailableTimes = async (): Promise<void> => {
|
||||
@@ -269,13 +338,13 @@ const openTimeSheet = async (): Promise<void> => {
|
||||
toast.error('请先选择美甲师并填写时长')
|
||||
return
|
||||
}
|
||||
timeSheetVisible.value = true
|
||||
updateTimeSheetVisible(true)
|
||||
await refreshAvailableTimes()
|
||||
}
|
||||
|
||||
const selectAvailableTime = (value: string): void => {
|
||||
form.appointmentTime = value
|
||||
timeSheetVisible.value = false
|
||||
updateTimeSheetVisible(false)
|
||||
void checkAvailability()
|
||||
}
|
||||
|
||||
@@ -334,6 +403,7 @@ const submit = async (): Promise<void> => {
|
||||
}
|
||||
const result = await managerPlanningApi.createAppointment(payload)
|
||||
removeDraftForm(session.userInfo)
|
||||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||||
markPlanningRefresh()
|
||||
toast.success('预约已创建')
|
||||
await router.replace(`/appointments/${result.orderItemId}`)
|
||||
@@ -350,6 +420,7 @@ const saveDraft = (): void => {
|
||||
|
||||
const clearDraft = (): void => {
|
||||
removeDraftForm(session.userInfo)
|
||||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||||
draftSavedAtText.value = ''
|
||||
toast.success('草稿已清空')
|
||||
}
|
||||
@@ -359,6 +430,10 @@ onMounted(async () => {
|
||||
await searchCustomers()
|
||||
await refreshAvailableTimes()
|
||||
})
|
||||
|
||||
watch(form, syncAppointmentDraft, { deep: true })
|
||||
watch(productModalVisible, syncAppointmentModalState)
|
||||
watch(timeSheetVisible, syncAppointmentModalState)
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -21,7 +21,12 @@
|
||||
|
||||
<button class="fab" type="button" @click="openCreate">+</button>
|
||||
|
||||
<BaseModal v-model="createModalVisible" title="新增客户" :has-unsaved-changes="createFormChanged">
|
||||
<BaseModal
|
||||
:model-value="createModalVisible"
|
||||
title="新增客户"
|
||||
:has-unsaved-changes="createFormChanged"
|
||||
@update:model-value="updateCreateModalVisible"
|
||||
>
|
||||
<div class="modal-form">
|
||||
<div class="field">
|
||||
<label class="required">客户姓名</label>
|
||||
@@ -40,7 +45,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive, ref } from 'vue'
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import BaseModal from '@/components/BaseModal.vue'
|
||||
import EmptyState from '@/components/EmptyState.vue'
|
||||
@@ -48,18 +53,71 @@ import LoadingState from '@/components/LoadingState.vue'
|
||||
import MobileShell from '@/components/MobileShell.vue'
|
||||
import { managerCustomersApi } from '@/api/managerCustomers'
|
||||
import type { UserInfo } from '@/types/common'
|
||||
import { useSessionStore } from '@/stores/session'
|
||||
import { useToastStore } from '@/stores/toast'
|
||||
import { readFormModalDraft, removeFormModalDraft, writeFormModalDraft } from '@/utils/form-modal-draft'
|
||||
|
||||
interface CreateCustomerForm {
|
||||
nickname: string
|
||||
phone: string
|
||||
}
|
||||
|
||||
const CREATE_CUSTOMER_DRAFT_NAME = 'customer-create'
|
||||
|
||||
const router = useRouter()
|
||||
const session = useSessionStore()
|
||||
const toast = useToastStore()
|
||||
const keyword = ref('')
|
||||
const loading = ref(false)
|
||||
const customers = ref<UserInfo[]>([])
|
||||
const createModalVisible = ref(false)
|
||||
const creating = ref(false)
|
||||
const createForm = reactive({ nickname: '', phone: '' })
|
||||
const buildEmptyCreateForm = (): CreateCustomerForm => ({ nickname: '', phone: '' })
|
||||
const createForm = reactive<CreateCustomerForm>(buildEmptyCreateForm())
|
||||
const createFormChanged = computed(() => Boolean(createForm.nickname || createForm.phone))
|
||||
|
||||
const snapshotCreateForm = (): CreateCustomerForm => ({
|
||||
nickname: createForm.nickname,
|
||||
phone: createForm.phone
|
||||
})
|
||||
|
||||
const applyCreateForm = (form: CreateCustomerForm): void => {
|
||||
createForm.nickname = form.nickname || ''
|
||||
createForm.phone = form.phone || ''
|
||||
}
|
||||
|
||||
const resetCreateForm = (): void => {
|
||||
applyCreateForm(buildEmptyCreateForm())
|
||||
}
|
||||
|
||||
const syncCreateDraft = (): void => {
|
||||
if (!createModalVisible.value || !createFormChanged.value) {
|
||||
return
|
||||
}
|
||||
writeFormModalDraft(session.userInfo, CREATE_CUSTOMER_DRAFT_NAME, snapshotCreateForm(), createModalVisible.value)
|
||||
}
|
||||
|
||||
const restoreCreateDraft = (): void => {
|
||||
const draft = readFormModalDraft<CreateCustomerForm>(session.userInfo, CREATE_CUSTOMER_DRAFT_NAME)
|
||||
if (!draft?.open) {
|
||||
return
|
||||
}
|
||||
applyCreateForm(draft.form)
|
||||
createModalVisible.value = true
|
||||
}
|
||||
|
||||
const clearCreateDraft = (): void => {
|
||||
removeFormModalDraft(session.userInfo, CREATE_CUSTOMER_DRAFT_NAME)
|
||||
}
|
||||
|
||||
const updateCreateModalVisible = (value: boolean): void => {
|
||||
createModalVisible.value = value
|
||||
if (!value) {
|
||||
clearCreateDraft()
|
||||
resetCreateForm()
|
||||
}
|
||||
}
|
||||
|
||||
const loadCustomers = async (): Promise<void> => {
|
||||
loading.value = true
|
||||
try {
|
||||
@@ -71,8 +129,8 @@ const loadCustomers = async (): Promise<void> => {
|
||||
}
|
||||
|
||||
const openCreate = (): void => {
|
||||
createForm.nickname = ''
|
||||
createForm.phone = ''
|
||||
clearCreateDraft()
|
||||
resetCreateForm()
|
||||
createModalVisible.value = true
|
||||
}
|
||||
|
||||
@@ -85,7 +143,7 @@ const createCustomer = async (): Promise<void> => {
|
||||
try {
|
||||
await managerCustomersApi.create({ nickname: createForm.nickname, phone: createForm.phone })
|
||||
toast.success('客户已新增')
|
||||
createModalVisible.value = false
|
||||
updateCreateModalVisible(false)
|
||||
await loadCustomers()
|
||||
} finally {
|
||||
creating.value = false
|
||||
@@ -93,8 +151,12 @@ const createCustomer = async (): Promise<void> => {
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
restoreCreateDraft()
|
||||
void loadCustomers()
|
||||
})
|
||||
|
||||
watch(createForm, syncCreateDraft, { deep: true })
|
||||
watch(createModalVisible, syncCreateDraft)
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -21,7 +21,12 @@
|
||||
|
||||
<button class="fab" type="button" @click="openCreate">+</button>
|
||||
|
||||
<BaseModal v-model="createModalVisible" title="新增员工" :has-unsaved-changes="createFormChanged">
|
||||
<BaseModal
|
||||
:model-value="createModalVisible"
|
||||
title="新增员工"
|
||||
:has-unsaved-changes="createFormChanged"
|
||||
@update:model-value="updateCreateModalVisible"
|
||||
>
|
||||
<div class="modal-form">
|
||||
<div class="field"><label class="required">姓名</label><input v-model.trim="createForm.nickname" placeholder="请输入姓名" /></div>
|
||||
<div class="field"><label class="required">手机号</label><input v-model.trim="createForm.phone" inputmode="numeric" placeholder="请输入手机号" /></div>
|
||||
@@ -35,7 +40,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive, ref } from 'vue'
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import BaseModal from '@/components/BaseModal.vue'
|
||||
import EmptyState from '@/components/EmptyState.vue'
|
||||
@@ -43,25 +48,87 @@ import LoadingState from '@/components/LoadingState.vue'
|
||||
import MobileShell from '@/components/MobileShell.vue'
|
||||
import { managerEmployeesApi } from '@/api/managerEmployees'
|
||||
import type { Artist } from '@/types/manager'
|
||||
import { useSessionStore } from '@/stores/session'
|
||||
import { useToastStore } from '@/stores/toast'
|
||||
import { readFormModalDraft, removeFormModalDraft, writeFormModalDraft } from '@/utils/form-modal-draft'
|
||||
|
||||
interface CreateEmployeeForm {
|
||||
nickname: string
|
||||
phone: string
|
||||
account: string
|
||||
password: string
|
||||
wechatId: string
|
||||
}
|
||||
|
||||
const CREATE_EMPLOYEE_DRAFT_NAME = 'employee-create'
|
||||
|
||||
const router = useRouter()
|
||||
const session = useSessionStore()
|
||||
const toast = useToastStore()
|
||||
const loading = ref(false)
|
||||
const employees = ref<Artist[]>([])
|
||||
const createModalVisible = ref(false)
|
||||
const creating = ref(false)
|
||||
const createForm = reactive({
|
||||
const buildEmptyCreateForm = (): CreateEmployeeForm => ({
|
||||
nickname: '',
|
||||
phone: '',
|
||||
account: '',
|
||||
password: '',
|
||||
wechatId: ''
|
||||
})
|
||||
const createForm = reactive<CreateEmployeeForm>(buildEmptyCreateForm())
|
||||
const createFormChanged = computed(() => {
|
||||
return Boolean(createForm.nickname || createForm.phone || createForm.account || createForm.password || createForm.wechatId)
|
||||
})
|
||||
|
||||
const snapshotCreateForm = (): CreateEmployeeForm => ({
|
||||
nickname: createForm.nickname,
|
||||
phone: createForm.phone,
|
||||
account: createForm.account,
|
||||
password: createForm.password,
|
||||
wechatId: createForm.wechatId
|
||||
})
|
||||
|
||||
const applyCreateForm = (form: CreateEmployeeForm): void => {
|
||||
createForm.nickname = form.nickname || ''
|
||||
createForm.phone = form.phone || ''
|
||||
createForm.account = form.account || ''
|
||||
createForm.password = form.password || ''
|
||||
createForm.wechatId = form.wechatId || ''
|
||||
}
|
||||
|
||||
const resetCreateForm = (): void => {
|
||||
applyCreateForm(buildEmptyCreateForm())
|
||||
}
|
||||
|
||||
const syncCreateDraft = (): void => {
|
||||
if (!createModalVisible.value || !createFormChanged.value) {
|
||||
return
|
||||
}
|
||||
writeFormModalDraft(session.userInfo, CREATE_EMPLOYEE_DRAFT_NAME, snapshotCreateForm(), createModalVisible.value)
|
||||
}
|
||||
|
||||
const restoreCreateDraft = (): void => {
|
||||
const draft = readFormModalDraft<CreateEmployeeForm>(session.userInfo, CREATE_EMPLOYEE_DRAFT_NAME)
|
||||
if (!draft?.open) {
|
||||
return
|
||||
}
|
||||
applyCreateForm(draft.form)
|
||||
createModalVisible.value = true
|
||||
}
|
||||
|
||||
const clearCreateDraft = (): void => {
|
||||
removeFormModalDraft(session.userInfo, CREATE_EMPLOYEE_DRAFT_NAME)
|
||||
}
|
||||
|
||||
const updateCreateModalVisible = (value: boolean): void => {
|
||||
createModalVisible.value = value
|
||||
if (!value) {
|
||||
clearCreateDraft()
|
||||
resetCreateForm()
|
||||
}
|
||||
}
|
||||
|
||||
const loadEmployees = async (): Promise<void> => {
|
||||
loading.value = true
|
||||
try {
|
||||
@@ -72,11 +139,8 @@ const loadEmployees = async (): Promise<void> => {
|
||||
}
|
||||
|
||||
const openCreate = (): void => {
|
||||
createForm.nickname = ''
|
||||
createForm.phone = ''
|
||||
createForm.account = ''
|
||||
createForm.password = ''
|
||||
createForm.wechatId = ''
|
||||
clearCreateDraft()
|
||||
resetCreateForm()
|
||||
createModalVisible.value = true
|
||||
}
|
||||
|
||||
@@ -96,7 +160,7 @@ const createEmployee = async (): Promise<void> => {
|
||||
wechatId: createForm.wechatId
|
||||
})
|
||||
toast.success('美甲师已新增')
|
||||
createModalVisible.value = false
|
||||
updateCreateModalVisible(false)
|
||||
await loadEmployees()
|
||||
} finally {
|
||||
creating.value = false
|
||||
@@ -104,8 +168,12 @@ const createEmployee = async (): Promise<void> => {
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
restoreCreateDraft()
|
||||
void loadEmployees()
|
||||
})
|
||||
|
||||
watch(createForm, syncCreateDraft, { deep: true })
|
||||
watch(createModalVisible, syncCreateDraft)
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -25,10 +25,11 @@
|
||||
</section>
|
||||
|
||||
<BaseModal
|
||||
v-model="editModalVisible"
|
||||
:model-value="editModalVisible"
|
||||
title="个人信息"
|
||||
:close-disabled="profileSaving || avatarUploading"
|
||||
:has-unsaved-changes="profileFormChanged"
|
||||
@update:model-value="updateEditModalVisible"
|
||||
>
|
||||
<div class="profile-avatar-row">
|
||||
<img v-if="profileForm.avatar" class="profile-avatar profile-avatar--image" :src="profileForm.avatar" alt="头像" />
|
||||
@@ -81,7 +82,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive, ref } from 'vue'
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { ChevronRight } from 'lucide-vue-next'
|
||||
import BaseModal from '@/components/BaseModal.vue'
|
||||
@@ -92,6 +93,7 @@ import { userApi } from '@/api/user'
|
||||
import { useSessionStore } from '@/stores/session'
|
||||
import { useToastStore } from '@/stores/toast'
|
||||
import type { UserInfo } from '@/types/common'
|
||||
import { readFormModalDraft, removeFormModalDraft, writeFormModalDraft } from '@/utils/form-modal-draft'
|
||||
|
||||
interface ProfileMenu {
|
||||
title: string
|
||||
@@ -99,6 +101,21 @@ interface ProfileMenu {
|
||||
action?: 'editProfile'
|
||||
}
|
||||
|
||||
interface ProfileDraftForm {
|
||||
nickname: string
|
||||
avatar: string
|
||||
contactPhone: string
|
||||
wechatId: string
|
||||
signature: string
|
||||
}
|
||||
|
||||
interface ProfileDraftData {
|
||||
form: ProfileDraftForm
|
||||
baseline: ProfileDraftForm
|
||||
}
|
||||
|
||||
const PROFILE_EDIT_DRAFT_NAME = 'profile-edit'
|
||||
|
||||
const router = useRouter()
|
||||
const session = useSessionStore()
|
||||
const toast = useToastStore()
|
||||
@@ -122,6 +139,69 @@ const profileBaseline = reactive({
|
||||
signature: ''
|
||||
})
|
||||
|
||||
const snapshotProfileForm = (): ProfileDraftForm => ({
|
||||
nickname: profileForm.nickname,
|
||||
avatar: profileForm.avatar,
|
||||
contactPhone: profileForm.contactPhone,
|
||||
wechatId: profileForm.wechatId,
|
||||
signature: profileForm.signature
|
||||
})
|
||||
|
||||
const snapshotProfileBaseline = (): ProfileDraftForm => ({
|
||||
nickname: profileBaseline.nickname,
|
||||
avatar: profileBaseline.avatar,
|
||||
contactPhone: profileBaseline.contactPhone,
|
||||
wechatId: profileBaseline.wechatId,
|
||||
signature: profileBaseline.signature
|
||||
})
|
||||
|
||||
const applyProfileForm = (form: ProfileDraftForm): void => {
|
||||
profileForm.nickname = form.nickname || ''
|
||||
profileForm.avatar = form.avatar || ''
|
||||
profileForm.contactPhone = form.contactPhone || ''
|
||||
profileForm.wechatId = form.wechatId || ''
|
||||
profileForm.signature = form.signature || ''
|
||||
}
|
||||
|
||||
const applyProfileBaseline = (form: ProfileDraftForm): void => {
|
||||
profileBaseline.nickname = form.nickname || ''
|
||||
profileBaseline.avatar = form.avatar || ''
|
||||
profileBaseline.contactPhone = form.contactPhone || ''
|
||||
profileBaseline.wechatId = form.wechatId || ''
|
||||
profileBaseline.signature = form.signature || ''
|
||||
}
|
||||
|
||||
const syncProfileDraft = (): void => {
|
||||
if (!editModalVisible.value || !profileFormChanged.value) {
|
||||
return
|
||||
}
|
||||
writeFormModalDraft(session.userInfo, PROFILE_EDIT_DRAFT_NAME, {
|
||||
form: snapshotProfileForm(),
|
||||
baseline: snapshotProfileBaseline()
|
||||
}, editModalVisible.value)
|
||||
}
|
||||
|
||||
const restoreProfileDraft = (): void => {
|
||||
const draft = readFormModalDraft<ProfileDraftData>(session.userInfo, PROFILE_EDIT_DRAFT_NAME)
|
||||
if (!draft?.open) {
|
||||
return
|
||||
}
|
||||
applyProfileBaseline(draft.form.baseline)
|
||||
applyProfileForm(draft.form.form)
|
||||
editModalVisible.value = true
|
||||
}
|
||||
|
||||
const clearProfileDraft = (): void => {
|
||||
removeFormModalDraft(session.userInfo, PROFILE_EDIT_DRAFT_NAME)
|
||||
}
|
||||
|
||||
const updateEditModalVisible = (value: boolean): void => {
|
||||
editModalVisible.value = value
|
||||
if (!value) {
|
||||
clearProfileDraft()
|
||||
}
|
||||
}
|
||||
|
||||
const menus: ProfileMenu[] = [
|
||||
{ title: '个人信息', action: 'editProfile' },
|
||||
{ title: '店铺设置', url: '/shop-settings' },
|
||||
@@ -170,6 +250,7 @@ const loadProfile = async (): Promise<void> => {
|
||||
}
|
||||
|
||||
const openEditProfile = (): void => {
|
||||
clearProfileDraft()
|
||||
const profile: Partial<UserInfo> = userInfo.value || {}
|
||||
profileForm.nickname = profile.nickname || ''
|
||||
profileForm.avatar = profile.avatar || ''
|
||||
@@ -252,14 +333,20 @@ const saveProfile = async (): Promise<void> => {
|
||||
})
|
||||
const profile = await userApi.info()
|
||||
syncProfileToSession(profile)
|
||||
editModalVisible.value = false
|
||||
updateEditModalVisible(false)
|
||||
toast.success('已保存')
|
||||
} finally {
|
||||
profileSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(loadProfile)
|
||||
onMounted(async () => {
|
||||
await loadProfile()
|
||||
restoreProfileDraft()
|
||||
})
|
||||
|
||||
watch(profileForm, syncProfileDraft, { deep: true })
|
||||
watch(editModalVisible, syncProfileDraft)
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
Reference in New Issue
Block a user