refactor: 拆分项目删除弹窗
This commit is contained in:
1 parent
f70230ce55
commit
496ab91651
1 file changed
+55
@@ -0,0 +1,55 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { computed, reactive, watch } from 'vue'
|
||||||
|
import { NAlert, NButton, NInput } from 'naive-ui'
|
||||||
|
import { AppDialog } from '../../../components/ui'
|
||||||
|
import { errorMessage } from '../../../lib/http'
|
||||||
|
import { projectsApi } from '../api'
|
||||||
|
import type { Project } from '../types'
|
||||||
|
|
||||||
|
/** 永久删除项目;要求输入项目名称二次确认。 */
|
||||||
|
const props = defineProps<{ project: Project | null }>()
|
||||||
|
const open = defineModel<boolean>('open', { default: false })
|
||||||
|
const emit = defineEmits<{ deleted: [id: string] }>()
|
||||||
|
const state = reactive({ confirm: '', pending: false, error: '' })
|
||||||
|
const expected = computed(() => props.project?.title || props.project?.topic || '')
|
||||||
|
const confirmed = computed(() => !!props.project && state.confirm.trim() === expected.value)
|
||||||
|
watch(
|
||||||
|
() => [open.value, props.project?.id],
|
||||||
|
() => {
|
||||||
|
if (!open.value) return
|
||||||
|
state.confirm = ''
|
||||||
|
state.error = ''
|
||||||
|
},
|
||||||
|
{ immediate: true }
|
||||||
|
)
|
||||||
|
async function remove() {
|
||||||
|
if (!props.project || state.pending || !confirmed.value) return
|
||||||
|
const id = props.project.id
|
||||||
|
state.pending = true
|
||||||
|
state.error = ''
|
||||||
|
try {
|
||||||
|
const result = await projectsApi.delete(id)
|
||||||
|
if (!result.deleted || result.id !== id) throw new Error('后端未确认项目删除结果。')
|
||||||
|
emit('deleted', id)
|
||||||
|
open.value = false
|
||||||
|
} catch (cause) {
|
||||||
|
state.error = errorMessage(cause)
|
||||||
|
} finally {
|
||||||
|
state.pending = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<AppDialog v-model:open="open" :title="`永久删除 · ${expected}`" description="删除后无法恢复。">
|
||||||
|
<NAlert type="error" :show-icon="false" class="mt-4">
|
||||||
|
会永久删除剧本、主体、身份、形态、分镜、首帧、视频、工作流记录,以及本地 Storage 中属于该项目的图片、视频和上传素材。
|
||||||
|
</NAlert>
|
||||||
|
<NAlert v-if="state.error" type="error" :show-icon="false" class="mt-3">{{ state.error }}</NAlert>
|
||||||
|
<p class="mt-5 text-xs">请输入 <strong>{{ expected }}</strong> 确认删除:</p>
|
||||||
|
<NInput v-model:value="state.confirm" class="mt-2" placeholder="输入项目名称" />
|
||||||
|
<div class="dialog-footer mt-6">
|
||||||
|
<NButton @click="open = false">取消</NButton>
|
||||||
|
<NButton type="error" :loading="state.pending" :disabled="!confirmed" @click="remove">永久删除项目</NButton>
|
||||||
|
</div>
|
||||||
|
</AppDialog>
|
||||||
|
</template>
|
||||||
Reference in new issue
Block a user