refactor: custom sheet list component code optimization (#477)

pull/480/head
Ryan Wang 2022-03-03 18:45:21 +08:00 committed by GitHub
parent fda9915883
commit fa08257918
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 78 additions and 71 deletions

View File

@ -16,7 +16,7 @@
<a-icon type="eye" /> <a-icon type="eye" />
{{ item.visits }} {{ item.visits }}
</span> </span>
<span @click="handleShowSheetComments(item)"> <span @click="handleOpenSheetComments(item)">
<a-icon type="message" /> <a-icon type="message" />
{{ item.commentCount }} {{ item.commentCount }}
</span> </span>
@ -37,19 +37,19 @@
:title="'你确定要发布【' + item.title + '】页面?'" :title="'你确定要发布【' + item.title + '】页面?'"
cancelText="取消" cancelText="取消"
okText="确定" okText="确定"
@confirm="handleEditStatusClick(item.id, 'PUBLISHED')" @confirm="handleChangeStatus(item.id, 'PUBLISHED')"
> >
还原 还原
</a-popconfirm> </a-popconfirm>
</a-menu-item> </a-menu-item>
<a-menu-item v-if="item.status === 'PUBLISHED' || item.status === 'DRAFT'"> <a-menu-item v-if="item.status === 'PUBLISHED' || item.status === 'DRAFT'">
<a-popconfirm <a-popconfirm
:title="'你确定要将【' + item.title + '】页面移到回收站'" :title="'你确定要删除【' + item.title + '】页面'"
cancelText="取消" cancelText="取消"
okText="确定" okText="确定"
@confirm="handleEditStatusClick(item.id, 'RECYCLE')" @confirm="handleChangeStatus(item.id, 'RECYCLE')"
> >
回收站 删除
</a-popconfirm> </a-popconfirm>
</a-menu-item> </a-menu-item>
<a-menu-item v-else-if="item.status === 'RECYCLE'"> <a-menu-item v-else-if="item.status === 'RECYCLE'">
@ -57,12 +57,12 @@
:title="'你确定要永久删除【' + item.title + '】页面?'" :title="'你确定要永久删除【' + item.title + '】页面?'"
cancelText="取消" cancelText="取消"
okText="确定" okText="确定"
@confirm="handleDeleteClick(item.id)" @confirm="handleDelete(item.id)"
> >
删除 永久删除
</a-popconfirm> </a-popconfirm>
</a-menu-item> </a-menu-item>
<a-menu-item @click="handleShowSheetSettings(item)"></a-menu-item> <a-menu-item @click="handleOpenSheetSettings(item)"></a-menu-item>
</a-menu> </a-menu>
</template> </template>
</a-dropdown> </a-dropdown>
@ -167,7 +167,7 @@
:overflowCount="999" :overflowCount="999"
:showZero="true" :showZero="true"
class="cursor-pointer" class="cursor-pointer"
@click="handleShowSheetComments(record)" @click="handleOpenSheetComments(record)"
/> />
</template> </template>
@ -199,7 +199,7 @@
:title="'你确定要发布【' + sheet.title + '】?'" :title="'你确定要发布【' + sheet.title + '】?'"
cancelText="取消" cancelText="取消"
okText="确定" okText="确定"
@confirm="handleEditStatusClick(sheet.id, 'PUBLISHED')" @confirm="handleChangeStatus(sheet.id, 'PUBLISHED')"
> >
<a-button class="!p-0" type="link">还原</a-button> <a-button class="!p-0" type="link">还原</a-button>
</a-popconfirm> </a-popconfirm>
@ -208,12 +208,12 @@
<a-popconfirm <a-popconfirm
v-if="sheet.status === 'PUBLISHED' || sheet.status === 'DRAFT'" v-if="sheet.status === 'PUBLISHED' || sheet.status === 'DRAFT'"
:title="'你确定要将【' + sheet.title + '】页面移到回收站'" :title="'你确定要删除【' + sheet.title + '】页面'"
cancelText="取消" cancelText="取消"
okText="确定" okText="确定"
@confirm="handleEditStatusClick(sheet.id, 'RECYCLE')" @confirm="handleChangeStatus(sheet.id, 'RECYCLE')"
> >
<a-button class="!p-0" type="link">回收站</a-button> <a-button class="!p-0" type="link">删除</a-button>
</a-popconfirm> </a-popconfirm>
<a-popconfirm <a-popconfirm
@ -221,12 +221,12 @@
:title="'你确定要永久删除【' + sheet.title + '】页面?'" :title="'你确定要永久删除【' + sheet.title + '】页面?'"
cancelText="取消" cancelText="取消"
okText="确定" okText="确定"
@confirm="handleDeleteClick(sheet.id)" @confirm="handleDelete(sheet.id)"
> >
<a-button class="!p-0" type="link">删除</a-button> <a-button class="!p-0" type="link">永久删除</a-button>
</a-popconfirm> </a-popconfirm>
<a-divider type="vertical" /> <a-divider type="vertical" />
<a-button class="!p-0" type="link" @click="handleShowSheetSettings(sheet)"></a-button> <a-button class="!p-0" type="link" @click="handleOpenSheetSettings(sheet)"></a-button>
</template> </template>
</a-table> </a-table>
<div class="page-wrapper"> <div class="page-wrapper">
@ -244,10 +244,10 @@
</div> </div>
<SheetSettingModal <SheetSettingModal
:loading="sheetSettingLoading" :loading="sheetSettingLoading"
:sheet="selectedSheet"
:savedCallback="onSheetSavedCallback" :savedCallback="onSheetSavedCallback"
:sheet="list.selected"
:visible.sync="sheetSettingVisible" :visible.sync="sheetSettingVisible"
@onClose="selectedSheet = {}" @onClose="list.selected = {}"
> >
<template #extraFooter> <template #extraFooter>
<a-button :disabled="selectPreviousButtonDisabled" @click="handleSelectPrevious"> </a-button> <a-button :disabled="selectPreviousButtonDisabled" @click="handleSelectPrevious"> </a-button>
@ -255,8 +255,8 @@
</template> </template>
</SheetSettingModal> </SheetSettingModal>
<TargetCommentListModal <TargetCommentListModal
:target-id="selectedSheet.id" :target-id="list.selected.id"
:title="`「${selectedSheet.title}」的评论`" :title="`「${list.selected.title}」的评论`"
:visible.sync="sheetCommentVisible" :visible.sync="sheetCommentVisible"
target="sheet" target="sheet"
@close="onSheetCommentsClose" @close="onSheetCommentsClose"
@ -269,9 +269,11 @@
</div> </div>
</template> </template>
<script> <script>
import { mixin, mixinDevice } from '@/mixins/mixin.js' // components
import SheetSettingModal from './SheetSettingModal' import SheetSettingModal from './SheetSettingModal'
import TargetCommentListModal from '@/components/Comment/TargetCommentListModal' import TargetCommentListModal from '@/components/Comment/TargetCommentListModal'
import { mixin, mixinDevice } from '@/mixins/mixin.js'
import apiClient from '@/utils/api-client' import apiClient from '@/utils/api-client'
import { sheetStatuses } from '@/core/constant' import { sheetStatuses } from '@/core/constant'
@ -304,7 +306,7 @@ const customColumns = [
}, },
{ {
title: '操作', title: '操作',
width: '180px', width: '200px',
scopedSlots: { customRender: 'action' } scopedSlots: { customRender: 'action' }
} }
] ]
@ -330,9 +332,10 @@ export default {
params: { params: {
page: 0, page: 0,
size: 10 size: 10
}
}, },
selectedSheet: {}, selected: {}
},
sheetSettingVisible: false, sheetSettingVisible: false,
sheetSettingLoading: false, sheetSettingLoading: false,
sheetCommentVisible: false sheetCommentVisible: false
@ -347,28 +350,17 @@ export default {
} }
}, },
selectPreviousButtonDisabled() { selectPreviousButtonDisabled() {
const index = this.list.data.findIndex(sheet => sheet.id === this.selectedSheet.id) const index = this.list.data.findIndex(sheet => sheet.id === this.list.selected.id)
return index === 0 && !this.list.hasPrevious return index === 0 && !this.list.hasPrevious
}, },
selectNextButtonDisabled() { selectNextButtonDisabled() {
const index = this.list.data.findIndex(sheet => sheet.id === this.selectedSheet.id) const index = this.list.data.findIndex(sheet => sheet.id === this.list.selected.id)
return index === this.list.data.length - 1 && !this.list.hasNext return index === this.list.data.length - 1 && !this.list.hasNext
} }
}, },
created() { created() {
this.handleListSheets() this.handleListSheets()
}, },
destroyed() {
if (this.sheetSettingVisible) {
this.sheetSettingVisible = false
}
},
beforeRouteLeave(to, from, next) {
if (this.sheetSettingVisible) {
this.sheetSettingVisible = false
}
next()
},
methods: { methods: {
async handleListSheets(enableLoading = true) { async handleListSheets(enableLoading = true) {
try { try {
@ -388,41 +380,52 @@ export default {
this.list.loading = false this.list.loading = false
} }
}, },
handleEditClick(sheet) { handleEditClick(sheet) {
this.$router.push({ name: 'SheetEdit', query: { sheetId: sheet.id } }) this.$router.push({ name: 'SheetEdit', query: { sheetId: sheet.id } })
}, },
handleEditStatusClick(sheetId, status) {
apiClient.sheet async handleChangeStatus(sheetId, status) {
.updateStatusById(sheetId, status) try {
.then(() => { await apiClient.sheet.updateStatusById(sheetId, status)
this.$message.success('操作成功!') this.$message.success('操作成功!')
}) } catch (e) {
.finally(() => { this.$log.error('Failed to change sheet status', e)
this.handleListSheets() } finally {
}) await this.handleListSheets()
}
}, },
handleDeleteClick(sheetId) {
apiClient.sheet async handleDelete(sheetId) {
.delete(sheetId) try {
.then(() => { await apiClient.sheet.delete(sheetId)
this.$message.success('删除成功!') this.$message.success('删除成功!')
}) } catch (e) {
.finally(() => { this.$log.error('Failed to delete sheet', e)
this.handleListSheets() } finally {
}) await this.handleListSheets()
}
}, },
handleShowSheetSettings(sheet) {
apiClient.sheet.get(sheet.id).then(response => { async handleOpenSheetSettings(sheet) {
this.selectedSheet = response.data try {
this.sheetSettingVisible = true this.sheetSettingVisible = true
}) this.sheetSettingLoading = true
const { data } = await apiClient.sheet.get(sheet.id)
this.list.selected = data
} catch (e) {
this.$log.error('Failed to open sheet settings', e)
} finally {
this.sheetSettingLoading = false
}
}, },
handleShowSheetComments(sheet) {
apiClient.sheet.get(sheet.id).then(response => { handleOpenSheetComments(sheet) {
this.selectedSheet = response.data this.list.selected = sheet
this.sheetCommentVisible = true this.sheetCommentVisible = true
})
}, },
handlePreview(sheetId) { handlePreview(sheetId) {
apiClient.sheet.getPreviewLinkById(sheetId).then(response => { apiClient.sheet.getPreviewLinkById(sheetId).then(response => {
window.open(response, '_blank') window.open(response, '_blank')
@ -446,23 +449,26 @@ export default {
this.list.params.size = size this.list.params.size = size
this.handleListSheets() this.handleListSheets()
}, },
onSheetCommentsClose() { onSheetCommentsClose() {
this.sheetCommentVisible = false this.sheetCommentVisible = false
this.selectedSheet = {} this.list.selected = {}
this.handleListSheets(false) this.handleListSheets(false)
}, },
onSheetSavedCallback() { onSheetSavedCallback() {
this.handleListSheets(false) this.handleListSheets(false)
}, },
/** /**
* Select previous sheet * Select previous sheet
*/ */
async handleSelectPrevious() { async handleSelectPrevious() {
const index = this.list.data.findIndex(post => post.id === this.selectedSheet.id) const index = this.list.data.findIndex(post => post.id === this.list.selected.id)
if (index > 0) { if (index > 0) {
this.sheetSettingLoading = true this.sheetSettingLoading = true
const response = await apiClient.sheet.get(this.list.data[index - 1].id) const response = await apiClient.sheet.get(this.list.data[index - 1].id)
this.selectedSheet = response.data this.list.selected = response.data
this.sheetSettingLoading = false this.sheetSettingLoading = false
return return
} }
@ -471,19 +477,20 @@ export default {
await this.handleListPosts() await this.handleListPosts()
this.sheetSettingLoading = true this.sheetSettingLoading = true
const response = await apiClient.sheet.get(this.list.data[this.list.data.length - 1].id) const response = await apiClient.sheet.get(this.list.data[this.list.data.length - 1].id)
this.selectedSheet = response.data this.list.selected = response.data
this.sheetSettingLoading = false this.sheetSettingLoading = false
} }
}, },
/** /**
* Select next sheet * Select next sheet
*/ */
async handleSelectNext() { async handleSelectNext() {
const index = this.list.data.findIndex(post => post.id === this.selectedSheet.id) const index = this.list.data.findIndex(post => post.id === this.list.selected.id)
if (index < this.list.data.length - 1) { if (index < this.list.data.length - 1) {
this.sheetSettingLoading = true this.sheetSettingLoading = true
const response = await apiClient.sheet.get(this.list.data[index + 1].id) const response = await apiClient.sheet.get(this.list.data[index + 1].id)
this.selectedSheet = response.data this.list.selected = response.data
this.sheetSettingLoading = false this.sheetSettingLoading = false
return return
} }
@ -492,7 +499,7 @@ export default {
await this.handleListPosts() await this.handleListPosts()
this.sheetSettingLoading = true this.sheetSettingLoading = true
const response = await apiClient.sheet.get(this.list.data[0].id) const response = await apiClient.sheet.get(this.list.data[0].id)
this.selectedSheet = response.data this.list.selected = response.data
this.sheetSettingLoading = false this.sheetSettingLoading = false
} }
} }